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

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.mapper.unit.PlatBuildingMapper;
import com.project.zcustom.service.unit.IPlatBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlatBuildingServiceImpl implements IPlatBuildingService {

    @Autowired
    private PlatBuildingMapper platBuildingMapper;

    public List<String> getPowerAndWaterMonth(){
        List<String> list = new ArrayList<>();
        String json = platBuildingMapper.getPowerAndWaterMonth();
        if (StringUtils.isEmpty(json)){
            return null;
        }
        JSONArray dataArray = JSONArray.parseArray(json);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataItem = dataArray.getJSONObject(i);
            String value = dataItem.getString("value");
            list.add(value);
        }
        return list;
    }

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

        String json = platBuildingMapper.getAirCondition();
        if (StringUtils.isEmpty(json)){
            return null;
        }
        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)){
            return null;
        }
        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);
            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 null;
        }
        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 List<Map<String, Object>> greenElectricity(){
        List<Map<String, Object>> resultList = new ArrayList<>();

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

    public List<Map<String, Object>> carbon(){
        List<Map<String, Object>> resultList = new ArrayList<>();
        String json = platBuildingMapper.carbon();
        if (json == null || json.isEmpty()) {
            return null;
        }
        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<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        String json = platBuildingMapper.energyYear();
        if (json == null || json.isEmpty()) {
            return null;
        }
        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");
        for (int i = 0; i < twoYearData.size(); i++) {
            JSONObject dataItem = twoYearData.getJSONObject(i);
            String year = dataItem.getString("value");
            String lastYear = dataItem.getString("value1");
            String month = dataItem.getString("key");
            Map<String, Object> data = new HashMap<>();
            data.put("year", year);
            data.put("lastYear", lastYear);
            data.put("month", month);
            resultList.add(data);
        }
        res.put("monthData", resultList);
        return res;
    }

    public Map<String, Object> powerYear(){
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        String json = platBuildingMapper.powerYear();
        if (json == null || json.isEmpty()) {
            return null;
        }
        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");
        for (int i = 0; i < twoYearData.size(); i++) {
            JSONObject dataItem = twoYearData.getJSONObject(i);
            String year = dataItem.getString("value");
            String lastYear = dataItem.getString("value1");
            String month = dataItem.getString("key");
            Map<String, Object> data = new HashMap<>();
            data.put("year", year);
            data.put("lastYear", lastYear);
            data.put("month", month);
            resultList.add(data);
        }
        res.put("monthData", resultList);
        return res;
    }

    public Map<String, Object> waterYear(){
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        String json = platBuildingMapper.waterYear();
        if (json == null || json.isEmpty()) {
            return null;
        }
        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");
        for (int i = 0; i < twoYearData.size(); i++) {
            JSONObject dataItem = twoYearData.getJSONObject(i);
            String year = dataItem.getString("value");
            String lastYear = dataItem.getString("value1");
            String month = dataItem.getString("key");
            Map<String, Object> data = new HashMap<>();
            data.put("year", year);
            data.put("lastYear", lastYear);
            data.put("month", month);
            resultList.add(data);
        }
        res.put("monthData", resultList);
        return res;
    }

    public List<Map<String, Object>> frequency(){
        List<Map<String, Object>> resultList = new ArrayList<>();
        String json = platBuildingMapper.frequency();
        if (json == null || json.isEmpty()) {
            return null;
        }
        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<>();
            data.put("month", month);
            data.put("num", num);
            resultList.add(data);
        }
        return resultList;
    }

    public Map<String, Object> patrol(){
        String json = platBuildingMapper.patrol();
        if (json == null || json.isEmpty()) {
            return null;
        }
        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");
        Map<String, Object> data = new HashMap<>();
        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 (json == null || json.isEmpty()) {
            return null;
        }
        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;
    }
}
