package com.energy.manage.service.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author chy
 * @date 2024/7/10 14:24
 * @desc
 */
public class TestJson2 {

    public static void main(String[] args) {
        String jsonString = "{\n" +
                "    \"dataContractType\": {\n" +
                "        \"type\": \"analysisExecuteOrder\",\n" +
                "        \"version\": \"1.2.0\"\n" +
                "    },\n" +
                "    \"dataContract\": {\n" +
                "        \"autoOrManual\": \"automatic\",\n" +
                "        \"dataFilter\": {\n" +
                "            \"powerFarmID\": \"010-00001\",\n" +
                "            \"turbines\": null,\n" +
                "            \"dataBatchNum\": \"B2024042211-0\",\n" +
                "            \"beginTime\": \"2023-01-01 00:00:00\",\n" +
                "            \"endTime\": \"2023-12-31 23:59:59\",\n" +
                "            \"excludingMonths\": null,\n" +
                "            \"customFilter\": {\n" +
                "                \"valueWindSpeed\": {\n" +
                "                    \"min\": null,\n" +
                "                    \"max\": null\n" +
                "                },\n" +
                "                \"valuePitchAngle\": {\n" +
                "                    \"min\": null,\n" +
                "                    \"max\": null\n" +
                "                },\n" +
                "                \"valueActivePower\": {\n" +
                "                    \"min\": null,\n" +
                "                    \"max\": null\n" +
                "                },\n" +
                "                \"valueGeneratorSpeed\": {\n" +
                "                    \"min\": null,\n" +
                "                    \"max\": null\n" +
                "                }\n" +
                "            }\n" +
                "        },\n" +
                "        \"configAnalysis\": [],\n" +
                "        \"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" +
                "            },\n" +
                "            \"cp\": {\n" +
                "                \"step\": 0.5,\n" +
                "                \"min\": 0,\n" +
                "                \"max\": 2\n" +
                "            },\n" +
                "            \"tsr\": {\n" +
                "                \"step\": 5,\n" +
                "                \"min\": 0,\n" +
                "                \"max\": 30\n" +
                "            },\n" +
                "            \"pitchAngle\": {\n" +
                "                \"step\": 1,\n" +
                "                \"min\": -1,\n" +
                "                \"max\": 20\n" +
                "            },\n" +
                "            \"activePower\": {\n" +
                "                \"step\": 250,\n" +
                "                \"min\": 0,\n" +
                "                \"max\": 2000\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}";
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
//        updateJsonByPropertyName(jsonObject, "autoOrManual", "123");
        System.out.println(jsonObject.toJSONString());

    }

    public static void updateJsonByPropertyName(JSONObject jsonObject, String propertyName, String propertyValue) {
        if (jsonObject.containsKey(propertyName)) {
            jsonObject.put(propertyName, propertyValue);
        } else {
            jsonObject.keySet().forEach(key -> {
                Object v = jsonObject.get(key);
                if (v instanceof JSONObject) {
                    updateJsonByPropertyName((JSONObject) v, key, propertyValue);
                } else if (v instanceof JSONArray) {
                    JSONArray jsonArray = (JSONArray) v;
                    for (int i = 0; i < jsonArray.size(); i++) {
                        Object item = jsonArray.get(i);
                        if (item instanceof JSONObject) {
                            updateJsonByPropertyName((JSONObject) item, key, propertyValue);
                        }
                    }
                }
            });
        }

    }
}
