liujie
2026-09-14 82a8bc62d5b9d5d607f6952329952b17eda2f0a0
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
package com.tourguide.modules.order.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourguide.config.properties.UniPushProperties;
import com.tourguide.modules.operation.entity.OperationScenicSpot;
import com.tourguide.modules.operation.mapper.OperationScenicSpotMapper;
import com.tourguide.modules.order.domain.OrderStatus;
import com.tourguide.modules.order.domain.RefundStatus;
import com.tourguide.modules.order.entity.TourOrder;
import com.tourguide.modules.order.mapper.TourOrderMapper;
import com.tourguide.modules.system.service.GuideNotificationService;
import com.tourguide.modules.user.entity.GuideReceivingSession;
import com.tourguide.modules.user.mapper.GuideReceivingSessionMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.List;
 
@Service
public class OrderLifecycleScheduler {
    private static final int BATCH_SIZE = 100;
    private final TourOrderMapper orderMapper;
    private final OperationScenicSpotMapper scenicMapper;
    private final GuideOrderMatchingService matchingService;
    private final GuideReceivingSessionMapper sessionMapper;
    private final GuideSessionTransitionService sessionTransitionService;
    private final GuideNotificationService notificationService;
    private final UniPushProperties uniPushProperties;
 
    public OrderLifecycleScheduler(TourOrderMapper orderMapper, OperationScenicSpotMapper scenicMapper,
                                   GuideOrderMatchingService matchingService,
                                   GuideReceivingSessionMapper sessionMapper,
                                   GuideSessionTransitionService sessionTransitionService,
                                   GuideNotificationService notificationService,
                                   UniPushProperties uniPushProperties) {
        this.orderMapper = orderMapper;
        this.scenicMapper = scenicMapper;
        this.matchingService = matchingService;
        this.sessionMapper = sessionMapper;
        this.sessionTransitionService = sessionTransitionService;
        this.notificationService = notificationService;
        this.uniPushProperties = uniPushProperties;
    }
 
    @Scheduled(fixedDelayString = "${tourguide.order.lifecycle-scan-delay-millis:10000}")
    public void process() {
        cancelExpiredUnpaidOrders();
        retryPendingOrders();
        sendServiceReminders();
        startDueGroupSessions();
        completeExpiredServiceSessions();
    }
 
    void cancelExpiredUnpaidOrders() {
        List<TourOrder> orders = orderMapper.selectList(new LambdaQueryWrapper<TourOrder>()
                .eq(TourOrder::getStatus, OrderStatus.PENDING_PAYMENT.getCode())
                .isNotNull(TourOrder::getScenicSpotId).orderByAsc(TourOrder::getPlacedAt).last("LIMIT " + BATCH_SIZE));
        LocalDateTime now = LocalDateTime.now();
        for (TourOrder order : orders) {
            OperationScenicSpot scenic = scenicMapper.selectById(order.getScenicSpotId());
            int minutes = scenic == null || scenic.getUnpaidCancelMinutes() == null || scenic.getUnpaidCancelMinutes() < 1
                    ? 15 : scenic.getUnpaidCancelMinutes();
            LocalDateTime baseTime = order.getPlacedAt() == null ? order.getCreatedAt() : order.getPlacedAt();
            if (baseTime != null && !baseTime.plusMinutes(minutes).isAfter(now)) orderMapper.cancelUnpaid(order.getId());
        }
    }
 
    void retryPendingOrders() {
        List<TourOrder> orders = orderMapper.selectList(new LambdaQueryWrapper<TourOrder>()
                .eq(TourOrder::getStatus, OrderStatus.PENDING_ACCEPT.getCode())
                .isNotNull(TourOrder::getPaymentTime)
                .and(wrapper -> wrapper.isNull(TourOrder::getRefundStatus)
                        .or().eq(TourOrder::getRefundStatus, RefundStatus.NONE.getCode()))
                .orderByAsc(TourOrder::getPaymentTime).last("LIMIT " + BATCH_SIZE));
        for (TourOrder order : orders) matchingService.match(order.getId());
    }
 
    void startDueGroupSessions() {
        List<GuideReceivingSession> sessions = sessionMapper.selectList(new LambdaQueryWrapper<GuideReceivingSession>()
                .eq(GuideReceivingSession::getStatus, "RECEIVING")
                .isNotNull(GuideReceivingSession::getServiceStartAt)
                .le(GuideReceivingSession::getServiceStartAt, LocalDateTime.now())
                .orderByAsc(GuideReceivingSession::getServiceStartAt).last("LIMIT " + BATCH_SIZE));
        for (GuideReceivingSession session : sessions) {
            sessionTransitionService.startIfDue(session);
        }
    }
 
    void completeExpiredServiceSessions() {
        LocalDateTime now = LocalDateTime.now();
        List<Long> sessionIds = orderMapper.selectDueServiceSessionIds(now, BATCH_SIZE);
        for (Long sessionId : sessionIds) {
            GuideReceivingSession session = sessionMapper.selectById(sessionId);
            if (session != null && "IN_SERVICE".equals(session.getStatus())) {
                sessionTransitionService.completeSession(session, now);
            }
        }
    }
 
    void sendServiceReminders() {
        int reminderMinutes = Math.max(1, uniPushProperties.getServiceReminderMinutes());
        LocalDateTime now = LocalDateTime.now();
        List<GuideReceivingSession> sessions = sessionMapper.selectList(
                new LambdaQueryWrapper<GuideReceivingSession>()
                        .eq(GuideReceivingSession::getStatus, "RECEIVING")
                        .isNull(GuideReceivingSession::getReminderPushedAt)
                        .isNotNull(GuideReceivingSession::getServiceStartAt)
                        .gt(GuideReceivingSession::getServiceStartAt, now)
                        .le(GuideReceivingSession::getServiceStartAt, now.plusMinutes(reminderMinutes))
                        .orderByAsc(GuideReceivingSession::getServiceStartAt)
                        .last("LIMIT " + BATCH_SIZE));
        for (GuideReceivingSession session : sessions) {
            TourOrder order = orderMapper.selectOne(new LambdaQueryWrapper<TourOrder>()
                    .eq(TourOrder::getReceivingSessionId, session.getId())
                    .eq(TourOrder::getGuideId, session.getGuideId())
                    .eq(TourOrder::getStatus, OrderStatus.PENDING_MEETING.getCode())
                    .orderByAsc(TourOrder::getId)
                    .last("LIMIT 1"));
            if (order == null) continue;
            LocalDateTime claimedAt = LocalDateTime.now();
            if (sessionMapper.claimServiceReminder(session.getId(), claimedAt) != 1) continue;
            if (!notificationService.pushServiceReminder(order)) {
                sessionMapper.releaseServiceReminder(session.getId(), claimedAt);
            }
        }
    }
}