订单关闭bug-update
This commit is contained in:
3
doc/sql/pending/user_order_update.sql
Normal file
3
doc/sql/pending/user_order_update.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE INDEX idx_user_order_search ON user_order (payment_status, order_status, payment_type, create_time);
|
||||
|
||||
update user_order set payment_status = 0 , order_status = 0 where payment_status = 2 and order_status = 5 and payment_type = 99;
|
||||
@@ -48,16 +48,15 @@ public class AliUserOrderCloseProcessor implements BasicProcessor {
|
||||
Date expireTime = DateUtil.getMinutesLate(new Date(), 30);
|
||||
AliPayAccountConfig config = PaymentCache.getAliConfig();
|
||||
|
||||
int batchSize = 500; // 【优化点3】分批处理,防止内存溢出
|
||||
int currentCount = 0;
|
||||
int batchSize = 500;
|
||||
|
||||
while (true) {
|
||||
// 2. 分批查询数据库
|
||||
// 【优化点1】增加 orderByAsc 保证按时间顺序处理,增加 last("LIMIT") 限制数量
|
||||
|
||||
List<CommonUserOrderEntity> payOrderList = commonUserOrderService.list(
|
||||
Wrappers.lambdaQuery(CommonUserOrderEntity.class)
|
||||
.eq(CommonUserOrderEntity::getPaymentStatus, PaymentStatus.UNPAID.getCode()) // 确保传的是 code 值,如果是枚举对象需调整
|
||||
.eq(CommonUserOrderEntity::getPaymentStatus, PaymentStatus.UNPAID.getCode())
|
||||
.eq(CommonUserOrderEntity::getOrderStatus, OrderStatus.PENDING_PAYMENT.getCode())
|
||||
.in(CommonUserOrderEntity::getPaymentType, List.of(1,2)) // 只查询支付宝和微信
|
||||
.le(CommonUserOrderEntity::getCreateTime, expireTime)
|
||||
.orderByAsc(CommonUserOrderEntity::getCreateTime)
|
||||
.last("LIMIT " + batchSize)
|
||||
@@ -91,21 +90,14 @@ public class AliUserOrderCloseProcessor implements BasicProcessor {
|
||||
* 核心原则:先远程交互,成功后再开短事务更新本地
|
||||
*/
|
||||
private void processSingleOrder(CommonUserOrderEntity payOrder , AliPayAccountConfig config) {
|
||||
Long orderId = payOrder.getId();
|
||||
String orderNo = PaymentType.fromCodeOrNull(payOrder.getPaymentType()).getAbbr() + BusinessModule.fromCodeIgnoreCaseOrNull(payOrder.getOrderType()).getAbbr() + payOrder.getId();
|
||||
log.info("处理超时订单,订单号: {}", orderNo);
|
||||
if (payOrder == null) {
|
||||
log.error("关联用户订单不存在,orderId: {}", orderId);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
AlipayTradeQueryResponse response = paymentService.aliQuery(orderNo);
|
||||
log.info("查询结果: {}", response);
|
||||
|
||||
|
||||
if (response == null) {
|
||||
updateOrderToClosed(payOrder);
|
||||
commonUserOrderService.closeOrderById(payOrder.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -119,26 +111,11 @@ public class AliUserOrderCloseProcessor implements BasicProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
updateOrderToClosed(payOrder);
|
||||
commonUserOrderService.closeOrderById(payOrder.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 事务方法:更新为关闭状态
|
||||
* 使用 update(entity, wrapper) 方式实现乐观锁检查
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateOrderToClosed( CommonUserOrderEntity userOrder) {
|
||||
// 支付单更新成功,再更新用户订单
|
||||
userOrder.setOrderStatus(OrderStatus.CLOSED_TIMEOUT.getCode());
|
||||
userOrder.setPaymentStatus(PaymentStatus.FAILED.getCode());
|
||||
// userOrder.setUpdateTime(new Date());
|
||||
commonUserOrderService.updateById(userOrder);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -103,4 +103,6 @@ public interface ICommonUserOrderService extends IService<CommonUserOrderEntity>
|
||||
|
||||
|
||||
boolean confirmItinerary(Long id);
|
||||
|
||||
boolean closeOrderById(Long id);
|
||||
}
|
||||
|
||||
@@ -591,6 +591,25 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
|
||||
return true;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean closeOrderById(Long id) {
|
||||
CommonUserOrderEntity userOrder = getById(id);
|
||||
if (userOrder == null) {
|
||||
throw new ServiceException("无效订单");
|
||||
}
|
||||
if (userOrder.getOrderStatus().equals(OrderStatus.CLOSED_TIMEOUT.getCode())) {
|
||||
return true;
|
||||
}
|
||||
if (userOrder.getPaymentStatus().equals(PaymentStatus.FAILED.getCode())) {
|
||||
return true;
|
||||
}
|
||||
userOrder.setOrderStatus(OrderStatus.CLOSED_TIMEOUT.getCode());
|
||||
userOrder.setPaymentStatus(PaymentStatus.FAILED.getCode());
|
||||
return updateById(userOrder);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void validateCertificates(CommonTourInfoEntity traveler, TourProductEntity product) {
|
||||
String name = Optional.ofNullable(traveler.getUserName()).orElse("未知用户");
|
||||
|
||||
Reference in New Issue
Block a user