package com.heyu.api.controller.bankcard;


import com.heyu.api.alibaba.handle.common.text.ABank2MetaVerifyHandle;
import com.heyu.api.alibaba.request.common.text.ABank4MetaVerifyRequest;
import com.heyu.api.alibaba.resp.TBankMetaVerifyResponse;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.EbAuthentication;
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.resp.certificate.BankCard2MetaResp;
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;

/***
 * 识别身份证
 * https://next.api.aliyun.com/api/Cloudauth/2019-03-07/BankMetaVerify?tab=DOC&lang=JAVA
 *
 *
 * https://console.cloud.tencent.com/api/explorer?Product=faceid&Version=2018-03-01&Action=BankCard2EVerification
 *
 *
 *
 * https://buy.cloud.tencent.com/iai_faceid
 *
 *
 *
 * https://www.aliyun.com/product/id-verification?spm=5176.29677750.nav-v2-dropdown-menu-1.d_searchresult_0.6cfb154ad4HNbv&scm=20140722.S_product@@%E4%BA%91%E4%BA%A7%E5%93%81@@3152909._.RL_%E6%A0%B8%E9%AA%8C-LOC_menu~UND~product-OR_ser-PAR1_213e36cb17449009328204518ef754-V_4-RE_productNew-P0_0-P1_0
 *
 *
 * 银行卡二要素核验
 *
 *
 * 银行卡二，三，四要素核验
 *
 * BankMetaVerify
 */
@Slf4j
@RestController
@RequestMapping("/bank/card/2")
public class BankCard2MetaController extends BaseController {

    @Autowired
    private ABank2MetaVerifyHandle aBankMetaVerifyHandle;


    public static final String SUCCESS_MSG = "银⾏卡号+姓名信息匹配";
    public static final String FAILED_MSG = "银⾏卡号+姓名信息不匹配";


    // http://localhost:8888/bank/card/2/verify?bankCardNumber=6214855713516769&realName=瞿贻晓
    // https://api.1024api.com/api-interface/bank/card/2/verify?bankCardNumber=6214855713516769&realName=瞿贻晓
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/verify")
    public R verify(ABank4MetaVerifyRequest bankCard2MetaRequest) {
        return doVerifyBankCard2(bankCard2MetaRequest);
    }

    public R doVerifyBankCard2(ABank4MetaVerifyRequest bankCard2MetaRequest) {
        BankCard2MetaResp bankCard2MetaResp = new BankCard2MetaResp();
        // //1 一致 0 不一致，2查无(卡状态异常或已注销)
        ApiR<TBankMetaVerifyResponse> aR = aBankMetaVerifyHandle.handle(bankCard2MetaRequest);
        if (aR.isSuccess()) {
            TBankMetaVerifyResponse bankMetaVerifyResponse = aR.getData();
            if (bankMetaVerifyResponse != null) {
                if (bankMetaVerifyResponse.getCode() == 400) {
                    bankCard2MetaResp.setCheckResult(ApiConstants.FAILED);
                    bankCard2MetaResp.setDesc(bankMetaVerifyResponse.getMsg());
                    return R.ok().setData(bankCard2MetaResp);
                }

                if (bankMetaVerifyResponse.getCode() == 200 && bankMetaVerifyResponse.getData() != null) {
                    int result = bankMetaVerifyResponse.getData().getResult();
                    if (result == 0) {
                        bankCard2MetaResp.setCheckResult(ApiConstants.SUCCESS);
                        bankCard2MetaResp.setDesc(SUCCESS_MSG);
                    } else if (result == 1) {
                        bankCard2MetaResp.setCheckResult(ApiConstants.FAILED);
                        bankCard2MetaResp.setDesc(FAILED_MSG);
                    } else {
                        bankCard2MetaResp.setCheckResult(result + "");
                        bankCard2MetaResp.setDesc(bankMetaVerifyResponse.getData().getMsg());
                    }
                }
                return R.ok().setData(bankCard2MetaResp);
            }
        }

        bankCard2MetaResp.setCheckResult(ApiConstants.FAILED);
        bankCard2MetaResp.setDesc(aR.getErrorMsg());

        return R.ok().setData(bankCard2MetaResp);
    }

}
