package com.njcn.prepare.harmonic.service.mysql.Impl.line;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.pms.api.DistributionMonitorClient;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.prepare.harmonic.mapper.mysql.area.RMpTargetWarnDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.day.RStatDataVDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.*;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.dto.AlarmDetailDayDTO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpTargetDiffDPO;
import com.njcn.prepare.harmonic.service.mysql.line.AlarmDetailService;
import com.njcn.prepare.harmonic.utils.PublicUtil;
import com.njcn.system.api.DicDataFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 数据是否异常
 *
 * @author qijian
 * @version 1.0.0
 * @createTime 2022/11/29 - 10:09
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AlarmDetailServiceImpl extends MppServiceImpl<AlarmMapper, RMpTargetDiffDPO> implements AlarmDetailService {

    private final DistributionMonitorClient distributionMonitorClient;

    private final MonitorClient monitorClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final RMpTargetWarnDMapper rMpTargetWarnDMapper;

    private final RMpMeasurePhaseReportDPOMapper rMpMeasurePhaseReportDPOMapper;

    private final RMpMeasureReportDPOMapper rMpMeasureReportDPOMapper;

    private final RMpHarmonicVRateReportDPOMapper rMpHarmonicVRateReportDPOMapper;

    private final RMpEventDetailDMapper rMpEventDetailDMapper;

    private final RMpPltReportDPOMapper rMpPltReportDPOMapper;

    private final RStatDataVDMapper rStatDataVDMapper;

//    @Override
//    @Transactional(rollbackFor = {Exception.class})
//    @Async("asyncExecutor")
//    public void targetDiff(LineParam lineParam) {
//        String startTime,endTime;
//        //月处理
//        if (Objects.equals(true,lineParam.getRepairFlag())){
//            List<String> rangeDate = NjcnDateUtils.findEveryDay(lineParam.getBeginTime(),lineParam.getEndTime());
//            for (String item : rangeDate) {
//                log.info(item+"-->开始执行");
//                startTime = item + " 00:00:00";
//                endTime = PublicUtil.getYesterday(item) +  " 00:00:00";
//                diffData(startTime,endTime,lineParam.getLineIds());
//            }
//        }
//        //日处理
//        else {
//            startTime = lineParam.getBeginTime();
//            endTime = PublicUtil.getYesterday(lineParam.getDataDate()) +  " 00:00:00";
//            diffData(startTime,endTime,lineParam.getLineIds());
//        }
//    }

    /*******************************************新算法***********************************************************************/
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void targetDiff(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>监测点稳态指标差值日表开始执行");
        List<String> lineIds = calculatedParam.getIdList();
        String nowDay = calculatedParam.getDataDate();
        String yesterday = PublicUtil.getYesterday(nowDay);
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(lineIds,20);
        for (List<String> pendingId : pendingIds) {
            List<RMpTargetDiffDPO> intersectionList = diffData(nowDay,yesterday,pendingId);
            if (!CollectionUtils.isEmpty(intersectionList)){
                this.saveOrUpdateBatchByMultiId(intersectionList);
            }
        }
    }

    /**
     * 生成指标差值日统计
     */
    public List<RMpTargetDiffDPO> diffData(String startTime, String endTime, List<String> list) {
        List<RMpTargetDiffDPO> intersectionList = new ArrayList<>();
        List<RMpTargetDiffDPO> localData = rStatDataVDMapper.getDayData(startTime,list);
        List<RMpTargetDiffDPO> yesterdayData = rStatDataVDMapper.getDayData(endTime,list);
        if (!CollectionUtils.isEmpty(localData) && !CollectionUtils.isEmpty(yesterdayData)){
            //取两个集合的交集,做算法处理取绝对值
            intersectionList = localData.stream().map(map->yesterdayData.stream().filter(m->Objects.equals(m.getLineId(),map.getLineId())).findFirst().map(m->{
                map.setVlDev(Math.abs(map.getVlDev()-m.getVlDev()));
                map.setFreqDev(Math.abs(map.getFreqDev()-m.getFreqDev()));
                map.setVUnbalance(Math.abs(map.getVUnbalance()-m.getVUnbalance()));
                map.setVHarmonic(Math.abs(map.getVHarmonic()-m.getVHarmonic()));
                map.setPlt(Math.abs(map.getPlt()-m.getPlt()));
                map.setSagTimes(Math.abs(map.getSagTimes()-m.getSagTimes()));
                map.setInterruptTimes(Math.abs(map.getInterruptTimes()-m.getInterruptTimes()));
                return map;
            }).orElse(null)).filter(Objects::nonNull).collect(Collectors.toList());
        }
        return intersectionList;
    }
    /*******************************************新算法结束***********************************************************************/


    @Override
    public List<AlarmDetailDayDTO> getAvgDiffData(String time,List<String> list) {
        String endTime = PublicUtil.getLastYearDay(time);
        //计算本年的rm值
        return this.baseMapper.getAvgDiffData(endTime,time,list);
    }

//    /**
//     * 处理数据（电压偏差告警日统计）
//     * @author qijian
//     * @date 2022/12/28
//     */
//    private void processVoltageDeviationAlarm(String date, Integer type, String lineId) {
//        //日期条件（取前天、昨天、今天）
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDate yesterday = localDate.plusDays(-1);
//        LocalDate beforeYesterday = yesterday.plusDays(-1);
//        double localMax = rMpMeasurePhaseReportDPOMapper.getMaxVDeUp(lineId, localDate);
//        double yesterdayMax = rMpMeasurePhaseReportDPOMapper.getMaxVDeUp(lineId, yesterday);
//        double beforeYesterdayMax = rMpMeasurePhaseReportDPOMapper.getMaxVDeUp(lineId, beforeYesterday);
//
//        //计算RM和CL值
//        double rm = ((localMax - yesterdayMax) + (yesterdayMax - beforeYesterdayMax)) / 2;
//        double cl = (localMax + yesterdayMax + beforeYesterdayMax) / 3;
//
//        //计算RL和UCL值
//        double rl = rm * 3.3;
//        double ucl = cl + rm * 3;
//
//        //如果本日最大值>UCL 且 本日最大值>昨日最大值，则告警
//        if (localMax > ucl && localMax > yesterdayMax){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsVDevWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//
//        //如果本日>昨日>前日，且前日的值超过RL值，则告警
//        if (localMax > yesterdayMax && yesterdayMax > beforeYesterdayMax && beforeYesterdayMax > rl){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsVDevWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//    }

//    /**
//     * 处理数据（频率偏差告警日统计）
//     * @author qijian
//     * @date 2022/12/29
//     */
//    private void processFrequencyDeviationAlarm(String date, Integer type, String lineId) {
//        //日期条件（取前天、昨天、今天）
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDate yesterday = localDate.plusDays(-1);
//        LocalDate beforeYesterday = yesterday.plusDays(-1);
//        double localMax = rMpMeasureReportDPOMapper.getMaxFrequencyDeviation(lineId, localDate);
//        double yesterdayMax = rMpMeasureReportDPOMapper.getMaxFrequencyDeviation(lineId, yesterday);
//        double beforeYesterdayMax = rMpMeasureReportDPOMapper.getMaxFrequencyDeviation(lineId, beforeYesterday);
//
//        //计算RM和CL值
//        double rm = ((localMax - yesterdayMax) + (yesterdayMax - beforeYesterdayMax)) / 2;
//        double cl = (localMax + yesterdayMax + beforeYesterdayMax) / 3;
//
//        //计算RL和UCL值
//        double rl = rm * 3.3;
//        double ucl = cl + rm * 3;
//
//        //如果本日最大值>UCL 且 本日最大值>昨日最大值，则告警
//        if (localMax > ucl && localMax > yesterdayMax){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsFreqWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//
//        //如果本日>昨日>前日，且前日的值超过RL值，则告警
//        if (localMax > yesterdayMax && yesterdayMax > beforeYesterdayMax && beforeYesterdayMax > rl){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsFreqWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//    }

//    /**
//     * 处理数据（三相电压不平衡告警日统计）
//     * @author qijian
//     * @date 2022/12/29
//     */
//    private void processUnbalance95Alarm(String date, Integer type, String lineId) {
//        //日期条件（取前天、昨天、今天）
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDate yesterday = localDate.plusDays(-1);
//        LocalDate beforeYesterday = yesterday.plusDays(-1);
//        double localMax = rMpMeasureReportDPOMapper.getMaxUnbalance95(lineId, localDate);
//        double yesterdayMax = rMpMeasureReportDPOMapper.getMaxUnbalance95(lineId, yesterday);
//        double beforeYesterdayMax = rMpMeasureReportDPOMapper.getMaxUnbalance95(lineId, beforeYesterday);
//
//        //计算RM和CL值
//        double rm = ((localMax - yesterdayMax) + (yesterdayMax - beforeYesterdayMax)) / 2;
//        double cl = (localMax + yesterdayMax + beforeYesterdayMax) / 3;
//
//        //计算RL和UCL值
//        double rl = rm * 3.3;
//        double ucl = cl + rm * 3;
//
//        //如果本日最大值>UCL 且 本日最大值>昨日最大值，则告警
//        if (localMax > ucl && localMax > yesterdayMax){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsUnbalanceWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//
//        //如果本日>昨日>前日，且前日的值超过RL值，则告警
//        if (localMax > yesterdayMax && yesterdayMax > beforeYesterdayMax && beforeYesterdayMax > rl){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsUnbalanceWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//    }

//    /**
//     * 处理数据（谐波电压告警日统计）
//     * @author qijian
//     * @date 2022/12/29
//     */
//    private void processVoltageHarmonicThd95Alarm(String date, Integer type, String lineId) {
//        //日期条件（取前天、昨天、今天）
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDate yesterday = localDate.plusDays(-1);
//        LocalDate beforeYesterday = yesterday.plusDays(-1);
//        double localMax = rMpHarmonicVRateReportDPOMapper.getMaxVoltageHarmonicThd95(lineId, localDate);
//        double yesterdayMax = rMpHarmonicVRateReportDPOMapper.getMaxVoltageHarmonicThd95(lineId, yesterday);
//        double beforeYesterdayMax = rMpHarmonicVRateReportDPOMapper.getMaxVoltageHarmonicThd95(lineId, beforeYesterday);
//
//        //计算RM和CL值
//        double rm = ((localMax - yesterdayMax) + (yesterdayMax - beforeYesterdayMax)) / 2;
//        double cl = (localMax + yesterdayMax + beforeYesterdayMax) / 3;
//
//        //计算RL和UCL值
//        double rl = rm * 3.3;
//        double ucl = cl + rm * 3;
//
//        //如果本日最大值>UCL 且 本日最大值>昨日最大值，则告警
//        if (localMax > ucl && localMax > yesterdayMax){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsVWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//
//        //如果本日>昨日>前日，且前日的值超过RL值，则告警
//        if (localMax > yesterdayMax && yesterdayMax > beforeYesterdayMax && beforeYesterdayMax > rl){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsVWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//    }

//    /**
//     * 处理数据（长时电压闪变告警日统计）
//     * @author qijian
//     * @date 2022/12/30
//     */
//    private void processFlashingAlarm(String date, Integer type, String lineId) {
//        //日期条件（取前天、昨天、今天）
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDate yesterday = localDate.plusDays(-1);
//        LocalDate beforeYesterday = yesterday.plusDays(-1);
//        double localMax = rMpPltReportDPOMapper.getMaxFlashing(lineId, localDate);
//        double yesterdayMax = rMpPltReportDPOMapper.getMaxFlashing(lineId, yesterday);
//        double beforeYesterdayMax = rMpPltReportDPOMapper.getMaxFlashing(lineId, beforeYesterday);
//
//        //计算RM和CL值
//        double rm = ((localMax - yesterdayMax) + (yesterdayMax - beforeYesterdayMax)) / 2;
//        double cl = (localMax + yesterdayMax + beforeYesterdayMax) / 3;
//
//        //计算RL和UCL值
//        double rl = rm * 3.3;
//        double ucl = cl + rm * 3;
//
//        //如果本日最大值>UCL 且 本日最大值>昨日最大值，则告警
//        if (localMax > ucl && localMax > yesterdayMax){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsFlickerWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//
//        //如果本日>昨日>前日，且前日的值超过RL值，则告警
//        if (localMax > yesterdayMax && yesterdayMax > beforeYesterdayMax && beforeYesterdayMax > rl){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsFlickerWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//    }

//    /**
//     * 处理数据（电压暂降告警日统计）
//     * @author qijian
//     * @date 2022/12/29
//     */
//    private void processSagTimesAlarm(String date, Integer type, String lineId) {
//        //日期条件（取前天、昨天、今天）
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDate yesterday = localDate.plusDays(-1);
//        LocalDate beforeYesterday = yesterday.plusDays(-1);
//        int localMax = rMpEventDetailDMapper.getSagTimes(lineId, localDate);
//        int yesterdayMax = rMpEventDetailDMapper.getSagTimes(lineId, yesterday);
//        int beforeYesterdayMax = rMpEventDetailDMapper.getSagTimes(lineId, beforeYesterday);
//
//        //计算RM和CL值
//        double rm = ((localMax - yesterdayMax) + (yesterdayMax - beforeYesterdayMax)) / 2;
//        double cl = (localMax + yesterdayMax + beforeYesterdayMax) / 3;
//
//        //计算RL和UCL值
//        double rl = rm * 3.3;
//        double ucl = cl + rm * 3;
//
//        //如果本日最大值>UCL 且 本日最大值>昨日最大值，则告警
//        if (localMax > ucl && localMax > yesterdayMax){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsSagWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//
//        //如果本日>昨日>前日，且前日的值超过RL值，则告警
//        if (localMax > yesterdayMax && yesterdayMax > beforeYesterdayMax && beforeYesterdayMax > rl){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsSagWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//    }

//    /**
//     * 处理数据（电压短时中断告警日统计）
//     * @author qijian
//     * @date 2022/12/29
//     */
//    private void processInterruptTimesAlarm(String date, Integer type, String lineId) {
//        //日期条件（取前天、昨天、今天）
//        LocalDate localDate = LocalDate.parse(date);
//        LocalDate yesterday = localDate.plusDays(-1);
//        LocalDate beforeYesterday = yesterday.plusDays(-1);
//        int localMax = rMpEventDetailDMapper.getInterruptTimes(lineId, localDate);
//        int yesterdayMax = rMpEventDetailDMapper.getInterruptTimes(lineId, yesterday);
//        int beforeYesterdayMax = rMpEventDetailDMapper.getInterruptTimes(lineId, beforeYesterday);
//
//        //计算RM和CL值
//        double rm = ((localMax - yesterdayMax) + (yesterdayMax - beforeYesterdayMax)) / 2;
//        double cl = (localMax + yesterdayMax + beforeYesterdayMax) / 3;
//
//        //计算RL和UCL值
//        double rl = rm * 3.3;
//        double ucl = cl + rm * 3;
//
//        //如果本日最大值>UCL 且 本日最大值>昨日最大值，则告警
//        if (localMax > ucl && localMax > yesterdayMax){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsInterruptWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//
//        //如果本日>昨日>前日，且前日的值超过RL值，则告警
//        if (localMax > yesterdayMax && yesterdayMax > beforeYesterdayMax && beforeYesterdayMax > rl){
//            RMpTargetWarnDPO rMpTargetWarnD = new RMpTargetWarnDPO();
//            rMpTargetWarnD.setMeasurementPointId(lineId);
//            rMpTargetWarnD.setDataDate(DateUtil.parse(date));
//            rMpTargetWarnD.setIsInterruptWarn(1);
//            addAlarmData(rMpTargetWarnD);
//            return;
//        }
//    }

//    /**
//     * 存入数据库
//     * @author qijian
//     * @date 2022/12/28
//     */
//    private void addAlarmData(RMpTargetWarnDPO rMpTargetWarnD) {
//        LambdaQueryWrapper<RMpTargetWarnDPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(RMpTargetWarnDPO::getMeasurementPointId, rMpTargetWarnD.getMeasurementPointId()).eq(RMpTargetWarnDPO::getDataDate, rMpTargetWarnD.getDataDate());
//        RMpTargetWarnDPO rMpTargetWarnDOne = rMpTargetWarnDMapper.selectOne(lambdaQueryWrapper);
//        if (Objects.nonNull(rMpTargetWarnDOne)) {
//            rMpTargetWarnDMapper.update(rMpTargetWarnD, lambdaQueryWrapper);
//        } else {
//            rMpTargetWarnDMapper.insert(rMpTargetWarnD);
//        }
//    }
}
