package com.heyu.api.controller.mobile;

import com.heyu.api.data.annotation.EbAuthentication;
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.resp.certificate.MobileMetaResp;
import com.heyu.api.tencent.handle.TMobile2MetaHandle;
import com.heyu.api.tencent.handle.TMobile3MetaHandle;
import com.heyu.api.tencent.request.TMobile3MetaVerificationRequest;
import com.heyu.api.tencent.resp.TMobile3MetaVerificationResp;
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("/mobile/23/meta")

@NotIntercept
public class Mobile23MetaVerificationController {


    public static final String SUCCESS_MSG = "信息匹配";
    public static final String FAILED_MSG = "信息不匹配";


    @Autowired
    private TMobile2MetaHandle tMobile2MetaHandle;


    @Autowired
    private TMobile3MetaHandle tMobile3MetaHandle;



    // https://api.1024api.com/api-interface/bank/card/234/verify?bankCardNumber=6214855713516769&realName=瞿贻晓
    // https://api.1024api.com/api-interface/mobile/23/meta/verify
    @RequestMapping("/verify")
    @EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
    public R verify(TMobile3MetaVerificationRequest request) {
        return doVerify(request);
    }

    public R doVerify(TMobile3MetaVerificationRequest request) {
        MobileMetaResp mobileMetaResp = new MobileMetaResp();
        ApiR<TMobile3MetaVerificationResp> tR = null;


        if (StringUtils.isNotBlank(request.getMobile())
                && StringUtils.isNotBlank(request.getRealName())
                && StringUtils.isNotBlank(request.getIdCardNumber())
        ) {
            tR = tMobile3MetaHandle.handle(request);
        } else if (StringUtils.isNotBlank(request.getMobile())
                && StringUtils.isNotBlank(request.getRealName())
        ) {
            tR = tMobile2MetaHandle.handle(request);
        } else {
            return R.error("手机号 + 姓名 不能为空");
        }


        if (tR.isSuccess()) {
            TMobile3MetaVerificationResp bankMetaVerifyResponse = tR.getData();

            if (bankMetaVerifyResponse != null) {
                if (bankMetaVerifyResponse.getCode() == 400) {
                    mobileMetaResp.setCheckResult(ApiConstants.FAILED);
                    mobileMetaResp.setDesc(bankMetaVerifyResponse.getMsg());
                    return R.ok().setData(mobileMetaResp);
                }

                if (bankMetaVerifyResponse.getCode() == 200 && bankMetaVerifyResponse.getData() != null) {
                    String result = bankMetaVerifyResponse.getData().getResult();
                    if ("0".equals(result)) {
                        mobileMetaResp.setCheckResult(ApiConstants.SUCCESS);
                        mobileMetaResp.setDesc(SUCCESS_MSG);
                    } else if ("1".equals(result)) {
                        mobileMetaResp.setCheckResult(ApiConstants.FAILED);
                        mobileMetaResp.setDesc(FAILED_MSG);
                    } else {
                        mobileMetaResp.setCheckResult(result + "");
                        mobileMetaResp.setDesc(bankMetaVerifyResponse.getData().getDesc());
                    }
                }
                return R.ok().setData(mobileMetaResp);
            }
        }
        return R.error(tR.getErrorMsg());
    }

}
