wlzboy
2025-11-15 caf56217dc2bf898b63b0e1f31a7098202c32825
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
package com.ruoyi.quartz.task;
 
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 {
            // 解析日期字符串
            String[] parts = dateStr.split("-");
            if (parts.length != 3) {
                throw new IllegalArgumentException("日期格式错误,应为: yyyy-MM-dd");
            }
            
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, Integer.parseInt(parts[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(parts[1]) - 1); // 月份从0开始
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parts[2]));
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date targetDate = calendar.getTime();
            
            // 批量计算里程统计
            int successCount = vehicleMileageStatsService.batchCalculateMileageStats(targetDate);
            
            logger.info("车辆里程统计定时任务执行完成 - 日期: {}, 成功统计: {} 辆车", dateStr, successCount);
            
        } catch (Exception e) {
            logger.error("车辆里程统计定时任务执行失败 - 日期: {}", dateStr, e);
            throw new RuntimeException("定时任务执行失败: " + e.getMessage());
        }
    }
}