package com.jiayue.passback.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.passback.entity.ForecastPowerShortTermHis;
import com.jiayue.passback.mapper.ForecastPowerShortTermHisMapper;
import com.jiayue.passback.service.ForecastPowerShortTermHisService;
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 ForecastPowerShortTermHisServiceImpl extends ServiceImpl<ForecastPowerShortTermHisMapper, ForecastPowerShortTermHis> implements ForecastPowerShortTermHisService {

    @Value("${dqType}")
    private String dqType;

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

        Date startTime = new Date(date.getTime() - 15 * 60 * 1000);
        //获取短期数据 (只获取配置文件中配置的短期类型)
        List<ForecastPowerShortTermHis> forecastPowerShortTermHisList = list(
                lambdaQuery().between(ForecastPowerShortTermHis::getGenDate, startTime,endTime)
                                .eq(ForecastPowerShortTermHis::getDataType,dqType)
                                        .getWrapper()
        );

        List<ForecastPowerShortTermHis> list = forecastPowerShortTermHisList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ForecastPowerShortTermHis::getForecastTime))), ArrayList::new
        ));

        list.sort((a,b)->{
            return (int) (a.getForecastTime() - b.getForecastTime());
        });
        map.put(ForecastPowerShortTermHis.class.getSimpleName(), list);

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