From 39f6fca28e3f78abe48f08e474ec011abb990b36 Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期三, 09 九月 2026 10:39:21 +0800
Subject: [PATCH] update

---
 backend/src/main/java/com/tourguide/modules/order/service/MiniTourOrderCommandService.java |   78 +++++++++++++++++++++-----------------
 1 files changed, 43 insertions(+), 35 deletions(-)

diff --git a/backend/src/main/java/com/tourguide/modules/order/service/MiniTourOrderCommandService.java b/backend/src/main/java/com/tourguide/modules/order/service/MiniTourOrderCommandService.java
index 62b53fb..145f740 100644
--- a/backend/src/main/java/com/tourguide/modules/order/service/MiniTourOrderCommandService.java
+++ b/backend/src/main/java/com/tourguide/modules/order/service/MiniTourOrderCommandService.java
@@ -10,6 +10,7 @@
 import com.tourguide.modules.operation.mapper.ScenicPricingMapper;
 import com.tourguide.modules.operation.service.PlatformSettingsService;
 import com.tourguide.modules.order.domain.OrderStatus;
+import com.tourguide.modules.order.domain.PaymentMethod;
 import com.tourguide.modules.order.domain.RefundStatus;
 import com.tourguide.modules.order.dto.MiniOrderCreateRequest;
 import com.tourguide.modules.order.dto.TourOrderResponse;
@@ -20,6 +21,7 @@
 import com.tourguide.modules.user.entity.GuideUser;
 import com.tourguide.modules.user.mapper.GuideUserMapper;
 import com.tourguide.modules.user.service.MiniUserService;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -31,6 +33,7 @@
 /** 小程序讲解订单创建及状态流转服务。 */
 @Service
 public class MiniTourOrderCommandService {
+    private static final int DEFAULT_UNPAID_CANCEL_MINUTES = 15;
     private final TourOrderMapper orderMapper;
     private final MiniUserService miniUserService;
     private final OperationScenicSpotMapper scenicSpotMapper;
@@ -38,8 +41,10 @@
     private final ScenicPricingMapper pricingMapper;
     private final GuideUserMapper guideUserMapper;
     private final PlatformSettingsService platformSettingsService;
-    private final GuideOrderMatchingService matchingService;
     private final GuideNotificationService notificationService;
+    private final OrderRefundService orderRefundService;
+    private final GuideOrderMatchingService matchingService;
+    private final boolean skipPayment;
 
     public MiniTourOrderCommandService(TourOrderMapper orderMapper,
                                        MiniUserService miniUserService,
@@ -48,8 +53,10 @@
                                        ScenicPricingMapper pricingMapper,
                                        GuideUserMapper guideUserMapper,
                                        PlatformSettingsService platformSettingsService,
+                                       GuideNotificationService notificationService,
+                                       OrderRefundService orderRefundService,
                                        GuideOrderMatchingService matchingService,
-                                       GuideNotificationService notificationService) {
+                                       @Value("${tourguide.order.skip-payment:false}") boolean skipPayment) {
         this.orderMapper = orderMapper;
         this.miniUserService = miniUserService;
         this.scenicSpotMapper = scenicSpotMapper;
@@ -57,8 +64,10 @@
         this.pricingMapper = pricingMapper;
         this.guideUserMapper = guideUserMapper;
         this.platformSettingsService = platformSettingsService;
-        this.matchingService = matchingService;
         this.notificationService = notificationService;
+        this.orderRefundService = orderRefundService;
+        this.matchingService = matchingService;
+        this.skipPayment = skipPayment;
     }
 
     @Transactional
@@ -101,32 +110,23 @@
         order.setAmount(subtotalAmount);
         order.setPlacedAt(now);
         order.setMeetingPointCount(1);
-        order.setStatus(OrderStatus.PENDING_PAYMENT.getCode());
+        Integer settlementDays = platformSettingsService.getSettings().getSettlementDays();
+        order.setSettlementDays(settlementDays == null ? 1 : Math.max(0, settlementDays));
+        if (skipPayment) {
+            order.setPaymentMethod(PaymentMethod.TEMPORARY_BYPASS.getCode());
+            order.setPaymentOrderNo("TEMP" + order.getOrderNo());
+            order.setPaymentAmount(subtotalAmount);
+            order.setPaymentTime(now);
+            order.setStatus(OrderStatus.PENDING_ACCEPT.getCode());
+        } else {
+            order.setStatus(OrderStatus.PENDING_PAYMENT.getCode());
+        }
         order.setRefundStatus(RefundStatus.NONE.getCode());
         orderMapper.insert(order);
-        return TourOrderResponse.from(order);
-    }
-
-    /**
-     * 确认游客订单支付成功,并将订单流转到等待导游接单状态。
-     * 当前小程序尚未接入微信支付回调,因此由待支付页面在支付动作成功后调用;
-     * 正式接入微信支付后应由服务端支付回调触发本方法对应的领域逻辑。
-     */
-    @Transactional
-    public TourOrderResponse confirmPayment(Long visitorId, Long orderId) {
-        TourOrder order = requireVisitorOrder(visitorId, orderId);
-        if (!OrderStatus.PENDING_PAYMENT.matches(order.getStatus())) {
-            throw new BusinessException(409, "当前订单状态不支持支付");
+        if (skipPayment) {
+            matchingService.match(order.getId());
         }
-        LocalDateTime now = LocalDateTime.now();
-        order.setPaymentMethod("WECHAT");
-        order.setPaymentOrderNo("LOCAL" + order.getOrderNo());
-        order.setPaymentAmount(order.getAmount());
-        order.setPaymentTime(now);
-        order.setStatus(OrderStatus.PENDING_ACCEPT.getCode());
-        orderMapper.updateById(order);
-        matchingService.match(order.getId());
-        return TourOrderResponse.from(orderMapper.selectById(order.getId()));
+        return TourOrderResponse.from(order);
     }
 
     public TourOrderResponse active(Long visitorId) {
@@ -160,15 +160,25 @@
                         .orderByDesc(ScenicPricing::getId)
                         .last("LIMIT 1"));
         GuideUser guide = order.getGuideId() == null ? null : guideUserMapper.selectById(order.getGuideId());
+        int unpaidCancelMinutes = scenicSpot == null || scenicSpot.getUnpaidCancelMinutes() == null
+                || scenicSpot.getUnpaidCancelMinutes() < 1
+                ? DEFAULT_UNPAID_CANCEL_MINUTES : scenicSpot.getUnpaidCancelMinutes();
         return TourOrderResponse.from(order)
                 .enrich(scenicSpot, meetingPoint, pricing, guide)
+                .withUnpaidCancelMinutes(unpaidCancelMinutes)
                 .withUnacceptedAutoRefundMinutes(
                         platformSettingsService.getSettings().getUnacceptedAutoRefundMinutes());
     }
 
     @Transactional
     public TourOrderResponse cancel(Long visitorId, Long orderId) {
-        TourOrder order = requireVisitorOrder(visitorId, orderId);
+        TourOrder order = orderMapper.selectForUpdate(orderId);
+        if (order == null || visitorId == null || !visitorId.equals(order.getVisitorId())) {
+            throw new BusinessException(404, "订单不存在");
+        }
+        if (OrderStatus.CANCELLED.matches(order.getStatus())) {
+            return TourOrderResponse.from(order);
+        }
         boolean notifyGuide = OrderStatus.PENDING_MEETING.matches(order.getStatus()) && order.getGuideId() != null;
         if (!OrderStatus.PENDING_PAYMENT.matches(order.getStatus())
                 && !OrderStatus.PENDING_ACCEPT.matches(order.getStatus())
@@ -176,15 +186,13 @@
             throw new BusinessException(409, "当前订单状态不支持取消");
         }
         order.setStatus(OrderStatus.CANCELLED.getCode());
-        if (order.getPaymentTime() != null) {
-            order.setRefundStatus(RefundStatus.REFUNDED.getCode());
-            order.setRefundAmount(order.getPaymentAmount());
-            order.setRefundTime(LocalDateTime.now());
-            order.setRefundOrderNo("RF" + generateOrderNo());
-            order.setRefundMethod(order.getPaymentMethod());
+        order.setCancelledAt(LocalDateTime.now());
+        if (order.getPaymentTime() == null) {
+            orderMapper.updateById(order);
+        } else {
+            orderRefundService.requestRefund(order);
         }
-        orderMapper.updateById(order);
-        if (notifyGuide) notificationService.notifyOrderCancelled(order);
+        if (notifyGuide) notificationService.notifyVisitorCancelledOrder(order);
         return TourOrderResponse.from(order);
     }
 

--
Gitblit v1.7.1