package com.tofly.xrtymis.custom.utils;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
@Slf4j
public class ForeignHttpUtils {

    public static String sendPost(String url, String path, String key, TreeMap<String,Object> map) throws Exception {

        String urlPath = url+path;
        String sign = dataSign(map,key);
        map.put("sign",sign);
        if(!map.containsKey("zzjl")){
            map.put("zzjl","");
        }
        log.info(JSONObject.toJSONString(map));
        String result2 = HttpRequest.post(urlPath)
                .header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")//头信息，多个头信息多次调用此方法即可
                .body(JSONObject.toJSONString(map))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("调用网厅平台"+result2);
        if(StringUtils.isNotEmpty(result2)){
            Map<String,Object> mapr=JSONObject.parseObject(result2,Map.class);
            if(mapr.containsKey("code")){
                return String.valueOf(mapr.get("code"));
            }
            // 抄表平台解析
            if(mapr.containsKey("result")){
                Map result = (Map)mapr.get("result");
                return String.valueOf(result.get("code"));
            }
        }
        return result2;
    }

    private static String dataSign(TreeMap<String, Object> map,String key) throws Exception {
        String asciiStr = asciiStr(map);
        asciiStr = asciiStr+key;

        return shaEncode(asciiStr);
    }

    private static String asciiStr(TreeMap<String, Object> map){
        StringBuilder sb = new StringBuilder();
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            sb.append("&").append(key).append("=").append(map.get(key));

        }
        if(sb.length()>0){
            sb.deleteCharAt(0);
        }

        return sb.toString();
    }

    public static String shaEncode(String inStr) throws Exception {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8);
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().toUpperCase();
    }


}
