baoshiwei
2025-04-23 c2375c2bcc0bf9e6a3af7f9776d5a0eb14370b40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
        }
    }
}