package com.njcn.harmonic.service.influxdb.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.njcn.dataProcess.param.LineCountEvaluateParam;
import com.njcn.influx.pojo.po.DataPlt;
import com.njcn.dataProcess.pojo.dto.DataPltDto;
import com.njcn.harmonic.service.influxdb.IDataPlt;
import com.njcn.influx.imapper.DataPltMapper;
import com.njcn.influx.pojo.po.DataV;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.redis.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/11/18  14:33【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class InfluxdbDataPltImpl  implements IDataPlt {

    private final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
    private final DataPltMapper dataPltMapper;
    private final RedisUtil redisUtil;
    private static final Map<String, String> PHASE_MAPPING = new HashMap<String, String>() {{
        put("AB", "A");
        put("BC", "B");
        put("CA", "C");
        put("M", "T");
    }};
    private static final Set<String> LINE_VOLTAGE_TYPES =
            Collections.unmodifiableSet(new HashSet<>(Arrays.asList("AB", "BC", "CA", "T")));
    private static final Set<String> PHASE_VOLTAGE_TYPES =
            Collections.unmodifiableSet(new HashSet<>(Arrays.asList("A", "B", "C", "T")));

    @Override
    public List<DataPltDto> getRawData(LineCountEvaluateParam lineParam) {
        List<DataPltDto> result = new ArrayList<>();
        List<DataPlt> list = getMinuteDataPlt(lineParam);
        list.forEach(item->{
            DataPltDto dto = new DataPltDto();
            BeanUtils.copyProperties(item,dto);
            dto.setMinTime(DATE_TIME_FORMATTER.format(item.getTime()));
            result.add(dto);
        });
        return result;
    }
    /**
     * 按监测点集合、时间条件获取dataI分钟数据
     * timeMap参数来判断是否进行数据处理 timeMap为空则不进行数据处理
     * 需要进行剔除异常数据时,这里会有三种情况判断
     * 1.无异常数据，则直接返回集合；
     * 2.异常数据和无异常数据参杂，剔除异常数据，只计算正常数据；
     * 3.全是异常数据，则使用异常数据进行计算，但是日表中需要标记出来，此数据有异常
     */
    public List<DataPlt> getMinuteDataPlt(LineCountEvaluateParam lineParam) {
        List<DataPlt> dataList;
        List<DataPlt> result = new ArrayList<>();
        List<DataPlt> data = new ArrayList<>();
        //获取监测点、接线方式数据
        Type type = new TypeToken<Map<String, Integer>>(){}.getType();
        Map<String, Integer> map = new Gson().fromJson(
                String.valueOf(redisUtil.getObjectByKey("wlLineDetail")),
                type
        );
        InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(DataPlt.class);
        influxQueryWrapper.regular(DataPlt::getLineId, lineParam.getLineId())
                .select(DataPlt::getLineId)
                .select(DataPlt::getPhaseType)
                .select(DataPlt::getPlt)
                .select(DataPlt::getQualityFlag)
                .select(DataPlt::getAbnormalFlag)
                .between(DataPlt::getTime, lineParam.getStartTime(), lineParam.getEndTime())
                .eq(DataPlt::getQualityFlag,"0");
        if(CollUtil.isNotEmpty(lineParam.getPhasicType())){
            influxQueryWrapper.regular(DataV::getPhaseType,lineParam.getPhasicType());
        }
        List<DataPlt> list = dataPltMapper.selectByQueryWrapper(influxQueryWrapper);
        if(CollUtil.isNotEmpty(list)){
            //过滤掉暂态事件影响的数据 true过滤 false不过滤
            if (lineParam.getDataType()) {
                dataList = list.stream().filter(item -> Objects.isNull(item.getAbnormalFlag())).collect(Collectors.toList());
            } else {
                dataList = list;
            }
            Map<String,List<DataPlt>> lineMap = dataList.stream().collect(Collectors.groupingBy(DataPlt::getLineId));
            //有异常数据
            if (CollectionUtil.isNotEmpty(lineParam.getAbnormalTime())) {
                lineMap.forEach((k,v)->{
                    List<String> timeList = lineParam.getAbnormalTime().get(k);
                    //有异常数据,当前监测点自身的异常数据
                    if (CollectionUtil.isNotEmpty(timeList)) {
                        List<DataPlt> filterList = v.stream().filter(item -> !timeList.contains(DATE_TIME_FORMATTER.format(item.getTime()))).collect(Collectors.toList());
                        //1.过滤掉异常数据后还有正常数据，则用正常数据计算
                        if (CollectionUtil.isNotEmpty(filterList)) {
                            result.addAll(filterList);
                        }
                        //2.过滤掉异常数据后没有正常数据，则用所有异常数据计算，但是需要标记数据为异常的
                        else {
                            v.parallelStream().forEach(item -> item.setQualityFlag("1"));
                            result.addAll(v);
                        }
                    }
                    //没有异常数据，则使用原数据
                    else {
                        result.addAll(v);
                    }
                });
            }
            //没有异常数据，则使用原数据
            else {
                result.addAll(dataList);
            }
        }
        if (CollectionUtil.isNotEmpty(result)) {
            if (!Objects.isNull(map)) {
                //现根据监测点分组，然后根据接线方式排除多于数据，在修改相别
                Map<String, List<DataPlt>> lineMap = result.stream().collect(Collectors.groupingBy(DataPlt::getLineId));
                lineMap.forEach((k,v)->{
                    if (Objects.isNull(map.get(k))) {
                        return;
                    }
                    Integer conType = map.get(k);
                    Set<String> validPhasicTypes = (conType != 0) ? LINE_VOLTAGE_TYPES : PHASE_VOLTAGE_TYPES;
                    List<DataPlt> result2 = v.stream().filter(item -> validPhasicTypes.contains(item.getPhaseType())).collect(Collectors.toList());
                    data.addAll(result2);
                });
            } else {
                data.addAll(result);
            }
            if (CollectionUtil.isNotEmpty(data)) {
                data.forEach(item -> {
                    String newType = PHASE_MAPPING.get(item.getPhaseType());
                    if (newType != null) {
                        item.setPhaseType(newType);
                    }
                });
            }
        }
        return data;
    }

}
