baoshiwei
2025-06-21 d9714be7130e14e063e6499637e1cc5241ff9dd3
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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);
        }
    }
 
    /**
     * 汇总上级电表数据
     */
    @Scheduled(cron = "0 * * * * ?")
    public void totalElectricityTask() {
        try {
            LocalDateTime now = LocalDateTime.now();
            log.info("开始执行全厂总电量统计任务,处理时间:{}", now);
            dataCleaningService.calculateTotalElectricity(now);
            log.info("全厂总电量统计任务执行完成");
        } catch (Exception e) {
            log.error("全厂总电量统计任务执行失败:{}", e.getMessage(), e);
        }
    }
    
    /**
     * 每分钟执行一次,统计当前小时、当天、当月和当年的用电量
     */
    @Scheduled(cron = "0 * * * * ?")
    public void periodicElectricityStatisticsTask() {
        try {
            LocalDateTime now = LocalDateTime.now();
            log.info("开始执行周期性用电量统计任务,处理时间:{}", now);
            
            // 统计当前小时用电量
            dataCleaningService.calculateHourlyElectricity(now);
            
            // 统计当天用电量
            dataCleaningService.calculateDailyElectricity(now);
            
            // 统计当月用电量
            dataCleaningService.calculateMonthlyElectricity(now);
            
            // 统计当年用电量
            dataCleaningService.calculateYearlyElectricity(now);
            
            log.info("周期性用电量统计任务执行完成");
        } catch (Exception e) {
            log.error("周期性用电量统计任务执行失败:{}", e.getMessage(), e);
        }
    }
}