package com.jjt.ep;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.nio.charset.StandardCharsets;

/**
 * EnvPowerTest$
 *
 * @author wukai
 * @date 2025/2/18 01:42
 */
public class EnvPowerTest {
    public static void main(String[] args) throws Exception {
        System.err.println(System.getProperty("java.net.preferIPv4Stack"));
        String uri = "http://192.168.79.21:18080/truelove/energy/manage/monthlyFinancialStatement";
        String date = "2025-03-03";
        JSONObject object = new JSONObject();
        object.set("selectTime", date);
        object.set("timeType", "DAY");
        HttpRequest request = HttpUtil.createPost(uri);
        request.setMethod(Method.POST);
        request.body(object.toString());
//        request.setConnectionTimeout(5000);
        try (HttpResponse execute = request.execute()) {
            if (!execute.isOk()) {
                throw new RuntimeException(execute.body());
            }
            String res = new String(execute.body().getBytes(), StandardCharsets.UTF_8);
            JSONObject obj = JSONUtil.parseObj(res, true);
            if (obj.getInt("code") == 200) {
                JSONArray datas = obj.getJSONArray("data");
                JSONObject data = datas.getJSONObject(0);
                JSONArray childrens = data.getJSONArray("childrens");
//                System.err.println(childrens);
                for (int i = 0; i < childrens.size(); i++) {
                    JSONObject cds = childrens.getJSONObject(i);
                    String name = cds.getStr("name");
                    String energyType = cds.getStr("energyType");
                    String spatialLevel = cds.getStr("spatialLevel");
                    String id = cds.getStr("id");
                    String upId = cds.getStr("upId");
                    System.err.println(name + "\t" + energyType + "\t" + spatialLevel + "\t" + id + "\t" + upId);
                    if ("1862779623916564481".equals(id)||"1851500076220350465".equals(id)) {
                        JSONArray cds1 = cds.getJSONArray("childrens");
                        for (int j = 0; j < cds1.size(); j++) {
                            JSONObject ccc = cds1.getJSONObject(j);
                            name = ccc.getStr("name");
                            energyType = ccc.getStr("energyType");
                            spatialLevel = ccc.getStr("spatialLevel");
                            id = ccc.getStr("id");
                            upId = ccc.getStr("upId");
                            System.err.println("\t\t"+name + "\t" + energyType + "\t" + spatialLevel + "\t" + id + "\t" + upId);
                            if ("1851500278780067842".equals(id)) {
                                JSONArray cds12 = ccc.getJSONArray("childrens");
                                for (int k = 0; k < cds12.size(); k++) {
                                    JSONObject ddd = cds12.getJSONObject(k);
                                    name = ddd.getStr("name");
                                    energyType = ddd.getStr("energyType");
                                    spatialLevel = ddd.getStr("spatialLevel");
                                    id = ddd.getStr("id");
                                    upId = ddd.getStr("upId");
                                    System.err.println("\t\t"+name + "\t" + energyType + "\t" + spatialLevel + "\t" + id + "\t" + upId);
                                }
                            }
                        }
                    }

                }


            }
        }
    }
}
