liujie
2026-07-29 c560a88ab429cd164d8eb6fcec42a273b898e230
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
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.MiniOrderListResponse;
import com.tourguide.modules.order.dto.MiniOrderSummaryResponse;
import com.tourguide.modules.order.entity.TourOrder;
import com.tourguide.modules.order.mapper.TourOrderMapper;
import com.tourguide.modules.review.entity.OrderReview;
import com.tourguide.modules.review.mapper.OrderReviewMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/** 游客小程序个人订单查询服务。 */
@Service
public class MiniTourOrderService {
    private static final String TAB_ALL = "all";
    private static final String TAB_PENDING_PAY = "pendingPay";
    private static final String TAB_PENDING_GUIDE = "pendingGuide";
    private static final String TAB_SERVING = "serving";
    private static final String TAB_COMPLETED = "completed";
 
    private final TourOrderMapper orderMapper;
    private final OrderReviewMapper reviewMapper;
 
    public MiniTourOrderService(TourOrderMapper orderMapper, OrderReviewMapper reviewMapper) {
        this.orderMapper = orderMapper;
        this.reviewMapper = reviewMapper;
    }
 
    public PageResponse<MiniOrderListResponse> page(Long visitorId, String tab, long page, long size) {
        String normalizedTab = StringUtils.hasText(tab) ? tab.trim() : TAB_ALL;
        LambdaQueryWrapper<TourOrder> query = new LambdaQueryWrapper<TourOrder>()
                .eq(TourOrder::getVisitorId, visitorId);
        applyTabFilter(query, normalizedTab);
        query.orderByDesc(TourOrder::getPlacedAt).orderByDesc(TourOrder::getId);
        Page<TourOrder> result = orderMapper.selectPage(new Page<TourOrder>(page, size), query);
        Set<Long> reviewedOrderIds = reviewedOrderIds(result.getRecords());
        List<MiniOrderListResponse> records = result.getRecords().stream()
                .map(order -> MiniOrderListResponse.from(order, reviewedOrderIds.contains(order.getId())))
                .collect(Collectors.toList());
        return new PageResponse<MiniOrderListResponse>(records, result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    public MiniOrderSummaryResponse summary(Long visitorId) {
        return new MiniOrderSummaryResponse(
                count(visitorId, Collections.singletonList(OrderStatus.PENDING_PAYMENT.getCode())),
                count(visitorId, Arrays.asList(
                        OrderStatus.PENDING_ACCEPT.getCode(), OrderStatus.PENDING_MEETING.getCode())),
                count(visitorId, Collections.singletonList(OrderStatus.IN_SERVICE.getCode())),
                count(visitorId, Collections.singletonList(OrderStatus.COMPLETED.getCode())));
    }
 
    private void applyTabFilter(LambdaQueryWrapper<TourOrder> query, String tab) {
        if (TAB_ALL.equals(tab)) { return; }
        if (TAB_PENDING_PAY.equals(tab)) {
            query.eq(TourOrder::getStatus, OrderStatus.PENDING_PAYMENT.getCode());
            return;
        }
        if (TAB_PENDING_GUIDE.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.eq(TourOrder::getStatus, OrderStatus.COMPLETED.getCode());
            return;
        }
        throw new BusinessException(400, "订单筛选状态不合法");
    }
 
    private Set<Long> reviewedOrderIds(List<TourOrder> orders) {
        if (orders.isEmpty()) { return Collections.emptySet(); }
        List<Long> orderIds = orders.stream().map(TourOrder::getId).collect(Collectors.toList());
        return reviewMapper.selectList(new LambdaQueryWrapper<OrderReview>()
                        .select(OrderReview::getOrderId)
                        .in(OrderReview::getOrderId, orderIds))
                .stream().map(OrderReview::getOrderId).collect(Collectors.toSet());
    }
 
    private long count(Long visitorId, List<String> statuses) {
        return orderMapper.selectCount(new LambdaQueryWrapper<TourOrder>()
                .eq(TourOrder::getVisitorId, visitorId)
                .in(TourOrder::getStatus, statuses));
    }
}