package com.heyu.api.tencent.handle;


import com.alibaba.fastjson.JSON;
import com.heyu.api.data.utils.BankCardUtils;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.TencentBaseHandle;
import com.heyu.api.tencent.request.TBankCard2EVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.BankCard2EVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.BankCard2EVerificationResponse;
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 TBankCard2EVerificationHandle extends TencentBaseHandle<TBankCard2EVerificationRequest, BankCard2EVerificationResponse> {

    @Autowired
    private FaceidClient client;

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

    @Override
    public BankCard2EVerificationResponse run(TBankCard2EVerificationRequest ap) throws Exception {
        BankCard2EVerificationRequest req = new BankCard2EVerificationRequest();
        req.setBankCard(ap.getBankCard());
        req.setName(ap.getName());
        req.setEncryption(ap.getEncryption());
        log.info("TBankCard2EVerificationHandle req : {}", JSON.toJSONString(req));
        return client.BankCard2EVerification(req);

    }
}
