|
@@ -3,6 +3,7 @@ package shop.alien.lawyer.service.impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.CommandLineRunner;
|
|
import org.springframework.boot.CommandLineRunner;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -26,6 +27,12 @@ import javax.annotation.PostConstruct;
|
|
|
@RequiredArgsConstructor
|
|
@RequiredArgsConstructor
|
|
|
public class OrderExpirationServiceImpl implements OrderExpirationService, CommandLineRunner {
|
|
public class OrderExpirationServiceImpl implements OrderExpirationService, CommandLineRunner {
|
|
|
|
|
|
|
|
|
|
+ @Value("${order.coefficient}")
|
|
|
|
|
+ private String coefficient;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${order.coefficients}")
|
|
|
|
|
+ private String coefficients;
|
|
|
|
|
+
|
|
|
private final BaseRedisService redisService;
|
|
private final BaseRedisService redisService;
|
|
|
private final RedisKeyExpirationHandler expirationHandler;
|
|
private final RedisKeyExpirationHandler expirationHandler;
|
|
|
private final LawyerConsultationOrderMapper orderMapper;
|
|
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:";
|
|
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分鐘
|
|
* 默認超時時間:30分鐘
|
|
|
*/
|
|
*/
|
|
|
private static final long DEFAULT_TIMEOUT_SECONDS = 30 * 60;
|
|
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);
|
|
expirationHandler.registerHandler(ORDER_PAYMENT_TIMEOUT_PREFIX, this::handleExpiredOrderKey);
|
|
|
log.info("訂單支付超時處理器註冊完成,前綴: {}", ORDER_PAYMENT_TIMEOUT_PREFIX);
|
|
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
|
|
@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
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void handleOrderPaymentTimeout(String orderNum) {
|
|
public void handleOrderPaymentTimeout(String orderNum) {
|
|
@@ -122,7 +166,7 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String key = ORDER_PAYMENT_TIMEOUT_PREFIX + orderNumber;
|
|
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,帶過期時間
|
|
// 設置Redis key,帶過期時間
|
|
|
// 當key過期時,會觸發RedisKeyExpirationListener
|
|
// 當key過期時,會觸發RedisKeyExpirationListener
|
|
@@ -131,6 +175,40 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
|
|
|
log.info("設置訂單支付超時監聽,訂單NO: {}, 超時時間: {}秒, key: {}", orderNumber, timeout, key);
|
|
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);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 取消訂單支付超時監聽(當訂單已支付時調用)
|
|
* 取消訂單支付超時監聽(當訂單已支付時調用)
|
|
|
*
|
|
*
|