package com.czg.service.market.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.constants.ParamCodeCst;
import com.czg.exception.CzgException;
import com.czg.system.service.SysParamsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 微信支付service
 *
 * @author Administrator
 */
@Component
@Slf4j
@Primary
public class AppWxServiceImpl extends BaseWx {

    @DubboReference
    private SysParamsService paramsService;

    public AppWxServiceImpl() {
        config = new Config();
    }

    @Override
    public String getAccessToken(boolean refresh) {
        init();

        String resp = HttpUtil.get(StrUtil.format("https://access-token.sxczgkj.com/accessToken?appId={}&appSecret={}", config.appId, config.appSecret));
//       响应 {"accessToken":"100_6C_jltHANT1y2Fot5PXKFDzPXTyWumCsao0oMoNRvJUTuxS0IOVO4nBmjdmx5dZfYItShFVSAKYzNDf7ZGLPlx52ii1Y1qerrbbSmIiLWCrec5qjBY4gV5Tfv8YKKTdABAEEN","appId":"wx212769170d2c6b2a"}
        if (StrUtil.isNotBlank(resp)) {
            JSONObject respInfo = JSONObject.parseObject(resp);
            return respInfo.getString("accessToken");
        } else {
            String response = HttpUtil.get(WX_ACCESS_TOKEN_URL,
                    Map.of("grant_type", "client_credential", "appid", config.appId, "secret", config.appSecret)
            );
            JSONObject jsonObject = JSONObject.parseObject(response);
            return jsonObject.getString("access_token");
        }
    }

    @Override
    public void init() throws CzgException {
        // 用户小程序参数
        Map<String, String> userMiniKeyMap = paramsService.getParamsByMap("user_mini_key_set", ParamCodeCst.USER_MINI_KEY_SET);

        // 微信支付参数
        Map<String, String> payKeyMap = paramsService.getParamsByMap("pay_key_set", ParamCodeCst.PAY_KEY_SET);

        // 小程序id
        config.appId = userMiniKeyMap.get(ParamCodeCst.Wechat.Mini.USER_WX_APP_ID);
        // 小程序secrete
        config.appSecret = userMiniKeyMap.get(ParamCodeCst.Wechat.Mini.USER_WX_SECRETE);

        config.certPath = "";
        // 微信支付公钥
        config.pubKey = payKeyMap.get(ParamCodeCst.Wechat.Pay.WX_PUB_KEY);
        // api支付证书私钥
        config.apiCertKey = payKeyMap.get(ParamCodeCst.Wechat.Pay.WX_API_CLIENT_KEY);
        // api支付证书公钥
        config.apiCert = payKeyMap.get(ParamCodeCst.Wechat.Pay.WX_API_CLIENT_CERT);
        try {
            config.privateKey = SecureUtil.rsa(config.apiCertKey, null).getPrivateKey();
        } catch (Exception e) {
            log.warn("微信加载api证书失败");
        }
        // 平台证书
        config.platformCertPath = "";
        // 平台证书编号
        config.platformCertNo = "";
        // 商户号
        config.mchId = payKeyMap.get(ParamCodeCst.Wechat.Pay.WX_MCH_ID);
        // v3密钥
        config.apiV3Key = payKeyMap.get(ParamCodeCst.Wechat.Pay.WX_V3_KEY);
        config.apiV2Key = "";
        // 回调地址
        config.notifyUrl = payKeyMap.get(ParamCodeCst.System.NATIVE_NOTIFY_URL) + "/wx/pay";
        config.refundNotifyUrl = "";
        config.transferNotifyUrl = payKeyMap.get(ParamCodeCst.System.NATIVE_NOTIFY_URL) + "/wx/transfer";
    }
}





