|
|
@@ -1,11 +1,13 @@
|
|
|
package shop.alien.store.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.boot.CommandLineRunner;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import shop.alien.entity.store.LawyerConsultationOrder;
|
|
|
+import shop.alien.mapper.LawyerConsultationOrderMapper;
|
|
|
import shop.alien.store.config.BaseRedisService;
|
|
|
import shop.alien.store.listener.RedisKeyExpirationHandler;
|
|
|
import shop.alien.store.service.OrderExpirationService;
|
|
|
@@ -25,7 +27,7 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
|
|
|
|
|
|
private final BaseRedisService redisService;
|
|
|
private final RedisKeyExpirationHandler expirationHandler;
|
|
|
- private final LawyerConsultationOrderServiceImpl orderService;
|
|
|
+ private final LawyerConsultationOrderMapper orderMapper;
|
|
|
|
|
|
/**
|
|
|
* Redis key前綴:訂單支付超時
|
|
|
@@ -60,13 +62,12 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
|
|
|
private void handleExpiredOrderKey(String expiredKey) {
|
|
|
try {
|
|
|
// 從key中提取訂單ID
|
|
|
- String orderIdStr = expiredKey.replace(ORDER_PAYMENT_TIMEOUT_PREFIX, "");
|
|
|
- Integer orderId = Integer.parseInt(orderIdStr);
|
|
|
+ String orderNo = expiredKey.replace(ORDER_PAYMENT_TIMEOUT_PREFIX, "");
|
|
|
|
|
|
- log.info("檢測到訂單支付超時,訂單ID: {}", orderId);
|
|
|
+ log.info("檢測到訂單支付超時,訂單no: {}", orderNo);
|
|
|
|
|
|
// 處理訂單支付超時
|
|
|
- handleOrderPaymentTimeout(orderId);
|
|
|
+ handleOrderPaymentTimeout(orderNo);
|
|
|
} catch (Exception e) {
|
|
|
log.error("處理過期訂單key失敗,key: {}", expiredKey, e);
|
|
|
}
|
|
|
@@ -74,36 +75,40 @@ public class OrderExpirationServiceImpl implements OrderExpirationService, Comma
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void handleOrderPaymentTimeout(Integer orderId) {
|
|
|
- log.info("開始處理訂單支付超時,訂單ID: {}", orderId);
|
|
|
+ public void handleOrderPaymentTimeout(String orderNum) {
|
|
|
+ log.info("開始處理訂單支付超時,訂單no: {}", orderNum);
|
|
|
|
|
|
try {
|
|
|
// 查詢訂單
|
|
|
- LawyerConsultationOrder order = orderService.getById(orderId);
|
|
|
+ LambdaQueryWrapper<LawyerConsultationOrder> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(LawyerConsultationOrder::getOrderNumber, orderNum).last("LIMIT 1");
|
|
|
+ LawyerConsultationOrder order = orderMapper.selectOne(queryWrapper);
|
|
|
if (order == null) {
|
|
|
- log.warn("訂單不存在,訂單ID: {}", orderId);
|
|
|
+ log.warn("訂單不存在,訂單no: {}", orderNum);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 只處理待支付狀態的訂單
|
|
|
if (order.getOrderStatus() != null && order.getOrderStatus() == 0) {
|
|
|
- log.info("訂單處於待支付狀態,開始取消訂單,訂單ID: {}", orderId);
|
|
|
-
|
|
|
+ log.info("訂單處於待支付狀態,開始取消訂單,訂單no: {}", orderNum);
|
|
|
+
|
|
|
+ LawyerConsultationOrder lawyerConsultationOrder = new LawyerConsultationOrder();
|
|
|
// 更新訂單狀態為已取消
|
|
|
- order.setOrderStatus(4); // 4:已取消
|
|
|
- order.setUpdatedTime(new java.util.Date());
|
|
|
+ lawyerConsultationOrder.setOrderStatus(4); // 4:已取消
|
|
|
+ lawyerConsultationOrder.setId(order.getId());
|
|
|
+ lawyerConsultationOrder.setUpdatedTime(new java.util.Date());
|
|
|
|
|
|
- boolean updated = orderService.updateById(order);
|
|
|
- if (updated) {
|
|
|
- log.info("訂單支付超時,已自動取消訂單,訂單ID: {}", orderId);
|
|
|
+ int updated = orderMapper.updateById(lawyerConsultationOrder);
|
|
|
+ if (updated > 0) {
|
|
|
+ log.info("訂單支付超時,已自動取消訂單,訂單no: {}", orderNum);
|
|
|
} else {
|
|
|
- log.error("取消訂單失敗,訂單ID: {}", orderId);
|
|
|
+ log.error("取消訂單失敗,訂單no: {}", orderNum);
|
|
|
}
|
|
|
} else {
|
|
|
- log.info("訂單狀態不是待支付,無需處理,訂單ID: {}, 當前狀態: {}", orderId, order.getOrderStatus());
|
|
|
+ log.info("訂單狀態不是待支付,無需處理,訂單no: {}, 當前狀態: {}", orderNum, order.getOrderStatus());
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.error("處理訂單支付超時失敗,訂單ID: {}", orderId, e);
|
|
|
+ log.error("處理訂單支付超時失敗,訂單no: {}", orderNum, e);
|
|
|
throw e;
|
|
|
}
|
|
|
}
|