liujie
2026-09-11 26dae811b447b0f13500a63e3a972a377f00be3c
backend/src/main/java/com/tourguide/modules/order/service/GuideTourOrderService.java
@@ -8,15 +8,23 @@
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.ScenicPricing;
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.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/** 导游端订单查询服务,所有查询均限定当前导游本人。 */
@@ -30,12 +38,21 @@
    private final TourOrderMapper orderMapper;
    private final GuideReceivingSessionMapper sessionMapper;
    private final GuideRealtimeStatusMapper realtimeStatusMapper;
    private final ScenicPricingMapper pricingMapper;
    public GuideTourOrderService(TourOrderMapper orderMapper, GuideReceivingSessionMapper sessionMapper,
                                 GuideRealtimeStatusMapper realtimeStatusMapper) {
        this(orderMapper, sessionMapper, realtimeStatusMapper, null);
    }
    @Autowired
    public GuideTourOrderService(TourOrderMapper orderMapper, GuideReceivingSessionMapper sessionMapper,
                                 GuideRealtimeStatusMapper realtimeStatusMapper,
                                 ScenicPricingMapper pricingMapper) {
        this.orderMapper = orderMapper;
        this.sessionMapper = sessionMapper;
        this.realtimeStatusMapper = realtimeStatusMapper;
        this.pricingMapper = pricingMapper;
    }
    public PageResponse<GuideOrderResponse> page(Long guideId, String tab, long page, long size) {
@@ -45,8 +62,11 @@
        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());
        return new PageResponse<GuideOrderResponse>(
                result.getRecords().stream().map(GuideOrderResponse::from).collect(Collectors.toList()),
                result.getRecords().stream()
                        .map(order -> GuideOrderResponse.from(order, durationByPackageId.get(order.getServicePackageId())))
                        .collect(Collectors.toList()),
                result.getTotal(), result.getCurrent(), result.getSize());
    }
@@ -55,7 +75,7 @@
        if (order == null) {
            throw new BusinessException(404, "订单不存在");
        }
        return GuideOrderResponse.from(order);
        return toResponse(order);
    }
    @Transactional
@@ -65,7 +85,7 @@
            throw new BusinessException(404, "订单不存在");
        }
        if (OrderStatus.IN_SERVICE.matches(order.getStatus())) {
            return GuideOrderResponse.from(order);
            return toResponse(order);
        }
        if (!OrderStatus.PENDING_MEETING.matches(order.getStatus()) || order.getReceivingSessionId() == null) {
            throw new BusinessException(400, "当前订单不能开始讲解");
@@ -77,7 +97,7 @@
        }
        if ("IN_SERVICE".equals(session.getStatus())) {
            order.setStatus(OrderStatus.IN_SERVICE.getCode());
            return GuideOrderResponse.from(order);
            return toResponse(order);
        }
        if (!"RECEIVING".equals(session.getStatus()) && !"ASSIGNED".equals(session.getStatus())) {
            throw new BusinessException(400, "接单会话已结束,不能开始讲解");
@@ -90,8 +110,34 @@
        }
        realtimeStatusMapper.disableReceiving(guideId);
        order.setStatus(OrderStatus.IN_SERVICE.getCode());
        order.setArrivedAt(startedAt);
        order.setServiceStartedAt(startedAt);
        return GuideOrderResponse.from(order);
        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();
        }
        return GuideOrderResponse.from(order, durationMinutes);
    }
    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) {