package com.heyu.api.tencent.handle;


import com.heyu.api.data.utils.BankCardUtils;
import com.heyu.api.data.utils.IDCardValidatorUtils;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.TencentBaseHandle;
import com.heyu.api.tencent.request.TBankCard3EVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.BankCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.BankCardVerificationResponse;
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=BankCard2EVerification
 *
 *银行卡二要素核验
 *
 */
@Component
@Slf4j
public class TBankCard3EVerificationHandle extends TencentBaseHandle<TBankCard3EVerificationRequest, BankCardVerificationResponse> {

    @Autowired
    private FaceidClient client;

    @Override
    public String check(TBankCard3EVerificationRequest p) {
        if (!BankCardUtils.isValidBankCard(p.getBankCard())) {
            log.info("银行卡不合法");
            return "无效银行卡";
        }
        if (StringUtils.isBlank(p.getBankCard())
                && StringUtils.isBlank(p.getName())
                && StringUtils.isBlank(p.getIdCard())) {
            return "请输入银⾏卡号+姓名 + 身份证号";
        }

        if (!IDCardValidatorUtils.validateIDCard(p.getIdCard())) {
            return "身份证号码验证不正确";
        }
        return null;
    }

    @Override
    public BankCardVerificationResponse run(TBankCard3EVerificationRequest ap) throws Exception {
        BankCardVerificationRequest req = new BankCardVerificationRequest();
        req.setBankCard(ap.getBankCard());
        req.setName(ap.getName());
        req.setIdCard(ap.getIdCard());
        req.setCertType(ap.getCertType());
        req.setEncryption(ap.getEncryption());
        return client.BankCardVerification(req);
    }
}
