package com.njcn.harmonic.service.impl;

import com.njcn.harmonic.constant.Param;
import com.njcn.harmonic.pojo.param.PulicTimeStatisParam;
import com.njcn.harmonic.pojo.vo.AssesVO;
import com.njcn.harmonic.service.AssesService;
import com.njcn.influxdb.utils.InfluxDbUtils;
import com.njcn.web.utils.ComAssesUtil;
import com.njcn.web.utils.PqsComasses;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.influxdb.dto.QueryResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author denghuajun
 * @date 2022/3/2
 *
 */
@Service
@AllArgsConstructor
public class AssesServiceImpl implements AssesService {

    private final InfluxDbUtils influxDbUtils;

    private final ComAssesUtil comAssesUtil;

    @Override
    public AssesVO getQualityAssessData(PulicTimeStatisParam pulicTimeStatisParam) {
        AssesVO assesVO = new AssesVO();
        List<Float> floatList = new ArrayList<>();
        Float syn = getCondition(pulicTimeStatisParam.getId(), pulicTimeStatisParam.getSearchBeginTime(), pulicTimeStatisParam.getSearchEndTime());
        floatList.add(syn);
        if (StringUtils.isNotBlank(pulicTimeStatisParam.getPeriodBeginTime()) && StringUtils.isNotBlank(pulicTimeStatisParam.getPeriodEndTime())) {
            floatList.add(getCondition(pulicTimeStatisParam.getId(), pulicTimeStatisParam.getPeriodBeginTime(), pulicTimeStatisParam.getPeriodEndTime()));
        }
        assesVO.setSyn(floatList);
        return assesVO;
    }

    /**
     * influxDB相关操作
     * 查询监测点的数据完整性
     */
    private Float getCondition(String lineList, String startTime, String endTime) {
        List<PqsComasses> pqsComasses = new ArrayList<>();
        float synData;
        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(Param.LINEID + "='").append(lineList).append("')");
            String sql = "SELECT" +
                    "                MEAN(Freq_Dev1) AS freDev1," +
                    "                MEAN(Freq_Dev2) AS freDev2," +
                    "                MEAN(Freq_Dev3) AS freDev3," +
                    "                MEAN(Freq_Dev4) AS freDev4," +
                    "                MEAN(Freq_Dev5) AS freDev5," +
                    "                MEAN(VU_Dev1) AS vuDev1," +
                    "                MEAN(VU_Dev2) AS vuDev2," +
                    "                MEAN(VU_Dev3) AS vuDev3," +
                    "                MEAN(VU_Dev4) AS vuDev4," +
                    "                MEAN(VU_Dev5) AS vuDev5," +
                    "                MEAN(Data_PLT1) AS dataPlt1," +
                    "                MEAN(Data_PLT2) AS dataPlt2," +
                    "                MEAN(Data_PLT3) AS dataPlt3," +
                    "                MEAN(Data_PLT4) AS dataPlt4," +
                    "                MEAN(Data_PLT5) AS dataPlt5," +
                    "                MEAN(V_Unbalance1) AS vUnbalance1," +
                    "                MEAN(V_Unbalance2) AS vUnbalance2," +
                    "                MEAN(V_Unbalance3) AS vUnbalance3," +
                    "                MEAN(V_Unbalance4) AS vUnbalance4," +
                    "                MEAN(V_Unbalance5) AS vUnbalance5," +
                    "                MEAN(V_THD1) AS vThd1," +
                    "                MEAN(V_THD2) AS vThd2," +
                    "                MEAN(V_THD3) AS vThd3," +
                    "                MEAN(V_THD4) AS vThd4," +
                    "                MEAN(V_THD5) AS vThd5," +
                    "                MEAN(Event1) AS event1," +
                    "                MEAN(Event2) AS event2," +
                    "                MEAN(Event3) AS event3," +
                    "                MEAN(Event4) AS event4," +
                    "                MEAN(Event5) AS event5" +
                    "                FROM" +
                    "        PQS_COMASSES" +
                    "        where " + stringBuilder.toString() +
                    " group by " + Param.LINEID;
            //结果集
            QueryResult result = influxDbUtils.query(sql);
            //处理结果集
            List<QueryResult.Series> list = result.getResults().get(0).getSeries();
            if (!CollectionUtils.isEmpty(list)) {
                List<List<Object>> valueList = list.get(0).getValues();
                PqsComasses pqsComassesData = new PqsComasses();
                List<Object> value = valueList.get(0);
                pqsComassesData.setFreqDev1(BigDecimal.valueOf(Float.parseFloat(value.get(1).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setFreqDev2(BigDecimal.valueOf(Float.parseFloat(value.get(2).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setFreqDev3(BigDecimal.valueOf(Float.parseFloat(value.get(3).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setFreqDev4(BigDecimal.valueOf(Float.parseFloat(value.get(4).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setFreqDev5(BigDecimal.valueOf(Float.parseFloat(value.get(5).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVuDev1(BigDecimal.valueOf(Float.parseFloat(value.get(6).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVuDev2(BigDecimal.valueOf(Float.parseFloat(value.get(7).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVuDev3(BigDecimal.valueOf(Float.parseFloat(value.get(8).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVuDev4(BigDecimal.valueOf(Float.parseFloat(value.get(9).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVuDev5(BigDecimal.valueOf(Float.parseFloat(value.get(10).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setDataPst1(BigDecimal.valueOf(Float.parseFloat(value.get(11).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setDataPst2(BigDecimal.valueOf(Float.parseFloat(value.get(12).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setDataPst3(BigDecimal.valueOf(Float.parseFloat(value.get(13).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setDataPst4(BigDecimal.valueOf(Float.parseFloat(value.get(14).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setDataPst5(BigDecimal.valueOf(Float.parseFloat(value.get(15).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVUnbalance1(BigDecimal.valueOf(Float.parseFloat(value.get(16).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVUnbalance2(BigDecimal.valueOf(Float.parseFloat(value.get(17).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVUnbalance3(BigDecimal.valueOf(Float.parseFloat(value.get(18).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVUnbalance4(BigDecimal.valueOf(Float.parseFloat(value.get(19).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVUnbalance5(BigDecimal.valueOf(Float.parseFloat(value.get(20).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVThd1(BigDecimal.valueOf(Float.parseFloat(value.get(21).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVThd2(BigDecimal.valueOf(Float.parseFloat(value.get(22).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVThd3(BigDecimal.valueOf(Float.parseFloat(value.get(23).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVThd4(BigDecimal.valueOf(Float.parseFloat(value.get(24).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setVThd5(BigDecimal.valueOf(Float.parseFloat(value.get(25).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setEvent1(BigDecimal.valueOf(Float.parseFloat(value.get(26).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setEvent2(BigDecimal.valueOf(Float.parseFloat(value.get(27).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setEvent3(BigDecimal.valueOf(Float.parseFloat(value.get(28).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setEvent4(BigDecimal.valueOf(Float.parseFloat(value.get(29).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComassesData.setEvent5(BigDecimal.valueOf(Float.parseFloat(value.get(30).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                pqsComasses.add(pqsComassesData);
            } else {
                //有监测点，但是无数据的处理
                return 3.14159f;
            }
        } else {
            //无监测点前台统一替换为“/”
            return 3.1415f;
        }
        synData = comAssesUtil.getAllComAss(pqsComasses);
        return synData;
    }
}
