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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.harmonic.pojo.dto.RMpMonitorAlarmCountMDTO;
import com.njcn.harmonic.pojo.po.RMpMonitorAlarmCountMPO;
import com.njcn.prepare.harmonic.mapper.mysql.area.RMpMonitorAlarmCountMMapper;
import com.njcn.prepare.harmonic.mapper.mysql.area.RMpTargetWarnDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.area.IRMpMonitorAlarmCountMService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.BeanUtils;
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.stream.Collectors;

/**
 * <p>
 * 监测点告警统计-月表 服务实现类
 * </p>
 *
 * @author xuyang
 * @since 2023-05-09
 */
@Service
@AllArgsConstructor
@Slf4j
public class RMpMonitorAlarmCountMServiceImpl extends MppServiceImpl<RMpMonitorAlarmCountMMapper, RMpMonitorAlarmCountMPO> implements IRMpMonitorAlarmCountMService {

    private final RMpTargetWarnDMapper rMpTargetWarnDMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void alarmMonth(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>监测点指标告警月统计算法开始");
        List<String> lineIds = calculatedParam.getIdList();
        DateTime beginMonth = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime endMonth = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(lineIds,5);
        for (List<String> pendingId : pendingIds) {
            List<RMpMonitorAlarmCountMDTO> monthList = rMpTargetWarnDMapper.getDate(beginMonth,endMonth,pendingId);
            if (!CollectionUtils.isEmpty(monthList)){
                List<RMpMonitorAlarmCountMPO> result = new ArrayList<>();
                List<RMpMonitorAlarmCountMDTO> harmonicList = new ArrayList<>(),eventList = new ArrayList<>();
                //判断稳态指标评级结果-根据稳态指标严重度之和排序(去除严重度为0的数据在作比较)
                List<RMpMonitorAlarmCountMDTO> list1 = monthList.stream().filter(item->item.getHarmonicTypeGradeTotal()>0).collect(Collectors.toList());
                List<RMpMonitorAlarmCountMDTO> list2 = monthList.stream().filter(item->item.getHarmonicTypeGradeTotal()<=0).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(list1)){
                    for (int i = 0; i < list1.size(); i++) {
                        if (i < list1.size()*0.3){
                            list1.get(i).setHarmonicTypeGrade(0);
                        } else if (i >= list1.size()*0.3 && i < list1.size()*0.7){
                            list1.get(i).setHarmonicTypeGrade(1);
                        } else {
                            list1.get(i).setHarmonicTypeGrade(2);
                        }
                        harmonicList.add(list1.get(i));
                    }
                }
                if (!CollectionUtils.isEmpty(list2)){
                    list2.forEach(item->{
                        item.setHarmonicTypeGrade(2);
                        harmonicList.add(item);
                    });
                }
                //判断暂态指标评级结果-根据暂态指标严重度之和排序(去除严重度为0的数据在作比较)
                List<RMpMonitorAlarmCountMDTO> list3 = harmonicList.stream().filter(item->item.getEventTypeGradeTotal()>0).collect(Collectors.toList());
                List<RMpMonitorAlarmCountMDTO> list4 = harmonicList.stream().filter(item->item.getEventTypeGradeTotal()<=0).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(list3)){
                    for (int i = 0; i < list3.size(); i++) {
                        if (i < list3.size()*0.3){
                            list3.get(i).setEventTypeGrade(0);
                        } else if (i >= list3.size()*0.3 && i < list3.size()*0.7){
                            list3.get(i).setEventTypeGrade(1);
                        } else {
                            list3.get(i).setEventTypeGrade(2);
                        }
                        eventList.add(list3.get(i));
                    }
                }
                if (!CollectionUtils.isEmpty(list4)){
                    list4.forEach(item->{
                        item.setEventTypeGrade(2);
                        eventList.add(item);
                    });
                }
                if (!CollectionUtils.isEmpty(eventList)){
                    eventList.forEach(item->{
                        RMpMonitorAlarmCountMPO rMpMonitorAlarmCountMpo = new RMpMonitorAlarmCountMPO();
                        BeanUtils.copyProperties(item,rMpMonitorAlarmCountMpo);
                        result.add(rMpMonitorAlarmCountMpo);
                    });
                    this.saveOrUpdateBatchByMultiId(result);
                }
            }
        }
    }
}
