package com.heyu.api.tencent.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.dao.api.ApiIdCardDao;
import com.heyu.api.data.entity.api.ApiIdCardEntity;
import com.heyu.api.data.service.api.ApiRechargeMoneyService;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.TencentBaseHandle;
import com.heyu.api.tencent.request.TTIdCardOCRVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardOCRVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardOCRVerificationResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/***
 *
 *
 * https://console.cloud.tencent.com/api/explorer?Product=faceid&Version=2018-03-01&Action=IdCardOCRVerification
 *
 *
 *身份证识别及信息核验
 */
@Component
@Slf4j
public class TIdCardOCRVerificationHandle extends TencentBaseHandle<TTIdCardOCRVerificationRequest, IdCardOCRVerificationResponse> {


    @Autowired
    private FaceidClient client;


    @Autowired
    private ApiIdCardDao apiIdCardDao;


    @Autowired
    private ApiRechargeMoneyService apiRechargeMoneyService;

    @Override
    public String check(TTIdCardOCRVerificationRequest ttIdCardOCRVerificationRequest) {
        if (StringUtils.isNotBlank(ttIdCardOCRVerificationRequest.getIdCard())
                && StringUtils.isBlank(ttIdCardOCRVerificationRequest.getName())) {
            return "姓名不能为空";
        }


        if (StringUtils.isBlank(ttIdCardOCRVerificationRequest.getIdCard())
                && StringUtils.isNotBlank(ttIdCardOCRVerificationRequest.getName())) {
            return "身份证号不能为空";
        }

        if (StringUtils.isBlank(ttIdCardOCRVerificationRequest.getIdCard())
                && StringUtils.isBlank(ttIdCardOCRVerificationRequest.getName())
                && StringUtils.isBlank(ttIdCardOCRVerificationRequest.getImageBase64())
                && StringUtils.isBlank(ttIdCardOCRVerificationRequest.getImageUrl())
        ) {
            return "姓名身份证号以及图片不能同时为空";
        }
        return null;
    }

    @Override
    public IdCardOCRVerificationResponse run(TTIdCardOCRVerificationRequest ap) throws Exception {
        IdCardOCRVerificationResponse resp = null;
        if (StringUtils.isNotBlank(ap.getIdCard()) && StringUtils.isNotBlank(ap.getName())) {
            ApiIdCardEntity apiIdCardEntity = apiIdCardDao.selectApiIdCardByIdCardNameChannelEnableStatus(ap.getIdCard(), ap.getName(), ApiConstants.tencent,1);
            if (apiIdCardEntity != null) {
                resp = JSONObject.parseObject(apiIdCardEntity.getApiResult(), IdCardOCRVerificationResponse.class);
                apiIdCardDao.updateApiIdCardUseCountById(1L, apiIdCardEntity.getId());
                return resp;
            }
        }

        // 实例化一个请求对象,每个接口都会对应一个request对象
        IdCardOCRVerificationRequest req = new IdCardOCRVerificationRequest();
        req.setIdCard(ap.getIdCard());
        req.setName(ap.getName());
        req.setImageUrl(ap.getImageUrl());
        req.setImageBase64(ap.getImageBase64());
        // 返回的resp是一个IdCardOCRVerificationResponse的实例，与请求对象对应
        resp = client.IdCardOCRVerification(req);

        if(resp ==null){
            return null;
        }

        int enable = 1;
        String result = resp.getResult();
        /***
         * 认证结果码，收费情况如下。
         * - 收费结果码：
         * 0: 姓名和身份证号一致。
         * -1: 姓名和身份证号不一致。
         * - 不收费结果码：
         * -2: 非法身份证号（长度、校验位等不正确）。
         * -3: 非法姓名（长度、格式等不正确）。
         *
         *
         * -4: 证件库服务异常。
         * -5: 证件库中无此身份证记录。
         * -6: 权威比对系统升级中，请稍后再试。
         * -7: 认证次数超过当日限制。
         *
         * 示例值：0
         */
        if (
                "-4".equals(result)
                || "-5".equals(result)
                || "-6".equals(result)
                || "-7".equals(result)
        ) {
            enable = 0 ;
        }

        ApiIdCardEntity apiIdCardEntity = new ApiIdCardEntity();
        apiIdCardEntity.setIdCard(ap.getIdCard());
        apiIdCardEntity.setRealName(ap.getName());
        apiIdCardEntity.setChannel(ApiConstants.tencent);
        apiIdCardEntity.setApiResult(JSON.toJSONString(resp));
        apiIdCardEntity.setEnableStatus(enable);
        apiIdCardEntity.setUseCount(1L);


        apiRechargeMoneyService.addUseCountAndSendWarnMsg(ApiConstants.IDCARD_VERIFICATION_TENCENT, 1);



        apiIdCardDao.insertApiIdCard(apiIdCardEntity);

        return resp;
    }
}
