wlzboy
6 天以前 09e6dc3fb7266620fafb5e341808a8eb36e080a1
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
package com.ruoyi.quartz.task;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.system.service.IVehicleMileageStatsService;
 
/**
 * 车辆里程统计定时任务
 */
@Component("vehicleMileageStatsTask")
public class VehicleMileageStatsTask {
    
    private static final Logger logger = LoggerFactory.getLogger(VehicleMileageStatsTask.class);
    
    @Autowired
    private IVehicleMileageStatsService vehicleMileageStatsService;
 
    /**
     * 计算昨日所有车辆的里程统计
     */
    public void calculateYesterdayMileage() {
        logger.info("开始执行车辆里程统计定时任务 - 统计昨日里程");
        
        try {
            // 获取昨天的日期
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date yesterday = calendar.getTime();
            
            // 批量计算里程统计
            int successCount = vehicleMileageStatsService.batchCalculateMileageStats(yesterday);
            
//            logger.info("车辆里程统计定时任务执行完成 - 成功统计: {} 辆车", successCount);
            
        } catch (Exception e) {
            logger.error("车辆里程统计定时任务执行失败", e);
            throw new RuntimeException("定时任务执行失败: " + e.getMessage());
        }
    }
 
    /**
     * 计算指定日期的里程统计(用于手动触发或补算历史数据)
     * 
     * @param dateStr 日期字符串,格式:yyyy-MM-dd
     */
    public void calculateMileageByDate(String dateStr) {
        logger.info("开始执行车辆里程统计定时任务 - 统计日期: {}", dateStr);
        
        try {
            if (dateStr == null || dateStr.trim().isEmpty()) {
                throw new IllegalArgumentException("日期不能为空");
            }
            
            // 使用SimpleDateFormat解析日期字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setLenient(false); // 严格解析日期
            Date targetDate = sdf.parse(dateStr.trim());
            
            // 批量计算里程统计
            int successCount = vehicleMileageStatsService.batchCalculateMileageStats(targetDate);
            
            logger.info("车辆里程统计定时任务执行完成 - 日期: {}, 成功统计: {} 辆车", dateStr, successCount);
            
        } catch (ParseException e) {
            logger.error("车辆里程统计定时任务执行失败 - 日期格式错误: {}", dateStr, e);
            throw new RuntimeException("定时任务执行失败: 日期格式错误,应为 yyyy-MM-dd");
        } catch (Exception e) {
            logger.error("车辆里程统计定时任务执行失败 - 日期: {}", dateStr, e);
            throw new RuntimeException("定时任务执行失败: " + e.getMessage());
        }
    }
 
    /**
     * 从GPS分段里程汇总生成昨日统计数据
     * (推荐使用此方法,基于已计算的分段里程数据汇总,性能更好)
     */
    public void aggregateYesterdayFromSegments() {
        logger.info("开始执行从分段里程汇总任务 - 统计昨日数据");
        
        try {
            // 获取昨天的日期
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date yesterday = calendar.getTime();
            
            // 从分段里程汇总生成统计
            int successCount = vehicleMileageStatsService.batchAggregateFromSegmentMileage(yesterday);
            
            logger.info("从分段里程汇总任务执行完成 - 成功统计: {} 辆车", successCount);
            
        } catch (Exception e) {
            logger.error("从分段里程汇总任务执行失败", e);
            throw new RuntimeException("汇总任务执行失败: " + e.getMessage());
        }
    }
 
    /**
     * 从GPS分段里程汇总生成指定日期的统计数据
     * 
     * @param dateStr 日期字符串,格式:yyyy-MM-dd
     */
    public void aggregateFromSegmentsByDate(String dateStr) {
        logger.info("开始执行从分段里程汇总任务 - 统计日期: {}", dateStr);
        
        try {
            if (dateStr == null || dateStr.trim().isEmpty()) {
                throw new IllegalArgumentException("日期不能为空");
            }
            
            // 使用SimpleDateFormat解析日期字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setLenient(false); // 严格解析日期
            Date targetDate = sdf.parse(dateStr.trim());
            
            // 从分段里程汇总生成统计
            int successCount = vehicleMileageStatsService.batchAggregateFromSegmentMileage(targetDate);
            
            logger.info("从分段里程汇总任务执行完成 - 日期: {}, 成功统计: {} 辆车", dateStr, successCount);
            
        } catch (ParseException e) {
            logger.error("从分段里程汇总任务执行失败 - 日期格式错误: {}", dateStr, e);
            throw new RuntimeException("汇总任务执行失败: 日期格式错误,应为 yyyy-MM-dd");
        } catch (Exception e) {
            logger.error("从分段里程汇总任务执行失败 - 日期: {}", dateStr, e);
            throw new RuntimeException("汇总任务执行失败: " + e.getMessage());
        }
    }
}