From 89b240c1f946d7b6035fa66a5ad70be43a43d3e4 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Sat, 7 Oct 2023 16:58:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=EF=BC=9A=E4=BB=8B=E5=85=A5?= =?UTF-8?q?=20TradeOrderHandler#cancelOrder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../trade/enums/order/TradeOrderTypeEnum.java | 17 +++++++++++++++++ .../order/TradeOrderUpdateServiceImpl.java | 10 +++++----- .../order/handler/TradeBargainHandler.java | 15 +++++++++++---- .../order/handler/TradeCombinationHandler.java | 12 +++++++++--- .../order/handler/TradeOrderHandler.java | 6 +++++- .../order/handler/TradeSeckillHandler.java | 12 ++++++++++-- 6 files changed, 57 insertions(+), 15 deletions(-) diff --git a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/order/TradeOrderTypeEnum.java b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/order/TradeOrderTypeEnum.java index 27596197f..f82071206 100644 --- a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/order/TradeOrderTypeEnum.java +++ b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/order/TradeOrderTypeEnum.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.trade.enums.order; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.core.IntArrayValuable; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -37,4 +38,20 @@ public enum TradeOrderTypeEnum implements IntArrayValuable { return ARRAYS; } + public static boolean isNormal(Integer type) { + return ObjectUtil.equal(type, NORMAL.getType()); + } + + public static boolean isSeckill(Integer type) { + return ObjectUtil.equal(type, SECKILL.getType()); + } + + public static boolean isBargain(Integer type) { + return ObjectUtil.equal(type, BARGAIN.getType()); + } + + public static boolean isCombination(Integer type) { + return ObjectUtil.equal(type, COMBINATION.getType()); + } + } 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 a0bd13f44..7c5ed91b3 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 @@ -625,12 +625,11 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { throw exception(ORDER_CANCEL_FAIL_STATUS_NOT_UNPAID); } - // 2. TODO 活动相关库存回滚需要活动 id,活动 id 怎么获取?app 端能否传过来;回复:从订单里拿呀 - tradeOrderHandlers.forEach(handler -> handler.cancelOrder()); - - // 3. 回滚库存 List orderItems = tradeOrderItemMapper.selectListByOrderId(id); + // 3. 回滚库存 productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(orderItems)); + // 3.1、 活动相关的回滚 + tradeOrderHandlers.forEach(handler -> handler.cancelOrder(order, orderItems)); // 4. 回滚优惠券 if (order.getCouponId() != null && order.getCouponId() > 0) { @@ -807,10 +806,11 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { // 2.2 如果全部退款,则进行取消订单 getSelf().cancelOrderByAfterSale(order, orderRefundPrice); - // TODO @puhui999:活动相关的回滚 // 3. 回滚库存 productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(Collections.singletonList(orderItem))); + // 3.1、 活动相关的回滚 + tradeOrderHandlers.forEach(handler -> handler.cancelOrder(order, Collections.singletonList(orderItem))); // 4.1 回滚积分:扣减用户积分(赠送的) reduceUserPoint(order.getUserId(), orderItem.getGivePoint(), MemberPointBizTypeEnum.AFTER_SALE_DEDUCT_GIVE, orderItem.getAfterSaleId()); diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeBargainHandler.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeBargainHandler.java index fc59d2d24..b83d2771b 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeBargainHandler.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeBargainHandler.java @@ -1,6 +1,5 @@ package cn.iocoder.yudao.module.trade.service.order.handler; -import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.module.promotion.api.bargain.BargainActivityApi; import cn.iocoder.yudao.module.promotion.api.bargain.BargainRecordApi; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO; @@ -26,9 +25,10 @@ public class TradeBargainHandler implements TradeOrderHandler { @Override public void beforeOrderCreate(TradeOrderDO order, List orderItems) { - if (ObjectUtil.notEqual(TradeOrderTypeEnum.BARGAIN.getType(), order.getType())) { + if (TradeOrderTypeEnum.isBargain(order.getType())) { return; } + // 扣减砍价活动的库存 bargainActivityApi.updateBargainActivityStock(order.getBargainActivityId(), -orderItems.get(0).getCount()); @@ -36,13 +36,20 @@ public class TradeBargainHandler implements TradeOrderHandler { @Override public void afterOrderCreate(TradeOrderDO order, List orderItems) { - if (ObjectUtil.notEqual(TradeOrderTypeEnum.BARGAIN.getType(), order.getType())) { + if (TradeOrderTypeEnum.isBargain(order.getType())) { return; } + // 记录砍价记录对应的订单编号 bargainRecordApi.updateBargainRecordOrderId(order.getBargainRecordId(), order.getId()); } - // TODO 芋艿:取消订单时,需要增加库存 + @Override + public void cancelOrder(TradeOrderDO order, List orderItems) { + if (TradeOrderTypeEnum.isBargain(order.getType())) { + return; + } + // TODO 芋艿:取消订单时,需要增加库存 + } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeCombinationHandler.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeCombinationHandler.java index f34da9616..a76596dd3 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeCombinationHandler.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeCombinationHandler.java @@ -1,7 +1,6 @@ package cn.iocoder.yudao.module.trade.service.order.handler; import cn.hutool.core.lang.Assert; -import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.module.promotion.api.combination.CombinationRecordApi; import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO; @@ -26,7 +25,7 @@ public class TradeCombinationHandler implements TradeOrderHandler { @Override public void beforeOrderCreate(TradeOrderDO order, List orderItems) { // 如果不是拼团订单则结束 - if (ObjectUtil.notEqual(TradeOrderTypeEnum.COMBINATION.getType(), order.getType())) { + if (TradeOrderTypeEnum.isCombination(order.getType())) { return; } Assert.isTrue(orderItems.size() == 1, "拼团时,只允许选择一个商品"); @@ -41,7 +40,7 @@ public class TradeCombinationHandler implements TradeOrderHandler { @Override public void afterPayOrder(TradeOrderDO order, List orderItems) { // 如果不是拼团订单则结束 - if (ObjectUtil.notEqual(TradeOrderTypeEnum.COMBINATION.getType(), order.getType())) { + if (TradeOrderTypeEnum.isCombination(order.getType())) { return; } @@ -53,4 +52,11 @@ public class TradeCombinationHandler implements TradeOrderHandler { combinationRecordApi.createCombinationRecord(TradeOrderConvert.INSTANCE.convert(order, item)); } + @Override + public void cancelOrder(TradeOrderDO order, List orderItems) { + if (TradeOrderTypeEnum.isCombination(order.getType())) { + return; + } + } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeOrderHandler.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeOrderHandler.java index 81c41650a..646ebd1b7 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeOrderHandler.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeOrderHandler.java @@ -40,7 +40,11 @@ public interface TradeOrderHandler { /** * 订单取消 + * + * @param order 订单 + * @param orderItems 订单项 */ - default void cancelOrder() {} + default void cancelOrder(TradeOrderDO order, List orderItems) { + } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeSeckillHandler.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeSeckillHandler.java index e9cf974b6..b9bd3e4bd 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeSeckillHandler.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeSeckillHandler.java @@ -1,6 +1,5 @@ package cn.iocoder.yudao.module.trade.service.order.handler; -import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.module.promotion.api.seckill.SeckillActivityApi; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO; @@ -23,12 +22,21 @@ public class TradeSeckillHandler implements TradeOrderHandler { @Override public void beforeOrderCreate(TradeOrderDO order, List orderItems) { - if (ObjectUtil.notEqual(TradeOrderTypeEnum.SECKILL.getType(), order.getType())) { + if (TradeOrderTypeEnum.isSeckill(order.getType())) { return; } + // 扣减秒杀活动的库存 seckillActivityApi.updateSeckillStock(order.getSeckillActivityId(), orderItems.get(0).getSkuId(), orderItems.get(0).getCount()); } + @Override + public void cancelOrder(TradeOrderDO order, List orderItems) { + if (TradeOrderTypeEnum.isSeckill(order.getType())) { + return; + } + + } + }