package com.energy.manage.common.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author LIKE
 * @date 2017年1月18日 下午6:07:06
 */
@Slf4j
public class HttpUtils {

    private final static String GET_UNION_STR = "?";

    /**
     * http请求时长
     */
    private final static int TIME_OUT = 3000;


    /**
     * get 方法
     * @param url
     * @param
     * @return
     */
    public static JSONObject doGet(String url, Map<String,Object> param){
        //做字符串拼接
        url = url + GET_UNION_STR + HttpUtil.toParams(param);
        return JSONObject.parseObject(HttpUtil.get(url,TIME_OUT));
    }

    /**
     * post 方法
     * @param url
     * @param params
     * @return
     */
    public static String doPost(String url, String params){
        try{
            // 创建POST请求
            HttpRequest request = HttpUtil.createPost(url);
            // 设置请求头Content-Type为application/json
            request.header("Content-Type", "application/json");
            return request.body(params).execute().body();
            // 执行请求
        }catch (Exception e){
            log.error("url = {} 请求异常",url,e);
            return "请求异常,请查看日志";
        }
    }

    public static void main(String[] args) {
        String param = "{\n" +
                "\t\"dataContractType\": {\n" +
                "\t\t\"type\": \"analysisExecuteOrder\",\n" +
                "\t\t\"version\": \"1.2.0\"\n" +
                "\t},\n" +
                "\t\"dataContract\": {\n" +
                "\t\t\"autoOrManual\": \"automatic\",\n" +
                "\t\t\"dataFilter\": {\n" +
                "\t\t\t\"powerFarmID\": \"WOF031300036\",\n" +
                "\t\t\t\"turbines\": null,\n" +
                "\t\t\t\"dataBatchNum\": \"zhaoyuan_20240528\",\n" +
                "\t\t\t\"beginTime\": \"2023-01-01 00:00:00\",\n" +
                "\t\t\t\"endTime\": \"2024-12-31 23:59:59\",\n" +
                "\t\t\t\"excludingMonths\": [\n" +
                "\t\t\t\t\"2023-12\",\n" +
                "\t\t\t\t\"2023-09\"\n" +
                "\t\t\t],\n" +
                "\t\t\t\"customFilter\": {\n" +
                "\t\t\t\t\"valueWindSpeed\": {\n" +
                "\t\t\t\t\t\"min\": null,\n" +
                "\t\t\t\t\t\"max\": null\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"valuePitchAngle\": {\n" +
                "\t\t\t\t\t\"min\": null,\n" +
                "\t\t\t\t\t\"max\": null\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"valueActivePower\": {\n" +
                "\t\t\t\t\t\"min\": null,\n" +
                "\t\t\t\t\t\"max\": null\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"valueGeneratorSpeed\": {\n" +
                "\t\t\t\t\t\"min\": null,\n" +
                "\t\t\t\t\t\"max\": null\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}\n" +
                "\t\t},\n" +
                "\t\t\"configAnalysis\": [\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"package\": \"algorithm.temperatureLargeComponentsAnalyst\",\n" +
                "\t\t\t\t\"className\": \"TemperatureLargeComponentsAnalyst\",\n" +
                "\t\t\t\t\"methodName\": \"executeAnalysis\",\n" +
                "\t\t\t\t\"scada\": \"minute\"\n" +
                "\t\t\t},\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"package\": \"algorithm.generatorSpeedPowerAnalyst\",\n" +
                "\t\t\t\t\"className\": \"GeneratorSpeedPowerAnalyst\",\n" +
                "\t\t\t\t\"methodName\": \"executeAnalysis\",\n" +
                "\t\t\t\t\"scada\": \"minute\"\n" +
                "\t\t\t},\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"package\": \"algorithm.generatorSpeedTorqueAnalyst\",\n" +
                "\t\t\t\t\"className\": \"GeneratorSpeedTorqueAnalyst\",\n" +
                "\t\t\t\t\"methodName\": \"executeAnalysis\",\n" +
                "\t\t\t\t\"scada\": \"minute\"\n" +
                "\t\t\t}\n" +
                "\t\t],\n" +
                "\t\t\"graphSets\": {\n" +
                "            \"DgeneratorSpeed\": {\n" +
                "                \"step\": 5,\n" +
                "                \"min\": 0,\n" +
                "                \"max\": 30\n" +
                "            },\n" +
                "            \"DgeneratorTorque\": {\n" +
                "                \"step\": 10000,\n" +
                "                \"min\": 0,\n" +
                "                \"max\": 100000\n" +
                "            },\n" +
                "            \"IgeneratorSpeed\": {\n" +
                "                \"step\": 200,\n" +
                "                \"min\": 1000,\n" +
                "                \"max\": 2000\n" +
                "            },\n" +
                "            \"IgeneratorTorque\": {\n" +
                "                \"step\": 2000,\n" +
                "                \"min\": 0,\n" +
                "                \"max\": 12000\n" +
                "\t\t\t},\n" +
                "\t\t\t\"cp\": {\n" +
                "\t\t\t\t\"step\": 0.5,\n" +
                "\t\t\t\t\"min\": 0,\n" +
                "\t\t\t\t\"max\": 2\n" +
                "\t\t\t},\n" +
                "\t\t\t\"tsr\": {\n" +
                "\t\t\t\t\"step\": 5,\n" +
                "\t\t\t\t\"min\": 0,\n" +
                "\t\t\t\t\"max\": 30\n" +
                "\t\t\t},\n" +
                "\t\t\t\"pitchAngle\": {\n" +
                "\t\t\t\t\"step\": 1,\n" +
                "\t\t\t\t\"min\": -1,\n" +
                "\t\t\t\t\"max\": 20\n" +
                "\t\t\t},\n" +
                "\t\t\t\"activePower\": {\n" +
                "\t\t\t\t\"step\": 250,\n" +
                "\t\t\t\t\"min\": 0,\n" +
                "\t\t\t\t\"max\": 2000\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";
        // 创建POST请求
        HttpRequest request = HttpUtil.createPost("http://192.168.5.7:8000/demo/dataAnalysis/analysis");
        // 设置请求头Content-Type为application/json
        request.header("Content-Type", "application/json");
        // 执行请求
        String response = request.body(param).execute().body();
        System.out.println(response);
    }
}
