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

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
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 RStatDataVDMapper rStatDataVDMapper;

    @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,5);
        for (List<String> pendingId : pendingIds) {
            List<RMpTargetDiffDPO> intersectionList = diffData(nowDay,yesterday,pendingId);
            if (!CollectionUtils.isEmpty(intersectionList)){
                this.saveOrUpdateBatchByMultiId(intersectionList);
            }
        }
    }

    /**
     * 生成指标差值日统计
     */
    private 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.getYesterday(time);
        //计算本年的rm值
        return this.baseMapper.getAvgDiffData(endTime,time,list);
    }

}
