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

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.addional.LargeBuildingControl;
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.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<Map<String, Object>> getPowerAndWaterMonth(){
        List<Map<String, Object>> resultList = 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 key = dataItem.getString("key");
            String value = dataItem.getString("value");
            Map<String, Object> data = new HashMap<>();
            data.put("name", key);
            data.put("value", 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)){
            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);
            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 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 Map<String, Object> greenElectricity(){

        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);
        return data;
    }

    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<>();
        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");
        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 (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);
        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 (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);
        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 (json == null || json.isEmpty()) {
            return null;
        }
        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();
        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;
    }

    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(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getThisWeekElectricity();
        JSONArray weekElectricity = JSONArray.parseArray(largeBuildingControl.getWeekElectricity());
        JSONArray roomElectricity = JSONArray.parseArray(largeBuildingControl.getRoomElectricity());
        Map<String, Object> map = new HashMap<>();
        map.put("total", weekElectricity);
        map.put("room", roomElectricity);
        return map;
    }

    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();
        JSONArray jsonArray = JSONArray.parseArray(json);
        return repeatElectricity(jsonArray);
    }

    public Map<String, Object> getMonthElectricity(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getMonthElectricity();
        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<String, Object> map = new HashMap<>();
        map.put("first", a);
        map.put("second", b);
        map.put("third", c);
        return map;
    }

    public Map<String, Object> getRoomLoad(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getRoomLoad();
        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<String, Object> map = new HashMap<>();
        map.put("first", a);
        map.put("second", b);
        map.put("third", c);
        return map;
    }

    public Map<String, Object> getWarnDevice(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getWarnDevice();
        JSONObject jsonObject = JSONObject.parseObject(largeBuildingControl.getWarnDevice());
        Map<String, Object> map = new HashMap<>();
        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();
        switch (type){
            case "1":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointOne());
            case "2":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointTwo());
            case "3":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointThree());
            case "4":
                return JSONArray.parseArray(largeBuildingControl.getAbnormalPointFour());
            default:
                return new JSONArray();
        }
    }

    public JSONArray getWarnEvent(){
        String json = platBuildingMapper.getWarnEvent();
        return JSONArray.parseArray(json);
    }

    public Map<String, Object> getWarnState(){
        LargeBuildingControl largeBuildingControl = platBuildingMapper.getWarnState();
        Map<String, Object> map = new HashMap<>();
        map.put("twentyThree", JSONObject.parseObject(largeBuildingControl.getWaterStateOne()));
        map.put("minusTwo", JSONObject.parseObject(largeBuildingControl.getWaterStateTwo()));
        return map;
    }


    private JSONObject repeatWarnDevice(JSONObject 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;
    }
}
