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.resp.certificate.MobileMetaResp;
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/3/meta")

@NotIntercept
public class Mobile3MetaVerificationController {


    public static final String SUCCESS_MSG = "手机号+姓名+身份证信息匹配";
    public static final String FAILED_MSG = "手机号+姓名+身份证信息不匹配";


    @Autowired
    private TMobile3MetaHandle tMobile3MetaHandle;

    // http://localhost:8888/mobile/3/meta/verify?mobile=18458195149&realName=瞿贻晓&idCardNumber=430529199209255030

    // https://api.1024api.com/api-interface/mobile/3/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 = tMobile3MetaHandle.handle(request);
        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());
    }

}
