package com.njcn;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.njcn.common.utils.HarmonicTimesUtil;
import com.njcn.influx.constant.InfluxDbSqlConstant;
import com.njcn.influx.deprecated.InfluxDBPublicParam;
import com.njcn.influx.imapper.DataVMapper;
import com.njcn.influx.pojo.constant.InfluxDBTableConstant;
import com.njcn.influx.pojo.po.DataV;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.prepare.PrepareApplication;
import lombok.SneakyThrows;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年12月10日 15:05
 */
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = PrepareApplication.class)
public class BaseJunitTest {

    @Resource
    private DataVMapper dataVMapper;
    @Resource
    private InfluxDbUtils influxDbUtils;

    //速度272
    @SneakyThrows
    @Test
    public void test1() {
        long time1 = System.currentTimeMillis();
        List<String> list = Arrays.asList("1213aad4e6f51b0fc74701bd7eb531d1");
        LocalDateTime startTime = LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.parse("2023-10-01", DatePattern.NORM_DATE_PATTERN));
        LocalDateTime endTime = LocalDateTimeUtil.endOfDay(LocalDateTimeUtil.parse("2023-10-01", DatePattern.NORM_DATE_PATTERN));

        List<DataV> result1 ;
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(DataV.class);
        //最小值
        influxQueryWrapper.minSamePrefixAndSuffix("v_","", HarmonicTimesUtil.harmonicTimesList(1,50,1));
        influxQueryWrapper.regular(DataV::getLineId, list)
                .eq(DataV::getValueType, "MIN")
                .min(DataV::getFreq)
                .min(DataV::getFreqDev)
                .min(DataV::getRms)
                .min(DataV::getRmsLvr)
                .min(DataV::getVNeg)
                .min(DataV::getVPos)
                .min(DataV::getVThd)
                .min(DataV::getVUnbalance)
                .min(DataV::getVZero)
                .min(DataV::getVlDev)
                .min(DataV::getVuDev)
                .groupBy(DataV::getLineId,DataV::getPhaseType,DataV::getQualityFlag,DataV::getValueType)
                .between(DataV::getTime, startTime, endTime);
        result1 = dataVMapper.getStatisticsByWraper(influxQueryWrapper);
        System.out.println("result1==:" + result1);

        //最大值
        influxQueryWrapper.maxSamePrefixAndSuffix("v_","", HarmonicTimesUtil.harmonicTimesList(1,50,1));
        influxQueryWrapper.regular(DataV::getLineId, list)
                .eq(DataV::getValueType, "MIN")
                .max(DataV::getFreq)
                .max(DataV::getFreqDev)
                .max(DataV::getRms)
                .max(DataV::getRmsLvr)
                .max(DataV::getVNeg)
                .max(DataV::getVPos)
                .max(DataV::getVThd)
                .max(DataV::getVUnbalance)
                .max(DataV::getVZero)
                .max(DataV::getVlDev)
                .max(DataV::getVuDev)
                .groupBy(DataV::getLineId,DataV::getPhaseType,DataV::getQualityFlag,DataV::getValueType)
                .between(DataV::getTime, startTime, endTime);
        result1 = dataVMapper.getStatisticsByWraper(influxQueryWrapper);

        //平均值
        influxQueryWrapper.meanSamePrefixAndSuffix("v_","", HarmonicTimesUtil.harmonicTimesList(1,50,1));
        influxQueryWrapper.regular(DataV::getLineId, list)
                .eq(DataV::getValueType, "MIN")
                .mean(DataV::getFreq)
                .mean(DataV::getFreqDev)
                .mean(DataV::getRms)
                .mean(DataV::getRmsLvr)
                .mean(DataV::getVNeg)
                .mean(DataV::getVPos)
                .mean(DataV::getVThd)
                .mean(DataV::getVUnbalance)
                .mean(DataV::getVZero)
                .mean(DataV::getVlDev)
                .mean(DataV::getVuDev)
                .groupBy(DataV::getLineId,DataV::getPhaseType,DataV::getQualityFlag,DataV::getValueType)
                .between(DataV::getTime, startTime, endTime);
        result1 = dataVMapper.getStatisticsByWraper(influxQueryWrapper);
        long time2 = System.currentTimeMillis();
        System.out.println("time1==:" + (time2-time1));
    }

    //速度174
    @SneakyThrows
    @Test
    public void test2() {
        long time1 = System.currentTimeMillis();
        String lineIndex = "1213aad4e6f51b0fc74701bd7eb531d1";
        String startTime = "2023-10-01 00:00:00";
        String endTime = "2023-10-01 23:59:59";

        List<DataV> result = new ArrayList<>();
        //最小值
        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        stringBuilder1.append("min(freq) AS freq,min(freq_dev) AS freq_dev,min(rms) AS rms,min(rms_lvr) AS rms_lvr,min(v_neg) AS v_neg,min(v_pos) AS v_pos,min(v_thd) AS v_thd,min(v_unbalance) AS v_unbalance,min(v_zero) AS v_zero,min(vl_dev) AS vl_dev,min(vu_dev) AS vu_dev,");
        for (int i = 1; i <= 50; i++) {
            if (i == 50) {
                stringBuilder1.append("min(v_").append(i).append(") AS v_").append(i);
            } else {
                stringBuilder1.append("min(v_").append(i).append(") AS v_").append(i).append(",");
            }
        }
        stringBuilder2.append("line_id='").append(lineIndex).append("' and ").append(InfluxDBPublicParam.TIME + " >= '").append(startTime).append("' and ").append(InfluxDBPublicParam.TIME).append(" <= '").append(endTime).append("' and value_type = 'MIN' group by line_id,phasic_type,value_type,quality_flag ").append(InfluxDBPublicParam.TIME_ZONE);
        String sql1 = "select " + stringBuilder1 + " from " + InfluxDBPublicParam.DATA_V + " where " + stringBuilder2;
        QueryResult sqlResult1 = influxDbUtils.query(sql1);
        InfluxDBResultMapper resultMapper1 = new InfluxDBResultMapper();
        List<DataV> list1 = resultMapper1.toPOJO(sqlResult1, DataV.class);
        //最大值
        StringBuilder stringBuilder3 = new StringBuilder();
        StringBuilder stringBuilder4 = new StringBuilder();
        stringBuilder3.append("max(freq) AS freq,max(freq_dev) AS freq_dev,max(rms) AS rms,max(rms_lvr) AS rms_lvr,max(v_neg) AS v_neg,max(v_pos) AS v_pos,max(v_thd) AS v_thd,max(v_unbalance) AS v_unbalance,max(v_zero) AS v_zero,max(vl_dev) AS vl_dev,max(vu_dev) AS vu_dev,");
        for (int i = 1; i <= 50; i++) {
            if (i == 50) {
                stringBuilder3.append("max(v_").append(i).append(") AS v_").append(i);
            } else {
                stringBuilder3.append("max(v_").append(i).append(") AS v_").append(i).append(",");
            }
        }
        stringBuilder4.append("line_id='").append(lineIndex).append("' and ").append(InfluxDBPublicParam.TIME + " >= '").append(startTime).append("' and ").append(InfluxDBPublicParam.TIME).append(" <= '").append(endTime).append("' and value_type = 'MAX' group by line_id,phasic_type,value_type,quality_flag ").append(InfluxDBPublicParam.TIME_ZONE);
        String sql2 = "select " + stringBuilder3 + " from " + InfluxDBPublicParam.DATA_V + " where " + stringBuilder4;
        QueryResult sqlResult2 = influxDbUtils.query(sql2);
        InfluxDBResultMapper resultMapper2 = new InfluxDBResultMapper();
        List<DataV> list2 = resultMapper2.toPOJO(sqlResult2, DataV.class);
        //平均值
        StringBuilder stringBuilder5 = new StringBuilder();
        StringBuilder stringBuilder6 = new StringBuilder();
        stringBuilder5.append("mean(freq) AS freq,mean(freq_dev) AS freq_dev,mean(rms) AS rms,mean(rms_lvr) AS rms_lvr,mean(v_neg) AS v_neg,mean(v_pos) AS v_pos,mean(v_thd) AS v_thd,mean(v_unbalance) AS v_unbalance,mean(v_zero) AS v_zero,mean(vl_dev) AS vl_dev,mean(vu_dev) AS vu_dev,");
        for (int i = 1; i <= 50; i++) {
            if (i == 50) {
                stringBuilder5.append("mean(v_").append(i).append(") AS v_").append(i);
            } else {
                stringBuilder5.append("mean(v_").append(i).append(") AS v_").append(i).append(",");
            }
        }
        stringBuilder6.append("line_id='").append(lineIndex).append("' and ").append(InfluxDBPublicParam.TIME + " >= '").append(startTime).append("' and ").append(InfluxDBPublicParam.TIME).append(" <= '").append(endTime).append("' and value_type = 'AVG' group by line_id,phasic_type,value_type,quality_flag ").append(InfluxDBPublicParam.TIME_ZONE);
        String sql3 = "select " + stringBuilder5 + " from " + InfluxDBPublicParam.DATA_V + " where " + stringBuilder6;
        QueryResult sqlResult3 = influxDbUtils.query(sql3);
        InfluxDBResultMapper resultMapper3 = new InfluxDBResultMapper();
        List<DataV> list3 = resultMapper3.toPOJO(sqlResult3, DataV.class);
//        //CP95值
//        StringBuilder stringBuilder7 = new StringBuilder();
//        StringBuilder stringBuilder8 = new StringBuilder();
//        stringBuilder7.append("percentile(freq,95) AS freq,percentile(freq_dev,95) AS freq_dev,percentile(rms,95) AS rms,percentile(rms_lvr,95) AS rms_lvr,percentile(v_neg,95) AS v_neg,percentile(v_pos,95) AS v_pos,percentile(v_thd,95) AS v_thd,percentile(v_unbalance,95) AS v_unbalance,percentile(v_zero,95) AS v_zero,percentile(vl_dev,95) AS vl_dev,percentile(vu_dev,95) AS vu_dev,");
//        for (int i = 1; i <= 50; i++) {
//            if (i == 50) {
//                stringBuilder7.append("percentile(v_").append(i).append(",95) AS v_").append(i);
//            } else {
//                stringBuilder7.append("percentile(v_").append(i).append(",95) AS v_").append(i).append(",");
//            }
//        }
//        stringBuilder8.append("line_id='").append(lineIndex).append("' and ").append(InfluxDBPublicParam.TIME + " >= '").append(startTime).append("' and ").append(InfluxDBPublicParam.TIME).append(" <= '").append(endTime).append("' and value_type = 'AVG' group by line_id,phasic_type,value_type,quality_flag ").append(InfluxDBPublicParam.TIME_ZONE);
//        String sql4 = "select " + stringBuilder7 + " from " + InfluxDBPublicParam.DATA_V + " where " + stringBuilder8;
//        QueryResult sqlResult4 = influxDbUtils.query(sql4);
//        InfluxDBResultMapper resultMapper4 = new InfluxDBResultMapper();
//        List<DataV> list4 = resultMapper4.toPOJO(sqlResult4, DataV.class);
//        list4.forEach(item -> {
//            item.setValueType("CP95");
//        });
        long time2 = System.currentTimeMillis();
        System.out.println("time==:" + (time2-time1));

    }


    @SneakyThrows
    @Test
    public void test3() {

        String lineIndex = "a166830bfc3a1b81a3635876ffed805e";
        String time1 = "2023-11-11 00:00:00";
        String time2 = "2023-11-11 23:59:59";

        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(DataV.class);
        influxQueryWrapper.regular(DataV::getLineId, lineIndex)
                .eq(DataV::getValueType, InfluxDbSqlConstant.CP95)
                .regular(DataV::getPhaseType, Arrays.asList(InfluxDBTableConstant.PHASE_TYPE_A,InfluxDBTableConstant.PHASE_TYPE_B,InfluxDBTableConstant.PHASE_TYPE_C))
                .eq(DataV::getQualityFlag, InfluxDBTableConstant.NORMAL)
                .between(DataV::getTime, time1, time2);
        List<DataV> list = dataVMapper.getStatisticsByWraper(influxQueryWrapper);
        System.out.println("list==:" + list);
    }

}
