package com.njcn.energy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.PubUtils;
import com.njcn.energy.mapper.*;
import com.njcn.energy.pojo.enums.EnergyResponseEnum;
import com.njcn.energy.pojo.enums.LoadEnum;
import com.njcn.energy.pojo.param.ElectComparaParam;
import com.njcn.energy.pojo.param.LoadStatisticParam;
import com.njcn.energy.pojo.po.EleEpdPqd;
import com.njcn.energy.pojo.po.EleLine;
import com.njcn.energy.pojo.po.EleLoadSet;
import com.njcn.energy.pojo.po.EleMd;
import com.njcn.energy.pojo.vo.EleRateVO;
import com.njcn.energy.pojo.vo.ElectCompareVO;
import com.njcn.energy.pojo.vo.LoadStatisticVO;
import com.njcn.energy.pojo.vo.LoadTendencyVO;
import com.njcn.energy.service.HighAnalyticService;
import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.enums.SystemResponseEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * pqs
 *
 * @author cdf
 * @date 2022/4/14
 */
@Service
@RequiredArgsConstructor
public class HighAnalyticServiceImpl implements HighAnalyticService {


    private final DicDataFeignClient dicDataFeignClient;

    private final EleLoadSetMapper eleLoadSetMapper;

    private final EleMdMapper eleMdMapper;

    private final EleEpdMapper eleEpdMapper;

    private final EleLineMapper eleLineMapper;

    private final EleRateSetMappser eleRateSetMappser;

    private final InfluxDbUtils influxDbUtils;



    @Override
    public List<LoadTendencyVO> getLoadTendency(String loadType, String start, String end) {
        List<LoadTendencyVO> list = new ArrayList<>();
        String[] time = formatTime(start, end);
        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();

        if (CollectionUtil.isNotEmpty(dictDataList)) {
            //获取负荷指标详情

            EleMd eleMd = eleMdMapper.selectById(loadType);
            if (Objects.isNull(eleMd)) {
                throw new BusinessException(CommonResponseEnum.FAIL);
            }
            List<String> allLineIdList = new ArrayList<>();
            for (DictData dictData : dictDataList) {
                LoadTendencyVO loadTendencyVO = new LoadTendencyVO();

                loadTendencyVO.setLoadName(dictData.getName());
                List<List<String>> itemList = new ArrayList<>();

                List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictData.getId());
                if (CollectionUtil.isNotEmpty(lineIdList)) {
                    if (CollectionUtil.isNotEmpty(lineIdList)) {
                        allLineIdList.addAll(lineIdList);
                        StringBuilder lineId = new StringBuilder();
                        for (int i = 0; i < lineIdList.size(); i++) {
                            if (i == lineIdList.size() - 1) {
                                lineId.append("'").append(lineIdList.get(i)).append("'");
                            } else {
                                lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                            }
                        }
                        StringBuilder sql = new StringBuilder("select ");
                        sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                                .append("' and time>'").append(time[0]).append("' and time< '").append(time[1]).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
                        List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                        if (CollectionUtil.isNotEmpty(mapList)) {
                            for (Map<String, Object> map : mapList) {
                                if (map.get(eleMd.getName()) != null) {
                                    List<String> it = new ArrayList<>();
                                    it.add(map.get("time").toString());
                                    Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                                    it.add(tem.toString());
                                    itemList.add(it);
                                }
                            }
                        }
                    }
                }
                loadTendencyVO.setValue(itemList);
                list.add(loadTendencyVO);
            }

            //获取所有负荷趋势
            LoadTendencyVO loadTendencyVO = new LoadTendencyVO();
            loadTendencyVO.setLoadName("总负荷");
            List<List<String>> itemList = new ArrayList<>();

            List<String> dicIdList = dictDataList.stream().filter(item -> item.getAlgoDescribe() == 1).map(DictData::getId).collect(Collectors.toList());
            List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dicIdList);
            StringBuilder lineId = new StringBuilder();
            for (int i = 0; i < lineIdsList.size(); i++) {
                if (i == lineIdsList.size() - 1) {
                    lineId.append("'").append(lineIdsList.get(i)).append("'");
                } else {
                    lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                }
            }

            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(time[0]).append("' and time< '").append(time[1]).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapList)) {
                for (Map<String, Object> map : mapList) {
                    if (map.get(eleMd.getName()) != null) {
                        List<String> it = new ArrayList<>();
                        it.add(map.get("time").toString());
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        it.add(tem.toString());
                        itemList.add(it);
                    }
                }
                loadTendencyVO.setValue(itemList);
                list.add(loadTendencyVO);
            }
        } else {
            throw new BusinessException(SystemResponseEnum.LOAD_TYPE_EMPTY);
        }
        return list;
    }


    @Override
    public List<LoadTendencyVO> getLoadDetail(String loadType, String start, String end) {
        List<LoadTendencyVO> listRes = new ArrayList<>();
        String[] time = formatTime(start, end);
        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();
        EleMd eleMd = eleMdMapper.selectById(loadType);
        //总负荷
        List<String> dicIdList = dictDataList.stream().filter(item -> item.getAlgoDescribe() == 1).map(DictData::getId).collect(Collectors.toList());
        LoadTendencyVO loadInner = new LoadTendencyVO();
        loadInner.setLoadName("使用总负荷");
        List<List<String>> itemList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dicIdList)) {
            List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dicIdList);
            StringBuilder lineId = new StringBuilder();
            for (int i = 0; i < lineIdsList.size(); i++) {
                if (i == lineIdsList.size() - 1) {
                    lineId.append("'").append(lineIdsList.get(i)).append("'");
                } else {
                    lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                }
            }
            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(time[0]).append("' and time< '").append(time[1]).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapList)) {
                for (Map<String, Object> map : mapList) {
                    if (map.get(eleMd.getName()) != null) {
                        List<String> it = new ArrayList<>();
                        it.add(map.get("time").toString());
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        it.add(tem.toString());
                        itemList.add(it);
                    }
                }
                loadInner.setValue(itemList);
                listRes.add(loadInner);
            }
        }

        //进线负荷
        LoadTendencyVO loadInnerJin = new LoadTendencyVO();
        loadInnerJin.setLoadName("进线负荷");
        List<List<String>> itemListJin = new ArrayList<>();

        LoadTendencyVO loadInnerJinYin = new LoadTendencyVO();
        loadInnerJinYin.setLoadName("进线功率因素");
        List<List<String>> itemListJinYin = new ArrayList<>();

        List<String> dictDataListJin = dictDataList.stream().filter(item -> item.getName().equals(LoadEnum.HIGH_LINE.getName())).map(DictData::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(dictDataListJin)) {
            List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dictDataListJin);
            StringBuilder lineId = new StringBuilder();
            for (int i = 0; i < lineIdsList.size(); i++) {
                if (i == lineIdsList.size() - 1) {
                    lineId.append("'").append(lineIdsList.get(i)).append("'");
                } else {
                    lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                }
            }
            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(time[0]).append("' and time< '").append(time[1]).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapList)) {
                for (Map<String, Object> map : mapList) {
                    if (map.get(eleMd.getName()) != null) {
                        List<String> it = new ArrayList<>();
                        it.add(map.get("time").toString());
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        it.add(tem.toString());
                        itemListJin.add(it);
                    }
                }
                loadInnerJin.setValue(itemListJin);
                listRes.add(loadInnerJin);
            }

            //进线功率因素
            StringBuilder sqlYin = new StringBuilder("select ");
            sqlYin.append("PF").append(" from power_quality_data").append(" where (LineId = ").append(lineId).append(") and Phase= 'T'")
                    .append(" and time>='").append(time[0]).append("' and time< '").append(time[1]).append("' order by time");
            List<Map<String, Object>> mapListYin = influxDbUtils.getResult(sqlYin.toString(), "");
            if (CollectionUtil.isNotEmpty(mapListYin)) {
                for (Map<String, Object> map : mapListYin) {
                    if ("-1000000".equals(map.get("PF").toString())) {
                        continue;
                    }
                    if (map.get("PF") != null) {
                        List<String> it = new ArrayList<>();
                        it.add(map.get("time").toString());
                        it.add(map.get("PF").toString());
                        itemListJinYin.add(it);
                    }

                }
                loadInnerJinYin.setValue(itemListJinYin);
                listRes.add(loadInnerJinYin);
            }

            //A相电压
            toAss("A","A相进线电压有效值",lineId,time,listRes);
            toAss("B","B相进线电压有效值",lineId,time,listRes);
            toAss("C","C相进线电压有效值",lineId,time,listRes);
        }

        //光伏
        LoadTendencyVO loadInnerSun = new LoadTendencyVO();
        loadInnerSun.setLoadName("光伏发电");
        List<List<String>> itemListSun = new ArrayList<>();
        List<String> dictDataListSun = dictDataList.stream().filter(item -> item.getName().equals(LoadEnum.SUN_LOAD.getName())).map(DictData::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(dictDataListSun)) {
            List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dictDataListSun);
            StringBuilder lineId = new StringBuilder();
            for (int i = 0; i < lineIdsList.size(); i++) {
                if (i == lineIdsList.size() - 1) {
                    lineId.append("'").append(lineIdsList.get(i)).append("'");
                } else {
                    lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                }
            }

            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(time[0]).append("' and time< '").append(time[1]).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapList)) {
                for (Map<String, Object> map : mapList) {
                    if (map.get(eleMd.getName()) != null) {
                        List<String> it = new ArrayList<>();
                        it.add(map.get("time").toString());
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        it.add(tem.toString());
                        itemListSun.add(it);
                    }
                }
                loadInnerSun.setValue(itemListSun);
                listRes.add(loadInnerSun);
            }
        }
        return listRes;
    }


    /*
     组装数据
     */
    private void toAss(String phase, String tag, StringBuilder lineId, String[] time, List<LoadTendencyVO> listRes) {
        LoadTendencyVO loadInnerA = new LoadTendencyVO();
        loadInnerA.setLoadName(tag);
        List<List<String>> itemListA = new ArrayList<>();
        String sqlA = "select " + "PhV" + " from power_quality_data" + " where (LineId = " + lineId + ") and Phase= '" + phase +
                "' and time>='" + time[0] + "' and time< '" + time[1] + "' order by time";
        List<Map<String, Object>> mapListA = influxDbUtils.getResult(sqlA, "");
        if (CollectionUtil.isNotEmpty(mapListA)) {
            for (Map<String, Object> map : mapListA) {
                if ("-1000000".equals(map.get("PhV").toString())) {
                    continue;
                }
                if (map.get("PhV") != null) {
                    List<String> it = new ArrayList<>();
                    it.add(map.get("time").toString());
                    it.add(map.get("PhV").toString());
                    itemListA.add(it);
                }

            }
            loadInnerA.setValue(itemListA);
            listRes.add(loadInnerA);
        }

    }

    @Override
    public List<Map<String, String>> getLoadRate(String loadType, String start) {
        List<LoadTendencyVO> list = new ArrayList<>();
        List<Map<String, String>> res = new ArrayList<>();

        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();

        if (CollectionUtil.isNotEmpty(dictDataList)) {
            //获取负荷指标详情
            dictDataList = dictDataList.stream().filter(item ->item.getAlgoDescribe().equals(1)).collect(Collectors.toList());

            EleMd eleMd = eleMdMapper.selectById(loadType);
            if (Objects.isNull(eleMd)) {
                throw new BusinessException(CommonResponseEnum.FAIL);
            }

            for (DictData dictData : dictDataList) {
                LoadTendencyVO loadTendencyVO = new LoadTendencyVO();

                loadTendencyVO.setLoadName(dictData.getName());
                List<List<String>> itemList = new ArrayList<>();

                List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictData.getId());
                if (CollectionUtil.isNotEmpty(lineIdList)) {
                    if (CollectionUtil.isNotEmpty(lineIdList)) {
                        StringBuilder lineId = new StringBuilder();
                        for (int i = 0; i < lineIdList.size(); i++) {
                            if (i == lineIdList.size() - 1) {
                                lineId.append("'").append(lineIdList.get(i)).append("'");
                            } else {
                                lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                            }
                        }
                        StringBuilder sql = new StringBuilder("select ");
                        sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                                .append("' and time='").append(start + "'");
                        List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                        if (CollectionUtil.isNotEmpty(mapList)) {
                            for (Map<String, Object> map : mapList) {
                                if (map.get(eleMd.getName()) != null) {
                                    List<String> cou = new ArrayList<>();
                                    cou.add(map.get("time").toString());
                                    cou.add(map.get(eleMd.getName()).toString());
                                    itemList.add(cou);
                                }
                            }
                        }
                        loadTendencyVO.setValue(itemList);
                        list.add(loadTendencyVO);
                    }
                }

            }


            float allTem = 0f;
            for (LoadTendencyVO loadTendencyVO : list) {
                allTem+= Float.parseFloat(loadTendencyVO.getValue().get(0).get(1));
            }

            for (LoadTendencyVO loadTendencyVO : list) {
                Map<String, String> map = new HashMap<>();
                map.put("name", loadTendencyVO.getLoadName());
                Float a = Float.parseFloat(loadTendencyVO.getValue().get(0).get(1)) / allTem;
                map.put("value", a.toString());
                res.add(map);
            }
        } else {
            throw new BusinessException(SystemResponseEnum.LOAD_TYPE_EMPTY);
        }


        return res;
    }

    @Override
    public List<LoadStatisticVO> getLoadStatistics(LoadStatisticParam loadStatisticParam) {
        List<LoadStatisticVO> res = new ArrayList<>();
        String[] time = formatTime(loadStatisticParam.getStartTime(), loadStatisticParam.getEndTime());
        String[] timeTong = formatTime(loadStatisticParam.getStartTimeBi(), loadStatisticParam.getEndTimeBi());
        loadStatisticParam.setStartTime(time[0]);
        loadStatisticParam.setEndTime(time[1]);
        loadStatisticParam.setStartTimeBi(timeTong[0]);
        loadStatisticParam.setEndTimeBi(timeTong[1]);
        //获取指标
        EleMd eleMd = eleMdMapper.selectById(loadStatisticParam.getTagType());
        if (Objects.isNull(eleMd)) {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }

        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            List<DictData> dictDataListDeal = dictDataList.stream().filter(num -> loadStatisticParam.getLoadType().contains(num.getId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(dictDataListDeal)) {

                for (DictData dictData : dictDataListDeal) {
                    LoadStatisticVO loadStatisticVO = new LoadStatisticVO();
                    loadStatisticVO.setLoadName(dictData.getName());
                    loadStatisticVO.setLoadId(dictData.getId());

                    LambdaQueryWrapper<EleLoadSet> loadSetLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    loadSetLambdaQueryWrapper.eq(EleLoadSet::getId, dictData.getId());
                    List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictData.getId());
                    if (CollectionUtil.isNotEmpty(lineIdList)) {
                        StringBuilder lineId = new StringBuilder();
                        for (int i = 0; i < lineIdList.size(); i++) {
                            if (i == lineIdList.size() - 1) {
                                lineId.append("'").append(lineIdList.get(i)).append("'");
                            } else {
                                lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                            }
                        }
                        //
                        getLoadStatisticZi(loadStatisticParam, eleMd, res, loadStatisticVO, lineId);
                    } else {
                        throw new BusinessException(EnergyResponseEnum.CAI_BIND);
                    }
                }
            }
            if (loadStatisticParam.getLoadType().size() > 1) {
                List<String> dicIdList = dictDataList.stream().filter(item -> item.getAlgoDescribe() == 1).map(DictData::getId).collect(Collectors.toList());
                List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dicIdList);
                StringBuilder lineId = new StringBuilder();
                for (int i = 0; i < lineIdsList.size(); i++) {
                    if (i == lineIdsList.size() - 1) {
                        lineId.append("'").append(lineIdsList.get(i)).append("'");
                    } else {
                        lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                    }
                }
                LoadStatisticVO loadStatisticVO = new LoadStatisticVO();
                loadStatisticVO.setLoadName("总负荷");
                loadStatisticVO.setLoadId("123");
                getLoadStatisticZi(loadStatisticParam, eleMd, res, loadStatisticVO, lineId);
            }
        } else {
            throw new BusinessException(SystemResponseEnum.LOAD_TYPE_EMPTY);
        }
        return res;
    }


    @Override
    public List<LoadTendencyVO> getElectTendency(String loadType, Integer type, String start, String end) {
        List<LoadTendencyVO> list = new ArrayList<>();
        String[] time = formatTime(start, end);

        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            //获取负荷指标详情
            EleEpdPqd eleEpdPqd = eleEpdMapper.selectById(loadType);
            if (Objects.isNull(eleEpdPqd)) {
                throw new BusinessException(CommonResponseEnum.FAIL);
            }
            for (DictData dictData : dictDataList) {
                LoadTendencyVO loadTendencyVO = new LoadTendencyVO();
                loadTendencyVO.setLoadName(dictData.getName());
                List<List<String>> itemList = new ArrayList<>();
                List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictData.getId());
                if (CollectionUtil.isNotEmpty(lineIdList)) {
                    StringBuilder lineId = new StringBuilder();
                    for (int i = 0; i < lineIdList.size(); i++) {
                        if (i == lineIdList.size() - 1) {
                            lineId.append("'").append(lineIdList.get(i)).append("'");
                        } else {
                            lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                        }
                    }
                    String sqlStr = "";
                    if (type == 0) {
                        //增量
                        sqlStr = "_add";
                    }
                    StringBuilder sql = new StringBuilder("select ");
                    sql.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId()).append(sqlStr).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                            .append("' and time>='").append(time[0]).append("' and time<= '").append(time[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                    List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                    if (CollectionUtil.isNotEmpty(mapList)) {
                        for (Map<String, Object> map : mapList) {
                            if (map.get(eleEpdPqd.getName()) != null) {
                                if (map.get(eleEpdPqd.getName()).toString().equals("-1000000.0")) {
                                    continue;
                                }
                                List<String> it = new ArrayList<>();
                                it.add(map.get("time").toString());
                                it.add(map.get(eleEpdPqd.getName()).toString());
                                itemList.add(it);
                            }
                        }
                    }
                    loadTendencyVO.setValue(itemList);
                    list.add(loadTendencyVO);
                }

            }

            //总电度
            LoadTendencyVO loadTendencyVO = new LoadTendencyVO();
            loadTendencyVO.setLoadName("总负荷");
            List<List<String>> itemList = new ArrayList<>();
            List<String> dicIdList = dictDataList.stream().filter(item -> item.getAlgoDescribe() == 1).map(DictData::getId).collect(Collectors.toList());
            List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dicIdList);
            StringBuilder lineId = new StringBuilder();
            for (int i = 0; i < lineIdsList.size(); i++) {
                if (i == lineIdsList.size() - 1) {
                    lineId.append("'").append(lineIdsList.get(i)).append("'");
                } else {
                    lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                }
            }
            String sqlStr = "";
            if (type == 0) {
                //增量
                sqlStr = "_add";
            }
            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId()).append(sqlStr).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                    .append("' and time>='").append(time[0]).append("' and time<= '").append(time[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapList)) {
                for (int i = 0; i < mapList.size(); i++) {
                    if (mapList.get(i).get(eleEpdPqd.getName()) != null) {
                        if (mapList.get(i).get(eleEpdPqd.getName()).toString().equals("-1000000.0")) {
                            continue;
                        }
                        List<String> it = new ArrayList<>();
                        it.add(mapList.get(i).get("time").toString());
                        it.add(mapList.get(i).get(eleEpdPqd.getName()).toString());
                        itemList.add(it);
                    }
                }
            }
            loadTendencyVO.setValue(itemList);
            list.add(loadTendencyVO);
        } else {
            throw new BusinessException(SystemResponseEnum.LOAD_TYPE_EMPTY);
        }
        return list;
    }

    @Override
    public List<ElectCompareVO> getElectCompare(ElectComparaParam electComparaParam) {
        List<ElectCompareVO> list = new ArrayList<>();
        String[] time = formatTime(electComparaParam.getStartTime(), electComparaParam.getEndTime());
        //获取指标
        EleEpdPqd eleEpdPqd = eleEpdMapper.selectById(electComparaParam.getTagType());
        if (Objects.isNull(eleEpdPqd)) {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
        //获取费率
        List<EleRateVO> rateList = eleRateSetMappser.getEleRateDetail(DicDataTypeEnum.RATE_TYPE.getName());
        List<String> feng = new ArrayList<>();
        List<String> pin = new ArrayList<>();
        List<String> gu = new ArrayList<>();

        for (EleRateVO eleRateVO : rateList) {
            if (eleRateVO.getRateTypeName().equals("峰")) {
                feng.add(eleRateVO.getRateTypeTime().split("-")[0]);
                feng.add(eleRateVO.getRateTypeTime().split("-")[1]);
                feng.add(eleRateVO.getRate());

            } else if (eleRateVO.getRateTypeName().equals("平")) {
                pin.add(eleRateVO.getRateTypeTime().split("-")[0]);
                pin.add(eleRateVO.getRateTypeTime().split("-")[1]);
                pin.add(eleRateVO.getRate());

            } else if (eleRateVO.getRateTypeName().equals("谷")) {
                gu.add(eleRateVO.getRateTypeTime().split("-")[0]);
                gu.add(eleRateVO.getRateTypeTime().split("-")[1]);
                gu.add(eleRateVO.getRate());
            }

        }
        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            List<DictData> dictDataListDeal = dictDataList.stream().filter(num -> electComparaParam.getLoadType().contains(num.getId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(dictDataListDeal)) {
                for (DictData dictData : dictDataListDeal) {
                    ElectCompareVO electCompareVO = new ElectCompareVO();
                    electCompareVO.setLoadName(dictData.getName());
                    electCompareVO.setLoadId(dictData.getId());

                    List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictData.getId());
                    if (CollectionUtil.isNotEmpty(lineIdList)) {
                        StringBuilder lineId = new StringBuilder();
                        for (int i = 0; i < lineIdList.size(); i++) {
                            if (i == lineIdList.size() - 1) {
                                lineId.append("'").append(lineIdList.get(i)).append("'");
                            } else {
                                lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                            }
                        }

                        StringBuilder sql = new StringBuilder("select ");
                        sql.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId()).append("_add").append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                                .append("' and time>='").append(time[0]).append("' and time<= '").append(time[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                        List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                        if (CollectionUtil.isNotEmpty(mapList)) {
                            if (electComparaParam.getStatisticType() == 0) {
                                float fengVal = 0f;
                                float pingVal = 0f;
                                float guVal = 0f;
                                for (Map<String, Object> map : mapList) {
                                    if (map.get(eleEpdPqd.getName()) != null) {
                                        int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                        if (hour >= Integer.parseInt(feng.get(0).substring(0, 2)) && hour < Integer.parseInt(feng.get(1).substring(0, 2))) {
                                            //峰
                                            fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        } else if (hour >= Integer.parseInt(pin.get(0).substring(0, 2)) && hour < Integer.parseInt(pin.get(1).substring(0, 2))) {
                                            //平
                                            pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());

                                        } else if (hour >= Integer.parseInt(gu.get(0).substring(0, 2)) && hour < Integer.parseInt(gu.get(1).substring(0, 2))) {
                                            //谷
                                            guVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()));
                                        } else if (hour >= Integer.parseInt(feng.get(3).substring(0, 2)) && hour < Integer.parseInt(feng.get(4).substring(0, 2))) {
                                            //峰2
                                            fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        } else if (hour >= Integer.parseInt(pin.get(3).substring(0, 2)) && hour < Integer.parseInt(pin.get(4).substring(0, 2))) {
                                            //平2
                                            pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    }
                                }
                                electCompareVO.setFeng(fengVal);
                                electCompareVO.setPing(pingVal);
                                electCompareVO.setGu(guVal);
                                electCompareVO.setAll(fengVal + pingVal + guVal);
                                list.add(electCompareVO);
                                //电度
                            } else if (electComparaParam.getStatisticType() == 1) {
                                //电费
                                float fengVal = 0f;
                                float pingVal = 0f;
                                float guVal = 0f;
                                for (Map<String, Object> map : mapList) {

                                    if (map.get(eleEpdPqd.getName()) != null) {
                                        int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                        if (hour >= Integer.parseInt(feng.get(0).substring(0, 2)) && hour < Integer.parseInt(feng.get(1).substring(0, 2))) {
                                            //峰
                                            fengVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(feng.get(2)));
                                        } else if (hour >= Integer.parseInt(pin.get(0).substring(0, 2)) && hour < Integer.parseInt(pin.get(1).substring(0, 2))) {
                                            //平
                                            pingVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(pin.get(2)));

                                        } else if (hour >= Integer.parseInt(gu.get(0).substring(0, 2)) && hour < Integer.parseInt(gu.get(1).substring(0, 2))) {
                                            //谷
                                            guVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(gu.get(2)));
                                        } else if (hour >= Integer.parseInt(feng.get(3).substring(0, 2)) && hour < Integer.parseInt(feng.get(4).substring(0, 2))) {
                                            //峰2
                                            fengVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(feng.get(5)));
                                        } else if (hour >= Integer.parseInt(pin.get(3).substring(0, 2)) && hour < Integer.parseInt(pin.get(4).substring(0, 2))) {
                                            //平2
                                            pingVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(pin.get(5)));
                                        }
                                    }
                                }
                                electCompareVO.setFeng(fengVal);
                                electCompareVO.setPing(pingVal);
                                electCompareVO.setGu(guVal);
                                electCompareVO.setAll(fengVal + pingVal + guVal);
                                list.add(electCompareVO);
                            }
                        }
                    } else {
                        throw new BusinessException(EnergyResponseEnum.CAI_BIND);
                    }


                }

                //****************总负荷
                ElectCompareVO electCompareVO = new ElectCompareVO();
                electCompareVO.setLoadName("总负荷");
                electCompareVO.setLoadId("");

                List<String> dicIdList = dictDataList.stream().filter(item -> item.getAlgoDescribe() == 1).map(DictData::getId).collect(Collectors.toList());
                List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dicIdList);
                StringBuilder lineId = new StringBuilder();
                for (int i = 0; i < lineIdsList.size(); i++) {
                    if (i == lineIdsList.size() - 1) {
                        lineId.append("'").append(lineIdsList.get(i)).append("'");
                    } else {
                        lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                    }
                }
                StringBuilder sql = new StringBuilder("select ");
                sql.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId()).append("_add").append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                        .append("' and time>='").append(time[0]).append("' and time<= '").append(time[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                if (CollectionUtil.isNotEmpty(mapList)) {
                    if (electComparaParam.getStatisticType() == 0) {
                        float fengVal = 0f;
                        float pingVal = 0f;
                        float guVal = 0f;
                        for (Map<String, Object> map : mapList) {
                            if (map.get(eleEpdPqd.getName()) != null) {
                                int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                if (hour >= Integer.parseInt(feng.get(0).substring(0, 2)) && hour < Integer.parseInt(feng.get(1).substring(0, 2))) {
                                    //峰
                                    fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                } else if (hour >= Integer.parseInt(pin.get(0).substring(0, 2)) && hour < Integer.parseInt(pin.get(1).substring(0, 2))) {
                                    //平
                                    pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());

                                } else if (hour >= Integer.parseInt(gu.get(0).substring(0, 2)) && hour < Integer.parseInt(gu.get(1).substring(0, 2))) {
                                    //谷
                                    guVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()));
                                } else if (hour >= Integer.parseInt(feng.get(3).substring(0, 2)) && hour < Integer.parseInt(feng.get(4).substring(0, 2))) {
                                    //峰2
                                    fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                } else if (hour >= Integer.parseInt(pin.get(3).substring(0, 2)) && hour < Integer.parseInt(pin.get(4).substring(0, 2))) {
                                    //平2
                                    pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                }
                            }
                        }
                        electCompareVO.setFeng(fengVal);
                        electCompareVO.setPing(pingVal);
                        electCompareVO.setGu(guVal);
                        electCompareVO.setAll(fengVal + pingVal + guVal);
                        list.add(electCompareVO);
                        //电度
                    } else if (electComparaParam.getStatisticType() == 1) {
                        //电费
                        float fengVal = 0f;
                        float pingVal = 0f;
                        float guVal = 0f;
                        for (Map<String, Object> map : mapList) {

                            if (map.get(eleEpdPqd.getName()) != null) {
                                int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                if (hour >= Integer.parseInt(feng.get(0).substring(0, 2)) && hour < Integer.parseInt(feng.get(1).substring(0, 2))) {
                                    //峰
                                    fengVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(feng.get(2)));
                                } else if (hour >= Integer.parseInt(pin.get(0).substring(0, 2)) && hour < Integer.parseInt(pin.get(1).substring(0, 2))) {
                                    //平
                                    pingVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(pin.get(2)));

                                } else if (hour >= Integer.parseInt(gu.get(0).substring(0, 2)) && hour < Integer.parseInt(gu.get(1).substring(0, 2))) {
                                    //谷
                                    guVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(gu.get(2)));
                                } else if (hour >= Integer.parseInt(feng.get(3).substring(0, 2)) && hour < Integer.parseInt(feng.get(4).substring(0, 2))) {
                                    //峰2
                                    fengVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(feng.get(5)));
                                } else if (hour >= Integer.parseInt(pin.get(3).substring(0, 2)) && hour < Integer.parseInt(pin.get(4).substring(0, 2))) {
                                    //平2
                                    pingVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(pin.get(5)));
                                }
                            }
                        }
                        electCompareVO.setFeng(fengVal);
                        electCompareVO.setPing(pingVal);
                        electCompareVO.setGu(guVal);
                        electCompareVO.setAll(fengVal + pingVal + guVal);
                        list.add(electCompareVO);
                    }
                }

                //****************总负荷
            }
        } else {
            throw new BusinessException(SystemResponseEnum.LOAD_TYPE_EMPTY);
        }
        return list;
    }

    @Override
    public List<Map<String, String>> getElectRate(ElectComparaParam electComparaParam) {
        List<Map<String, String>> res = new ArrayList<>();
        List<ElectCompareVO> list = new ArrayList<>();
        String[] time = formatTime(electComparaParam.getStartTime(), electComparaParam.getEndTime());

        //获取指标
        EleEpdPqd eleEpdPqd = eleEpdMapper.selectById(electComparaParam.getTagType());
        if (Objects.isNull(eleEpdPqd)) {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
        //获取费率
        List<EleRateVO> rateList = eleRateSetMappser.getEleRateDetail(DicDataTypeEnum.RATE_TYPE.getName());
        List<String> feng = new ArrayList<>();
        List<String> pin = new ArrayList<>();
        List<String> gu = new ArrayList<>();

        for (EleRateVO eleRateVO : rateList) {
            if (eleRateVO.getRateTypeName().equals("峰")) {
                feng.add(eleRateVO.getRateTypeTime().split("-")[0]);
                feng.add(eleRateVO.getRateTypeTime().split("-")[1]);
                feng.add(eleRateVO.getRate());
            } else if (eleRateVO.getRateTypeName().equals("平")) {
                pin.add(eleRateVO.getRateTypeTime().split("-")[0]);
                pin.add(eleRateVO.getRateTypeTime().split("-")[1]);
                pin.add(eleRateVO.getRate());
            } else if (eleRateVO.getRateTypeName().equals("谷")) {
                gu.add(eleRateVO.getRateTypeTime().split("-")[0]);
                gu.add(eleRateVO.getRateTypeTime().split("-")[1]);
                gu.add(eleRateVO.getRate());
            }
        }
        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            if (CollectionUtil.isNotEmpty(dictDataList)) {
                for (DictData dictData : dictDataList) {
                    ElectCompareVO electCompareVO = new ElectCompareVO();
                    electCompareVO.setLoadName(dictData.getName());
                    electCompareVO.setLoadId(dictData.getId());
                    List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictData.getId());
                    if (CollectionUtil.isNotEmpty(lineIdList)) {
                        StringBuilder lineId = new StringBuilder();
                        for (int i = 0; i < lineIdList.size(); i++) {
                            if (i == lineIdList.size() - 1) {
                                lineId.append("'").append(lineIdList.get(i)).append("'");
                            } else {
                                lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                            }
                        }
                        StringBuilder sql = new StringBuilder("select ");
                        sql.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId()).append("_add").append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                                .append("' and time>='").append(time[0]).append("' and time<= '").append(time[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                        List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                        if (CollectionUtil.isNotEmpty(mapList)) {

                            if (electComparaParam.getStatisticType() == 0) {
                                float fengVal = 0f;
                                float pingVal = 0f;
                                float guVal = 0f;
                                for (Map<String, Object> map : mapList) {
                                    if (map.get(eleEpdPqd.getName()) != null) {
                                        int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                        if (hour >= Integer.parseInt(feng.get(0).substring(0, 2)) && hour < Integer.parseInt(feng.get(1).substring(0, 2))) {
                                            //峰
                                            fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        } else if (hour >= Integer.parseInt(pin.get(0).substring(0, 2)) && hour < Integer.parseInt(pin.get(1).substring(0, 2))) {
                                            //平
                                            pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());

                                        } else if (hour >= Integer.parseInt(gu.get(0).substring(0, 2)) && hour < Integer.parseInt(gu.get(1).substring(0, 2))) {
                                            //谷
                                            guVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()));
                                        } else if (hour >= Integer.parseInt(feng.get(3).substring(0, 2)) && hour < Integer.parseInt(feng.get(4).substring(0, 2))) {
                                            //峰2
                                            fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        } else if (hour >= Integer.parseInt(pin.get(3).substring(0, 2)) && hour < Integer.parseInt(pin.get(4).substring(0, 2))) {
                                            //平2
                                            pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    }
                                }
                                electCompareVO.setFeng(fengVal);
                                electCompareVO.setPing(pingVal);
                                electCompareVO.setGu(guVal);
                                electCompareVO.setAll(fengVal + pingVal + guVal);
                                list.add(electCompareVO);
                                //电度
                            } else if (electComparaParam.getStatisticType() == 1) {
                                //电费
                                float fengVal = 0f;
                                float pingVal = 0f;
                                float guVal = 0f;
                                for (Map<String, Object> map : mapList) {

                                    if (map.get(eleEpdPqd.getName()) != null) {
                                        int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                        if (hour >= Integer.parseInt(feng.get(0).substring(0, 2)) && hour < Integer.parseInt(feng.get(1).substring(0, 2))) {
                                            //峰
                                            fengVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(feng.get(2)));
                                        } else if (hour >= Integer.parseInt(pin.get(0).substring(0, 2)) && hour < Integer.parseInt(pin.get(1).substring(0, 2))) {
                                            //平
                                            pingVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(pin.get(2)));

                                        } else if (hour >= Integer.parseInt(gu.get(0).substring(0, 2)) && hour < Integer.parseInt(gu.get(1).substring(0, 2))) {
                                            //谷
                                            guVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(gu.get(2)));
                                        } else if (hour >= Integer.parseInt(feng.get(3).substring(0, 2)) && hour < Integer.parseInt(feng.get(4).substring(0, 2))) {
                                            //峰2
                                            fengVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(feng.get(5)));
                                        } else if (hour >= Integer.parseInt(pin.get(3).substring(0, 2)) && hour < Integer.parseInt(pin.get(4).substring(0, 2))) {
                                            //平2
                                            pingVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(pin.get(5)));
                                        }
                                    }
                                }
                                electCompareVO.setFeng(fengVal);
                                electCompareVO.setPing(pingVal);
                                electCompareVO.setGu(guVal);
                                electCompareVO.setAll(fengVal + pingVal + guVal);
                                list.add(electCompareVO);
                            }
                        }
                    } else {
                        throw new BusinessException(EnergyResponseEnum.CAI_BIND);
                    }


                }

                List<ElectCompareVO> all = list.stream().filter(item -> item.getLoadName().equals("高压进线") || item.getLoadName().equals("光伏发电")).collect(Collectors.toList());
                Float allMy = 0f;
                for (ElectCompareVO e : all) {
                    allMy += e.getAll();
                }
                List<ElectCompareVO> itemList = list.stream().filter(item -> !item.getLoadName().equals("高压进线") && !item.getLoadName().equals("光伏发电")).collect(Collectors.toList());

                for (ElectCompareVO electCompareVO1 : itemList) {
                    Map<String, String> map = new HashMap<>();
                    map.put("name", electCompareVO1.getLoadName());
                    Float a = electCompareVO1.getAll() / allMy;
                    map.put("value", a.toString());
                    res.add(map);

                }
            }
        }
        return res;
    }

    @Override
    public List<LoadStatisticVO> getElectStatistic(ElectComparaParam electComparaParam) {
        List<LoadStatisticVO> res = new ArrayList<>();
        String[] time = formatTime(electComparaParam.getStartTime(), electComparaParam.getEndTime());
        String[] timeTong = formatTime(electComparaParam.getStartTimeBi(), electComparaParam.getEndTimeBi());

        //获取指标
        EleEpdPqd eleEpdPqd = eleEpdMapper.selectById(electComparaParam.getTagType());
        if (Objects.isNull(eleEpdPqd)) {
            throw new BusinessException(CommonResponseEnum.FAIL);
        }
        //获取费率

        List<EleRateVO> rate = null;

        List<String> rateVal = new ArrayList<>();

        List<String> feng = new ArrayList<>();
        List<String> pin = new ArrayList<>();
        List<String> gu = new ArrayList<>();
        if (StrUtil.isNotBlank(electComparaParam.getValType())) {
            rate = eleRateSetMappser.getEleRateById(electComparaParam.getValType());
            for (EleRateVO eleRateVO : rate) {
                if (eleRateVO.getRateTypeName().equals("峰")) {
                    rateVal.add(eleRateVO.getRateTypeTime().split("-")[0]);
                    rateVal.add(eleRateVO.getRateTypeTime().split("-")[1]);
                    rateVal.add(eleRateVO.getRate());
                } else if (eleRateVO.getRateTypeName().equals("平")) {
                    rateVal.add(eleRateVO.getRateTypeTime().split("-")[0]);
                    rateVal.add(eleRateVO.getRateTypeTime().split("-")[1]);
                    rateVal.add(eleRateVO.getRate());
                } else if (eleRateVO.getRateTypeName().equals("谷")) {
                    rateVal.add(eleRateVO.getRateTypeTime().split("-")[0]);
                    rateVal.add(eleRateVO.getRateTypeTime().split("-")[1]);
                    rateVal.add(eleRateVO.getRate());
                }
            }
        } else {
            rate = eleRateSetMappser.getEleRateDetail(DicDataTypeEnum.RATE_TYPE.getName());
            for (EleRateVO eleRateVO : rate) {
                if (eleRateVO.getRateTypeName().equals("峰")) {
                    feng.add(eleRateVO.getRateTypeTime().split("-")[0]);
                    feng.add(eleRateVO.getRateTypeTime().split("-")[1]);
                    feng.add(eleRateVO.getRate());
                } else if (eleRateVO.getRateTypeName().equals("平")) {
                    pin.add(eleRateVO.getRateTypeTime().split("-")[0]);
                    pin.add(eleRateVO.getRateTypeTime().split("-")[1]);
                    pin.add(eleRateVO.getRate());
                } else if (eleRateVO.getRateTypeName().equals("谷")) {
                    gu.add(eleRateVO.getRateTypeTime().split("-")[0]);
                    gu.add(eleRateVO.getRateTypeTime().split("-")[1]);
                    gu.add(eleRateVO.getRate());
                }
            }
        }


        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();

        if (CollectionUtil.isNotEmpty(dictDataList)) {
            List<DictData>  dictDataListQuery = dictDataList.stream().filter(num -> electComparaParam.getLoadType().contains(num.getId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(dictDataListQuery)) {
                for (DictData dictData : dictDataListQuery) {
                    LoadStatisticVO loadStatisticVO = new LoadStatisticVO();
                    loadStatisticVO.setLoadName(dictData.getName());
                    loadStatisticVO.setLoadId(dictData.getId());
                    List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictData.getId());
                    if (CollectionUtil.isNotEmpty(lineIdList)) {
                        StringBuilder lineId = new StringBuilder();
                        for (int i = 0; i < lineIdList.size(); i++) {
                            if (i == lineIdList.size() - 1) {
                                lineId.append("'").append(lineIdList.get(i)).append("'");
                            } else {
                                lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                            }
                        }
                        float beforeVal = 0f;
                        float afterVal = 0f;
                        StringBuilder sql = new StringBuilder("select ");
                        sql.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId() + "_add").append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                                .append("' and time>='").append(time[0]).append("' and time<= '").append(time[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                        List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                        if (CollectionUtil.isNotEmpty(mapList)) {
                            if (electComparaParam.getStatisticType() == 0) {
                                //电度
                                if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                                    for (Map<String, Object> map : mapList) {
                                        if (map.get(eleEpdPqd.getName()) != null) {
                                            int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                            if (rate.get(0).getRateTypeName().equals("峰")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //峰
                                                    beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("平")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //平
                                                    beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                                if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                                    //谷
                                                    beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    //总电度
                                    beforeVal = getAssEle(mapList, eleEpdPqd, feng, pin, gu, 1);

                                }

                            } else if (electComparaParam.getStatisticType() == 1) {
                                if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                                    for (Map<String, Object> map : mapList) {
                                        if (map.get(eleEpdPqd.getName()) != null) {
                                            int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                            if (rate.get(0).getRateTypeName().equals("峰")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //峰
                                                    beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("平")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //平
                                                    beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                                if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                                    //谷
                                                    beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    //电费
                                    beforeVal = getAssEle(mapList, eleEpdPqd, feng, pin, gu, 2);
                                }
                            }
                        }
                        loadStatisticVO.setBeforeNum(beforeVal);
                        StringBuilder sqlAfter = new StringBuilder("select ");
                        //同比环比值
                        sqlAfter.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId() + "_add").append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                                .append("' and time>='").append(timeTong[0]).append("' and time<= '").append(timeTong[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                        List<Map<String, Object>> mapListAfter = influxDbUtils.getResult(sqlAfter.toString(), "");
                        if (CollectionUtil.isNotEmpty(mapListAfter)) {
                            if (electComparaParam.getStatisticType() == 0) {
                                if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                                    for (Map<String, Object> map : mapListAfter) {
                                        if (map.get(eleEpdPqd.getName()) != null) {
                                            int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                            if (rate.get(0).getRateTypeName().equals("峰")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //峰
                                                    afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("平")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //平
                                                    afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                                if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                                    //谷
                                                    afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    afterVal = getAssEle(mapListAfter, eleEpdPqd, feng, pin, gu, 1);
                                }
                            } else if (electComparaParam.getStatisticType() == 1) {
                                if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                                    for (Map<String, Object> map : mapListAfter) {
                                        if (map.get(eleEpdPqd.getName()) != null) {
                                            int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                            if (rate.get(0).getRateTypeName().equals("峰")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //峰
                                                    afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("平")) {
                                                if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                                    //平
                                                    afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                                }
                                            } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                                if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                                    //谷
                                                    afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    afterVal = getAssEle(mapListAfter, eleEpdPqd, feng, pin, gu, 2);
                                }
                            }
                        }
                        loadStatisticVO.setAfterNum(afterVal);
                        res.add(loadStatisticVO);
                    } else {
                        throw new BusinessException(EnergyResponseEnum.CAI_BIND);
                    }
                }

                //***************总负荷
                LoadStatisticVO loadStatisticVO = new LoadStatisticVO();
                loadStatisticVO.setLoadName("总负荷");
                loadStatisticVO.setLoadId("");

                List<String> dicIdList = dictDataList.stream().filter(item -> item.getAlgoDescribe() == 1).map(DictData::getId).collect(Collectors.toList());
                List<String> lineIdsList = eleLoadSetMapper.getAllLineListRelationIds(dicIdList);
                StringBuilder lineId = new StringBuilder();
                for (int i = 0; i < lineIdsList.size(); i++) {
                    if (i == lineIdsList.size() - 1) {
                        lineId.append("'").append(lineIdsList.get(i)).append("'");
                    } else {
                        lineId.append("'").append(lineIdsList.get(i)).append("'").append(" or LineId=");
                    }
                }
                float beforeVal = 0f;
                float afterVal = 0f;
                StringBuilder sql = new StringBuilder("select ");
                sql.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId() + "_add").append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                        .append("' and time>='").append(time[0]).append("' and time<= '").append(time[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                if (CollectionUtil.isNotEmpty(mapList)) {

                    if (electComparaParam.getStatisticType() == 0) {
                        //电度
                        if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                            for (Map<String, Object> map : mapList) {
                                if (map.get(eleEpdPqd.getName()) != null) {
                                    int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                    if (rate.get(0).getRateTypeName().equals("峰")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //峰
                                            beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("平")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //平
                                            beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                        if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                            //谷
                                            beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    }
                                }
                            }
                        } else {
                            beforeVal = getAssEle(mapList, eleEpdPqd, feng, pin, gu, 1);
                        }

                    } else if (electComparaParam.getStatisticType() == 1) {
                        //电费
                        if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                            for (Map<String, Object> map : mapList) {
                                if (map.get(eleEpdPqd.getName()) != null) {
                                    int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                    if (rate.get(0).getRateTypeName().equals("峰")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //峰
                                            beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("平")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //平
                                            beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                        if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                            //谷
                                            beforeVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                        }
                                    }
                                }
                            }
                        } else {
                            beforeVal = getAssEle(mapList, eleEpdPqd, feng, pin, gu, 2);
                        }
                    }
                }
                loadStatisticVO.setBeforeNum(beforeVal);

                StringBuilder sqlAfter = new StringBuilder("select ");
                //同比环比值
                sqlAfter.append("SUM(").append(eleEpdPqd.getName()).append(") as ").append(eleEpdPqd.getName()).append(" from ").append(eleEpdPqd.getClassId() + "_add").append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleEpdPqd.getPhase())
                        .append("' and time>='").append(timeTong[0]).append("' and time<= '").append(timeTong[1]).append("' and ").append(eleEpdPqd.getName()).append(" !=-1000000").append(" group by time(15m)");
                List<Map<String, Object>> mapListAfter = influxDbUtils.getResult(sqlAfter.toString(), "");
                if (CollectionUtil.isNotEmpty(mapListAfter)) {
                    //电度
                    if (electComparaParam.getStatisticType() == 0) {
                        if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                            for (Map<String, Object> map : mapListAfter) {
                                if (map.get(eleEpdPqd.getName()) != null) {
                                    int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                    if (rate.get(0).getRateTypeName().equals("峰")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //峰
                                            afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("平")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //平
                                            afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                        if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                            //谷
                                            afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                                        }
                                    }
                                }
                            }
                        } else {
                            afterVal = getAssEle(mapListAfter, eleEpdPqd, feng, pin, gu, 1);
                        }

                    } else if (electComparaParam.getStatisticType() == 1) {
                        if (StrUtil.isNotBlank(electComparaParam.getValType())) {
                            for (Map<String, Object> map : mapListAfter) {
                                if (map.get(eleEpdPqd.getName()) != null) {
                                    int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                                    if (rate.get(0).getRateTypeName().equals("峰")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //峰
                                            afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("平")) {
                                        if ((hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) || (hour >= Integer.parseInt(rateVal.get(3).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(4).substring(0, 2)))) {
                                            //平
                                            afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                        }
                                    } else if (rate.get(0).getRateTypeName().equals("谷")) {
                                        if (hour >= Integer.parseInt(rateVal.get(0).substring(0, 2)) && hour < Integer.parseInt(rateVal.get(1).substring(0, 2))) {
                                            //谷
                                            afterVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString()) * Float.parseFloat(rateVal.get(2));
                                        }
                                    }
                                }
                            }
                        } else {
                            afterVal = getAssEle(mapListAfter, eleEpdPqd, feng, pin, gu, 2);
                        }
                    }
                }
                loadStatisticVO.setAfterNum(afterVal);
                res.add(loadStatisticVO);

                //***************总负荷
            }

        } else {
            throw new BusinessException(SystemResponseEnum.LOAD_TYPE_EMPTY);
        }
        return res;
    }

    @Override
    public List<LoadTendencyVO> getAirTendency(String start, String end) {
        List<LoadTendencyVO> listRes = new ArrayList<>();

        if (start.length() < 11) {
            LocalDateTime startTime = PubUtils.beginTimeToLocalDateTime(start);
            start = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            LocalDateTime endTime = PubUtils.endTimeToLocalDateTime(end);
            end = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.ELE_LOAD_TYPE.getName()).getData();
        dictDataList = dictDataList.stream().filter(item -> item.getName().equals(LoadEnum.AIR_LOAD.getName())).collect(Collectors.toList());


        if (CollectionUtil.isNotEmpty(dictDataList)) {

            List<String> lineIdList = eleLoadSetMapper.getAllLineListRelation(dictDataList.get(0).getId());
            if (CollectionUtil.isNotEmpty(lineIdList)) {
                if (CollectionUtil.isNotEmpty(lineIdList)) {
                    StringBuilder lineId = new StringBuilder();
                    for (int i = 0; i < lineIdList.size(); i++) {
                        if (i == lineIdList.size() - 1) {
                            lineId.append("'").append(lineIdList.get(i)).append("'");
                        } else {
                            lineId.append("'").append(lineIdList.get(i)).append("'").append(" or LineId=");
                        }

                    }
                    List<List<String>> itemList = new ArrayList<>();
                    StringBuilder sql = new StringBuilder("select ");
                    sql.append("SUM(WattHr) as ").append("WattHr").append(" from ").append("power_data_add").append(" where (LineId = ").append(lineId).append(") and Phase = 'T'")
                            .append(" and time>='").append(start).append("' and time< '").append(end).append("' and ").append("WattHr").append(" !=-1000000").append(" group by time(15m)");
                    List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
                    if (CollectionUtil.isNotEmpty(mapList)) {
                        LoadTendencyVO loadTendencyVO = new LoadTendencyVO();
                        loadTendencyVO.setLoadName("耗电量");
                        for (Map<String, Object> map : mapList) {
                            if (map.get("WattHr") != null) {
                                List<String> it = new ArrayList<>();
                                it.add(map.get("time").toString());
                                it.add(map.get("WattHr").toString());
                                itemList.add(it);
                            }

                        }
                        loadTendencyVO.setValue(itemList);
                        listRes.add(loadTendencyVO);
                    }
                }

            } else {
                throw new BusinessException(EnergyResponseEnum.CAI_BIND);
            }
        } else {
            throw new BusinessException(SystemResponseEnum.LOAD_TYPE_EMPTY);
        }

        //空调处理
        //获取所有外机空调
        LambdaQueryWrapper<EleLine> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(EleLine::getName, "外机");
        List<EleLine> lineList = eleLineMapper.selectList(lambdaQueryWrapper);
        List<String> ids = lineList.stream().map(EleLine::getLineId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(ids)) {
            StringBuilder lineId = new StringBuilder();
            for (int i = 0; i < ids.size(); i++) {
                if (i == ids.size() - 1) {
                    lineId.append("'").append(ids.get(i)).append("'");
                } else {
                    lineId.append("'" + ids.get(i) + "'").append(" or LineId=");
                }

            }

            List<List<String>> itemList = new ArrayList<>();
            List<List<String>> itemTwoList = new ArrayList<>();
            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(ACOutInNum) as ").append("ACOutInNum,").append("MIN(ACOutTmp) as ACOutTmp").append(" from ").append("air_data").append(" where (LineId = ").append(lineId).append(") and Phase = 'M'")
                    .append(" and time>='").append(start).append("' and time< '").append(end).append("' and ACOutInNum != -1000000 and ACOutTmp !=-1000000").append(" group by time(5m)");
            List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapList)) {
                LoadTendencyVO loadTendencyVO = new LoadTendencyVO();
                loadTendencyVO.setLoadName("内机开机数量");
                LoadTendencyVO loadTendencyVOWai = new LoadTendencyVO();
                loadTendencyVOWai.setLoadName("外机温度");
                for (Map<String, Object> map : mapList) {
                    if (map.get("ACOutInNum") != null) {
                        List<String> it = new ArrayList<>();
                        List<String> ittwo = new ArrayList<>();
                        it.add(map.get("time").toString());
                        it.add(map.get("ACOutInNum").toString());
                        ittwo.add(map.get("time").toString());
                        ittwo.add(map.get("ACOutTmp").toString());
                        itemList.add(it);
                        itemTwoList.add(ittwo);
                    }

                }
                loadTendencyVO.setValue(itemList);
                loadTendencyVOWai.setValue(itemTwoList);
                listRes.add(loadTendencyVO);
                listRes.add(loadTendencyVOWai);
            }
        }

        return listRes;
    }


    /**
     * 获取负荷值
     *
     * @author cdf
     * @date 2022/4/26
     */

    private void getLoadStatisticZi(LoadStatisticParam loadStatisticParam, EleMd eleMd, List<LoadStatisticVO> res, LoadStatisticVO loadStatisticVO, StringBuilder lineId) {
        //单一负荷
        if (loadStatisticParam.getLoadType().size() == 1) {
            List<Float> valListBefore = new ArrayList<>();
            List<Float> valListAfter = new ArrayList<>();


            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(loadStatisticParam.getStartTime()).append("' and time< '").append(loadStatisticParam.getEndTime()).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapListBefore = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapListBefore)) {
                for (Map<String, Object> map : mapListBefore) {
                    if (map.get(eleMd.getName()) != null) {
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        valListBefore.add(tem);
                    }
                }
            }

            sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(loadStatisticParam.getStartTimeBi()).append("' and time< '").append(loadStatisticParam.getEndTimeBi()).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapListAfter = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapListAfter)) {
                for (Map<String, Object> map : mapListAfter) {
                    if (map.get(eleMd.getName()) != null) {
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        valListAfter.add(tem);
                    }
                }
            }

            Float beforeMax = 0f;
            Float afterMax = 0f;

            Float beforeMin = 0f;
            Float afterMin = 0f;

            Float beforeAvg = 0f;
            Float afterAvg = 0f;
            if (CollectionUtil.isNotEmpty(valListBefore)) {
                beforeMax = Collections.max(valListBefore);
                beforeMin = Collections.min(valListBefore);

                float tem = 0f;
                for (int i = 0; i < valListBefore.size(); i++) {
                    tem += valListBefore.get(i);
                }
                beforeAvg = tem / valListBefore.size();
            } else {
                beforeMax = 3.14159f;
                beforeMin = 3.14159f;
                beforeAvg = 3.14159f;
            }
            if (CollectionUtil.isNotEmpty(valListAfter)) {
                afterMax = Collections.max(valListAfter);
                afterMin = Collections.min(valListAfter);
                float tem = 0f;
                for (int i = 0; i < valListAfter.size(); i++) {
                    tem += valListAfter.get(i);
                }
                afterAvg = tem / valListAfter.size();
            } else {
                afterMax = 3.14159f;
                afterMin = 3.14159f;
                afterAvg = 3.14159f;
            }

            for (int i = 0; i < loadStatisticParam.getValType().size(); i++) {
                if (loadStatisticParam.getValType().get(i) == 0) {
                    LoadStatisticVO loadStatisticVOMax = new LoadStatisticVO();
                    loadStatisticVOMax.setLoadName("最大值");
                    //最大值
                    loadStatisticVOMax.setBeforeNum(beforeMax);
                    loadStatisticVOMax.setAfterNum(afterMax);
                    res.add(loadStatisticVOMax);
                } else if (loadStatisticParam.getValType().get(i) == 1) {
                    //最小值
                    LoadStatisticVO loadStatisticVOMin = new LoadStatisticVO();
                    loadStatisticVOMin.setLoadName("最小值");
                    loadStatisticVOMin.setBeforeNum(beforeMin);
                    loadStatisticVOMin.setAfterNum(afterMin);
                    res.add(loadStatisticVOMin);
                } else if (loadStatisticParam.getValType().get(i) == 2) {
                    LoadStatisticVO loadStatisticVOAvg = new LoadStatisticVO();
                    loadStatisticVOAvg.setLoadName("平均值");
                    loadStatisticVOAvg.setBeforeNum(beforeAvg);
                    loadStatisticVOAvg.setAfterNum(afterAvg);
                    res.add(loadStatisticVOAvg);
                }
            }
        } else {
            //负荷类型不唯一

            Float ping = 0f;
            Float after = 0f;


            StringBuilder sql = new StringBuilder("select ");
            sql.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(loadStatisticParam.getStartTime()).append("' and time< '").append(loadStatisticParam.getEndTime()).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapList = influxDbUtils.getResult(sql.toString(), "");
            if (CollectionUtil.isNotEmpty(mapList)) {
                List<Float> valList = new ArrayList<>();
                for (Map<String, Object> map : mapList) {
                    if (map.get(eleMd.getName()) != null) {
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        valList.add(tem);
                    }
                }
                if (CollectionUtil.isNotEmpty(valList)) {
                    if (loadStatisticParam.getValType().get(0) == 0) {
                        //最大值
                        ping = Collections.max(valList);
                    } else if (loadStatisticParam.getValType().get(0) == 1) {
                        //最小值
                        ping = Collections.min(valList);
                    } else if (loadStatisticParam.getValType().get(0) == 2) {
                        float tem = 0f;
                        for (int i = 0; i < valList.size(); i++) {
                            tem += valList.get(i);
                        }
                        ping = tem / valList.size();
                    }
                } else {
                    ping = 3.14159f;
                }
            }
            loadStatisticVO.setBeforeNum(ping);


            StringBuilder sqlAfter = new StringBuilder("select ");
            sqlAfter.append("SUM(").append(eleMd.getName()).append(") as ").append(eleMd.getName()).append(" from ").append(eleMd.getClassId()).append(" where (LineId = ").append(lineId).append(") and Phase= '").append(eleMd.getPhase())
                    .append("' and time>'").append(loadStatisticParam.getStartTimeBi()).append("' and time< '").append(loadStatisticParam.getEndTimeBi()).append("' and ").append(eleMd.getName()).append(" !=-1000000").append(" group by time(15m)");
            List<Map<String, Object>> mapListAfter = influxDbUtils.getResult(sqlAfter.toString(), "");
            if (CollectionUtil.isNotEmpty(mapListAfter)) {
                List<Float> valList = new ArrayList<>();
                for (Map<String, Object> map : mapListAfter) {
                    if (map.get(eleMd.getName()) != null) {
                        Float tem = Float.parseFloat(map.get(eleMd.getName()).toString()) / 1000;
                        valList.add(tem);
                    }
                }

                if (CollectionUtil.isNotEmpty(valList)) {
                    if (loadStatisticParam.getValType().get(0) == 0) {
                        //最大值
                        after = Collections.max(valList);
                    } else if (loadStatisticParam.getValType().get(0) == 1) {
                        //最小值
                        after = Collections.min(valList);
                    } else if (loadStatisticParam.getValType().get(0) == 2) {
                        float tem = 0f;
                        for (int i = 0; i < valList.size(); i++) {
                            tem += valList.get(i);
                        }
                        after = tem / valList.size();
                    }
                } else {
                    after = 3.14159f;
                }
            }
            loadStatisticVO.setAfterNum(after);
            res.add(loadStatisticVO);
        }
    }

    //时间格式化
    private String[] formatTime(String start, String end) {
        String[] time = new String[2];
        LocalDateTime startTime = PubUtils.beginTimeToLocalDateTime(start);
        time[0] = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = PubUtils.endTimeToLocalDateTime(end);
        time[1] = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return time;
    }


    //获取电度电费
    private float getAssEle(List<Map<String, Object>> mapList, EleEpdPqd eleEpdPqd, List<String> feng, List<String> pin, List<String> gu, int type) {
        float fengVal = 0f;
        float pingVal = 0f;
        float guVal = 0f;
        //电度
        for (Map<String, Object> map : mapList) {
            if (map.get(eleEpdPqd.getName()) != null) {
                int hour = Integer.parseInt(map.get("time").toString().substring(11, 13));
                if (hour >= Integer.parseInt(feng.get(0).substring(0, 2)) && hour < Integer.parseInt(feng.get(1).substring(0, 2))) {
                    //峰
                    fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                } else if (hour >= Integer.parseInt(pin.get(0).substring(0, 2)) && hour < Integer.parseInt(pin.get(1).substring(0, 2))) {
                    //平
                    pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());

                } else if (hour >= Integer.parseInt(gu.get(0).substring(0, 2)) && hour < Integer.parseInt(gu.get(1).substring(0, 2))) {
                    //谷
                    guVal += (Float.parseFloat(map.get(eleEpdPqd.getName()).toString()));
                } else if (hour >= Integer.parseInt(feng.get(3).substring(0, 2)) && hour < Integer.parseInt(feng.get(4).substring(0, 2))) {
                    //峰2
                    fengVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                } else if (hour >= Integer.parseInt(pin.get(3).substring(0, 2)) && hour < Integer.parseInt(pin.get(4).substring(0, 2))) {
                    //平2
                    pingVal += Float.parseFloat(map.get(eleEpdPqd.getName()).toString());
                }
            }
        }
        if (type == 1) {
            return fengVal + pingVal + guVal;
        } else {
            return fengVal * Float.parseFloat(feng.get(2)) + pingVal * Float.parseFloat(pin.get(2)) + guVal * Float.parseFloat(gu.get(2));
        }
    }


}
