liujie
2026-09-11 4f6f15b818704079ff2e4e4719b798f48d68a335
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.exception.BusinessException;
import com.tourguide.modules.order.domain.OrderStatus;
import com.tourguide.modules.order.domain.RefundStatus;
import com.tourguide.modules.order.dto.GuideEarningItemResponse;
import com.tourguide.modules.order.dto.GuideEarningsResponse;
import com.tourguide.modules.order.entity.TourOrder;
import com.tourguide.modules.order.mapper.TourOrderMapper;
import com.tourguide.modules.user.service.GuideUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
/** 查询导游已完成订单的业绩快照。 */
@Service
public class GuideEarningsService {
    private final TourOrderMapper orderMapper;
    private final GuideUserService guideUserService;
 
    public GuideEarningsService(TourOrderMapper orderMapper, GuideUserService guideUserService) {
        this.orderMapper = orderMapper;
        this.guideUserService = guideUserService;
    }
 
    public GuideEarningsResponse get(Long guideId, String serviceType, String scenicName,
                                     LocalDate date, long pageNum, long pageSize) {
        guideUserService.findEnabledById(guideId);
        validateServiceType(serviceType);
        long safePage = Math.max(pageNum, 1);
        long safeSize = Math.min(Math.max(pageSize, 1), 100);
        LocalDate today = LocalDate.now();
        BigDecimal todayEarnings = safeAmount(orderMapper.selectGuideEarningSum(
                guideId, today.atStartOfDay(), today.plusDays(1).atStartOfDay()));
        LocalDate monthStart = today.withDayOfMonth(1);
        BigDecimal monthEarnings = safeAmount(orderMapper.selectGuideEarningSum(
                guideId, monthStart.atStartOfDay(), monthStart.plusMonths(1).atStartOfDay()));
 
        LambdaQueryWrapper<TourOrder> query = new LambdaQueryWrapper<TourOrder>()
                .eq(TourOrder::getGuideId, guideId)
                .eq(TourOrder::getStatus, OrderStatus.COMPLETED.getCode())
                .isNotNull(TourOrder::getGuideEarningAmount)
                .and(wrapper -> wrapper.isNull(TourOrder::getRefundStatus)
                        .or().ne(TourOrder::getRefundStatus, RefundStatus.REFUNDED.getCode()))
                .eq(StringUtils.hasText(scenicName), TourOrder::getScenicName,
                        StringUtils.hasText(scenicName) ? scenicName.trim() : null)
                .ge(date != null, TourOrder::getCompletedAt, date == null ? null : date.atStartOfDay())
                .lt(date != null, TourOrder::getCompletedAt,
                        date == null ? null : date.plusDays(1).atStartOfDay())
                .orderByDesc(TourOrder::getCompletedAt)
                .orderByDesc(TourOrder::getId);
        if (StringUtils.hasText(serviceType)) {
            query.in(TourOrder::getOrderType, resolveOrderTypes(serviceType));
        }
        Page<TourOrder> result = orderMapper.selectPage(new Page<TourOrder>(safePage, safeSize), query);
        List<GuideEarningItemResponse> records = result.getRecords().stream()
                .map(GuideEarningItemResponse::new)
                .collect(Collectors.toList());
        return new GuideEarningsResponse(todayEarnings, monthEarnings,
                orderMapper.selectGuideEarningScenics(guideId), records,
                result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    private void validateServiceType(String serviceType) {
        if (StringUtils.hasText(serviceType)
                && !"QUICK".equals(serviceType)
                && !"PREMIUM".equals(serviceType)
                && !"GROUP_QUICK".equals(serviceType)
                && !"GROUP_PREMIUM".equals(serviceType)
                && !"PRIVATE_QUICK".equals(serviceType)
                && !"PRIVATE_PREMIUM".equals(serviceType)) {
            throw new BusinessException(400, "订单类型不支持");
        }
    }
 
    private List<String> resolveOrderTypes(String serviceType) {
        if ("QUICK".equals(serviceType)) {
            return Arrays.asList("GROUP_QUICK", "PRIVATE_QUICK");
        }
        if ("PREMIUM".equals(serviceType)) {
            return Arrays.asList("GROUP_PREMIUM", "PRIVATE_PREMIUM");
        }
        return Collections.singletonList(serviceType);
    }
 
    private BigDecimal safeAmount(BigDecimal amount) {
        return amount == null ? BigDecimal.ZERO.setScale(2) : amount;
    }
}