liujie
2026-03-15 d943874d94e69806cb53b649b378075794bdf683
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.xiaopeng.maintenance.schedule;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xiaopeng.api.feignClient.account.XpOperationUserNotificationClient;
import com.xiaopeng.api.model.account.XpOperationsUser;
import com.xiaopeng.api.model.account.XpOperationsUserNotification;
import com.xiaopeng.api.model.maintenance.XpTask;
import com.xiaopeng.api.model.maintenance.XpTaskRule;
import com.xiaopeng.api.model.vehicleConvoy.XpVehicle;
import com.xiaopeng.api.vo.maintenance.TaskScheduleConstants;
import com.xiaopeng.common.core.enums.NotificationTypeEnum;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.maintenance.mapper.XpTaskMapper;
import com.xiaopeng.maintenance.mapper.XpTaskRuleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
 
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * 事故任务即将超时检查定时任务
 *
 * <p>每分钟执行一次,判断事故任务在即将超时窗口内并发送通知。</p>
 *
 * @author mitao
 * @since 2026-03-14
 */
@Deprecated
public class AccidentTaskWillTimeoutScheduleTask {
 
    private static final Logger log = LoggerFactory.getLogger(AccidentTaskWillTimeoutScheduleTask.class);
 
    private static final String TASK_NAME = "事故任务即将超时检查定时任务";
 
    private static final String PUSH_TITLE = "事故即将超时提示";
 
    private static final String CONTENT_TEMPLATE =
            "车辆%s在%s发生的紧急事故即将超时,剩余时间%s分钟,请及时到达现场!";
 
    @Autowired
    private XpTaskMapper taskMapper;
 
    @Autowired
    private XpTaskRuleMapper taskRuleMapper;
 
    @Autowired
    private XpOperationUserNotificationClient xpOperationUserNotificationClient;
 
    @Autowired
    private AccidentTaskNotificationSupport notificationSupport;
 
    /**
     * 每分钟执行一次
     */
    @Scheduled(cron = "0 * * * * ?")
    public void executeAccidentTaskWillTimeoutCheck() {
        log.info("========== 开始执行{} ==========", TASK_NAME);
 
        try {
            XpTaskRule taskRule = getTaskRule();
            Integer processingTimeLimit = taskRule == null ? null : taskRule.getProcessingTimeLimit();
            Integer factorMinutes = calcWillTimeoutFactorMinutes(processingTimeLimit);
            if (factorMinutes == null || factorMinutes <= 0) {
                log.info("未配置有效处理时限或即将超时因子<=0,跳过执行,processingTimeLimit={}", processingTimeLimit);
                return;
            }
 
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime windowEnd = now.plusMinutes(factorMinutes);
 
            List<XpTask> tasks = queryWillTimeoutAccidentTasks(now, windowEnd);
            if (CollectionUtils.isEmpty(tasks)) {
                log.info("未找到即将超时事故任务,跳过执行");
                return;
            }
 
            Set<Long> vehicleIds = tasks.stream()
                    .map(XpTask::getVehicleId)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toSet());
            Map<Long, XpVehicle> vehicleMap = notificationSupport.fetchVehicles(vehicleIds);
 
            Set<Long> allUserIds = new HashSet<>();
            for (XpTask task : tasks) {
                allUserIds.addAll(notificationSupport.parseUserIds(task.getHandleUserId()));
            }
            Map<Long, XpOperationsUser> userMap = notificationSupport.fetchOperationUsers(allUserIds);
 
            int notifiedTaskCount = 0;
            int notificationCount = 0;
            int pushCount = 0;
            List<XpOperationsUserNotification> allNotifications = new ArrayList<>();
 
            for (XpTask task : tasks) {
                try {
                    if (task == null || task.getId() == null) {
                        continue;
                    }
                    Set<Long> userIds = notificationSupport.parseUserIds(task.getHandleUserId());
                    if (CollectionUtils.isEmpty(userIds)) {
                        log.warn("事故任务ID={} 未配置有效处理人,跳过通知", task.getId());
                        continue;
                    }
 
                    int affectedRows = markWillTimeoutNoticeSent(task.getId(), now, windowEnd);
                    if (affectedRows <= 0) {
                        continue;
                    }
 
                    List<XpOperationsUser> users = userIds.stream()
                            .map(userMap::get)
                            .filter(Objects::nonNull)
                            .collect(Collectors.toList());
                    if (CollectionUtils.isEmpty(users)) {
                        log.warn("事故任务ID={} 未匹配到有效运维人员,跳过通知", task.getId());
                        continue;
                    }
 
                    String plateNumber = resolvePlateNumber(vehicleMap.get(task.getVehicleId()));
                    String vehicleLocation = StringUtils.defaultString(task.getVehicleLocation());
                    String remainMinutes = String.valueOf(calcRemainMinutes(now, task.getEndTime()));
                    String content = String.format(CONTENT_TEMPLATE, plateNumber, vehicleLocation, remainMinutes);
 
                    String bodyJson = notificationSupport.buildPushBodyJson(
                            content,
                            NotificationTypeEnum.ACCIDENT_WILL_TIMEOUT.getCode(),
                            task.getId()
                    );
 
                    allNotifications.addAll(notificationSupport.buildNotifications(
                            users,
                            NotificationTypeEnum.ACCIDENT_WILL_TIMEOUT.getCode(),
                            content,
                            task.getId(),
                            bodyJson
                    ));
 
                    pushCount += notificationSupport.pushToUsersByDeviceType(users, PUSH_TITLE, bodyJson);
                    notifiedTaskCount++;
                } catch (Exception e) {
                    log.error("处理即将超时事故任务通知失败,任务ID={}", task == null ? null : task.getId(), e);
                }
            }
 
            if (!CollectionUtils.isEmpty(allNotifications)) {
                xpOperationUserNotificationClient.addNotificationBatch(allNotifications);
                notificationCount = allNotifications.size();
            }
 
            log.info("========== {} 执行完成,任务数: {},通知记录数: {},推送次数: {} ==========",
                    TASK_NAME, notifiedTaskCount, notificationCount, pushCount);
        } catch (Exception e) {
            log.error("{}执行失败", TASK_NAME, e);
        }
    }
 
    /**
     * 获取任务规则配置
     */
    private XpTaskRule getTaskRule() {
        return taskRuleMapper.selectOne(new LambdaQueryWrapper<XpTaskRule>().last("limit 1"));
    }
 
    /**
     * 计算即将超时窗口(分钟):floor(processingTimeLimit * 0.2)
     */
    private Integer calcWillTimeoutFactorMinutes(Integer processingTimeLimit) {
        if (processingTimeLimit == null || processingTimeLimit <= 0) {
            return 0;
        }
        return (int) Math.floor(processingTimeLimit * 0.2d);
    }
 
    /**
     * 查询即将超时事故任务:now < endTime <= windowEnd 且未发送即将超时通知
     */
    private List<XpTask> queryWillTimeoutAccidentTasks(LocalDateTime now, LocalDateTime windowEnd) {
        return taskMapper.selectList(new LambdaQueryWrapper<XpTask>()
                .eq(XpTask::getTaskCategory, TaskScheduleConstants.TASK_CATEGORY_ACCIDENT)
                .in(XpTask::getStatus,
                        TaskScheduleConstants.TASK_STATUS_PENDING_ARRIVE,
                        TaskScheduleConstants.TASK_STATUS_PENDING)
                .isNotNull(XpTask::getEndTime)
                .gt(XpTask::getEndTime, now)
                .le(XpTask::getEndTime, windowEnd)
                .eq(XpTask::getWillTimeoutNoticeSent, 0));
    }
 
    /**
     * 原子更新(抢占式)标记即将超时通知已发送
     */
    private int markWillTimeoutNoticeSent(Long taskId, LocalDateTime now, LocalDateTime windowEnd) {
        return taskMapper.update(null, new LambdaUpdateWrapper<XpTask>()
                .set(XpTask::getWillTimeoutNoticeSent, 1)
                .eq(XpTask::getId, taskId)
                .eq(XpTask::getTaskCategory, TaskScheduleConstants.TASK_CATEGORY_ACCIDENT)
                .in(XpTask::getStatus,
                        TaskScheduleConstants.TASK_STATUS_PENDING_ARRIVE,
                        TaskScheduleConstants.TASK_STATUS_PENDING)
                .isNotNull(XpTask::getEndTime)
                .gt(XpTask::getEndTime, now)
                .le(XpTask::getEndTime, windowEnd)
                .eq(XpTask::getWillTimeoutNoticeSent, 0));
    }
 
    /**
     * 计算剩余分钟数(向上取整,避免出现 0 分钟)
     */
    private long calcRemainMinutes(LocalDateTime now, LocalDateTime endTime) {
        if (endTime == null) {
            return 0;
        }
        long seconds = Duration.between(now, endTime).getSeconds();
        if (seconds <= 0) {
            return 0;
        }
        return (seconds + 59) / 60;
    }
 
    private String resolvePlateNumber(XpVehicle vehicle) {
        return vehicle == null ? "" : StringUtils.defaultString(vehicle.getPlateNumber());
    }
}