liujie
2026-09-09 39f6fca28e3f78abe48f08e474ec011abb990b36
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
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.user.entity.GuideReceivingSession;
import com.tourguide.modules.user.mapper.GuideRealtimeStatusMapper;
import com.tourguide.modules.user.mapper.GuideReceivingSessionMapper;
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.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;
 
    public GuideTourOrderService(TourOrderMapper orderMapper, GuideReceivingSessionMapper sessionMapper,
                                 GuideRealtimeStatusMapper realtimeStatusMapper) {
        this.orderMapper = orderMapper;
        this.sessionMapper = sessionMapper;
        this.realtimeStatusMapper = realtimeStatusMapper;
    }
 
    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);
        return new PageResponse<GuideOrderResponse>(
                result.getRecords().stream().map(GuideOrderResponse::from).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 GuideOrderResponse.from(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 GuideOrderResponse.from(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 GuideOrderResponse.from(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 GuideOrderResponse.from(order);
    }
 
    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, "订单筛选状态不合法");
    }
}