package com.heyu.api.tencent;


import com.alibaba.fastjson.JSON;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.resp.TMobile3MetaVerificationResp;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class TencentBaseHandle<P, RR> {

    /**
     * 较验参数
     */
    public abstract String check(P p);

    public ApiR<RR> handle(P p) {
        try {
            log.info("TencentBaseHandle param : {}", JSON.toJSONString(p));
            String check = check(p);
            if (StringUtils.isNotBlank(check)) {
                log.info("TencentBaseHandle check failed : {}", check);
                return ApiR.error(check);
            }
            RR r = run(p);

            if(r == null){
                log.error("TencentBaseHandle run failed : {}", JSON.toJSONString(p));
                return ApiR.error("服务器异常请稍后重试");
            }

            log.info("TencentBaseHandle result : {}", JSON.toJSONString(r));
            return ApiR.setData(r).setReceiveMoney(true);
        } catch (Exception e) {
            log.error("请求腾讯异常", e);
        }
        log.error("TencentBaseHandle run end failed  : {}", JSON.toJSONString(p));
        return ApiR.error("服务器异常请稍后重试");
    }

    public abstract RR run(P ap) throws Exception;

    public String checkIdCard(TencentIdCardBaseRequest tencentIdCardBaseRequest) {
        if (StringUtils.isBlank(tencentIdCardBaseRequest.getIdCard())) {
            return "idCard不能为空";
        }
        if (StringUtils.isBlank(tencentIdCardBaseRequest.getName())) {
            return "name 真实姓名不能为空";
        }
        return null;
    }






    public boolean checkMobileResp(TMobile3MetaVerificationResp bank2MetaVerifyResponse) {
        if (bank2MetaVerifyResponse != null) {
            if (bank2MetaVerifyResponse.getCode() == 400) {
                return true;

            }
            if (bank2MetaVerifyResponse.getData() != null) {
                if ("1".equals(bank2MetaVerifyResponse.getData().getResult())
                        || "2".equals(bank2MetaVerifyResponse.getData().getResult())) {
                    return true;
                }
            }
        }
        return false;
    }


}
