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 TMobile2MetaHandle 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())) {


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


        return "";
    }

    @Override
    public TMobile3MetaVerificationResp run(TMobile3MetaVerificationRequest ap) throws Exception {
        TMobile3MetaVerificationResp resp = null;

        ApiMobileMetaEntity apiMobileMeta = apiMobileMetaDao.selectApiMobileMetaByMobileRealNameChannelEnableStatus(ap.getMobile(), ap.getRealName(), 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-mall/mobile_two/check";
        String method = "POST";
        String appcode = ApiConstants.APP_CODE;

        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("mobile", ap.getMobile());
        bodys.put("name", ap.getRealName());
        /**
         *
         *
         * 重要提示如下:
         * HttpUtils请从
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
         * 下载
         *
         * 相应的依赖请参照
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
         */
        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);

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

        apiMobileMeta = new ApiMobileMetaEntity();
        apiMobileMeta.setMobile(ap.getMobile());
        apiMobileMeta.setRealName(ap.getRealName());
        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;
    }
}
