package com.jiayue.ssi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ssi.entity.ElectricField;
import com.jiayue.ssi.entity.ForecastPowerShortTerm;
import com.jiayue.ssi.mapper.ForecastPowerShortTermMapper;
import com.jiayue.ssi.service.ElectricFieldService;
import com.jiayue.ssi.service.ForecastPowerShortTermService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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


@Service
@Slf4j
public class ForecastPowerShortTermServiceImpl extends ServiceImpl<ForecastPowerShortTermMapper, ForecastPowerShortTerm> implements ForecastPowerShortTermService {
  @Autowired
  ForecastPowerShortTermMapper forecastPowerShortTermMapper;
  @Autowired
  ElectricFieldService electricFieldService;

  /**
   * 按时间段删除数据
   *
   * @param startTime
   * @param endTime
   * @param listDq
   */
  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public void deleteBetweenAndGenTime(Long startTime, Long endTime, List<ForecastPowerShortTerm> listDq) {
    QueryWrapper wrapper = new QueryWrapper<>();
    wrapper.ge("forecast_time",startTime);
    wrapper.le("forecast_time",endTime);
    this.remove(wrapper);
    //保存短期数据
    this.saveBatch(listDq);
  }
  /**
   * 按时间查询实时预测短期
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 结果集
   */
  @Override
  public Map<String, Object> findByForecastTimeBetween(Long startTime, Long endTime) {
    Map<String, Object> map = new HashMap<>();
    ElectricField electricField = electricFieldService.getOne(null);
    map.put("cap",electricField.getCapacity());
    List<ForecastPowerShortTerm> list = new ArrayList<>();
    list = forecastPowerShortTermMapper.findByForecastTimeBetween(startTime, endTime);
    list.sort(Comparator.comparing(ForecastPowerShortTerm::getForecastTime));

    long startTimeLong = startTime;
    long endTimeLong = endTime;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    long timeStep = 900000L;
    if (startTimeLong % timeStep != 0) {
      startTimeLong = startTimeLong - (startTimeLong % timeStep)+timeStep;
    }
    List<Float> datas = new ArrayList<>();
    List<String> times = new ArrayList<>();

    for (long i = startTimeLong; i < endTimeLong; i = i + timeStep) {
      long finalI = i;
      List<ForecastPowerShortTerm> p = list.stream().filter(t -> t.getForecastTime() == finalI).collect(Collectors.toList());
      if (p != null && p.size() > 0) {
        datas.add(p.get(0).getFpValue().floatValue());
      } else {
        datas.add(null);
      }
      String timeFormat = sdf.format(new Date(i));
      times.add(timeFormat);
    }
    map.put("times", times);
    map.put("datas", datas);
    return map;
  }
}
