Преглед на файлове

订单超时监控增加逻辑,配置系数

jyc преди 3 седмици
родител
ревизия
6b7db52d92

+ 16 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/service/OrderExpirationService.java

@@ -24,6 +24,22 @@ public interface OrderExpirationService {
     void setOrderPaymentTimeout(String orderNumber, long timeoutSeconds);
 
     /**
+     * 設置订单待接单超时監聽
+     *
+     * @param orderNumber 訂單NO
+     * @param timeoutSeconds 超時時間(秒),默認30分鐘
+     */
+    void setOrderAcceptTimeout(String orderNumber, long timeoutSeconds);
+
+    /**
+     * 設置訂單退款超時監聽
+     *
+     * @param orderNumber 訂單NO
+     * @param timeoutSeconds 超時時間(秒),默認30分鐘
+     */
+    void setOrderRefundTimeout(String orderNumber, long timeoutSeconds);
+
+    /**
      * 取消訂單支付超時監聽(當訂單已支付時調用)
      * 
      * @param orderNumber 訂單NO

+ 79 - 1
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/OrderExpirationServiceImpl.java

@@ -3,6 +3,7 @@ package shop.alien.lawyer.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -26,6 +27,12 @@ import javax.annotation.PostConstruct;
 @RequiredArgsConstructor
 public class OrderExpirationServiceImpl implements OrderExpirationService, CommandLineRunner {
 
+    @Value("${order.coefficient}")
+    private String coefficient;
+
+    @Value("${order.coefficients}")
+    private String coefficients;
+
     private final BaseRedisService redisService;
     private final RedisKeyExpirationHandler expirationHandler;
     private final LawyerConsultationOrderMapper orderMapper;
@@ -36,6 +43,16 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
     private static final String ORDER_PAYMENT_TIMEOUT_PREFIX = "lawyer:order:payment:timeout:";
 
     /**
+     * Redis key前綴:訂單接单超時
+     */
+    private static final String ORDER_ACCEPT_TIMEOUT_PREFIX = "lawyer:order:accept:timeout:";
+
+    /**
+     * Redis key前綴:訂單退款超時
+     */
+    private static final String ORDER_REFUND_TIMEOUT_PREFIX = "lawyer:order:refund:timeout:";
+
+    /**
      * 默認超時時間:30分鐘
      */
     private static final long DEFAULT_TIMEOUT_SECONDS = 30 * 60;
@@ -48,6 +65,14 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
         // 註冊訂單支付超時處理器
         expirationHandler.registerHandler(ORDER_PAYMENT_TIMEOUT_PREFIX, this::handleExpiredOrderKey);
         log.info("訂單支付超時處理器註冊完成,前綴: {}", ORDER_PAYMENT_TIMEOUT_PREFIX);
+
+        // 註冊订单待接单超时處理器
+        expirationHandler.registerHandler(ORDER_ACCEPT_TIMEOUT_PREFIX, this::handleRefundOrderKey);
+        log.info("訂單支付超時處理器註冊完成,前綴: {}", ORDER_ACCEPT_TIMEOUT_PREFIX);
+
+        // 註冊訂單退款超時處理器
+        expirationHandler.registerHandler(ORDER_REFUND_TIMEOUT_PREFIX, this::handleRefundOrderKey);
+        log.info("訂單支付超時處理器註冊完成,前綴: {}", ORDER_REFUND_TIMEOUT_PREFIX);
     }
 
     @Override
@@ -74,6 +99,25 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
         }
     }
 
+    /**
+     * 處理過期的訂單key
+     *
+     * @param expiredKey 過期的key,格式:order:payment:timeout:{orderId}
+     */
+    private void handleRefundOrderKey(String expiredKey) {
+        try {
+            // 從key中提取訂單ID
+            String orderNo = expiredKey.replace(ORDER_PAYMENT_TIMEOUT_PREFIX, "");
+
+            log.info("檢測到訂單支付超時,訂單no: {}", orderNo);
+
+            // 處理訂單支付超時
+            handleOrderPaymentTimeout(orderNo);
+        } catch (Exception e) {
+            log.error("處理過期訂單key失敗,key: {}", expiredKey, e);
+        }
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void handleOrderPaymentTimeout(String orderNum) {
@@ -122,7 +166,7 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
         }
         
         String key = ORDER_PAYMENT_TIMEOUT_PREFIX + orderNumber;
-        long timeout = timeoutSeconds > 0 ? timeoutSeconds : DEFAULT_TIMEOUT_SECONDS;
+        long timeout = timeoutSeconds > 0 ? timeoutSeconds : 60 * Long.parseLong(coefficient);
         
         // 設置Redis key,帶過期時間
         // 當key過期時,會觸發RedisKeyExpirationListener
@@ -131,6 +175,40 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
         log.info("設置訂單支付超時監聽,訂單NO: {}, 超時時間: {}秒, key: {}", orderNumber, timeout, key);
     }
 
+    @Override
+    public void setOrderAcceptTimeout(String orderNumber, long timeoutSeconds) {
+        if (orderNumber == null) {
+            log.warn("訂單ID為null,無法設置支付超時監聽");
+            return;
+        }
+
+        String key = ORDER_ACCEPT_TIMEOUT_PREFIX + orderNumber;
+        long timeout = timeoutSeconds > 0 ? timeoutSeconds : 60 * Long.parseLong(coefficients);
+
+        // 設置Redis key,帶過期時間
+        // 當key過期時,會觸發RedisKeyExpirationListener
+        redisService.setString(key, String.valueOf(orderNumber), timeout);
+
+        log.info("設置訂單支付超時監聽,訂單NO: {}, 超時時間: {}秒, key: {}", orderNumber, timeout, key);
+    }
+
+    @Override
+    public void setOrderRefundTimeout(String orderNumber, long timeoutSeconds) {
+        if (orderNumber == null) {
+            log.warn("訂單ID為null,無法設置支付超時監聽");
+            return;
+        }
+
+        String key = ORDER_REFUND_TIMEOUT_PREFIX + orderNumber;
+        long timeout = timeoutSeconds > 0 ? timeoutSeconds : 60 * Long.parseLong(coefficients);
+
+        // 設置Redis key,帶過期時間
+        // 當key過期時,會觸發RedisKeyExpirationListener
+        redisService.setString(key, String.valueOf(orderNumber), timeout);
+
+        log.info("設置訂單支付超時監聽,訂單NO: {}, 超時時間: {}秒, key: {}", orderNumber, timeout, key);
+    }
+
     /**
      * 取消訂單支付超時監聽(當訂單已支付時調用)
      *