package com.jiayue.ipfcst.fileupload.job;

import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
import com.jiayue.ipfcst.common.data.entity.ForecastPowerUltraShortTermHis;
import com.jiayue.ipfcst.common.data.job.BaseJob;
import com.jiayue.ipfcst.console.service.ForecastPowerUltraShortTermService;
import com.jiayue.ipfcst.console.service.SysParameterService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Slf4j
public class MakeCdqUploadLineJob extends BaseJob {

  @Autowired
  private ForecastPowerUltraShortTermService forecastPowerUltraShortTermService;
  @Autowired
  private SysParameterService sysParameterService;

  @Override
  public boolean execute(JobDataMap jobDataMap) {
    log.info("开始执行首页上报超短期数据生成定时任务");
    // 获取当前系统时间
    Date systemDate = new Date();
    // 超短期默认天数
    int cdqPoint = sysParameterService.getTranSysParameter("FILE_CDQ_POINT", "16");
    // 超短期提前N分钟生成文件
    int cdqUpMin = sysParameterService.getTranSysParameter("CDQ_UP_MIN", "0");


    Long startTime;

    try {
      startTime = DateMomentUtil.getMomentTime(systemDate.getTime() + 15 * 60 * 1000 + cdqUpMin * 1000 * 60, 1, 15 * 60 * 1000L);
    } catch (Exception e) {
      log.error("数据超短期文件生成时刻步长错误", e);
      startTime = DateMomentUtil.getDayStartTime(systemDate.getTime());
    }
    Long endTime = startTime + cdqPoint * 15 * 60 * 1000 - 1000;

    List<ForecastPowerUltraShortTermHis> forecastPowerUltraShortTerm = forecastPowerUltraShortTermService.getForecastPowerUltraShortTerm(startTime, endTime);
    log.info("执行首页上报超短期数据生成定时任务结束，生成记录数："+forecastPowerUltraShortTerm.size());
    return true;
  }

}
