Commit 8850df1a09b3b5dc904191a5fda8acf1c137ec1c

Authored by YunaiV
1 parent 567cbeae

【功能优化】商城:查询订单详情接口,增加 sync 主动同步支付状态

yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java
@@ -21,6 +21,7 @@ import cn.iocoder.yudao.module.trade.service.price.TradePriceService; @@ -21,6 +21,7 @@ import cn.iocoder.yudao.module.trade.service.price.TradePriceService;
21 import com.google.common.collect.Maps; 21 import com.google.common.collect.Maps;
22 import io.swagger.v3.oas.annotations.Operation; 22 import io.swagger.v3.oas.annotations.Operation;
23 import io.swagger.v3.oas.annotations.Parameter; 23 import io.swagger.v3.oas.annotations.Parameter;
  24 +import io.swagger.v3.oas.annotations.Parameters;
24 import io.swagger.v3.oas.annotations.tags.Tag; 25 import io.swagger.v3.oas.annotations.tags.Tag;
25 import jakarta.annotation.Resource; 26 import jakarta.annotation.Resource;
26 import jakarta.validation.Valid; 27 import jakarta.validation.Valid;
@@ -88,21 +89,32 @@ public class AppTradeOrderController { @@ -88,21 +89,32 @@ public class AppTradeOrderController {
88 89
89 @GetMapping("/get-detail") 90 @GetMapping("/get-detail")
90 @Operation(summary = "获得交易订单") 91 @Operation(summary = "获得交易订单")
91 - @Parameter(name = "id", description = "交易订单编号") 92 + @Parameters({
  93 + @Parameter(name = "id", description = "交易订单编号"),
  94 + @Parameter(name = "sync", description = "是否同步支付状态", example = "true")
  95 + })
92 @PreAuthenticated 96 @PreAuthenticated
93 - public CommonResult<AppTradeOrderDetailRespVO> getOrder(@RequestParam("id") Long id) {  
94 - // 查询订单 97 + public CommonResult<AppTradeOrderDetailRespVO> getOrderDetail(@RequestParam("id") Long id,
  98 + @RequestParam(value = "sync", required = false) Boolean sync) {
  99 + // 1.1 查询订单
95 TradeOrderDO order = tradeOrderQueryService.getOrder(getLoginUserId(), id); 100 TradeOrderDO order = tradeOrderQueryService.getOrder(getLoginUserId(), id);
96 if (order == null) { 101 if (order == null) {
97 return success(null); 102 return success(null);
98 } 103 }
  104 + // 1.2 sync 仅在等待支付
  105 + if (Boolean.TRUE.equals(sync)
  106 + && TradeOrderStatusEnum.isUnpaid(order.getStatus()) && !order.getPayStatus()) {
  107 + tradeOrderUpdateService.syncOrderPayStatusQuietly(order.getId(), order.getPayOrderId());
  108 + // 重新查询,因为同步后,可能会有变化
  109 + order = tradeOrderQueryService.getOrder(id);
  110 + }
99 111
100 - // 查询订单项 112 + // 2.1 查询订单项
101 List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(order.getId()); 113 List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(order.getId());
102 - // 查询物流公司 114 + // 2.2 查询物流公司
103 DeliveryExpressDO express = order.getLogisticsId() != null && order.getLogisticsId() > 0 ? 115 DeliveryExpressDO express = order.getLogisticsId() != null && order.getLogisticsId() > 0 ?
104 deliveryExpressService.getDeliveryExpress(order.getLogisticsId()) : null; 116 deliveryExpressService.getDeliveryExpress(order.getLogisticsId()) : null;
105 - // 最终组合 117 + // 2.3 最终组合
106 return success(TradeOrderConvert.INSTANCE.convert02(order, orderItems, tradeOrderProperties, express)); 118 return success(TradeOrderConvert.INSTANCE.convert02(order, orderItems, tradeOrderProperties, express));
107 } 119 }
108 120
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java
@@ -50,6 +50,17 @@ public interface TradeOrderUpdateService { @@ -50,6 +50,17 @@ public interface TradeOrderUpdateService {
50 void updateOrderPaid(Long id, Long payOrderId); 50 void updateOrderPaid(Long id, Long payOrderId);
51 51
52 /** 52 /**
  53 + * 同步订单的支付状态
  54 + *
  55 + * 1. Quietly 表示,即使同步失败,也不会抛出异常
  56 + * 2. 什么时候回出现异常?因为是主动同步,可能和支付模块的回调通知 {@link #updateOrderPaid(Long, Long)} 存在并发冲突,导致抛出异常
  57 + *
  58 + * @param id 订单编号
  59 + * @param payOrderId 支付订单编号
  60 + */
  61 + void syncOrderPayStatusQuietly(Long id, Long payOrderId);
  62 +
  63 + /**
53 * 【管理员】发货交易订单 64 * 【管理员】发货交易订单
54 * 65 *
55 * @param deliveryReqVO 发货请求 66 * @param deliveryReqVO 发货请求
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java
@@ -302,6 +302,22 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { @@ -302,6 +302,22 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
302 TradeOrderLogUtils.setUserInfo(order.getUserId(), UserTypeEnum.MEMBER.getValue()); 302 TradeOrderLogUtils.setUserInfo(order.getUserId(), UserTypeEnum.MEMBER.getValue());
303 } 303 }
304 304
  305 + @Override
  306 + public void syncOrderPayStatusQuietly(Long id, Long payOrderId) {
  307 + PayOrderRespDTO payOrder = payOrderApi.getOrder(payOrderId);
  308 + if (payOrder == null) {
  309 + return;
  310 + }
  311 + if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) {
  312 + return;
  313 + }
  314 + try {
  315 + getSelf().updateOrderPaid(id, payOrderId);
  316 + } catch (Throwable e) {
  317 + log.warn("[syncOrderPayStatusQuietly][id({}) payOrderId({}) 同步支付状态失败]", id, payOrderId, e);
  318 + }
  319 + }
  320 +
305 /** 321 /**
306 * 校验支付订单的合法性 322 * 校验支付订单的合法性
307 * 323 *