package com.gyee.alarm.service.auto.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.alarm.mapper.auto.ProEconEarlyReportMapper;
import com.gyee.alarm.model.auto.ProBasicFeatureStat;
import com.gyee.alarm.model.auto.ProEconEarlyReport;
import com.gyee.alarm.service.auto.IProEconEarlyReportService;
import com.gyee.alarm.util.StringUtils;
import com.gyee.alarm.util.realtimesource.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 预警报告 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2023-09-05
 */
@Service
public class ProEconEarlyReportServiceImpl extends ServiceImpl<ProEconEarlyReportMapper, ProEconEarlyReport> implements IProEconEarlyReportService {


    @Resource
    private ProEconEarlyReportMapper proEconEarlyReportMapper;

    public List<ProEconEarlyReport> findEarlyReportByDate(String stationId, Date startTime,Date endTime)
    {
        List<ProEconEarlyReport> ls=new ArrayList<>();
        if(StringUtils.notEmp(stationId) && StringUtils.notEmp(startTime) && StringUtils.notEmp(endTime))
        {
            ls= proEconEarlyReportMapper.findEarlyReportByDate(stationId,  startTime, endTime);
        }
        return ls;
    }


    public ProBasicFeatureStat getAvgAlarmCount(List<ProBasicFeatureStat> alarmCountList) {
        OptionalDouble avgCount = alarmCountList.stream().mapToLong(ProBasicFeatureStat::getTotal).average();
        OptionalDouble avgTime = alarmCountList.stream().mapToDouble(ProBasicFeatureStat::getTotalSeconds).average();
        ProBasicFeatureStat alarmCount = new ProBasicFeatureStat();
        alarmCount.setTotal(avgCount.isPresent() ? (long) avgCount.getAsDouble() : 0);
        alarmCount.setTotalSeconds(avgTime.isPresent() ? avgTime.getAsDouble() : 0);
        return alarmCount;

    }

    /**
     * @param lst alarmcount 集合
     * @return
     * @return java.util.Map<java.lang.String, com.gyee.wisdom.alarm.schedule.entity.ProBasicFeatureStat>
     * @author Wanghs
     * @description 将每一台风机的所有规则报警次数时长求和并根据总持续时间排序，取出最大的几条数据
     * @date 2022/8/23
     **/
    public Map<String, ProBasicFeatureStat> getSumAlarmCountByWindturbine(List<ProBasicFeatureStat> lst, int limit) {

        //根据风机编号分组，并次数求和
        Map<String, Long> mapCount = lst.stream().filter(s -> StringUtil.isNotBlank(s.getWtId())).collect(Collectors.groupingBy(ProBasicFeatureStat::getWtId, Collectors.summingLong(ProBasicFeatureStat::getTotal)));
        //根据风机编号分组，并时长求和
        Map<String, Double> mapTime = lst.stream().filter(s -> StringUtil.isNotBlank(s.getWtId())).collect(Collectors.groupingBy(ProBasicFeatureStat::getWtId, Collectors.summingDouble(ProBasicFeatureStat::getTotalSeconds)));

        List<ProBasicFeatureStat> alarmCountList = new ArrayList<>();
        for (Map.Entry<String, Double> entry :
                mapTime.entrySet()) {
            ProBasicFeatureStat data = new ProBasicFeatureStat();
            data.setWtId(entry.getKey());
            data.setTotalSeconds(entry.getValue());
            if (mapCount.containsKey(entry.getKey())) {
                data.setTotal(mapCount.get(entry.getKey()));
            }
            alarmCountList.add(data);
        }
        Map<String, ProBasicFeatureStat> collect = alarmCountList.stream().
                sorted(Comparator.comparing(ProBasicFeatureStat::getTotalSeconds, Comparator.reverseOrder())).
                limit(limit).collect(Collectors.toMap(ProBasicFeatureStat::getWtId, s -> s, (key1, key2) -> key2));
        return collect;

    }

    public List<ProEconEarlyReport> findEarlyReportById( String id)
    {
        List<ProEconEarlyReport> ls=new ArrayList<>();
        if(StringUtils.notEmp(id) )
        {
            ls= proEconEarlyReportMapper.findEarlyReportById(id);
        }
        return ls;
    }

    public ProEconEarlyReport findLastReport()
    {
        ProEconEarlyReport vo=proEconEarlyReportMapper.findLastReport();
        return vo;

    }
}
