package com.heyu.api.controller.bankcard;


import com.heyu.api.alibaba.handle.common.text.ABank3MetaVerifyHandle;
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://market.aliyun.com/apimarket/detail/cmapi00066584#sku=yuncode60584000011
 *
 *  *
 *  *银行卡二要素核验
 */
@Slf4j
@RestController
@RequestMapping("/bank/card/3")
public class BankCard3MetaController extends BaseController {


    @Autowired
    private ABank3MetaVerifyHandle aBank3MetaVerifyHandle;


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



    // http://localhost:8888/bank/card/3/verify?bankCard=6214855713516769&realName=瞿贻晓&idCardNumber=430529199209255030
    // https://api.1024api.com/api-interface/bank/card/3/verify?bankCard=6214855713516769&realName=瞿贻晓&idCardNumber=430529199209255030
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    @RequestMapping("/verify")
    public R verify(ABank4MetaVerifyRequest bankCard3MetaRequest) {

        return doVerifyBankCard3(bankCard3MetaRequest);
    }

    public R doVerifyBankCard3(ABank4MetaVerifyRequest bankCard3MetaRequest){
        BankCard2MetaResp bankCard2MetaResp = new BankCard2MetaResp();
        // //1 一致 0 不一致，2查无(卡状态异常或已注销)
        ApiR<TBankMetaVerifyResponse> aR = aBank3MetaVerifyHandle.handle(bankCard3MetaRequest);
        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);
    }

}
