package com.njcn.product.advance.harmonicUp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.common.utils.PubUtils;

import com.njcn.influx.imapper.CommonMapper;
import com.njcn.influx.imapper.DataHarmRateVMapper;
import com.njcn.influx.imapper.DataIMapper;
import com.njcn.influx.pojo.bo.HarmonicHistoryData;
import com.njcn.influx.pojo.constant.InfluxDBTableConstant;
import com.njcn.product.advance.harmonicUp.pojo.param.HistoryParam;
import com.njcn.product.advance.harmonicUp.pojo.vo.HistoryDataResultVO;
import com.njcn.product.advance.harmonicUp.pojo.vo.QueryResultLimitVO;
import com.njcn.product.advance.harmonicUp.service.HistoryResultService;
import com.njcn.product.system.dict.mapper.DictDataMapper;

import com.njcn.product.terminal.mysqlTerminal.mapper.LineMapper;
import com.njcn.product.terminal.mysqlTerminal.mapper.OverlimitMapper;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.LineDevGetDTO;
import com.njcn.product.terminal.mysqlTerminal.pojo.po.Overlimit;
import com.njcn.product.terminal.mysqlTerminal.pojo.po.PqsDeviceUnit;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author denghuajun
 * @date 2022/3/14
 */
@Slf4j
@Service
@AllArgsConstructor
public class HistoryResultServiceImpl implements HistoryResultService {


    private final CommonMapper commonMapper;

    private final DataIMapper dataIMapper;

    private final DataHarmRateVMapper dataHarmRateVMapper;

    private final LineMapper lineMapper;
    private final OverlimitMapper overlimitMapper;
    private final DictDataMapper dictDataMapper;


    @Override
    public List<HistoryDataResultVO> getHistoryResult(HistoryParam historyParam) {
        List<HistoryDataResultVO> historyDataResultVOList = new ArrayList<>();
        //获取监测点
        String[] points = historyParam.getLineId();
        Integer number = 0;
        for (int i = 0; i < points.length; i++) {
            HistoryDataResultVO historyDataResultVO;

            //获取指标
            String[] contions = historyParam.getCondition();
            for (int j = 0; j < contions.length; j++) {
                if ("40".equals(contions[j]) || "41".equals(contions[j]) || "42".equals(contions[j]) || "43".equals(contions[j])
                        || "44".equals(contions[j]) || "45".equals(contions[j]) || "50".equals(contions[j]) || "51".equals(contions[j])
                        || "52".equals(contions[j])) {
                    number = historyParam.getHarmonic();
                }
                if ("46".equals(contions[j]) || "47".equals(contions[j]) || "48".equals(contions[j]) || "49".equals(contions[j])) {
                    number = historyParam.getInHarmonic();
                }
                historyDataResultVO = getCondition(historyParam.getSearchBeginTime(), historyParam.getSearchEndTime(), points[i], contions[j], number, historyParam.getValueType(), historyParam.getPtType());

                historyDataResultVOList.add(historyDataResultVO);

            }
        }
        return historyDataResultVOList;
    }



    /**
     * influxDB相关操作
     * 查询稳态数据分析
     */
    @SneakyThrows
    private HistoryDataResultVO getCondition(String startTime, String endTime, String lineId, String contion, Integer number, Integer valueType, Integer ptType) {
        HistoryDataResultVO historyDataResultVO = new HistoryDataResultVO();
        QueryResultLimitVO queryResultLimitVO = getQueryResult(startTime, endTime, lineId, contion, number, valueType, ptType);
        List<HarmonicHistoryData> harmonicHistoryDataList = queryResultLimitVO.getHarmonicHistoryDataList();
        BeanUtil.copyProperties(queryResultLimitVO, historyDataResultVO);
        //时间轴
        List<Date> time = new ArrayList<>();
        //A相值
        List<Float> aValue;
        //B相值
        List<Float> bValue = new ArrayList<>();
        //C相值
        List<Float> cValue = new ArrayList<>();
        //针对统计相别为T时存放的数据
        List<Float> fValue = new ArrayList<>();
        List<List<Object>> objectListData = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(harmonicHistoryDataList)) {
            //相别统计为T时，业务数据处理
            if (StrUtil.isBlank(harmonicHistoryDataList.get(0).getPhasicType()) || harmonicHistoryDataList.get(0).getPhasicType().equalsIgnoreCase("t")) {
                for (HarmonicHistoryData harmonicHistoryData : harmonicHistoryDataList) {
                    time.add(PubUtils.instantToDate(harmonicHistoryData.getTime()));
                    fValue.add(BigDecimal.valueOf(harmonicHistoryData.getAValue()).setScale(4, RoundingMode.HALF_UP).floatValue());
                    //返回结果有多个值，需要额外处理下
                    if (Integer.parseInt(contion) == 14) {
                        bValue.add(BigDecimal.valueOf(harmonicHistoryData.getBValue()).setScale(4, RoundingMode.HALF_UP).floatValue());
                        cValue.add(BigDecimal.valueOf(harmonicHistoryData.getCValue()).setScale(4, RoundingMode.HALF_UP).floatValue());
                    }
                }
                //组装二维数组
                for (int i = 0; i < time.size(); i++) {
                    List<Object> objects = new ArrayList<>();
                    objects.add(time.get(i));
                    objects.add(fValue.get(i));
                    if (Integer.parseInt(contion) == 14) {
                        objects.add(bValue.get(i));
                        objects.add(cValue.get(i));
                    }
                    objectListData.add(objects);
                }
                historyDataResultVO.setTopLimit(queryResultLimitVO.getTopLimit());
                historyDataResultVO.setLowerLimit(queryResultLimitVO.getLowerLimit());
                historyDataResultVO.setMinValue(Collections.min(fValue));
                historyDataResultVO.setMaxValue(Collections.max(fValue));
                historyDataResultVO.setValue(objectListData);
            } else {
                //按时间分组
                Map<Instant, List<HarmonicHistoryData>> map = harmonicHistoryDataList.stream().collect(Collectors.groupingBy(HarmonicHistoryData::getTime, TreeMap::new, Collectors.toList()));

                Float maxI = null;
                Float minI = null;
                for (Map.Entry<Instant, List<HarmonicHistoryData>> entry : map.entrySet()) {
                    List<HarmonicHistoryData> val = entry.getValue();
                    Object[] objects = {PubUtils.instantToDate(entry.getKey()), 0, 0, 0};
                    //需要保证val的长度为3
                    if (val.size() != 3) {
                        for (int i = 0; i < 3 - val.size(); i++) {
                            HarmonicHistoryData tem = new HarmonicHistoryData();
                            tem.setAValue(0f);
                            val.add(tem);
                        }
                    }

                    for (HarmonicHistoryData harmonicHistoryData : val) {
                        if (InfluxDBTableConstant.PHASE_TYPE_A.equalsIgnoreCase(harmonicHistoryData.getPhasicType())) {

                            BigDecimal a = BigDecimal.valueOf(harmonicHistoryData.getAValue()).setScale(4, RoundingMode.HALF_UP);
                            objects[1] = a;
                            maxI = max(maxI, a.floatValue());
                            minI = min(minI, a.floatValue());

                        } else if (InfluxDBTableConstant.PHASE_TYPE_B.equalsIgnoreCase(harmonicHistoryData.getPhasicType())) {
                            BigDecimal b = BigDecimal.valueOf(harmonicHistoryData.getAValue()).setScale(4, RoundingMode.HALF_UP);
                            objects[2] = b;
                            maxI = max(maxI, b.floatValue());
                            minI = min(minI, b.floatValue());

                        } else if (InfluxDBTableConstant.PHASE_TYPE_C.equalsIgnoreCase(harmonicHistoryData.getPhasicType())) {
                            BigDecimal c = BigDecimal.valueOf(harmonicHistoryData.getAValue()).setScale(4, RoundingMode.HALF_UP);
                            objects[3] = c;
                            maxI = max(maxI, c.floatValue());
                            minI = min(minI, c.floatValue());

                        }
                    }
                    List<Object> list = new ArrayList<>(Arrays.asList(objects));

                    objectListData.add(list);

                }

                historyDataResultVO.setMaxValue(maxI);
                historyDataResultVO.setMinValue(minI);
                historyDataResultVO.setTopLimit(queryResultLimitVO.getTopLimit());
                historyDataResultVO.setLowerLimit(queryResultLimitVO.getLowerLimit());
                historyDataResultVO.setValue(objectListData);
            }

        } else {
            return historyDataResultVO;
        }
        return historyDataResultVO;
    }


    private Float max(Float ding, Float a) {
        if (Objects.isNull(ding)) {
            ding = a;
        }
        if (a > ding) {
            ding = a;
        }
        return ding;
    }

    private Float min(Float ding, Float a) {
        if (Objects.isNull(ding)) {
            ding = a;
        }
        if (a < ding) {
            ding = a;
        }
        return ding;
    }

    private QueryResultLimitVO getQueryResult(String startTime, String endTime, String lineId, String contion, Integer number, Integer valueType, Integer ptType) {
        PqsDeviceUnit pqsDeviceUnit = new PqsDeviceUnit();
        QueryResultLimitVO queryResultLimitVO = new QueryResultLimitVO();
        if (!lineId.isEmpty()) {
            Float topLimit = 0f;
            Float lowerLimit = 0f;


            //获取监测点信息
            LineDevGetDTO lineDetailData = lineMapper.getMonitorDetail(lineId);
            //获取限值
            Overlimit overlimit = overlimitMapper.selectById(lineId);

            //组装sql语句
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(InfluxDBTableConstant.TIME + " >= '").append(startTime).append("' and ").append(InfluxDBTableConstant.TIME).append(" <= '").append(endTime).append("' and (");
            //sql语句
            stringBuilder.append(InfluxDBTableConstant.LINE_ID + "='").append(lineId).append("')");
            String valueTypeName = "";
            switch (valueType) {
                case 1:
                    valueTypeName = "AVG";
                    break;
                case 2:
                    valueTypeName = "MIN";
                    break;
                case 3:
                    valueTypeName = "MAX";
                    break;
                case 4:
                    valueTypeName = "CP95";
                    break;
                default:
                    break;
            }
            if (!Integer.valueOf(contion).equals(60) && !Integer.valueOf(contion).equals(61) && !Integer.valueOf(contion).equals(62)) {
                stringBuilder.append(" and ").append(InfluxDBTableConstant.VALUE_TYPE + "='").append(valueTypeName).append("'");
            }
            String sql = "";
            List<String> phasicType = new ArrayList<>();
            List<String> unit = new ArrayList<>();
            String targetName = "";
            switch (Integer.parseInt(contion)) {
                case 10:
                    //相电压有效值
                    sql = "SELECT time as time, rms as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_v WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add(pqsDeviceUnit.getPhaseVoltage());
                    targetName = "相电压有效值";
                    break;
                case 11:
                    //线电压有效值
                    sql = "SELECT time as time, rms_lvr as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + "  FROM data_v WHERE " + stringBuilder +
                            " and (phasic_type ='A'  or phasic_type ='B' or phasic_type ='C')order by time asc tz('Asia/Shanghai');";
                    phasicType.add("AB相");
                    phasicType.add("BC相");
                    phasicType.add("CA相");
                    unit.add(pqsDeviceUnit.getLineVoltage());
                    targetName = "线电压有效值";
                    break;
                case 12:
                    //电压偏差
                    sql = "SELECT time as time, vu_dev as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + "  FROM data_v WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    topLimit = overlimit.getVoltageDev();
                    lowerLimit = overlimit.getUvoltageDev();
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    unit.add("%");
                    targetName = "电压偏差";
                    break;
                case 13:
                    //三相电压不平衡度
                    sql = "SELECT time as time, v_unbalance as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + "  FROM data_v WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    topLimit = overlimit.getUbalance();
                    phasicType.add("三相电压不平衡度");
                    unit.add("%");
                    targetName = "三相电压不平衡度";
                    break;
                case 14:
                    //电压不平衡
                    sql = "SELECT time as time, v_zero as aValue, v_pos as bValue, v_neg as cValue  ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_v WHERE " + stringBuilder +
                            " and (phasic_type ='T') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("零序电压");
                    phasicType.add("正序电压");
                    phasicType.add("负序电压");
                    unit.add(pqsDeviceUnit.getNoPositiveV());
                    unit.add(pqsDeviceUnit.getPositiveV());
                    unit.add(pqsDeviceUnit.getNoPositiveV());
                    targetName = "电压不平衡";
                    break;
                case 15:
                    //电压总谐波畸变率
                    sql = "SELECT time as time, v_thd as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + "  FROM data_v WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    topLimit = overlimit.getUaberrance();
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    unit.add("%");
                    targetName = "电压总谐波畸变率";
                    break;
                case 20:
                    //电流有效值
                    sql = "SELECT time as time, rms as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_i WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add("A");
                    targetName = "电流有效值";
                    break;
                case 21:
                    //电流总畸变率
                    sql = "SELECT time as time, i_thd as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_i WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add("%");
                    targetName = "电流总谐波畸变率";
                    break;
                case 22:
                    //负序电流
                    sql = "SELECT time as time, i_neg as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_i WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    topLimit = overlimit.getINeg();
                    phasicType.add("负序电流");
                    unit.add("A");
                    targetName = "负序电流";
                    break;
                case 30:
                    //频率 V9暂时代表Freq
                    sql = "SELECT time as time, freq as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_v WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    topLimit = 50 + overlimit.getFreqDev();
                    lowerLimit = 50 - overlimit.getFreqDev();
                    phasicType.add("频率");
                    unit.add("Hz");
                    targetName = "频率";
                    break;
                case 40:
                    //谐波电压含有率
                    if (number == 1) {
                        targetName = "基波电压幅值";
                        //修改幅值表
                        sql = "SELECT time as time, v_1 as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_v WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                        unit.add(pqsDeviceUnit.getPhaseVoltage());
                    } else {
                        targetName = "谐波电压含有率";
                        sql = "SELECT time as time, v_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmrate_v WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                        if (number < 26) {
                            topLimit = PubUtils.getValueByMethod(overlimit, "getUharm", number);
                        }
                        unit.add("%");
                    }
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }


                    break;
                case 41:
                    //谐波电流含有率
                    if (number == 1) {
                        targetName = "谐波电流幅值";
                        sql = "SELECT time as time, i_1 as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmrate_i WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";

                        unit.add("A");
                    } else {
                        targetName = "谐波电流含有率";
                        sql = "SELECT time as time, i_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmrate_i WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                        unit.add("%");
                    }
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");


                    break;
                case 42:
                    //谐波电压幅值
                    if (number == 1) {
                        sql = "SELECT time as time, v_1 as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_v WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    } else {
                        sql = "SELECT time as time, v_" + number + "*1000 as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_v WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    }
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    if (number == 1) {
                        unit.add(pqsDeviceUnit.getVfundEffective());
                        targetName = "基波电压幅值";

                    } else {
                        unit.add("V");
                        targetName = "谐波电压幅值";

                    }
                    break;
                case 43:
                    //谐波电流幅值
                    if (number == 1) {
                        sql = "SELECT time as time, i_1 as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_i WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                        targetName = "基波电流幅值";

                    } else {
                        sql = "SELECT time as time, i_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_i WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                        if (number < 26) {
                            topLimit = PubUtils.getValueByMethod(overlimit, "getIharm", number);
                        }
                        targetName = "谐波电流幅值";

                    }
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add("A");
                    break;
                case 44:
                    //谐波电压相角
                    if (number == 1) {
                        targetName = "基波电压相角";

                        sql = "SELECT time as time, v_1 as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmphasic_v WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    } else {
                        targetName = "谐波电压相角";
                        sql = "SELECT time as time, v_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmphasic_v WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    }
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    unit.add("°");
                    break;
                case 45:
                    //谐波电流相角
                    if (number == 1) {
                        sql = "SELECT time as time, i_1 as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmphasic_i WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                        targetName = "基波电流相角";

                    } else {
                        sql = "SELECT time as time, i_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmphasic_i WHERE " + stringBuilder +
                                " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                        targetName = "谐波电流相角";

                    }
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add("°");
                    break;
                case 46:
                    //间谐波电压含有率
                    sql = "SELECT time as time, v_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_inharm_v WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    if(number<17){
                        topLimit = PubUtils.getValueByMethod(overlimit, "getInuharm", number);
                    }else {
                        topLimit = 0.0f;
                    }
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    unit.add("%");
                    targetName = "间谐波电压含有率";
                    break;
                case 47:
                    //间谐波电流含有率
                    sql = "SELECT time as time, i_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_inharm_i WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add("%");
                    targetName = "间谐波电流含有率";
                    break;
                case 48:
                    //间谐波电压幅值
                    sql = "SELECT time as time, v_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_inharm_v WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    targetName = "间谐波电压幅值";
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    unit.add(pqsDeviceUnit.getPhaseVoltage());
                    break;
                case 49:
                    //间谐波电流幅值
                    sql = "SELECT time as time, i_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_inharm_i WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add("A");
                    targetName = "间谐波电流幅值";
                    break;
                case 50:
                    //谐波有功功率
                    sql = "SELECT time as time, p_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_p WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    if (number == 1) {
                        unit.add(pqsDeviceUnit.getFundActiveP());
                    } else {
                        unit.add("W");
                    }
                    targetName = "谐波有功功率";
                    break;
                case 51:
                    //谐波无功功率
                    sql = "SELECT time as time, q_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_q WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    if (number == 1) {
                        unit.add(pqsDeviceUnit.getTotalNoP());
                    } else {
                        unit.add("Var");
                    }
                    targetName = "谐波无功功率";
                    break;
                case 52:
                    //谐波视在功率
                    sql = "SELECT time as time, s_" + number + " as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_s WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    if (number == 1) {
                        unit.add(pqsDeviceUnit.getTotalViewP());
                    } else {
                        unit.add("VA");
                    }
                    targetName = "谐波视在功率";
                    break;
                case 53:
                    //三相有功功率
                    sql = "SELECT time as time, p as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_p WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add(pqsDeviceUnit.getTotalActiveP());
                    targetName = "三相有功功率";
                    break;
                case 54:
                    //三相无功功率
                    sql = "SELECT time as time, q as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_q WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add(pqsDeviceUnit.getTotalNoP());
                    targetName = "三相无功功率";
                    break;
                case 55:
                    //三相视在功率
                    sql = "SELECT time as time, s as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_s WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    unit.add(pqsDeviceUnit.getTotalViewP());
                    targetName = "三相视在功率";
                    break;
                case 56:
                    //三相总有功功率
                    sql = "SELECT time as time, p as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_p WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    phasicType.add("三相总有功功率");
                    unit.add(pqsDeviceUnit.getTotalActiveP());
                    targetName = "三相总有功功率";
                    break;
                case 57:
                    //三相总无功功率
                    sql = "SELECT time as time, q as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_q WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    phasicType.add("三相总无功功率");
                    unit.add(pqsDeviceUnit.getTotalNoP());
                    targetName = "三相总无功功率";
                    break;
                case 58:
                    //三相总视在功率
                    sql = "SELECT time as time, s as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_s WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    phasicType.add("三相总视在功率");
                    unit.add(pqsDeviceUnit.getTotalViewP());
                    targetName = "三相总视在功率";
                    break;
                case 59:
                    //视在功率因数
                    sql = "SELECT time as time, pf as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_p WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') group by phasic_type order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    targetName = "视在功率因数";
                    break;
                case 591:
                    //位移功率因数
                    sql = "SELECT time as time, df as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_p WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    phasicType.add("A相");
                    phasicType.add("B相");
                    phasicType.add("C相");
                    targetName = "位移功率因数";
                    break;
                case 592:
                    //总视在功率因数
                    sql = "SELECT time as time, pf as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_p WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    phasicType.add("总视在功率因数");
                    targetName = "总视在功率因数";
                    break;
                case 593:
                    //总位移功率因数
                    sql = "SELECT time as time, df as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_harmpower_p WHERE " + stringBuilder +
                            " and phasic_type ='T' order by time asc tz('Asia/Shanghai');";
                    phasicType.add("总位移功率因数");
                    targetName = "总位移功率因数";
                    break;
                case 61:
                    //长时闪变
                    sql = "SELECT time as time, plt as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_plt WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    topLimit = overlimit.getFlicker();
                    targetName = "长时闪变";
                    break;
                case 60:
                    //短时闪变
                    sql = "SELECT time as time, pst as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_flicker WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    topLimit = overlimit.getFlicker();
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    targetName = "短时闪变";
                    break;
                case 62:
                    //电压波动
                    sql = "SELECT time as time, fluc as aValue ," + InfluxDBTableConstant.PHASIC_TYPE + " FROM data_fluc WHERE " + stringBuilder +
                            " and (phasic_type ='A' or phasic_type ='B' or phasic_type ='C') order by time asc tz('Asia/Shanghai');";
                    if (ptType == 0) {
                        phasicType.add("A相");
                        phasicType.add("B相");
                        phasicType.add("C相");
                    } else {
                        phasicType.add("AB相");
                        phasicType.add("BC相");
                        phasicType.add("CA相");
                    }
                    targetName = "电压波动";
                    unit.add("%");
                    break;
                default:
                    break;
            }
            //大致有3种类型
            //1、一次查询返回3条记录，分别为A/B/C三相的结果
            //2、一次查询返回一条记录，以T相为条件，返回某3个指标值
            //3、一次查询返回一条记录，以T相为条件，返回某1个指标值
            List<HarmonicHistoryData> harmonicHistoryData = commonMapper.getHistoryResult(sql);
            queryResultLimitVO.setHarmonicHistoryDataList(harmonicHistoryData);
            queryResultLimitVO.setTopLimit(topLimit);
            queryResultLimitVO.setLowerLimit(lowerLimit);
            queryResultLimitVO.setPhaiscType(phasicType);
            queryResultLimitVO.setUnit(unit);
            queryResultLimitVO.setLineName(lineDetailData.getPointName());
            queryResultLimitVO.setHarmNum(number);
            queryResultLimitVO.setTargetName(targetName);
            queryResultLimitVO.setWiringMethod(lineDetailData.getWiringMethod());
        } else {
            return queryResultLimitVO;
        }
        return queryResultLimitVO;
    }



}
