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

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.constants.ParamCodeCst;
import com.czg.system.service.SysParamsService;
import com.ijpay.core.kit.RsaKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.Map;


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

    @DubboReference
    private SysParamsService paramsService;

    public WxServiceImpl() {
        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));
        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() {
        // 商户小程序参数
        Map<String, String> shopMiniKeyMap = paramsService.getParamsByMap("shop_mini_key_set", ParamCodeCst.SHOP_MINI_KEY_SET);

        // 小程序id
        config.appId = shopMiniKeyMap.get(ParamCodeCst.Wechat.Mini.SHOP_WX_APP_ID);
        // 小程序secrete
        config.appSecret = shopMiniKeyMap.get(ParamCodeCst.Wechat.Mini.SHOP_WX_SECRETE);

        // 微信支付参数
        Map<String, String> payKeyMap = paramsService.getParamsByMap("pay_key_set", ParamCodeCst.PAY_KEY_SET);
        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 = RsaKit.loadPrivateKey(config.apiCertKey);
        } 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";
    }

    public BaseWx getAppService() {
        return this;
    }
}

