package com.njcn.influx.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.njcn.influx.imapper.DataFlickerMapper;
import com.njcn.influx.pojo.dto.DataFlickerDTO;
import com.njcn.influx.pojo.po.DataFlicker;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.influx.service.DataFlickerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2023年05月05日 09:05
 */
@Service
public class DataFlickerServiceImpl implements DataFlickerService {

    @Resource
    private DataFlickerMapper dataFlickerMapper;


    @Override
    public List<DataFlickerDTO> getDataFlicker(String lineIndex, String startTime, String endTime) {
        //最小值
        List<DataFlickerDTO> result1 ;
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(DataFlicker.class, DataFlickerDTO.class);
        influxQueryWrapper.eq(DataFlicker::getLineId, lineIndex)
                .min(DataFlicker::getFluc)
                .min(DataFlicker::getPlt)
                .min(DataFlicker::getPst)
                .groupBy(DataFlicker::getLineId,DataFlicker::getPhaseType,DataFlicker::getQualityFlag)
                .between(DataFlicker::getTime, startTime, endTime);
        result1 = dataFlickerMapper.getStatisticsByWraper(influxQueryWrapper);
        result1.forEach(item -> {
            item.setValueType("MIN");
        });
        //最大值
        List<DataFlickerDTO> result2 ;
        influxQueryWrapper.initSql();
        influxQueryWrapper.eq(DataFlicker::getLineId, lineIndex)
                .max(DataFlicker::getFluc)
                .max(DataFlicker::getPlt)
                .max(DataFlicker::getPst)
                .groupBy(DataFlicker::getLineId,DataFlicker::getPhaseType,DataFlicker::getQualityFlag)
                .between(DataFlicker::getTime, startTime, endTime);
        result2 = dataFlickerMapper.getStatisticsByWraper(influxQueryWrapper);
        result2.forEach(item -> {
            item.setValueType("MAX");
        });
        //平均值
        List<DataFlickerDTO> result3 ;
        influxQueryWrapper.initSql();
        influxQueryWrapper.eq(DataFlicker::getLineId, lineIndex)
                .mean(DataFlicker::getFluc)
                .mean(DataFlicker::getPlt)
                .mean(DataFlicker::getPst)
                .groupBy(DataFlicker::getLineId,DataFlicker::getPhaseType,DataFlicker::getQualityFlag)
                .between(DataFlicker::getTime, startTime, endTime);
        result3 = dataFlickerMapper.getStatisticsByWraper(influxQueryWrapper);
        result3.forEach(item -> {
            item.setValueType("AVG");
        });
        List<DataFlickerDTO> result4 ;
        influxQueryWrapper.initSql();
        influxQueryWrapper.eq(DataFlicker::getLineId, lineIndex)
                .percentile(DataFlicker::getFluc, 95)
                .percentile(DataFlicker::getPlt, 95)
                .percentile(DataFlicker::getPst, 95)
                .groupBy(DataFlicker::getLineId,DataFlicker::getPhaseType,DataFlicker::getQualityFlag)
                .between(DataFlicker::getTime, startTime, endTime);
        //CP95值
        result4 = dataFlickerMapper.getStatisticsByWraper(influxQueryWrapper);
        result4.forEach(item -> {
            item.setValueType("CP95");
        });
        List<DataFlickerDTO> result = new ArrayList<>();
        result.addAll(result1);
        result.addAll(result2);
        result.addAll(result3);
        result.addAll(result4);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            System.out.println(objectMapper.writeValueAsString(result));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
