liujie
2026-09-14 15defa991fcd30b9fa60bab17d2230b5929bf2a2
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
236
237
238
239
package com.tourguide.modules.order.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tourguide.common.api.PageResponse;
import com.tourguide.common.exception.BusinessException;
import com.tourguide.modules.order.domain.OrderStatus;
import com.tourguide.modules.order.dto.GuideOrderResponse;
import com.tourguide.modules.order.entity.TourOrder;
import com.tourguide.modules.order.mapper.TourOrderMapper;
import com.tourguide.modules.operation.entity.ScenicMeetingPoint;
import com.tourguide.modules.operation.entity.ScenicPricing;
import com.tourguide.modules.operation.mapper.ScenicMeetingPointMapper;
import com.tourguide.modules.operation.mapper.ScenicPricingMapper;
import com.tourguide.modules.user.entity.GuideReceivingSession;
import com.tourguide.modules.user.mapper.GuideRealtimeStatusMapper;
import com.tourguide.modules.user.mapper.GuideReceivingSessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
/** 导游端订单查询服务,所有查询均限定当前导游本人。 */
@Service
public class GuideTourOrderService {
    private static final String TAB_ALL = "all";
    private static final String TAB_ACCEPTED = "accepted";
    private static final String TAB_SERVING = "serving";
    private static final String TAB_COMPLETED = "completed";
 
    private final TourOrderMapper orderMapper;
    private final GuideReceivingSessionMapper sessionMapper;
    private final GuideRealtimeStatusMapper realtimeStatusMapper;
    private final ScenicPricingMapper pricingMapper;
    private final ScenicMeetingPointMapper meetingPointMapper;
 
    public GuideTourOrderService(TourOrderMapper orderMapper, GuideReceivingSessionMapper sessionMapper,
                                 GuideRealtimeStatusMapper realtimeStatusMapper) {
        this(orderMapper, sessionMapper, realtimeStatusMapper, null, null);
    }
 
    public GuideTourOrderService(TourOrderMapper orderMapper, GuideReceivingSessionMapper sessionMapper,
                                 GuideRealtimeStatusMapper realtimeStatusMapper,
                                 ScenicPricingMapper pricingMapper) {
        this(orderMapper, sessionMapper, realtimeStatusMapper, pricingMapper, null);
    }
 
    @Autowired
    public GuideTourOrderService(TourOrderMapper orderMapper, GuideReceivingSessionMapper sessionMapper,
                                 GuideRealtimeStatusMapper realtimeStatusMapper,
                                 ScenicPricingMapper pricingMapper,
                                 ScenicMeetingPointMapper meetingPointMapper) {
        this.orderMapper = orderMapper;
        this.sessionMapper = sessionMapper;
        this.realtimeStatusMapper = realtimeStatusMapper;
        this.pricingMapper = pricingMapper;
        this.meetingPointMapper = meetingPointMapper;
    }
 
    public PageResponse<GuideOrderResponse> page(Long guideId, String tab, long page, long size) {
        LambdaQueryWrapper<TourOrder> query = new LambdaQueryWrapper<TourOrder>()
                .eq(TourOrder::getGuideId, guideId);
        applyTabFilter(query, normalizeTab(tab));
        query.orderByDesc(TourOrder::getAcceptedAt)
                .orderByDesc(TourOrder::getId);
        Page<TourOrder> result = orderMapper.selectPage(new Page<TourOrder>(page, size), query);
        Map<Long, Integer> durationByPackageId = durationByPackageId(result.getRecords());
        Map<Long, ScenicMeetingPoint> meetingPointByOrderId = meetingPointByOrderId(result.getRecords());
        return new PageResponse<GuideOrderResponse>(
                result.getRecords().stream()
                        .map(order -> GuideOrderResponse.from(order,
                                durationByPackageId.get(order.getServicePackageId()),
                                meetingPointByOrderId.get(order.getId())))
                        .collect(Collectors.toList()),
                result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    public GuideOrderResponse detail(Long guideId, Long orderId) {
        TourOrder order = findOwnedOrder(guideId, orderId);
        if (order == null) {
            throw new BusinessException(404, "订单不存在");
        }
        return toResponse(order);
    }
 
    @Transactional
    public GuideOrderResponse start(Long guideId, Long orderId) {
        TourOrder order = findOwnedOrder(guideId, orderId);
        if (order == null) {
            throw new BusinessException(404, "订单不存在");
        }
        if (OrderStatus.IN_SERVICE.matches(order.getStatus())) {
            return toResponse(order);
        }
        if (!OrderStatus.PENDING_MEETING.matches(order.getStatus()) || order.getReceivingSessionId() == null) {
            throw new BusinessException(400, "当前订单不能开始讲解");
        }
 
        GuideReceivingSession session = sessionMapper.selectForUpdate(order.getReceivingSessionId(), guideId);
        if (session == null) {
            throw new BusinessException(400, "接单会话不存在或已结束");
        }
        if ("IN_SERVICE".equals(session.getStatus())) {
            order.setStatus(OrderStatus.IN_SERVICE.getCode());
            return toResponse(order);
        }
        if (!"RECEIVING".equals(session.getStatus()) && !"ASSIGNED".equals(session.getStatus())) {
            throw new BusinessException(400, "接单会话已结束,不能开始讲解");
        }
 
        LocalDateTime startedAt = LocalDateTime.now();
        if (sessionMapper.startSession(session.getId(), guideId) != 1
                || orderMapper.startSessionOrders(session.getId(), startedAt) == 0) {
            throw new BusinessException(409, "讲解状态已变化,请刷新后重试");
        }
        realtimeStatusMapper.disableReceiving(guideId);
        order.setStatus(OrderStatus.IN_SERVICE.getCode());
        order.setArrivedAt(startedAt);
        order.setServiceStartedAt(startedAt);
        return toResponse(order);
    }
 
    private GuideOrderResponse toResponse(TourOrder order) {
        Integer durationMinutes = null;
        if (pricingMapper != null && order.getServicePackageId() != null) {
            ScenicPricing pricing = pricingMapper.selectById(order.getServicePackageId());
            durationMinutes = pricing == null ? null : pricing.getDurationMinutes();
        }
        ScenicMeetingPoint meetingPoint = meetingPointByOrderId(Collections.singletonList(order)).get(order.getId());
        return GuideOrderResponse.from(order, durationMinutes, meetingPoint);
    }
 
    private Map<Long, ScenicMeetingPoint> meetingPointByOrderId(List<TourOrder> orders) {
        if (meetingPointMapper == null || orders == null || orders.isEmpty()) {
            return Collections.emptyMap();
        }
        Set<Long> meetingPointIds = orders.stream()
                .map(TourOrder::getMeetingPointId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Set<Long> scenicSpotIds = orders.stream()
                .map(TourOrder::getScenicSpotId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        if (meetingPointIds.isEmpty() && scenicSpotIds.isEmpty()) {
            return Collections.emptyMap();
        }
 
        Map<Long, ScenicMeetingPoint> pointsById = new HashMap<Long, ScenicMeetingPoint>();
        if (!meetingPointIds.isEmpty()) {
            meetingPointMapper.selectBatchIds(meetingPointIds)
                    .forEach(point -> pointsById.put(point.getId(), point));
        }
 
        Map<String, ScenicMeetingPoint> pointsByScenicAndName = new HashMap<String, ScenicMeetingPoint>();
        if (!scenicSpotIds.isEmpty()) {
            meetingPointMapper.selectList(new LambdaQueryWrapper<ScenicMeetingPoint>()
                            .in(ScenicMeetingPoint::getScenicSpotId, scenicSpotIds))
                    .forEach(point -> pointsByScenicAndName.put(
                            meetingPointKey(point.getScenicSpotId(), point.getPointName()), point));
        }
 
        Map<Long, ScenicMeetingPoint> result = new HashMap<Long, ScenicMeetingPoint>();
        for (TourOrder order : orders) {
            ScenicMeetingPoint point = pointsById.get(order.getMeetingPointId());
            if (point == null) {
                point = pointsByScenicAndName.get(
                        meetingPointKey(order.getScenicSpotId(), order.getMeetingPointName()));
            }
            if (point != null && order.getId() != null) {
                result.put(order.getId(), point);
            }
        }
        return result;
    }
 
    private String meetingPointKey(Long scenicSpotId, String meetingPointName) {
        return String.valueOf(scenicSpotId) + '\u0000' + String.valueOf(meetingPointName);
    }
 
    private Map<Long, Integer> durationByPackageId(List<TourOrder> orders) {
        if (pricingMapper == null || orders == null || orders.isEmpty()) {
            return Collections.emptyMap();
        }
        Set<Long> packageIds = orders.stream()
                .map(TourOrder::getServicePackageId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        if (packageIds.isEmpty()) {
            return Collections.emptyMap();
        }
        return pricingMapper.selectBatchIds(packageIds).stream()
                .filter(pricing -> pricing.getDurationMinutes() != null)
                .collect(Collectors.toMap(ScenicPricing::getId, ScenicPricing::getDurationMinutes));
    }
 
    private TourOrder findOwnedOrder(Long guideId, Long orderId) {
        return orderMapper.selectOne(new LambdaQueryWrapper<TourOrder>()
                .eq(TourOrder::getId, orderId)
                .eq(TourOrder::getGuideId, guideId)
                .last("LIMIT 1"));
    }
 
    private String normalizeTab(String tab) {
        return StringUtils.hasText(tab) ? tab.trim() : TAB_ALL;
    }
 
    private void applyTabFilter(LambdaQueryWrapper<TourOrder> query, String tab) {
        if (TAB_ALL.equals(tab)) {
            query.ne(TourOrder::getStatus, OrderStatus.PENDING_PAYMENT.getCode());
            return;
        }
        if (TAB_ACCEPTED.equals(tab)) {
            query.in(TourOrder::getStatus,
                    OrderStatus.PENDING_ACCEPT.getCode(), OrderStatus.PENDING_MEETING.getCode());
            return;
        }
        if (TAB_SERVING.equals(tab)) {
            query.eq(TourOrder::getStatus, OrderStatus.IN_SERVICE.getCode());
            return;
        }
        if (TAB_COMPLETED.equals(tab)) {
            query.in(TourOrder::getStatus, Arrays.asList(
                    OrderStatus.COMPLETED.getCode(), OrderStatus.CANCELLED.getCode(), OrderStatus.REFUNDED.getCode()));
            return;
        }
        throw new BusinessException(400, "订单筛选状态不合法");
    }
}