package com.heyu.api.controller.idcard;


import com.heyu.api.request.certificate.ApiIdentityCardSecondRequest;
import com.heyu.api.resp.certificate.ApiIdentityCardSecondResp;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.handle.TIdCardVerificationHandle;
import com.heyu.api.tencent.request.TIdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * 身份证的二要素的校验
 */
@Slf4j
@RestController
@RequestMapping("/id/card")
@NotIntercept
public class IdCardSecondCheckController {

    @Autowired
    private TIdCardVerificationHandle idCardVerificationHandle;

    // http://localhost:8888/id/card/certification?idCardNumber=430529199209255030&realName=瞿贻晓



    @RequestMapping("/certification")
    @CacheResult
    public R certification(ApiIdentityCardSecondRequest apiIdentityCardRequest) {
        // 1.申明返回值
        ApiIdentityCardSecondResp apiIdentityCardSecondResp = new ApiIdentityCardSecondResp();

        // 2. 请求参数封装
        TIdCardVerificationRequest tIdCardVerificationRequest = new TIdCardVerificationRequest();
        tIdCardVerificationRequest.setIdCard(apiIdentityCardRequest.getIdCardNumber());
        tIdCardVerificationRequest.setName(apiIdentityCardRequest.getRealName());

        // 3. 调用
        ApiR<IdCardVerificationResponse> tR = idCardVerificationHandle.handle(tIdCardVerificationRequest);
        if (tR.isSuccess()) {
            // 4 . 结果封装
            IdCardVerificationResponse idCardVerificationResponse = tR.getData();
            Integer checkResult = 1;
            if (StringUtils.equals(ApiConstants.SUCCESS, idCardVerificationResponse.getResult())) {
                checkResult = 0;
            }
            apiIdentityCardSecondResp.setCheckResult(checkResult);
            apiIdentityCardSecondResp.setDesc(idCardVerificationResponse.getDescription());
        }
        // 返回
        return R.ok().setData(apiIdentityCardSecondResp);
    }

}
