package com.yn.bftl.thirdparty.modules.contract.util;

import com.alibaba.fastjson.JSON;
import com.qiyuesuo.sdk.v2.SdkClient;
import com.qiyuesuo.sdk.v2.json.JSONUtils;
import com.qiyuesuo.sdk.v2.request.SdkRequest;
import com.qiyuesuo.sdk.v2.response.SdkResponse;
import com.yn.bftl.common.common.exception.YnceError;
import com.yn.bftl.common.modules.configuration.entity.ThirdPartyPaySet;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.common.util.AssertUtil;
import com.yn.bftl.thirdparty.common.util.ConfigurationUtil;
import com.yn.bftl.thirdparty.common.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;

/**
 * 契约锁资源创建
 *
 * @author chendehuihuo
 * @date 2022/7/20 11:36
 */
@Slf4j
public class ContractLockUtil {

    public static  volatile ThirdPartyPaySet CONFIGURATION = ConfigurationUtil.CONFIGURATION;

    private static SdkClient sdkClient;

    private static volatile ContractLockUtil CONTRACT_LOCK = new ContractLockUtil();

    private static long TIMEMILLIS = System.currentTimeMillis();

    private ContractLockUtil() {

    }

    public static ContractLockUtil getInstance() {
        if (sdkClient == null) {
            connect();
        }
        return CONTRACT_LOCK;
    }

    // 测试环境 https://openapi.qiyuesuo.cn
//    private static String SERVER_URL = "https://openapi.qiyuesuo.cn";
    private static String SERVER_URL = CONFIGURATION.getContractLockServerUrl();
    // 晨的挥霍沙漏 MAAesQ3XfD
    private static String ACCESS_KEY = CONFIGURATION.getContractLockAccessKey();
    // 晨的挥霍沙漏 tN84u4O6pCrWDyXY1a63lMn6YITHXz
    private static String ACCESS_SECRET = CONFIGURATION.getContractLockAccessSecret();
    // 晨的挥霍沙漏 gohnDQZvAj40Gdzc
    private static String SECRET_KEY = CONFIGURATION.getContractLockSecretKey();

    private static String THIRD_PARTY_URL = CONFIGURATION.getThirdPartyUrl();

    /**
     * 发起连接方法
     *
     * @author chendehuihuo
     * @date 2022-07-20 14:44
     * @return
     */
    private static void connect() {
        AssertUtil.notEmpty(SERVER_URL, "契约锁配置参数不完整");
        AssertUtil.notEmpty(ACCESS_KEY, "契约锁配置参数不完整");
        AssertUtil.notEmpty(ACCESS_SECRET, "契约锁配置参数不完整");
        sdkClient = new SdkClient(SERVER_URL, ACCESS_KEY, ACCESS_SECRET,3000,30000);
    }

    public SdkClient sdkClient() {
        return sdkClient;
    }

    public <T> T service(Object request, Class<T> resultClazz) {
        // 10分钟更新一次静态变量，后面可以通过PCredis更新，做判断
        long newTimeMillis = System.currentTimeMillis();
        long interval = 640000;
        if (newTimeMillis - interval > TIMEMILLIS) {
            CONFIGURATION = SpringContextHolder.getBean(ConfigurationUtil.class).toThird();
            connect();
        }
        String response = sdkClient.service((SdkRequest) request);
        SdkResponse<T> responseObj = JSONUtils.toQysResponse(response, resultClazz);
        if (responseObj.getCode() == 0) {
            return responseObj.getResult();
        } else {
            log.error("契约锁请求失败，错误码:{}，错误信息:{}，请求参数{}", responseObj.getCode(), responseObj.getMessage(), JSON.toJSONString(request));
            throw new YnceErrorException(YnceError.YNCE_216002, responseObj.getMessage());
        }
    }

    public static String getSecretKey() {
        return SECRET_KEY;
    }

    public static String getThirdPartyUrl() {
        return THIRD_PARTY_URL;
    }
}
