package com.jiayue.passback.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.passback.entity.ForecastPowerUltraShortTermHis;
import com.jiayue.passback.mapper.ForecastPowerUltraShortTermHisMapper;
import com.jiayue.passback.service.ForecastPowerUltraShortTermHisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ForecastPowerUltraShortTermHisServiceImpl extends ServiceImpl<ForecastPowerUltraShortTermHisMapper, ForecastPowerUltraShortTermHis> implements ForecastPowerUltraShortTermHisService {
    @Value("${cdqUpMin}")
    private int cdqUpMin;

    @Override
    public Map<String, Object> GenerateData(Date date) {
        Map<String, Object> map = new HashMap<>();

        Date startTime = new Date(date.getTime() - cdqUpMin * 15 * 60 * 1000);

        Date endTime = new Date(startTime.getTime() + 15 * 60 * 1000 - 1);

        List<ForecastPowerUltraShortTermHis> forecastPowerUltraShortTermHisList = list(lambdaQuery().between(ForecastPowerUltraShortTermHis::getGenDate, startTime, endTime).getWrapper());

        List<ForecastPowerUltraShortTermHis> list = forecastPowerUltraShortTermHisList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ForecastPowerUltraShortTermHis::getForecastTime))), ArrayList::new
        ));
        list.sort((a, b) -> {
            return (int) (a.getForecastTime() - b.getForecastTime());
        });

        map.put(ForecastPowerUltraShortTermHis.class.getSimpleName(), list);
        log.info("获取超短期记录数{}", list.size());
        return map;
    }
}
