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 page(Long guideId, String tab, long page, long size) { LambdaQueryWrapper query = new LambdaQueryWrapper() .eq(TourOrder::getGuideId, guideId); applyTabFilter(query, normalizeTab(tab)); query.orderByDesc(TourOrder::getAcceptedAt) .orderByDesc(TourOrder::getId); Page result = orderMapper.selectPage(new Page(page, size), query); return new PageResponse( 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() .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 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, "订单筛选状态不合法"); } }