package com.zhitan.engine.scheduler;
|
|
import com.zhitan.engine.service.DataCleaningService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.time.LocalDateTime;
|
|
/**
|
* 数据清洗定时任务调度器
|
* 负责定期执行数据清洗和统计任务
|
*/
|
@Slf4j
|
@Component
|
public class DataCleaningScheduler {
|
|
@Autowired
|
private DataCleaningService dataCleaningService;
|
|
/**
|
* 每小时执行一次,按小时统计用电量
|
* 每小时的第5分钟执行,处理上一个小时的数据
|
*/
|
@Scheduled(cron = "0 5 * * * ?")
|
public void hourlyElectricityTask() {
|
try {
|
LocalDateTime lastHour = LocalDateTime.now().minusHours(1);
|
log.info("开始执行按小时统计用电量任务,处理时间:{}", lastHour);
|
dataCleaningService.calculateHourlyElectricity(lastHour);
|
log.info("按小时统计用电量任务执行完成");
|
} catch (Exception e) {
|
log.error("按小时统计用电量任务执行失败:{}", e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 每天执行一次,按天统计用电量
|
* 每天凌晨1:10执行,处理前一天的数据
|
*/
|
@Scheduled(cron = "0 10 1 * * ?")
|
public void dailyElectricityTask() {
|
try {
|
LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
|
log.info("开始执行按天统计用电量任务,处理时间:{}", yesterday);
|
dataCleaningService.calculateDailyElectricity(yesterday);
|
log.info("按天统计用电量任务执行完成");
|
} catch (Exception e) {
|
log.error("按天统计用电量任务执行失败:{}", e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 每月执行一次,按月统计用电量
|
* 每月1日凌晨2:10执行,处理上个月的数据
|
*/
|
@Scheduled(cron = "0 10 2 1 * ?")
|
public void monthlyElectricityTask() {
|
try {
|
LocalDateTime lastMonth = LocalDateTime.now().minusMonths(1);
|
log.info("开始执行按月统计用电量任务,处理时间:{}", lastMonth);
|
dataCleaningService.calculateMonthlyElectricity(lastMonth);
|
log.info("按月统计用电量任务执行完成");
|
} catch (Exception e) {
|
log.error("按月统计用电量任务执行失败:{}", e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 每年执行一次,按年统计用电量
|
* 每年1月1日凌晨3:10执行,处理上一年的数据
|
*/
|
@Scheduled(cron = "0 10 3 1 1 ?")
|
public void yearlyElectricityTask() {
|
try {
|
LocalDateTime lastYear = LocalDateTime.now().minusYears(1);
|
log.info("开始执行按年统计用电量任务,处理时间:{}", lastYear);
|
dataCleaningService.calculateYearlyElectricity(lastYear);
|
log.info("按年统计用电量任务执行完成");
|
} catch (Exception e) {
|
log.error("按年统计用电量任务执行失败:{}", e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 手动触发任务,用于测试或补充历史数据
|
*
|
* @param timeType 时间类型:HOUR, DAY, MONTH, YEAR
|
* @param dateTime 统计时间点
|
*/
|
public void manualTrigger(String timeType, LocalDateTime dateTime) {
|
try {
|
log.info("手动触发{}统计任务,处理时间:{}", timeType, dateTime);
|
dataCleaningService.calculateElectricityByTimeType(timeType, dateTime);
|
log.info("手动触发{}统计任务执行完成", timeType);
|
} catch (Exception e) {
|
log.error("手动触发{}统计任务执行失败:{}", timeType, e.getMessage(), e);
|
}
|
}
|
}
|