package com.hfkj.channel.lakala;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.lakala.config.LaKaLaConfig;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.GenerateUtil;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.config.CommonSysConst;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 拉卡拉配置 服务
 * @author hurui
 */
@Service("laKaLaConfigService")
public class LaKaLaConfigService {

    /**
     * 微信参数配置
     * @param merCupNo  银联商户号
     * @param subMchId  微信子商户号
     * @param subAppid  关联APPID 绑定特约商户或渠道公众号、小程序、APP支付等对应的APPID
     * @param jsapiPath 授权目录
     * @param confType 配置类型 配置类型 类型=SUB_APPID时，subAppid必传, 类型=SUBSCRIBE_APPID时，subscribeAppid必传 ,类型=UNION时，subAppid和subscribeAppid必传 类型=JSAPI_PATH时，jsapiPath必传
     * @return
     */
    public JSONObject wechatParamConf(String merCupNo,String channlId,String subMchId,String subAppid,String jsapiPath,String confType) {
        // 请求参数
        Map<String, Object> param = new HashMap<>();
        param.put("timestamp", new Date().getTime());
        param.put("rnd", GenerateUtil.generateRandomString(18));
        param.put("ver", "1.0.0");
        param.put("reqId", new Date().getTime());

        Map<String, Object> map = new HashMap<>();
        map.put("merCupNo", merCupNo);
        map.put("channlId", channlId); // 微信渠道号 （仅支持拉卡拉渠道配置）
        map.put("subMchId", subMchId);
        map.put("subAppid", subAppid);
        map.put("jsapiPath", jsapiPath);
        map.put("confType", confType);
        param.put("reqData", map);

        // 生成签名
        Map<String, Object> reqParam = LaKaLaConfig.generateSignParamsV2(JSONObject.parseObject(JSONObject.toJSONString(param)));

        // 头部参数
        Map<String, Object> heard = new HashMap<>();
        heard.put("Authorization", reqParam.get("Authorization"));

        JSONObject responseBody = HttpsUtils.doPost(CommonSysConst.getSysConfig().getLkl_request_url() + "/api/v2/mms/openApi/wechatParamConf", param, heard);
        System.out.println("Authorization" + JSONObject.toJSONString(reqParam.get("Authorization")));
        System.out.println("request：" + JSONObject.toJSONString(param));
        System.out.println("response：" + responseBody.toJSONString());
        if (!responseBody.getString("retCode").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseBody.getString("retMsg"));
        }

        return responseBody;
    }

    /**
     * 商户报备结果查询
     * @param merCupNo
     * @return
     */
    public JSONObject querySubMerInfo(String channlId,String merCupNo) {
        // 请求参数
        Map<String, Object> param = new HashMap<>();
        param.put("timestamp", new Date().getTime());
        param.put("rnd", GenerateUtil.generateRandomString(18));
        param.put("ver", "1.0.0");
        param.put("reqId", new Date().getTime());

        Map<String, Object> map = new HashMap<>();
        map.put("version", "1.0");
        map.put("orderNo", new Date().getTime());
        map.put("orgCode", channlId);
        map.put("merCupNo", merCupNo);
        map.put("registerStatus", "SUCCESS");
        param.put("reqData", map);

        // 生成签名
        Map<String, Object> reqParam = LaKaLaConfig.generateSignParamsV2(JSONObject.parseObject(JSONObject.toJSONString(param)));

        // 头部参数
        Map<String, Object> heard = new HashMap<>();
        heard.put("Authorization", reqParam.get("Authorization"));

        JSONObject responseBody = HttpsUtils.doPost(CommonSysConst.getSysConfig().getLkl_request_url() + "/api/v2/mms/openApi/querySubMerInfo", param, heard);
        System.out.println("Authorization" + JSONObject.toJSONString(reqParam.get("Authorization")));
        System.out.println("request：" + JSONObject.toJSONString(param));
        System.out.println("response：" + responseBody.toJSONString());
        if (!responseBody.getString("retCode").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseBody.getString("retMsg"));
        }
        return responseBody.getJSONObject("respData");
    }

    /**
     * 商户报备结果查询
     * @param merCupNo
     * @return
     */
    public JSONObject queryWechatConfig(String subMerchantId,String merCupNo) {
        // 请求参数
        Map<String, Object> param = new HashMap<>();
        param.put("timestamp", new Date().getTime());
        param.put("ver", "1.0.0");
        param.put("reqId", GenerateUtil.generateRandomString(18));

        Map<String, Object> map = new HashMap<>();
        map.put("tradeMode", "WECHAT");
        map.put("subMerchantId", subMerchantId);
        map.put("merchantNo", merCupNo);
        param.put("reqData", map);

        // 生成签名
        Map<String, Object> reqParam = LaKaLaConfig.generateSignParamsV2(JSONObject.parseObject(JSONObject.toJSONString(param)));

        // 头部参数
        Map<String, Object> heard = new HashMap<>();
        heard.put("Authorization", reqParam.get("Authorization"));

        JSONObject responseBody = HttpsUtils.doPost(CommonSysConst.getSysConfig().getLkl_request_url() + "/api/v2/mms/sme/queryWechatConfig", param, heard);
        System.out.println("Authorization" + JSONObject.toJSONString(reqParam.get("Authorization")));
        System.out.println("request：" + JSONObject.toJSONString(param));
        System.out.println("response：" + responseBody.toJSONString());
        if (!responseBody.getString("retCode").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseBody.getString("retMsg"));
        }
        return responseBody.getJSONObject("respData");
    }

}
