package com.hfkj.channel.tianque.service;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.tianque.config.TianQueConfig;
import com.hfkj.channel.tianque.utils.RequestUtil;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 营销转账
 * @className: TianQueFundManageService
 * @author: HuRui
 * @date: 2023/10/8
 **/
@Component
public class TianQueFundManageService {

    private static Logger log = LoggerFactory.getLogger(TianQueFundManageService.class);

    /**
     * 营销转账
     * @param mno 商户号
     * @param orderNo 交易号
     * @param amount  交易金额
     * @param remark  备注
     * @param investor 服务费出资方，枚举值  01:服务商出资  02:商户出资
     * @return
     */
    public JSONObject orgTransfer(String mno, String orderNo, BigDecimal amount,String remark, String investor) {
        log.info("========= Start 营销转账接口 Start ===========");
        Map<String, Object> param = new HashMap<>();
        param.put("mno", mno);
        param.put("orderNo", orderNo);
        param.put("amount", amount);
        param.put("content", remark);
        param.put("investor", investor);

        JSONObject responseBody = RequestUtil.request(TianQueConfig.requestUrl + "/capital/fundManage/orgTransfer", param);
        log.info("请求参数：",JSONObject.toJSONString(param));
        log.info("响应参数：", responseBody.toJSONString());
        System.out.println("body：" + JSONObject.toJSONString(param));
        System.out.println("response：" + responseBody.toJSONString());
        if (!"0000".equals(responseBody.getString("bizCode"))) {
            throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.COMMON_ERROR, responseBody.getString("bizMsg"));
        }

        log.info("========= END 营销转账 END ===========");
        return responseBody;
    }

    /**
     * 转账结果查询
     * @param orderNo
     * @return
     */
    public JSONObject getTransferInfo(String orderNo) {
        log.info("========= Start 营销转账 Start ===========");
        Map<String, Object> param = new HashMap<>();
        param.put("orderNo", orderNo);

        JSONObject responseBody = RequestUtil.request(TianQueConfig.requestUrl + "/capital/fundManage/getTransferInfo", param);
        log.info("请求参数：",JSONObject.toJSONString(param));
        log.info("响应参数：", responseBody.toJSONString());
        System.out.println("body：" + JSONObject.toJSONString(param));
        System.out.println("response：" + responseBody.toJSONString());
        if (!"0000".equals(responseBody.getString("bizCode"))) {
            throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.COMMON_ERROR, responseBody.getString("bizMsg"));
        }
        log.info("========= END 营销转账 END ===========");
        return responseBody;
    }
}
