package com.kmall.manager.manager.quotalInquiry;

import com.kmall.common.utils.JacksonUtils;
import com.kmall.common.utils.MD5Sign;
import com.kmall.common.utils.ResponseData;
import com.kmall.manager.dto.CrossBoundaryQuotalDto;
import com.kmall.manager.manager.common.OkHttpUtilss;
import com.kmall.manager.manager.merch.OmsMerchPropertiesBuilder;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
 * 跨境额度查询
 * @author huangyq
 * @version 1.0
 * 2019-11-27 14:30
 */
public class CrossBoundaryQuotalInquiryUtil {
    private static Logger loggger = LoggerFactory.getLogger(CrossBoundaryQuotalInquiryUtil.class);

    public static ResponseData getCrossBoundaryQuotaQuery(CrossBoundaryQuotalDto crossBoundaryQuotalDto){
        Map<String,String> sParaTemp = new TreeMap<String, String>();
        sParaTemp.put("merchId", crossBoundaryQuotalDto.getMerchId());
        sParaTemp.put("merchName", crossBoundaryQuotalDto.getMerchName());
        sParaTemp.put("thirdPartyMerchCode", crossBoundaryQuotalDto.getThirdPartyMerchCode());
        sParaTemp.put("thirdPartyMerchName", crossBoundaryQuotalDto.getThirdPartyMerchName());
        sParaTemp.put("idCard", crossBoundaryQuotalDto.getIdCard());
        sParaTemp.put("name", crossBoundaryQuotalDto.getName());
        sParaTemp.put("isCheckIdCard", crossBoundaryQuotalDto.getIsCheckIdCard());
        String params = JacksonUtils.toJson(sParaTemp);

        Map map = new HashMap();
        map.put("data", params);
        map.put("timestamp", System.currentTimeMillis()/1000);
        map.put("merchId", crossBoundaryQuotalDto.getMerchId());
        String md5Salt = OmsMerchPropertiesBuilder.instance().getMd5Salt();

        String si = MD5Sign.sign(map, md5Salt);
        map.put("sign",si);
        String requestData = JacksonUtils.toJson(map);
        loggger.info("请求oms跨境额度查询接口参数："+requestData);
        String response = buildRequestPost(requestData);
        loggger.info("oms跨境额度查询接口返回参数："+response);
        ResponseData responseData = JacksonUtils.fromStringJson(response, ResponseData.class);
        return responseData;
    }

    private static String buildRequestPost(String requestData){
        String url = OmsMerchPropertiesBuilder.instance().getQuotaInquiryUrl();
//        String url = "http://192.168.1.121:8680/al/cus/yearLimit";
        // 同步访问,返回结果字符串
        Request request = OkHttpUtilss.buildRequest(url,requestData);
        // 同步访问,返回结果字符串
        String response = null;
        try {
            response = OkHttpUtilss.post(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}
