package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.utils.DESUtil;
import cn.hanghui.outapi.platform.common.utils.Preconditions;
import cn.hanghui.outapi.platform.tenant.entity.dto.RiskPcResp;
import cn.hanghui.outapi.platform.tenant.entity.enums.RiskRespCodeEnum;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.service.impl
 * @class RiskServer
 * @date 2024/3/1 上午9:28
 * @description
 */
@Slf4j
@Service
public class RiskServer {

    //风险人
    @Value("${risk.appid}")
    private String riskAppId;
    @Value("${risk.appsecret}")
    private String riskAppsecret;
    @Value("${risk.deskey}")
    private String riskDeskey;
    @Value("${risk.baseURL}")
    private String riskBaseURL;
    @Value("${risk.tokenUri}")
    private String riskTokenUri;
    @Value("${risk.personFxpfjxUri}")
    private String riskPersonFxpfjxUri;

    /**
     * 风险人token
     */
    private static String CACHE_TOKEN_FXPFJX_KEY = "open:token:fxpfjx:key";

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 获取评测结果
     *
     * @param name
     * @param idCard
     * @return
     */
    public RiskPcResp getFxpfjx(String name, String idCard) {
        try {
//            name="谌涛";
//            idCard="430923198812097216";
            String fxpfjxToken = getFxpfjxToken();
            JSONObject object = new JSONObject();
            object.put("token", fxpfjxToken);
            JSONObject body = new JSONObject();
            body.put("name", name);
            body.put("idcard", idCard);
            log.info("--->获取风险评分请求参数：{}", body.toJSONString());
            object.put("body", DESUtil.encrypt(body.toJSONString(), riskDeskey));
            String resp = HttpUtil.post(riskBaseURL + riskPersonFxpfjxUri, object.toJSONString(), 5000);
            log.info("--->获取风险评分返回参数：{}", resp);
            RiskPcResp riskPcResp = JSONObject.parseObject(resp, RiskPcResp.class);
            if (RiskRespCodeEnum.TOKEN_ERROR.getCode().equals(riskPcResp.getHeader().getCode())) {
                getFxpfjx(name, idCard);
            }
            return riskPcResp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getFxpfjxToken() {
        try {
            if (redisTemplate.hasKey(CACHE_TOKEN_FXPFJX_KEY)
                    && Preconditions.isNotBlank(redisTemplate.opsForValue().get(CACHE_TOKEN_FXPFJX_KEY))
                    && Preconditions.isNotBlank((String) redisTemplate.opsForValue().get(CACHE_TOKEN_FXPFJX_KEY))) {
                return (String) redisTemplate.opsForValue().get(CACHE_TOKEN_FXPFJX_KEY);
            }
            JSONObject object = new JSONObject();
            object.put("token", null);
            JSONObject body = new JSONObject();
            body.put("appid", riskAppId);
            body.put("secret", riskAppsecret);
            object.put("body", body);
            String resp = HttpUtil.post(riskBaseURL + riskTokenUri, object.toJSONString(), 5000);
            log.info("--->获取风险评分token返回参数：{}", resp);
            RiskPcResp riskPcResp = JSONObject.parseObject(resp, RiskPcResp.class);
            if (RiskRespCodeEnum.SUCCESS.getCode().equals(riskPcResp.getHeader().getCode())) {
                String token = riskPcResp.getBody().getToken();
                redisTemplate.opsForValue().set(CACHE_TOKEN_FXPFJX_KEY, token, 6000, TimeUnit.SECONDS);
                return token;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
