From 8850df1a09b3b5dc904191a5fda8acf1c137ec1c Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 24 Sep 2024 12:44:45 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E3=80=91=E5=95=86=E5=9F=8E=EF=BC=9A=E6=9F=A5=E8=AF=A2=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E8=AF=A6=E6=83=85=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20sync=20=E4=B8=BB=E5=8A=A8=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/order/AppTradeOrderController.java | 24 ++++++++++++++----- .../order/TradeOrderUpdateService.java | 11 +++++++++ .../order/TradeOrderUpdateServiceImpl.java | 16 +++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java index 4359feb07..2871ac315 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java +++ b/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; import com.google.common.collect.Maps; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.validation.Valid; @@ -88,21 +89,32 @@ public class AppTradeOrderController { @GetMapping("/get-detail") @Operation(summary = "获得交易订单") - @Parameter(name = "id", description = "交易订单编号") + @Parameters({ + @Parameter(name = "id", description = "交易订单编号"), + @Parameter(name = "sync", description = "是否同步支付状态", example = "true") + }) @PreAuthenticated - public CommonResult getOrder(@RequestParam("id") Long id) { - // 查询订单 + public CommonResult getOrderDetail(@RequestParam("id") Long id, + @RequestParam(value = "sync", required = false) Boolean sync) { + // 1.1 查询订单 TradeOrderDO order = tradeOrderQueryService.getOrder(getLoginUserId(), id); if (order == null) { return success(null); } + // 1.2 sync 仅在等待支付 + if (Boolean.TRUE.equals(sync) + && TradeOrderStatusEnum.isUnpaid(order.getStatus()) && !order.getPayStatus()) { + tradeOrderUpdateService.syncOrderPayStatusQuietly(order.getId(), order.getPayOrderId()); + // 重新查询,因为同步后,可能会有变化 + order = tradeOrderQueryService.getOrder(id); + } - // 查询订单项 + // 2.1 查询订单项 List orderItems = tradeOrderQueryService.getOrderItemListByOrderId(order.getId()); - // 查询物流公司 + // 2.2 查询物流公司 DeliveryExpressDO express = order.getLogisticsId() != null && order.getLogisticsId() > 0 ? deliveryExpressService.getDeliveryExpress(order.getLogisticsId()) : null; - // 最终组合 + // 2.3 最终组合 return success(TradeOrderConvert.INSTANCE.convert02(order, orderItems, tradeOrderProperties, express)); } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java index 56b7cbc56..55a696e2a 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java @@ -49,6 +49,17 @@ public interface TradeOrderUpdateService { */ void updateOrderPaid(Long id, Long payOrderId); + /** + * 同步订单的支付状态 + * + * 1. Quietly 表示,即使同步失败,也不会抛出异常 + * 2. 什么时候回出现异常?因为是主动同步,可能和支付模块的回调通知 {@link #updateOrderPaid(Long, Long)} 存在并发冲突,导致抛出异常 + * + * @param id 订单编号 + * @param payOrderId 支付订单编号 + */ + void syncOrderPayStatusQuietly(Long id, Long payOrderId); + /** * 【管理员】发货交易订单 * diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java index 76ac2f204..e0c303ed3 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java +++ b/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 { TradeOrderLogUtils.setUserInfo(order.getUserId(), UserTypeEnum.MEMBER.getValue()); } + @Override + public void syncOrderPayStatusQuietly(Long id, Long payOrderId) { + PayOrderRespDTO payOrder = payOrderApi.getOrder(payOrderId); + if (payOrder == null) { + return; + } + if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) { + return; + } + try { + getSelf().updateOrderPaid(id, payOrderId); + } catch (Throwable e) { + log.warn("[syncOrderPayStatusQuietly][id({}) payOrderId({}) 同步支付状态失败]", id, payOrderId, e); + } + } + /** * 校验支付订单的合法性 *