package com.heyu.api.tencent.handle;

import com.alibaba.fastjson.JSONObject;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.dao.api.ApiMobileMetaDao;
import com.heyu.api.data.entity.api.ApiMobileMetaEntity;
import com.heyu.api.data.service.api.ApiRechargeMoneyService;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.TencentBaseHandle;
import com.heyu.api.tencent.request.TMobile3MetaVerificationRequest;
import com.heyu.api.tencent.resp.TMobile3MetaVerificationResp;
import com.heyu.api.data.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/***
 * https://market.aliyun.com/apimarket/detail/cmapi00066757#sku=yuncode6075700003
 *
 *身份信息认证（二要素核验）
 *
 *
 */
@Component
public class TMobile3MetaHandle extends TencentBaseHandle<TMobile3MetaVerificationRequest, TMobile3MetaVerificationResp> {


    @Autowired
    private ApiMobileMetaDao apiMobileMetaDao;

    @Autowired
    private ApiRechargeMoneyService apiRechargeMoneyService;


    @Override
    public String check(TMobile3MetaVerificationRequest tMobile3MetaVerificationRequest) {


        if (StringUtils.isBlank(tMobile3MetaVerificationRequest.getMobile())
                || StringUtils.isBlank(tMobile3MetaVerificationRequest.getRealName())
                || StringUtils.isBlank(tMobile3MetaVerificationRequest.getIdCardNumber())

        ) {


            return "手机号 + 真实姓名 + 身份证号 不能为空";
        }
        return null;
    }

    @Override
    public TMobile3MetaVerificationResp run(TMobile3MetaVerificationRequest ap) throws Exception {
        ApiMobileMetaEntity apiMobileMeta2 = apiMobileMetaDao.selectApiMobileMetaByMobileRealNameChannelEnableStatus(ap.getMobile(), ap.getRealName(), ApiConstants.third, 1);
        if (apiMobileMeta2 != null) {
            TMobile3MetaVerificationResp resp = JSONObject.parseObject(apiMobileMeta2.getApiResult(), TMobile3MetaVerificationResp.class);
            // 如果二要素都失败，那直接返回 ，这样增加撞库的次数，节约成本
            if (checkMobileResp(resp)) {
                apiMobileMetaDao.updateApiMobileMetaUseCountById(1L, apiMobileMeta2.getId());
                return resp;
            }
        }


        TMobile3MetaVerificationResp resp = null;
        ApiMobileMetaEntity apiMobileMeta = apiMobileMetaDao.selectApiMobileMetaByMobileRealNameIdCardNumberChannelEnableStatus(ap.getMobile(), ap.getRealName(), ap.getIdCardNumber(), ApiConstants.third, 1);
        if (apiMobileMeta != null) {
            resp = JSONObject.parseObject(apiMobileMeta.getApiResult(), TMobile3MetaVerificationResp.class);
            apiMobileMetaDao.updateApiMobileMetaUseCountById(1L, apiMobileMeta.getId());
            return resp;
        }


        String host = "https://kzmobilev2.market.alicloudapi.com";
        String path = "/api/mobile_three/check";
        String method = "GET";
        String appcode = ApiConstants.APP_CODE;
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", ap.getMobile());
        querys.put("name", ap.getRealName());
        querys.put("idcard", ap.getIdCardNumber());


        HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);

        //获取response的body
        String result = EntityUtils.toString(response.getEntity());


        apiMobileMeta = new ApiMobileMetaEntity();
        apiMobileMeta.setMobile(ap.getMobile());
        apiMobileMeta.setRealName(ap.getRealName());
        apiMobileMeta.setIdCardNumber(ap.getIdCardNumber());

        apiMobileMeta.setChannel(ApiConstants.third);
        apiMobileMeta.setEnableStatus(1);
        apiMobileMeta.setApiResult(result);
        apiMobileMetaDao.insertApiMobileMeta(apiMobileMeta);


        apiRechargeMoneyService.addUseCountAndSendWarnMsg(ApiConstants.mobile_verification_3, 1);

        resp = JSONObject.parseObject(result, TMobile3MetaVerificationResp.class);
        return resp;
    }
}
