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.dto.TourOrderResponse; import com.tourguide.modules.order.domain.OrderStatus; import com.tourguide.modules.order.domain.RefundStatus; import com.tourguide.modules.order.entity.TourOrder; import com.tourguide.modules.order.mapper.TourOrderMapper; import com.tourguide.modules.system.service.GuideNotificationService; import com.tourguide.modules.review.entity.OrderReview; import com.tourguide.modules.review.mapper.OrderReviewMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @Service public class TourOrderService { private final TourOrderMapper orderMapper; private final OrderReviewMapper reviewMapper; private final GuideNotificationService notificationService; public TourOrderService(TourOrderMapper orderMapper, OrderReviewMapper reviewMapper, GuideNotificationService notificationService) { this.orderMapper = orderMapper; this.reviewMapper = reviewMapper; this.notificationService = notificationService; } public PageResponse page(long page, long size, String keyword, Long visitorId, String scenicName, String status, LocalDate startDate, LocalDate endDate) { LambdaQueryWrapper query = new LambdaQueryWrapper(); if (StringUtils.hasText(keyword)) { String searchKeyword = keyword.trim(); query.and(wrapper -> wrapper.like(TourOrder::getOrderNo, searchKeyword) .or().like(TourOrder::getVisitorNickname, searchKeyword) .or().like(TourOrder::getVisitorMobile, searchKeyword) .or().like(TourOrder::getGuideName, searchKeyword) .or().like(TourOrder::getGuideMobile, searchKeyword)); } query.eq(visitorId != null, TourOrder::getVisitorId, visitorId) .eq(StringUtils.hasText(scenicName), TourOrder::getScenicName, scenicName) .eq(StringUtils.hasText(status), TourOrder::getStatus, status) .ge(startDate != null, TourOrder::getCreatedAt, startDate == null ? null : startDate.atStartOfDay()) .lt(endDate != null, TourOrder::getCreatedAt, endDate == null ? null : endDate.plusDays(1).atStartOfDay()) .orderByDesc(TourOrder::getId); Page result = orderMapper.selectPage(new Page(page, size), query); return new PageResponse( result.getRecords().stream().map(TourOrderResponse::from).collect(Collectors.toList()), result.getTotal(), result.getCurrent(), result.getSize()); } public TourOrderResponse detail(Long id) { TourOrder order = requireOrder(id); OrderReview review = reviewMapper.selectOne(new LambdaQueryWrapper() .eq(OrderReview::getOrderId, id) .last("LIMIT 1")); return TourOrderResponse.from(order, review); } public List scenicOptions() { return orderMapper.selectScenicOptions(); } @Transactional public TourOrderResponse refund(Long id, BigDecimal refundAmount) { TourOrder order = requireOrderForUpdate(id); boolean notifyGuide = OrderStatus.PENDING_MEETING.matches(order.getStatus()) && order.getGuideId() != null; if (OrderStatus.REFUNDED.matches(order.getStatus()) || RefundStatus.REFUNDED.matches(order.getRefundStatus())) { throw new BusinessException(409, "订单已退款,请勿重复操作"); } if (OrderStatus.CANCELLED.matches(order.getStatus())) { throw new BusinessException(409, "已取消订单不能退款"); } if (!OrderStatus.isRefundable(order.getStatus())) { throw new BusinessException(409, "当前订单状态不支持退款"); } BigDecimal refundableAmount = order.getPaymentAmount() == null ? order.getAmount() : order.getPaymentAmount(); if (refundableAmount == null || refundableAmount.compareTo(BigDecimal.ZERO) <= 0) { throw new BusinessException(409, "订单无可退金额"); } if (refundAmount == null || refundAmount.compareTo(BigDecimal.ZERO) <= 0) { throw new BusinessException(400, "退款金额必须大于0"); } if (refundAmount.compareTo(refundableAmount) > 0) { throw new BusinessException(400, "退款金额不能超过当前可退金额"); } order.setRefundStatus(RefundStatus.REFUNDED.getCode()); order.setStatus(OrderStatus.REFUNDED.getCode()); order.setRefundAmount(refundAmount); order.setRefundTime(LocalDateTime.now()); order.setRefundOrderNo(generateRefundOrderNo()); order.setRefundMethod(order.getPaymentMethod()); orderMapper.updateById(order); if (notifyGuide) notificationService.notifyOrderCancelled(order); return TourOrderResponse.from(order); } private TourOrder requireOrder(Long id) { TourOrder order = orderMapper.selectById(id); if (order == null) { throw new BusinessException(404, "订单不存在"); } return order; } private TourOrder requireOrderForUpdate(Long id) { TourOrder order = orderMapper.selectForUpdate(id); if (order == null) { throw new BusinessException(404, "订单不存在"); } return order; } private String generateRefundOrderNo() { String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); return "RF" + time + UUID.randomUUID().toString().replace("-", "").substring(0, 6).toUpperCase(); } }