package com.project.zcustom.service.service.smart.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.project.common.utils.StringUtils;
import com.project.zcustom.domain.addional.LargeBuildingControl;
import com.project.zcustom.domain.addional.LargeBuildingLoadDetail;
import com.project.zcustom.mapper.unit.PlatBuildingMapper;
import com.project.zcustom.service.service.smart.IPlatBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@Service
public class PlatBuildingServiceImpl implements IPlatBuildingService {

    @Autowired
    private PlatBuildingMapper platBuildingMapper;


    public List<Map<String, Object>> getPowerAndWaterMonth(){
        List<Map<String, Object>> resultList = new ArrayList<>();

        String json = platBuildingMapper.getPowerAndWaterMonth();
        if (StringUtils.isEmpty(json)){
            json = "[{\n" +
                    "\t\"key\": \"water\",\n" +
                    "\t\"value\": \"0\"\n" +
                    "}, {\n" +
                    "\t\"key\": \"power\",\n" +
                    "\t\"value\": \"0\"\n" +
                    "}]";
        }
        JSONArray dataArray = JSONArray.parseArray(json);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String key = dataItem.getString("key");
            String value = dataItem.getString("value");
            Map<String, Object> data = new HashMap<>();
            data.put("name", key);
            data.put("value", new BigDecimal(value));
            resultList.add(data);
        }
        return resultList;
    }

    public List<Map<String, Object>> getAirCondition(){
        List<Map<String, Object>> resultList = new ArrayList<>();

        String json = platBuildingMapper.getAirCondition();
        if (StringUtils.isEmpty(json)){
           json = "[{\n" +
                   "\t\"name\": \"温度\",\n" +
                   "\t\"value\": \"0.00\",\n" +
                   "\t\"value1\": \"0.00\"\n" +
                   "}, {\n" +
                   "\t\"name\": \"湿度\",\n" +
                   "\t\"value\": \"0.00\",\n" +
                   "\t\"value1\": \"0.00\"\n" +
                   "}, {\n" +
                   "\t\"name\": \"二氧化碳\",\n" +
                   "\t\"value\": \"0.00\",\n" +
                   "\t\"value1\": \"0.00\"\n" +
                   "}, {\n" +
                   "\t\"name\": \"PM2.5\",\n" +
                   "\t\"value\": \"0.00\",\n" +
                   "\t\"value1\": \"0.00\"\n" +
                   "}, {\n" +
                   "\t\"name\": \"PM10\",\n" +
                   "\t\"value\": \"0.00\",\n" +
                   "\t\"value1\": \"0.00\"\n" +
                   "}]";
        }
        JSONArray dataArray = JSONArray.parseArray(json);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String name = dataItem.getString("name");
            String valueIn = dataItem.getString("value1");
            String valueOut = dataItem.getString("value");
            Map<String, Object> data = new HashMap<>();
            data.put("name", name);
            data.put("valueIn", valueIn);
            data.put("valueOut", valueOut);
            resultList.add(data);
        }
        return resultList;
    }

    public List<Map<String, Object>> getTarget(){
        List<Map<String, Object>> resultList = new ArrayList<>();

        String json = platBuildingMapper.getTarget();
        if (StringUtils.isEmpty(json)){
            json = "[{\n" +
                    "\t\"guideValue\": 0.00,\n" +
                    "\t\"datumValue\": 0.00,\n" +
                    "\t\"tieValue\": 0.00,\n" +
                    "\t\"value\": \"0.00\",\n" +
                    "\t\"name\": \"单位建筑面积电耗(kW·h)\"\n" +
                    "}, {\n" +
                    "\t\"guideValue\": 0.00,\n" +
                    "\t\"datumValue\": 0.00,\n" +
                    "\t\"tieValue\": 0.00,\n" +
                    "\t\"value\": \"\",\n" +
                    "\t\"name\": \"单位建筑面积水耗(m³)\"\n" +
                    "}, {\n" +
                    "\t\"guideValue\": 0.00,\n" +
                    "\t\"datumValue\": 0.00,\n" +
                    "\t\"tieValue\": 0.00,\n" +
                    "\t\"value\": \"0.00\",\n" +
                    "\t\"name\": \"单位建筑面积综合能耗(kgce)\"\n" +
                    "}, {\n" +
                    "\t\"guideValue\": 0.00,\n" +
                    "\t\"datumValue\": 0.00,\n" +
                    "\t\"tieValue\": 0.00,\n" +
                    "\t\"value\": \"0.00\",\n" +
                    "\t\"name\": \"人均电耗(kW·h)\"\n" +
                    "}, {\n" +
                    "\t\"guideValue\": 0.00,\n" +
                    "\t\"datumValue\": 0.00,\n" +
                    "\t\"tieValue\": 0.00,\n" +
                    "\t\"value\": \"0.00\",\n" +
                    "\t\"name\": \"人均水耗(m³)\"\n" +
                    "}, {\n" +
                    "\t\"guideValue\": 0.00,\n" +
                    "\t\"datumValue\": 0.00,\n" +
                    "\t\"tieValue\": 0.00,\n" +
                    "\t\"value\": \"0.00\",\n" +
                    "\t\"name\": \"人均综合能耗(kgce)\"\n" +
                    "}]";
        }
        JSONArray dataArray = JSONArray.parseArray(json);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String name = dataItem.getString("name");
            String guideValue = dataItem.getString("guideValue");
            String datumValue = dataItem.getString("datumValue");
            String tieValue = dataItem.getString("tieValue");
            String value = dataItem.getString("value");
            Map<String, Object> data = new HashMap<>();
            data.put("name", name);
            data.put("guideValue", guideValue);
            data.put("datumValue", datumValue);
            data.put("tieValue", tieValue);
            BigDecimal bigDecimal1 = new BigDecimal(guideValue);
            BigDecimal bigDecimal2 = new BigDecimal(datumValue);
            BigDecimal bigDecimal3 = new BigDecimal(tieValue);
            BigDecimal bigDecimal = bigDecimal1.add(bigDecimal2).add(bigDecimal3);
            data.put("total", bigDecimal.toString());
            data.put("value", value);
            resultList.add(data);
        }
        return resultList;
    }

    public List<Map<String, Object>> useElectricity(){
        List<Map<String, Object>> resultList = new ArrayList<>();

        String json = platBuildingMapper.useElectricity();
        if (StringUtils.isEmpty(json)){
            return resultList;
        }
        JSONArray dataArray = JSONArray.parseArray(json);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String itemValue = dataItem.getString("itemValue");
            String itemName = dataItem.getString("itemName");
            Map<String, Object> data = new HashMap<>();
            data.put("itemValue", itemValue);
            data.put("itemName", itemName);
            resultList.add(data);
        }
        return resultList;
    }


    public Map<String, Object> greenElectricity(){
        Map<String, Object> data = new HashMap<>();
        String json = platBuildingMapper.greenElectricity();
        if (StringUtils.isEmpty(json)){
            data.put("publicEnergy", 0);
            data.put("grennEnergy", 100);
            return data;
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        String publicEnergy = jsonObject.getString("publicEnergy");
        String grennEnergy = jsonObject.getString("grennEnergy");

        data.put("publicEnergy", publicEnergy);
        data.put("grennEnergy", grennEnergy);
        return data;
    }

    public List<Map<String, Object>> carbon(){
        List<Map<String, Object>> resultList = new ArrayList<>();
        String json = platBuildingMapper.carbon();
        if ( StringUtils.isBlank(json)) {
            return resultList;
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONArray dataArray = jsonObject.getJSONArray("data");
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String name = dataItem.getString("name");
            JSONArray dataArrayItem = dataItem.getJSONArray("data");
            Map<String, Object> data = new HashMap<>();
            data.put("year", name);  // 设置年份字段
            data.put("monthData", dataArrayItem.toJavaList(Double.class));
            resultList.add(data);
        }
        return resultList;
    }

    public Map<String, Object> energyYear(){
        Map<String, Object> res = new HashMap<>();
        String json = platBuildingMapper.energyYear();
        if (StringUtils.isBlank(json)) {
            return res;
        }
        JSONObject dataObject = JSONObject.parseObject(json);
        String yearTotal = dataObject.getString("yearTotal");
        String yearGrowth = dataObject.getString("yearGrowth");
        res.put("yearTotal", yearTotal);
        res.put("yearGrowth", yearGrowth);
        JSONArray twoYearData = dataObject.getJSONArray("twoYearData");
        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        for (int i = 0; i < twoYearData.size(); i++) {
            JSONObject dataItem = twoYearData.getJSONObject(i);
            String year = dataItem.getString("value");
            String lastYear = dataItem.getString("value1");
            xList.add(year);
            yList.add(lastYear);
        }
        res.put("year", xList);
        res.put("lastYear", yList);
        return res;
    }

    public Map<String, Object> powerYear(){
        Map<String, Object> res = new HashMap<>();
        String json = platBuildingMapper.powerYear();
        if (StringUtils.isBlank(json)) {
            return res;
        }
        JSONObject dataObject = JSONObject.parseObject(json);
        String yearTotal = dataObject.getString("yearTotal");
        String yearGrowth = dataObject.getString("yearGrowth");
        res.put("yearTotal", yearTotal);
        res.put("yearGrowth", yearGrowth);
        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        JSONArray twoYearData = dataObject.getJSONArray("twoYearData");
        for (int i = 0; i < twoYearData.size(); i++) {
            JSONObject dataItem = twoYearData.getJSONObject(i);
            String year = dataItem.getString("value");
            String lastYear = dataItem.getString("value1");
            xList.add(year);
            yList.add(lastYear);
        }
        res.put("year", xList);
        res.put("lastYear", yList);
        return res;
    }

    public Map<String, Object> waterYear(){
        Map<String, Object> res = new HashMap<>();
        String json = platBuildingMapper.waterYear();
        if (StringUtils.isBlank(json)) {
            return res;
        }
        JSONObject dataObject = JSONObject.parseObject(json);
        String yearTotal = dataObject.getString("yearTotal");
        String yearGrowth = dataObject.getString("yearGrowth");
        res.put("yearTotal", yearTotal);
        res.put("yearGrowth", yearGrowth);
        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        JSONArray twoYearData = dataObject.getJSONArray("twoYearData");
        for (int i = 0; i < twoYearData.size(); i++) {
            JSONObject dataItem = twoYearData.getJSONObject(i);
            String year = dataItem.getString("value");
            String lastYear = dataItem.getString("value1");
            xList.add(year);
            yList.add(lastYear);
        }
        res.put("year", xList);
        res.put("lastYear", yList);
        return res;
    }

    public Map<String, Object> frequency(){
        Map<String, Object> map = new HashMap<>();
        String json = platBuildingMapper.frequency();
        if ( StringUtils.isBlank(json)) {
            map.put("xList", this.getLastSixMonths());
            Integer[] patrolArray = {0,0,0,0,0,0};
            map.put("yList", patrolArray);
            return map;
        }
        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();
        JSONArray dataArray = JSONArray.parseArray(json);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String month = dataItem.getString("month") + "月";
            String num = dataItem.getString("num");
            Map<String, Object> data = new HashMap<>();
            xList.add(month);
            yList.add(num);
        }
        map.put("xList", xList);
        map.put("yList", yList);
        return map;
    }

    public Map<String, Object> patrol(){
        String json = platBuildingMapper.patrol();
        Map<String, Object> data = new HashMap<>();
        if (StringUtils.isBlank(json)) {
            Integer[] patrolArray = {0,0,0,0,0,0};
            data.put("patrolArray", patrolArray);
            data.put("monthArray", this.getLastSixMonths());
            return data;
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONArray dataArray = jsonObject.getJSONArray("data");
        JSONObject dataItem = dataArray.getJSONObject(0);
        JSONArray patrolArray = dataItem.getJSONArray("data");
        JSONArray monthArray = jsonObject.getJSONArray("xAxis");

        data.put("patrolArray", patrolArray.toJavaList(Double.class));
        data.put("monthArray", monthArray.toJavaList(String.class));
        return data;
    }

    public List<Map<String, Object>> patrolPoint(){
        List<Map<String, Object>> resultList = new ArrayList<>();
        String json = platBuildingMapper.patrolPoint();
        if (StringUtils.isBlank(json)) {
            return resultList;
        }
        JSONArray dataArray = JSONArray.parseArray(json);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String number = dataItem.getString("number");
            String floor = dataItem.getString("floor");
            Map<String, Object> data = new HashMap<>();
            data.put("number", number);
            data.put("floor", floor);
            resultList.add(data);
        }
        return resultList;
    }

    public int updateAirNum(Long num){
        if (platBuildingMapper.getExist() > 0){
            platBuildingMapper.updateAirNum(num);
        }
        return 0;
    }

    public Long getAirNum(){
        return platBuildingMapper.getAirNum();
    }

    public Map<String, Object> getThisWeekElectricity(){
        Map<String, Object> map = new HashMap<>();
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getThisWeekElectricity();
        if (Objects.isNull(largeBuildingControl)) {
            String total = "[{\n" +
                    "\t\"name\": \"光伏用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"楼外设备用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"充电桩用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"地下室用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"制冷机组用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"消防用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"电梯用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"办公用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"公共照明用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"供热设备用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"厨房用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"机房总用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"生活水泵用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"冷源泵用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"热源泵用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}, {\n" +
                    "\t\"name\": \"通信辅助设备用电量\",\n" +
                    "\t\"num\": 0.0\n" +
                    "}]";
            map.put("total", JSONArray.parseArray(total));
            map.put("room", "");
            return map;
        }
        JSONArray weekElectricity = JSONArray.parseArray(largeBuildingControl.getWeekElectricity());
        JSONArray roomElectricity = JSONArray.parseArray(largeBuildingControl.getRoomElectricity());
        map.put("total", weekElectricity);
        map.put("room", roomElectricity);
        return map;
    }

    public JSONObject getControlElevator(){
        String result = platBuildingMapper.getControlElevator();
        if (StringUtils.isBlank(result)){
            result = "{\"weekTotal\":0.00}";
        }
        return JSONObject.parseObject(result);
    }

    public JSONArray getThisAmmeterDetail(String type){
        String result = platBuildingMapper.getThisAmmeterDetail(type);
        if (StringUtils.isNotBlank(result)){
            return JSONArray.parseArray(result);
        }
        return new JSONArray();
    }

    public List<Map<String, Object>> getTodayElectricity(){
        String json = platBuildingMapper.getTodayElectricity();
        if (StringUtils.isBlank(json)) {
            json = "[{\n" +
                    "\t\"time\": \"00\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"01\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"02\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"03\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"04\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"05\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}]";
        }
        JSONArray jsonArray = JSONArray.parseArray(json);
        return repeatElectricity(jsonArray);
    }

    public Map<String, Object> getMonthElectricity(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getMonthElectricity();
        Map<String, Object> map = new HashMap<>();
        if (Objects.isNull(largeBuildingControl)) {
            largeBuildingControl = new LargeBuildingControl();
            String one = "[{\n" +
                    "\t\"time\": \"2025-01\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-02\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-03\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-04\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-05\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-06\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-07\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-08\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-09\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-10\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-11\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}, {\n" +
                    "\t\"time\": \"2025-12\",\n" +
                    "\t\"Kwh\": \"0.0\"\n" +
                    "}]";
            largeBuildingControl.setMonthElectricityOne(one);
            largeBuildingControl.setMonthElectricityTwo(one);
            largeBuildingControl.setMonthElectricityThree(one);
        }
        JSONArray x = JSONArray.parseArray(largeBuildingControl.getMonthElectricityOne());
        JSONArray y = JSONArray.parseArray(largeBuildingControl.getMonthElectricityTwo());
        JSONArray z = JSONArray.parseArray(largeBuildingControl.getMonthElectricityThree());
        List<Map<String, Object>> a = repeatElectricity(x);
        List<Map<String, Object>> b = repeatElectricity(y);
        List<Map<String, Object>> c = repeatElectricity(z);

        map.put("first", a);
        map.put("second", b);
        map.put("third", c);
        return map;
    }

    public Map<String, Object> getRoomLoad(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getRoomLoad();
        Map<String, Object> map = new HashMap<>();
        if (Objects.isNull(largeBuildingControl)) {
            largeBuildingControl = new LargeBuildingControl();
            String json = "[{\n" +
                    "\t\"kz\": 0\n" +
                    "}, {\n" +
                    "\t\"qz\": 0\n" +
                    "}, {\n" +
                    "\t\"zc\": 0\n" +
                    "}, {\n" +
                    "\t\"zz\": 0\n" +
                    "}, {\n" +
                    "\t\"gz\": 0\n" +
                    "}]";
            largeBuildingControl.setRoomLoadOne(json);
            largeBuildingControl.setRoomLoadTwo(json);
            largeBuildingControl.setRoomLoadThree(json);
        }
        JSONArray x = JSONArray.parseArray(largeBuildingControl.getRoomLoadOne());
        JSONArray y = JSONArray.parseArray(largeBuildingControl.getRoomLoadTwo());
        JSONArray z = JSONArray.parseArray(largeBuildingControl.getRoomLoadThree());
        List<Integer> a = new ArrayList<>();
        List<Integer> b = new ArrayList<>();
        List<Integer> c = new ArrayList<>();
        for (Object it : x){
            JSONObject jsonObject = (JSONObject) it;
            Integer value = (Integer) jsonObject.values().iterator().next();
            a.add(value);
        }
        for (Object it : y){
            JSONObject jsonObject = (JSONObject) it;
            Integer value = (Integer) jsonObject.values().iterator().next();
            b.add(value);
        }
        for (Object it : z){
            JSONObject jsonObject = (JSONObject) it;
            Integer value = (Integer) jsonObject.values().iterator().next(); // 获取第一个值（每个 JSON 对象只有一个键值对）
            c.add(value);
        }
        map.put("first", a);
        map.put("second", b);
        map.put("third", c);
        return map;
    }

    public List<LargeBuildingLoadDetail> getRoomLoadDetail(Long kind, Long state) {
        return platBuildingMapper.getRoomLoadDetail(kind, state);
    }

    public Map<String, Object> getWarnDevice(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getWarnDevice();

        Map<String, Object> map = new HashMap<>();
        if (Objects.isNull(largeBuildingControl)) {
            String warnDevice = "{\n" +
                    "\t\"db\": {\n" +
                    "\t\t\"zcdw\": \"0\",\n" +
                    "\t\t\"ycdw\": \"0\"\n" +
                    "\t},\n" +
                    "\t\"ywj\": {\n" +
                    "\t\t\"zcdw\": \"0\",\n" +
                    "\t\t\"ycdw\": \"0\"\n" +
                    "\t},\n" +
                    "\t\"sj\": {\n" +
                    "\t\t\"zcdw\": \"0\",\n" +
                    "\t\t\"ycdw\": \"0\"\n" +
                    "\t},\n" +
                    "\t\"yw\": {\n" +
                    "\t\t\"zcdw\": \"0\",\n" +
                    "\t\t\"ycdw\": \"0\"\n" +
                    "\t}\n" +
                    "}";
            largeBuildingControl = new LargeBuildingControl();
            largeBuildingControl.setWarnDevice(warnDevice);
            largeBuildingControl.setWaterControlAbnormalNum(0L);
            largeBuildingControl.setWaterControlNormalNum(0L);
            largeBuildingControl.setWaterDeviceAirNum(0L);
        }
        JSONObject jsonObject = JSONObject.parseObject(largeBuildingControl.getWarnDevice());
        JSONObject water = jsonObject.getJSONObject("sj");
        water.put("zcdw", largeBuildingControl.getWaterControlNormalNum() + largeBuildingControl.getWaterDeviceAirNum());
        water.put("air", largeBuildingControl.getWaterDeviceAirNum());
        water.put("control", largeBuildingControl.getWaterControlNormalNum() + largeBuildingControl.getWaterControlAbnormalNum());
        map.put("db", repeatWarnDevice(jsonObject.getJSONObject("db")));
        map.put("ywj", repeatWarnDevice(jsonObject.getJSONObject("ywj")));
        map.put("sj", repeatWarnDevice(water));
        map.put("yw", repeatWarnDevice(jsonObject.getJSONObject("yw")));
        return map;
    }

    public JSONArray getAbnormalPoint(String type){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getAbnormalPoint();
        if (Objects.isNull(largeBuildingControl)) {
            return new JSONArray();
        }
        switch (type){
            case "1":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointTwo());
            case "2":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointThree());
            case "3":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointOne());
            case "4":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointFour());
            default:
                return new JSONArray();
        }
    }

    public JSONArray getWarnEvent(){
        String json = platBuildingMapper.getWarnEvent();
        if (StringUtils.isBlank(json)) {
            json = "[{\"db\":\"0\",\"sj\":\"0\",\"yw\":\"0\",\"ywj\":\"0\",\"month\":\"1\"},{\"db\":\"0\",\"sj\":\"0\",\"yw\":\"0\",\"ywj\":\"0\",\"month\":\"2\"},{\"db\":\"0\",\"sj\":\"0\",\"yw\":\"0\",\"ywj\":\"0\",\"month\":\"3\"},{\"db\":\"0\",\"sj\":\"0\",\"yw\":\"0\",\"ywj\":\"0\",\"month\":\"4\"},{\"db\":\"0\",\"sj\":\"0\",\"yw\":\"0\",\"ywj\":\"0\",\"month\":\"5\"},{\"db\":\"0\",\"sj\":\"0\",\"yw\":\"0\",\"ywj\":\"0\",\"month\":\"6\"}]";
        }
        return JSONArray.parseArray(json);
    }

    public Map<String, Object> getWarnState(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getWarnState();
        Map<String, Object> map = new HashMap<>();
        if (Objects.isNull(largeBuildingControl)) {
            map.put("twentyThree", JSONObject.parseObject("{\"shsx\":[{\"dqsw\":\"0m\"},{\"zdsw\":\"0m\"},{\"zgsw\":\"00m\"}],\"xfsx\":[{\"dqsw\":\"0m\"},{\"zdsw\":\"0m\"},{\"zgsw\":\"0m\"}]}"));
            map.put("minusTwo", JSONObject.parseObject("{\"gqsx\":[{\"dqsw\":\"0m\"},{\"zdsw\":\"0m\"},{\"zgsw\":\"0m\"}],\"dqsx\":[{\"dqsw\":\"0m\"},{\"zdsw\":\"0m\"},{\"zgsw\":\"0m\"}]}"));
            return map;
        }
        map.put("twentyThree", JSONObject.parseObject(largeBuildingControl.getWaterStateOne()));
        map.put("minusTwo", JSONObject.parseObject(largeBuildingControl.getWaterStateTwo()));
        return map;
    }


    private JSONObject repeatWarnDevice(JSONObject jsonObject){
        if (Objects.isNull(jsonObject)) {
            return new JSONObject();
        }
        Long x = Long.parseLong(jsonObject.getString("zcdw")) + Long.parseLong(jsonObject.getString("ycdw"));
        jsonObject.put("total", x);
        return jsonObject;
    }

    private List<Map<String, Object>> repeatElectricity(JSONArray jsonArray){
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject item = jsonArray.getJSONObject(i);
            Map<String, Object> map = new HashMap<>();
            map.put("time", item.getString("time"));
            map.put("Kwh", item.getString("Kwh"));
            list.add(map);
        }
        return list;
    }

    private String[] getLastSixMonths() {
        LocalDate currentDate = LocalDate.now();
        // 自定义格式化模式，确保月份没有前导零并附加“月”字
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M'月'");
        String[] monthsArray = new String[6]; // 创建一个长度为6的数组存储月份信息

        for (int i = 0; i < 6; i++) {
            LocalDate firstDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth());
            monthsArray[5 - i] = firstDayOfMonth.format(formatter);
            currentDate = currentDate.minusMonths(1);
        }
        return monthsArray;
    }

    public JSONObject getPhotovoltaicPower(){
        String result = platBuildingMapper.getPhotovoltaicPower();
        if (StringUtils.isBlank(result)){
            result = "{\n" +
                    "        \"sum\":\"0\",\n" +
                    "        \"bm\":\"0\",\n" +
                    "        \"gfb\":\"0\"\n" +
                    "    }";
        }
        return JSONObject.parseObject(result);
    }
}
