package com.shcd.scheduling;

import com.shcd.entity.ShorePowerDeviceUsage;
import com.shcd.enums.AreaSortEnum;
import com.shcd.service.ShorePowerDeviceUsageService;
import com.shcd.service.SpStatementInlandLowService;
import com.shcd.service.SpStatementQuarterReportService;
import com.shcd.service.SpStatementShipUsageMonthService;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ShorePowerReportGenerateScheduler {

  @Resource
  SpStatementShipUsageMonthService spStatementShipUsageMonthService;

  @Resource
  SpStatementInlandLowService spStatementInlandLowService;

  @Autowired
  private SpStatementQuarterReportService spStatementQuarterReportService;

  @Resource
  private ShorePowerDeviceUsageService shorePowerDeviceUsageService;

  //  @PostConstruct
  @Scheduled(cron = "0 0 0 25 * ?")
  public void generateSpInlandLowStatement() {
    try {
      // 生成各区上报数据   内河低压分析表
      log.warn("开始生成 内河低压分析表-各区上报数据  ========");
      generateReport1();
      log.warn("内河低压分析表-各区上报数据  ======== 生成结束");
      log.warn("生成下个月的内河非标岸电使用情况表  ======");
      generateDeviceUsage();
      log.warn("生成下个月的内河非标岸电使用情况表  ====== 生成结束");
////       生成市级平台自动上传数据   内河低压分析表
      log.warn("开始生成 内河低压分析表-市级平台自动上传数据  ========");
      generateReport2();
      log.warn("内河低压分析表-市级平台自动上传数据  ========生成结束");
      //生成上海市长江经济带船舶使用岸电情况报表
      log.warn(
          "开始生成 上海市长江经济带船舶使用岸电情况报表   以及  上海港船舶使用岸电情况分年度对比分析表  以及  靠泊上港集团码头的集装箱船舶(2024年上海市岸电使用指标Part2) ========");
      generateReport3();
      log.warn("上海市长江经济带船舶使用岸电情况报表 ========生成结束");
      log.warn("开始生成  岸电季度报表   ================");
      generateSpQuarterReport();
      log.warn("岸电季度报表 ========生成结束");
    } catch (Exception e) {
      log.error("中间表生成逻辑出错....原因:{}", e.getMessage());
    }
  }

  private void generateDeviceUsage() throws ParseException {
    try {
      shorePowerDeviceUsageService.generateDeviceUsageNextMonth(getShorePowerNextMonthDate());
    } catch (Exception e) {
      log.error("生成下个月的内河非标岸电使用情况表出错....原因:{}", e.getMessage());
    }

  }

  //@PostConstruct
  public void generateSpQuarterReport() {
    try {
      spStatementQuarterReportService.generateReportSpQuarterReport(getShorePowerDate());
    } catch (Exception e) {
      log.error("季度表出错....原因:{}", e.getMessage());
    }
  }

  private void generateReport3() {
    try {
      spStatementShipUsageMonthService.makeShangHaiShorePowerUsageRecord(getShorePowerDate());
    } catch (Exception e) {
      log.error("上海市长江经济带船舶使用岸电情况报表出错....原因:{}", e.getMessage());
    }
  }

  private void generateReport1() {
    try {
      spStatementInlandLowService.makeReport1(getShorePowerDate(), getShorePowerLastMonthDate());
    } catch (Exception e) {
      log.error("内河低压分析表-市级平台自动上传数据出错....原因:{}", e.getMessage());
    }
  }

  private void generateReport2() {
    try {
      spStatementInlandLowService.makeReport2(getShorePowerDate(), getShorePowerLastMonthDate(),
          getShorePowerLast2MonthDate(), getShorePowerLast3MonthDate());
    } catch (Exception e) {
      log.error("内河低压分析表-各区上报出错....原因:{}", e.getMessage());
    }
  }

  /**
   * 获取当前年月
   * @return
   */
  private static String getShorePowerDate() {
    String date = null;
    LocalDate now = LocalDate.now();
    date = DateTimeFormatter.ofPattern("yyyy-MM").format(now);
    return date;
  }

  /**
   * 获取下个月年月
   * @return
   */
  private static String getShorePowerNextMonthDate() {
    String date = null;
    date = DateTimeFormatter.ofPattern("yyyy-MM").format(LocalDate.now().plusMonths(1));
    return date;
  }

  /**
   * 获取上个月年月
   * @return
   */
  private static String getShorePowerLastMonthDate() {
    String date = null;
    date = DateTimeFormatter.ofPattern("yyyy-MM").format(LocalDate.now().plusMonths(-1));
    return date;
  }

  /**
   * 获取上上个月年月
   * @return
   */
  private static String getShorePowerLast2MonthDate() {
    String date = null;
    date = DateTimeFormatter.ofPattern("yyyy-MM").format(LocalDate.now().plusMonths(-2));
    return date;
  }

  /**
   * 获取上上上个月年月
   * @return
   */
  private static String getShorePowerLast3MonthDate() {
    String date = null;
    date = DateTimeFormatter.ofPattern("yyyy-MM").format(LocalDate.now().plusMonths(-3));
    return date;
  }
}
