package com.njcn.harmonic.service.impl;

import com.njcn.device.api.LineFeignClient;
import com.njcn.device.pojo.vo.LineDetailDataVO;
import com.njcn.harmonic.constant.Param;
import com.njcn.harmonic.pojo.param.HistoryHarmOverLimitParam;
import com.njcn.harmonic.pojo.vo.HistoryHarmOverLimitVO;
import com.njcn.harmonic.service.NormLimitService;
import com.njcn.influxdb.utils.InfluxDbUtils;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.influxdb.dto.QueryResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

/**
 * 类介绍
 *
 * @author denghuajun
 * @version 1.0.0
 * @date 2022/3/18
 */
@Service
@AllArgsConstructor
public class NormLimitServiceImpl implements NormLimitService {

    private final InfluxDbUtils influxDbUtils;

    private final LineFeignClient lineFeignClient;

    @SneakyThrows
    @Override
    public List<HistoryHarmOverLimitVO> getHistoryTableData(HistoryHarmOverLimitParam historyHarmOverLimitParam) {
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOList = new ArrayList<>();
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVO;
        //处理接口数据
        String[] lineIds = new String[historyHarmOverLimitParam.getLineId().length];
        int[] types = new int[historyHarmOverLimitParam.getCondition().length];
        int[] inde = new int[0];
        int[] inharm = new int[0];
        for (int i = 0; i < historyHarmOverLimitParam.getLineId().length; i++) {
            lineIds[i] = historyHarmOverLimitParam.getLineId()[i];
        }

        for (int i = 0; i < historyHarmOverLimitParam.getCondition().length; i++) {
            types[i] = Integer.parseInt(historyHarmOverLimitParam.getCondition()[i]);
        }

        if (historyHarmOverLimitParam.getHarmonics() != null) {
            inde = new int[historyHarmOverLimitParam.getHarmonics().length];
            for (int i = 0; i < historyHarmOverLimitParam.getHarmonics().length; i++) {
                inde[i] = Integer.parseInt(historyHarmOverLimitParam.getHarmonics()[i]);
            }
        }

        if (historyHarmOverLimitParam.getInHarmonics() != null) {
            inharm = new int[historyHarmOverLimitParam.getInHarmonics().length];
            for (int i = 0; i < historyHarmOverLimitParam.getInHarmonics().length; i++) {
                inharm[i] = Integer.parseInt(historyHarmOverLimitParam.getInHarmonics()[i]);
            }
        }
        for (int i = 0; i < lineIds.length; i++) {
            //查询数据
            for (int j = 0; j < types.length; j++) {
                historyHarmOverLimitVO = getCondition(historyHarmOverLimitParam.getSearchBeginTime(), historyHarmOverLimitParam.getSearchEndTime(), String.valueOf(lineIds[i]), String.valueOf(types[j]), inde, inharm);
                historyHarmOverLimitVOList.addAll(historyHarmOverLimitVO);
            }
        }
        //获取值
        //处理数据
        return historyHarmOverLimitVOList;
    }

    /**
     * influxDB相关操作
     * 查询稳态数据分析
     */
    @SneakyThrows
    private List<HistoryHarmOverLimitVO> getCondition(String startTime, String endTime, String lineId, String contion, int[] number, int[] valueType) {
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOList = new ArrayList<>();
        //数据的转化
        historyHarmOverLimitVOList = getQueryResult(startTime, endTime, lineId, contion, number, valueType);
        return historyHarmOverLimitVOList;
    }

    private List<HistoryHarmOverLimitVO> getQueryResult(String startTime, String endTime, String lineList, String contion, int[] number, int[] inHarmNum) {
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOList = new ArrayList<>();
        QueryResult queryResult = null;
        if (!lineList.isEmpty()) {
            //组装sql语句
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(Param.TIME + " >= '").append(startTime).append(Param.START_TIME).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append(Param.END_TIME).append("' and (");
            //sql语句
            stringBuilder.append("lineid='").append(lineList).append("')");
            String sql;
            switch (Integer.parseInt(contion)) {
                case 12:
                    //电压偏差
                    sql = "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'CP95' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'CP95' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, Voltage_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'CP95' order by time asc;";
                    queryResult = influxDbUtils.query(sql);
                    historyHarmOverLimitVOList = getNormData(queryResult, contion, lineList, "电压偏差", "%");
                    break;
                case 13:
                    //三相电压不平衡度
                    sql = "SELECT time as time, UBalance as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, UBalance as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, UBalance as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, UBalance as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'CP95' order by time asc;";
                    queryResult = influxDbUtils.query(sql);
                    historyHarmOverLimitVOList = getNormData(queryResult, contion, lineList, "三相电压不平衡度", "/");
                    break;
                case 15:
                    //电压总谐波畸变率
                    sql = "SELECT time as time, UAberrance as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, UAberrance as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A'and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, UAberrance as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, UAberrance as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'CP95' and value_type = 'AVG' order by time asc;" +
                            " SELECT time as time, UAberrance as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B'  and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, UAberrance as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B'  and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, UAberrance as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'AVG' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, UAberrance as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'CP95' order by time asc;" +
                            "SELECT time as time, UAberrance as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, UAberrance as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, UAberrance as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, UAberrance as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'CP95' order by time asc;";
                    queryResult = influxDbUtils.query(sql);
                    historyHarmOverLimitVOList = getNormData(queryResult, contion, lineList, "电压总谐波畸变率", "%");
                    break;
                case 22:
                    //负序电流
                    sql = "SELECT time as time, I_Neg as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'MAX'  order by time asc;" +
                            "SELECT time as time, I_Neg as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, I_Neg as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, I_Neg as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'CP95' order by time asc;";
                    queryResult = influxDbUtils.query(sql);
                    historyHarmOverLimitVOList = getNormData(queryResult, contion, lineList, "负序电流", "/");
                    break;
                case 30:
                    //频率 V9暂时代表Freq
                    sql = "SELECT time as time, Freq_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, Freq_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, Freq_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, Freq_Dev as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='T' and value_type = 'CP95' order by time asc;";
                    queryResult = influxDbUtils.query(sql);
                    historyHarmOverLimitVOList = getNormData(queryResult, contion, lineList, "频率", "Hz");
                    break;
                case 40:
                    for (int i = 0; i < number.length; i++) {
                        List<HistoryHarmOverLimitVO> harmOverLimitVOList;
                        //谐波电压含有率
                        sql = "SELECT time as time, UHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'CP95' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'CP95' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, UHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'CP95' order by time asc;";
                        queryResult = influxDbUtils.query(sql);
                        harmOverLimitVOList = getNormData(queryResult, contion, lineList, "谐波电压含有率", "%", number[i]);
                        historyHarmOverLimitVOList.addAll(harmOverLimitVOList);
                    }
                    break;
                case 43:
                    for (int i = 0; i < number.length; i++) {
                        List<HistoryHarmOverLimitVO> harmOverLimitVOList;
                        //谐波电流幅值
                        sql = "SELECT time as time, IHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'CP95' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'CP95' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, IHarm_" + number[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'CP95' order by time asc;";
                        queryResult = influxDbUtils.query(sql);
                        harmOverLimitVOList = getNormData(queryResult, contion, lineList, "谐波电流幅值", "A", number[i]);
                        historyHarmOverLimitVOList.addAll(harmOverLimitVOList);
                    }
                    break;
                case 46:
                    for (int i = 0; i < inHarmNum.length; i++) {
                        List<HistoryHarmOverLimitVO> harmOverLimitVOList;
                        //间谐波电压含有率
                        sql = "SELECT time as time, InUHARM_" + inHarmNum[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='A' and value_type = 'CP95' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='B' and value_type = 'CP95' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'MAX' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'MIN' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'AVG' order by time asc;" +
                                "SELECT time as time, InUHARM_" + inHarmNum[i] + " as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                                " and phasic_type ='C' and value_type = 'CP95' order by time asc;";
                        queryResult = influxDbUtils.query(sql);
                        harmOverLimitVOList = getNormData(queryResult, contion, lineList, "间谐波电压含有率", "%", inHarmNum[i]);
                        historyHarmOverLimitVOList.addAll(harmOverLimitVOList);
                    }
                    break;
                case 61:
                    //长时闪变
                    sql = "SELECT time as time, Flicker as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, Flicker as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, Flicker as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, Flicker as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='A' and value_type = 'CP95' order by time asc;" +
                            "SELECT time as time, Flicker as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, Flicker as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, Flicker as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, Flicker as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='B' and value_type = 'CP95' order by time asc;" +
                            "SELECT time as time, Flicker as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'MAX' order by time asc;" +
                            "SELECT time as time, Flicker as aValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'MIN' order by time asc;" +
                            "SELECT time as time, Flicker as bValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'AVG' order by time asc;" +
                            "SELECT time as time, Flicker as cValue FROM PQS_AbnormalData WHERE " + stringBuilder.toString() +
                            " and phasic_type ='C' and value_type = 'CP95' order by time asc;";
                    queryResult = influxDbUtils.query(sql);
                    historyHarmOverLimitVOList = getNormData(queryResult, contion, lineList, "长时闪变", "/");
                    break;
                default:
                    break;
            }
        } else {
            return historyHarmOverLimitVOList;
        }
        return historyHarmOverLimitVOList;
    }


    //数据组装
    @SneakyThrows
    private List<HistoryHarmOverLimitVO> getNormData(QueryResult queryResult, String contion, String lineList, String phaseName, String unit) {
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOList = new ArrayList<>();
        List<QueryResult.Result> qusery = queryResult.getResults();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //获取监测点详情
        LineDetailDataVO lineDetailDataVO = lineFeignClient.getLineDetailData(lineList).getData();
        if (!CollectionUtils.isEmpty(qusery.get(0).getSeries())) {
            String time = "";
            Float aMax = 0.0f, aMin = 0.0f, aAvg = 0.0f, aCp95 = 0.0f, bMax = 0.0f, bMin = 0.0f, bAvg = 0.0f, bCp95 = 0.0f, cMax = 0.0f, cMin = 0.0f, cAvg = 0.0f, cCp95 = 0.0f;
            for (int i = 0; i < qusery.get(0).getSeries().get(0).getValues().size(); i++) {
                HistoryHarmOverLimitVO historyDataResultVO = new HistoryHarmOverLimitVO();
                //A相的最大值
                if (!CollectionUtils.isEmpty(qusery.get(0).getSeries())) {
                    aMax = Float.parseFloat(qusery.get(0).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setMaxValue(aMax);
                }
                //A相的最小值
                if (!CollectionUtils.isEmpty(qusery.get(1).getSeries())) {
                    aMin = Float.parseFloat(qusery.get(1).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setMinValue(aMin);
                }
                //A相的评价值
                if (!CollectionUtils.isEmpty(qusery.get(2).getSeries())) {
                    aAvg = Float.parseFloat(qusery.get(2).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setAvgValue(aAvg);
                }
                //A相的CP95
                if (!CollectionUtils.isEmpty(qusery.get(3).getSeries())) {
                    time = String.valueOf(qusery.get(3).getSeries().get(0).getValues().get(i).get(0));
                    aCp95 = Float.parseFloat(qusery.get(3).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setTime(dateFormat.format(sdf.parse(time)));
                    historyDataResultVO.setCp95Value(aCp95);
                }
                historyDataResultVO.setLineName(lineDetailDataVO.getLineName());
                historyDataResultVO.setLineId(lineList);
                historyDataResultVO.setNumber(0);
                historyDataResultVO.setSubName(lineDetailDataVO.getBdName());
                historyDataResultVO.setTargetCode(contion);
                historyDataResultVO.setTargetName(phaseName);
                if (Integer.parseInt(contion)==12 || Integer.parseInt(contion)==15){
                    historyDataResultVO.setPhaseType("A");
                    historyDataResultVO.setStatisticalType(Math.abs(aMax) > Math.abs(aMin) ? 3 : 2);
                }else if(Integer.parseInt(contion)==13 || Integer.parseInt(contion)==22){
                    historyDataResultVO.setPhaseType("/");
                    historyDataResultVO.setStatisticalType(Math.abs(aMax) > Math.abs(aCp95) ? 3 : 4);
                }else {
                    historyDataResultVO.setPhaseType("A");
                    historyDataResultVO.setStatisticalType(3);
                }
                historyDataResultVO.setScale(lineDetailDataVO.getScale());
                historyDataResultVO.setUnit(unit);
                historyHarmOverLimitVOList.add(historyDataResultVO);
            }
            if ("12".equals(contion) || "15".equals(contion) || "40".equals(contion) || "61".equals(contion)) {
                for (int i = 0; i < qusery.get(4).getSeries().get(0).getValues().size(); i++) {
                    HistoryHarmOverLimitVO historyDataResultVOB = new HistoryHarmOverLimitVO();
                    //B相的最大值
                    if (!CollectionUtils.isEmpty(qusery.get(4).getSeries())) {
                        bMax = Float.parseFloat(qusery.get(4).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOB.setMaxValue(bMax);
                    }
                    //B相的最小值
                    if (!CollectionUtils.isEmpty(qusery.get(5).getSeries())) {
                        bMin = Float.parseFloat(qusery.get(5).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOB.setMinValue(bMin);
                    }
                    //B相的平均值
                    if (!CollectionUtils.isEmpty(qusery.get(6).getSeries())) {
                        bAvg = Float.parseFloat(qusery.get(6).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOB.setAvgValue(bAvg);
                    }
                    //B相的CP95
                    if (!CollectionUtils.isEmpty(qusery.get(7).getSeries())) {
                        time = String.valueOf(qusery.get(7).getSeries().get(0).getValues().get(i).get(0));
                        bCp95 = Float.parseFloat(qusery.get(7).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOB.setTime(dateFormat.format(sdf.parse(time)));
                        historyDataResultVOB.setCp95Value(bCp95);
                    }
                    historyDataResultVOB.setLineName(lineDetailDataVO.getLineName());
                    historyDataResultVOB.setLineId(lineList);
                    historyDataResultVOB.setNumber(0);
                    historyDataResultVOB.setSubName(lineDetailDataVO.getBdName());
                    historyDataResultVOB.setTargetCode(contion);
                    historyDataResultVOB.setTargetName(phaseName);
                    if (Integer.parseInt(contion)==12 || Integer.parseInt(contion)==15){
                        historyDataResultVOB.setPhaseType("B");
                        historyDataResultVOB.setStatisticalType(Math.abs(aMax) > Math.abs(aMin) ? 3 : 2);
                    }else if(Integer.parseInt(contion)==13 || Integer.parseInt(contion)==22){
                        historyDataResultVOB.setPhaseType("/");
                        historyDataResultVOB.setStatisticalType(Math.abs(aMax) > Math.abs(aCp95) ? 3 : 4);
                    }else {
                        historyDataResultVOB.setPhaseType("B");
                        historyDataResultVOB.setStatisticalType(3);
                    }
                    historyDataResultVOB.setScale(lineDetailDataVO.getScale());
                    historyDataResultVOB.setUnit(unit);
                    historyHarmOverLimitVOList.add(historyDataResultVOB);
                }
                for (int i = 0; i < qusery.get(8).getSeries().get(0).getValues().size(); i++) {
                    HistoryHarmOverLimitVO historyDataResultVOC = new HistoryHarmOverLimitVO();
                    //C相的最大值
                    if (!CollectionUtils.isEmpty(qusery.get(8).getSeries())) {
                        cMax = Float.parseFloat(qusery.get(8).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOC.setMaxValue(cMax);
                    }
                    //C相的最小值
                    if (!CollectionUtils.isEmpty(qusery.get(9).getSeries())) {
                        cMin = Float.parseFloat(qusery.get(9).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOC.setMinValue(cMin);
                    }
                    //C相的平均值
                    if (!CollectionUtils.isEmpty(qusery.get(10).getSeries())) {
                        cAvg = Float.parseFloat(qusery.get(10).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOC.setAvgValue(cAvg);
                    }
                    //C相的CP95
                    if (!CollectionUtils.isEmpty(qusery.get(11).getSeries())) {
                        time = String.valueOf(qusery.get(11).getSeries().get(0).getValues().get(i).get(0));
                        cCp95 = Float.parseFloat(qusery.get(11).getSeries().get(0).getValues().get(i).get(1).toString());
                        historyDataResultVOC.setTime(dateFormat.format(sdf.parse(time)));
                        historyDataResultVOC.setCp95Value(cCp95);
                    }
                    historyDataResultVOC.setLineName(lineDetailDataVO.getLineName());
                    historyDataResultVOC.setLineId(lineList);
                    historyDataResultVOC.setNumber(0);
                    historyDataResultVOC.setSubName(lineDetailDataVO.getBdName());
                    historyDataResultVOC.setTargetCode(contion);
                    historyDataResultVOC.setTargetName(phaseName);
                    if (Integer.parseInt(contion)==12 || Integer.parseInt(contion)==15){
                        historyDataResultVOC.setPhaseType("C");
                        historyDataResultVOC.setStatisticalType(Math.abs(aMax) > Math.abs(aMin) ? 3 : 2);
                    }else if(Integer.parseInt(contion)==13 || Integer.parseInt(contion)==22){
                        historyDataResultVOC.setPhaseType("/");
                        historyDataResultVOC.setStatisticalType(Math.abs(aMax) > Math.abs(aCp95) ? 3 : 4);
                    }else {
                        historyDataResultVOC.setPhaseType("C");
                        historyDataResultVOC.setStatisticalType(3);
                    }
                    historyDataResultVOC.setScale(lineDetailDataVO.getScale());
                    historyDataResultVOC.setUnit(unit);
                    historyHarmOverLimitVOList.add(historyDataResultVOC);
                }
            }
        }
        return historyHarmOverLimitVOList;
    }

    @SneakyThrows
    private List<HistoryHarmOverLimitVO> getNormData(QueryResult queryResult, String contion, String lineList, String phaseName, String unit, int number) {
        List<HistoryHarmOverLimitVO> historyHarmOverLimitVOList = new ArrayList<>();
        List<QueryResult.Result> qusery = queryResult.getResults();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //获取监测点详情
        LineDetailDataVO lineDetailDataVO = lineFeignClient.getLineDetailData(lineList).getData();
        if (!CollectionUtils.isEmpty(qusery.get(0).getSeries())) {
            String time = "";
            Float aMax = 0.0f, aMin = 0.0f, aAvg = 0.0f, aCp95 = 0.0f, bMax = 0.0f, bMin = 0.0f, bAvg = 0.0f, bCp95 = 0.0f, cMax = 0.0f, cMin = 0.0f, cAvg = 0.0f, cCp95 = 0.0f;
            HistoryHarmOverLimitVO historyDataResultVO = new HistoryHarmOverLimitVO();
            for (int i = 0; i < qusery.get(0).getSeries().get(0).getValues().size(); i++) {
                //A相的最大值
                if (!CollectionUtils.isEmpty(qusery.get(0).getSeries())) {
                    aMax = Float.parseFloat(qusery.get(0).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setMaxValue(aMax);
                }
                //A相的最小值
                if (!CollectionUtils.isEmpty(qusery.get(1).getSeries())) {
                    aMin = Float.parseFloat(qusery.get(1).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setMinValue(aMin);
                }
                //A相的评价值
                if (!CollectionUtils.isEmpty(qusery.get(2).getSeries())) {
                    aAvg = Float.parseFloat(qusery.get(2).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setAvgValue(aAvg);
                }
                //A相的CP95
                if (!CollectionUtils.isEmpty(qusery.get(3).getSeries())) {
                    time = String.valueOf(qusery.get(3).getSeries().get(0).getValues().get(i).get(0));
                    aCp95 = Float.parseFloat(qusery.get(3).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVO.setTime(dateFormat.format(sdf.parse(time)));
                    historyDataResultVO.setCp95Value(aCp95);
                }
                historyDataResultVO.setLineName(lineDetailDataVO.getLineName());
                historyDataResultVO.setLineId(lineList);
                historyDataResultVO.setNumber(number);
                historyDataResultVO.setSubName(lineDetailDataVO.getBdName());
                historyDataResultVO.setTargetCode(contion);
                historyDataResultVO.setTargetName(number + "次" + phaseName);
                historyDataResultVO.setPhaseType("A");
                historyDataResultVO.setStatisticalType(4);
                historyDataResultVO.setScale(lineDetailDataVO.getScale());
                historyDataResultVO.setUnit(unit);
                historyHarmOverLimitVOList.add(historyDataResultVO);
            }
            for (int i = 0; i < qusery.get(4).getSeries().get(0).getValues().size(); i++) {
                HistoryHarmOverLimitVO historyDataResultVOB = new HistoryHarmOverLimitVO();
                //B相的最大值
                if (!CollectionUtils.isEmpty(qusery.get(4).getSeries())) {
                    bMax = Float.parseFloat(qusery.get(4).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOB.setMaxValue(bMax);
                }
                //B相的最小值
                if (!CollectionUtils.isEmpty(qusery.get(5).getSeries())) {
                    bMin = Float.parseFloat(qusery.get(5).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOB.setMinValue(bMin);
                }
                //B相的平均值
                if (!CollectionUtils.isEmpty(qusery.get(6).getSeries())) {
                    bAvg = Float.parseFloat(qusery.get(6).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOB.setAvgValue(bAvg);
                }
                //B相的CP95
                if (!CollectionUtils.isEmpty(qusery.get(7).getSeries())) {
                    time = String.valueOf(qusery.get(7).getSeries().get(0).getValues().get(i).get(0));
                    bCp95 = Float.parseFloat(qusery.get(7).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOB.setTime(dateFormat.format(sdf.parse(time)));
                    historyDataResultVOB.setCp95Value(bCp95);
                }
                historyDataResultVOB.setLineName(lineDetailDataVO.getLineName());
                historyDataResultVOB.setLineId(lineList);
                historyDataResultVOB.setNumber(number);
                historyDataResultVOB.setSubName(lineDetailDataVO.getBdName());
                historyDataResultVOB.setTargetCode(contion);
                historyDataResultVOB.setTargetName(number + "次" + phaseName);
                historyDataResultVOB.setPhaseType("B");
                historyDataResultVOB.setScale(lineDetailDataVO.getScale());
                historyDataResultVOB.setUnit(unit);
                historyHarmOverLimitVOList.add(historyDataResultVOB);
            }
            for (int i = 0; i < qusery.get(8).getSeries().get(0).getValues().size(); i++) {
                HistoryHarmOverLimitVO historyDataResultVOC = new HistoryHarmOverLimitVO();
                //C相的最大值
                if (!CollectionUtils.isEmpty(qusery.get(8).getSeries())) {
                    cMax = Float.parseFloat(qusery.get(8).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOC.setMaxValue(cMax);
                }
                //C相的最小值
                if (!CollectionUtils.isEmpty(qusery.get(9).getSeries())) {
                    cMin = Float.parseFloat(qusery.get(9).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOC.setMinValue(cMin);
                }
                //C相的平均值
                if (!CollectionUtils.isEmpty(qusery.get(10).getSeries())) {
                    cAvg = Float.parseFloat(qusery.get(10).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOC.setAvgValue(cAvg);
                }
                //C相的CP95
                if (!CollectionUtils.isEmpty(qusery.get(11).getSeries())) {
                    time = String.valueOf(qusery.get(11).getSeries().get(0).getValues().get(i).get(0));
                    cCp95 = Float.parseFloat(qusery.get(11).getSeries().get(0).getValues().get(i).get(1).toString());
                    historyDataResultVOC.setTime(dateFormat.format(sdf.parse(time)));
                    historyDataResultVOC.setCp95Value(cCp95);
                }
                historyDataResultVOC.setLineName(lineDetailDataVO.getLineName());
                historyDataResultVOC.setLineId(lineList);
                historyDataResultVOC.setNumber(number);
                historyDataResultVOC.setSubName(lineDetailDataVO.getBdName());
                historyDataResultVOC.setTargetCode(contion);
                historyDataResultVOC.setTargetName(number + "次" + phaseName);
                historyDataResultVOC.setPhaseType("C");
                historyDataResultVOC.setScale(lineDetailDataVO.getScale());
                historyDataResultVOC.setUnit(unit);
                historyHarmOverLimitVOList.add(historyDataResultVOC);
            }
        }
        return historyHarmOverLimitVOList;
    }
}
