package com.njcn.prepare.harmonic.service.mysql.busbar.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.BusBarAndHisMonitorDTO;
import com.njcn.device.pms.pojo.po.RStatBusbarHarmonicYPO;
import com.njcn.harmonic.pojo.po.RStatDataVD;
import com.njcn.harmonic.pojo.po.day.RStatDataPltDPO;
import com.njcn.harmonic.pojo.po.day.RStatDataVDPO;
import com.njcn.prepare.harmonic.mapper.mysql.busbar.StationBusBarReportMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.busbar.IStationBusBarReportService;
import com.njcn.prepare.harmonic.service.mysql.day.IRStatDataPltDService;
import com.njcn.prepare.harmonic.service.mysql.day.IRStatDataVDService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * pqs
 *
 * @author cdf
 * @date 2024/1/25
 */
@Service
@RequiredArgsConstructor
public class StationBusBarReportServiceImpl extends MppServiceImpl<StationBusBarReportMapper, RStatBusbarHarmonicYPO> implements IStationBusBarReportService {

    private final CommTerminalGeneralClient commTerminalGeneralClient;

    private final IRStatDataVDService irStatDataVDService;

    private final IRStatDataPltDService irStatDataPltDService;

    @Override
    public void handleEveryDayAllYear(CalculatedParam<?> calculatedParam) {
        DateTime date = DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);
        String begin = DateUtil.format(DateUtil.beginOfYear(date), DatePattern.NORM_DATE_PATTERN);
        List<BusBarAndHisMonitorDTO> busBatList = commTerminalGeneralClient.getBusBarAndHisMonitor().getData();

        List<RStatBusbarHarmonicYPO> resultPoList = new ArrayList<>();
        for (BusBarAndHisMonitorDTO busBarAndHisMonitorDTO : busBatList) {
            RStatBusbarHarmonicYPO rStatBusbarHarmonicYPO = new RStatBusbarHarmonicYPO();
            rStatBusbarHarmonicYPO.setBusbarId(busBarAndHisMonitorDTO.getBusBarId());
            rStatBusbarHarmonicYPO.setDataDate(LocalDate.parse(begin));

            LambdaQueryWrapper<RStatDataVDPO> vdQuery = new LambdaQueryWrapper<>();
            vdQuery.select(RStatDataVDPO::getLineId, RStatDataVDPO::getPhaseType, RStatDataVDPO::getVThd, RStatDataVDPO::getRms, RStatDataVDPO::getValueType, RStatDataVDPO::getVUnbalance)
                    .in(RStatDataVDPO::getLineId, busBarAndHisMonitorDTO.getMonitorIds()).between(RStatDataVDPO::getTime, begin, calculatedParam.getDataDate());
            List<RStatDataVDPO> rStatDataVDPOList = irStatDataVDService.list(vdQuery);

            Map<String, List<RStatDataVDPO>> vMap = rStatDataVDPOList.stream().collect(Collectors.groupingBy(RStatDataVDPO::getValueType));
            if (vMap.containsKey("MAX")) {
                List<RStatDataVDPO> maxList = vMap.get("MAX");
                Double rms = maxList.stream().filter(item -> !item.getPhaseType().equals("A")).map(RStatDataVDPO::getRms).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                Double thd = maxList.stream().filter(item -> item.getPhaseType().equals("A")).map(RStatDataVDPO::getVThd).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                Double un = maxList.stream().filter(item -> item.getPhaseType().equals("A")).map(RStatDataVDPO::getVUnbalance).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                rStatBusbarHarmonicYPO.setVEffectiveMax(rms);
                rStatBusbarHarmonicYPO.setVThdMax(thd);
                rStatBusbarHarmonicYPO.setUnbalanceMax(un);
            }else {
                rStatBusbarHarmonicYPO.setVEffectiveMax(3.14159);
                rStatBusbarHarmonicYPO.setVThdMax(3.14159);
                rStatBusbarHarmonicYPO.setUnbalanceMax(3.14159);
            }

            if (vMap.containsKey("MIN")) {
                List<RStatDataVDPO> maxList = vMap.get("MIN");
                Double rms = maxList.stream().filter(item -> !item.getPhaseType().equals("A")).map(RStatDataVDPO::getRms).min(Comparator.comparing(Function.identity())).orElse(3.14159);
                Double thd = maxList.stream().filter(item -> item.getPhaseType().equals("A")).map(RStatDataVDPO::getVThd).min(Comparator.comparing(Function.identity())).orElse(3.14159);
                Double un = maxList.stream().filter(item -> item.getPhaseType().equals("A")).map(RStatDataVDPO::getVUnbalance).min(Comparator.comparing(Function.identity())).orElse(3.14159);
                rStatBusbarHarmonicYPO.setVEffectiveMin(rms);
                rStatBusbarHarmonicYPO.setVThdMin(thd);
                rStatBusbarHarmonicYPO.setUnbalanceMin(un);
            }else {
                rStatBusbarHarmonicYPO.setVEffectiveMin(3.14159);
                rStatBusbarHarmonicYPO.setVThdMin(3.14159);
                rStatBusbarHarmonicYPO.setUnbalanceMin(3.14159);
            }

            if (vMap.containsKey("AVG")) {
                List<RStatDataVDPO> maxList = vMap.get("AVG");
                Double rms = maxList.stream().filter(item -> !item.getPhaseType().equals("A")).mapToDouble(RStatDataVDPO::getRms).average().orElse(3.14159);
                Double thd = maxList.stream().filter(item -> item.getPhaseType().equals("A")).mapToDouble(RStatDataVDPO::getVThd).average().orElse(3.14159);
                Double un = maxList.stream().filter(item -> item.getPhaseType().equals("A")).mapToDouble(RStatDataVDPO::getVUnbalance).average().orElse(3.14159);
                rStatBusbarHarmonicYPO.setVEffectiveAvg(rms);
                rStatBusbarHarmonicYPO.setVThdAvg(thd);
                rStatBusbarHarmonicYPO.setUnbalanceAvg(un);
            }else {
                rStatBusbarHarmonicYPO.setVEffectiveAvg(3.14159);
                rStatBusbarHarmonicYPO.setVThdAvg(3.14159);
                rStatBusbarHarmonicYPO.setUnbalanceAvg(3.14159);
            }

            if (vMap.containsKey("CP95")) {
                List<RStatDataVDPO> cp95List = vMap.get("CP95");
                Double rms = cp95List.stream().filter(item -> !item.getPhaseType().equals("A")).map(RStatDataVDPO::getRms).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                Double thd = cp95List.stream().filter(item -> item.getPhaseType().equals("A")).map(RStatDataVDPO::getVThd).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                Double un = cp95List.stream().filter(item -> item.getPhaseType().equals("A")).map(RStatDataVDPO::getVUnbalance).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                rStatBusbarHarmonicYPO.setVEffective95(rms);
                rStatBusbarHarmonicYPO.setVThd95(thd);
                rStatBusbarHarmonicYPO.setUnbalance95(un);
            }else {
                rStatBusbarHarmonicYPO.setVEffective95(3.14159);
                rStatBusbarHarmonicYPO.setVThd95(3.14159);
                rStatBusbarHarmonicYPO.setUnbalance95(3.14159);
            }

            //长时闪变
            LambdaQueryWrapper<RStatDataPltDPO> pltdQuery = new LambdaQueryWrapper<>();
            pltdQuery.select(RStatDataPltDPO::getPlt,RStatDataPltDPO::getPhaseType,RStatDataPltDPO::getValueType)
                    .in(RStatDataPltDPO::getLineId, busBarAndHisMonitorDTO.getMonitorIds()).between(RStatDataPltDPO::getTime, begin,calculatedParam.getDataDate());
            List<RStatDataPltDPO> pltList = irStatDataPltDService.list(pltdQuery);
            Map<String, List<RStatDataPltDPO>> pltMap = pltList.stream().collect(Collectors.groupingBy(RStatDataPltDPO::getValueType));
            if (pltMap.containsKey("MAX")) {
                List<RStatDataPltDPO> maxList = pltMap.get("MAX");
                Double plt = maxList.stream().map(RStatDataPltDPO::getPlt).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                rStatBusbarHarmonicYPO.setFlickerMax(plt);
            }else {
                rStatBusbarHarmonicYPO.setFlickerMax(3.14159);
            }
            if (pltMap.containsKey("MIN")) {
                List<RStatDataPltDPO> minList = pltMap.get("MIN");
                Double plt = minList.stream().map(RStatDataPltDPO::getPlt).min(Comparator.comparing(Function.identity())).orElse(3.14159);
                rStatBusbarHarmonicYPO.setFlickerMin(plt);
            }else {
                rStatBusbarHarmonicYPO.setFlickerMin(3.14159);
            }
            if (pltMap.containsKey("AVG")) {
                List<RStatDataPltDPO> maxList = pltMap.get("AVG");
                Double plt = maxList.stream().mapToDouble(RStatDataPltDPO::getPlt).average().orElse(3.14159);
                rStatBusbarHarmonicYPO.setFlickerAvg(plt);
            }else {
                rStatBusbarHarmonicYPO.setFlickerAvg(3.14159);
            }
            if (pltMap.containsKey("CP95")) {
                List<RStatDataPltDPO> cp95List = pltMap.get("CP95");
                Double plt = cp95List.stream().map(RStatDataPltDPO::getPlt).max(Comparator.comparing(Function.identity())).orElse(3.14159);
                rStatBusbarHarmonicYPO.setFlicker95(plt);
            }else {
                rStatBusbarHarmonicYPO.setFlicker95(3.14159);
            }
            resultPoList.add(rStatBusbarHarmonicYPO);
        }

        this.saveOrUpdateBatchByMultiId(resultPoList);
    }



}
