wlzboy
2025-11-29 364adbc9a93a396b74e154f910c2a0a72bfb1a0f
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.ruoyi.quartz.task;
 
import java.util.Calendar;
import java.util.Date;
import javax.annotation.PostConstruct;
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.IVehicleGpsSegmentMileageService;
import com.ruoyi.system.service.ISysConfigService;
 
/**
 * 车辆GPS分段里程计算定时任务
 */
@Component("vehicleGpsSegmentMileageTask")
public class VehicleGpsSegmentMileageTask {
    
    private static final Logger logger = LoggerFactory.getLogger(VehicleGpsSegmentMileageTask.class);
    
    @Autowired
    private IVehicleGpsSegmentMileageService segmentMileageService;
    
    @Autowired
    private ISysConfigService configService;
    
    /**
     * 服务启动时执行补偿计算
     * 检查最近7天内未被处理的GPS坐标并进行补偿计算
     */
    @PostConstruct
    public void init() {
        // 启动后延迟执行,避免影响服务启动速度
        new Thread(() -> {
            try {
                // 延迟30秒启动,确保所有服务已就绪
                Thread.sleep(30000);
                
                logger.info("========== 开始执行GPS分段里程补偿计算 ==========");
                
                // 获取配置的回溯天数,默认7天
                int lookbackDays = 7;
                String lookbackConfig = configService.selectConfigByKey("gps.mileage.compensation.days");
                if (lookbackConfig != null && !lookbackConfig.isEmpty()) {
                    try {
                        lookbackDays = Integer.parseInt(lookbackConfig);
                    } catch (NumberFormatException e) {
                        logger.warn("补偿回溯天数配置错误,使用默认值7天");
                    }
                }
                
                // 执行补偿计算
                int successCount = segmentMileageService.compensateCalculation(lookbackDays);
                
                logger.info("========== GPS分段里程补偿计算完成 - 成功处理 {} 辆车 ==========", successCount);
                
            } catch (InterruptedException e) {
                logger.error("补偿计算线程被中断", e);
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                logger.error("GPS分段里程补偿计算失败", e);
            }
        }, "GPS-Compensation-Thread").start();
    }
 
    /**
     * 计算最近一段时间的GPS分段里程
     * 默认计算最近1小时的数据
     */
    public void calculateRecentSegmentMileage() {
        calculateRecentSegmentMileage("60");
    }
 
    /**
     * 计算最近指定分钟数的GPS分段里程
     * 
     * @param params 参数字符串,格式:分钟数(如:60表示最近60分钟)
     */
    public void calculateRecentSegmentMileage(String params) {
        try {
            // 解析参数:要计算的时间范围(分钟)
            int minutes = 60; // 默认60分钟
            if (params != null && !params.trim().isEmpty()) {
                try {
                    minutes = Integer.parseInt(params.trim());
                } catch (NumberFormatException e) {
                    logger.warn("参数格式错误,使用默认值60分钟: {}", params);
                }
            }
            
            // 获取配置的时间间隔
            int segmentMinutes = 5; // 默认5分钟
            String segmentConfig = configService.selectConfigByKey("gps.mileage.segment.minutes");
            if (segmentConfig != null && !segmentConfig.isEmpty()) {
                try {
                    segmentMinutes = Integer.parseInt(segmentConfig);
                } catch (NumberFormatException e) {
                    logger.warn("分段时间间隔配置错误,使用默认值5分钟");
                }
            }
            
            // 计算时间范围(向前回溯指定分钟数)
            Calendar cal = Calendar.getInstance();
            Date endTime = cal.getTime();
            cal.add(Calendar.MINUTE, -minutes);
            Date startTime = cal.getTime();
            
            // 注意:此方法只计算最近时间段的数据,历史遗漏数据由补偿机制处理
            
            logger.info("开始计算GPS分段里程 - 时间范围: {} 到 {}, 时间段间隔: {}分钟", 
                       startTime, endTime, segmentMinutes);
            
            // 批量计算
            int successCount = segmentMileageService.batchCalculateSegmentMileage(startTime, endTime);
            
            logger.info("GPS分段里程计算完成 - 成功处理 {} 辆车", successCount);
            
        } catch (Exception e) {
            logger.error("GPS分段里程计算任务执行失败", e);
        }
    }
 
    /**
     * 计算指定日期的GPS分段里程
     * 
     * @param params 参数字符串,格式:yyyy-MM-dd(如:2025-01-15)
     */
    public void calculateDateSegmentMileage(String params) {
        try {
            if (params == null || params.trim().isEmpty()) {
                logger.error("日期参数不能为空");
                return;
            }
            
            // 解析日期
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(params.trim());
            
            // 计算当天的起止时间
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date startTime = cal.getTime();
            
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endTime = cal.getTime();
            
            logger.info("开始计算指定日期GPS分段里程 - 日期: {}, 时间范围: {} 到 {}", 
                       params, startTime, endTime);
            
            // 批量计算
            int successCount = segmentMileageService.batchCalculateSegmentMileage(startTime, endTime);
            
            logger.info("指定日期GPS分段里程计算完成 - 成功处理 {} 辆车", successCount);
            
        } catch (Exception e) {
            logger.error("指定日期GPS分段里程计算任务执行失败 - 参数: {}", params, e);
        }
    }
}