package com.jiayue.ipfcst.fileupload.job;

import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
import com.jiayue.ipfcst.common.data.entity.ForecastPowerUltraShortTermOneHis;
import com.jiayue.ipfcst.common.data.job.BaseJob;
import com.jiayue.ipfcst.console.service.ForecastPowerUltraShortTermOneService;
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 MakeCdqOneUploadLineJob extends BaseJob {

  @Autowired
  private ForecastPowerUltraShortTermOneService forecastPowerUltraShortTermOneService;
  @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<ForecastPowerUltraShortTermOneHis> forecastPowerUltraShortTerm = forecastPowerUltraShortTermOneService.getForecastPowerUltraShortTerm(startTime, endTime);
    log.info("执行首页上报超短期数据生成定时任务结束，生成记录数："+forecastPowerUltraShortTerm.size());
    return true;
  }

}
