From 67809be46d94a124d0bb7a92171f1f672072a791 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Fri, 4 Oct 2024 13:46:20 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E3=80=91=E5=95=86=E5=9F=8E:=20APP=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E7=A7=AF=E5=88=86=E5=95=86=E5=9F=8E=E6=B4=BB=E5=8A=A8=E4=B8=8B?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../promotion/api/point/PointActivityApi.java | 17 +++++ .../api/point/PointActivityApiImpl.java | 10 +++ .../dal/mysql/point/PointActivityMapper.java | 31 +++++++++ .../dal/mysql/point/PointProductMapper.java | 29 +++++++++ .../service/point/PointActivityService.java | 18 ++++++ .../point/PointActivityServiceImpl.java | 37 +++++++++++ .../seckill/SeckillActivityServiceImpl.java | 2 +- .../trade/enums/order/TradeOrderTypeEnum.java | 4 ++ .../dal/dataobject/order/TradeOrderDO.java | 7 ++ .../order/handler/TradePointOrderHandler.java | 64 +++++++++++++++++++ .../TradePointActivityPriceCalculator.java | 7 +- 11 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradePointOrderHandler.java diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApi.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApi.java index 6e4250c67..0b612cf21 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApi.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApi.java @@ -21,5 +21,22 @@ public interface PointActivityApi { */ PointValidateJoinRespDTO validateJoinPointActivity(Long activityId, Long skuId, Integer count); + /** + * 更新积分商城商品库存(减少) + * + * @param id 活动编号 + * @param skuId sku 编号 + * @param count 数量(正数) + */ + void updatePointStockDecr(Long id, Long skuId, Integer count); + + /** + * 更新积分商城商品库存(增加) + * + * @param id 活动编号 + * @param skuId sku 编号 + * @param count 数量(正数) + */ + void updatePointStockIncr(Long id, Long skuId, Integer count); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApiImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApiImpl.java index 729551797..e97e54c8d 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApiImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/point/PointActivityApiImpl.java @@ -23,4 +23,14 @@ public class PointActivityApiImpl implements PointActivityApi { return pointActivityService.validateJoinPointActivity(activityId, skuId, count); } + @Override + public void updatePointStockDecr(Long id, Long skuId, Integer count) { + pointActivityService.updatePointStockDecr(id, skuId, count); + } + + @Override + public void updatePointStockIncr(Long id, Long skuId, Integer count) { + pointActivityService.updatePointStockIncr(id, skuId, count); + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointActivityMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointActivityMapper.java index 46db5841e..d724c32ee 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointActivityMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointActivityMapper.java @@ -1,10 +1,12 @@ package cn.iocoder.yudao.module.promotion.dal.mysql.point; +import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO; import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointActivityDO; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import org.apache.ibatis.annotations.Mapper; /** @@ -25,4 +27,33 @@ public interface PointActivityMapper extends BaseMapperX { .orderByDesc(PointActivityDO::getId)); } + /** + * 更新活动库存(减少) + * + * @param id 活动编号 + * @param count 扣减的库存数量(正数) + * @return 影响的行数 + */ + default int updateStockDecr(Long id, int count) { + Assert.isTrue(count > 0); + return update(null, new LambdaUpdateWrapper() + .eq(PointActivityDO::getId, id) + .ge(PointActivityDO::getStock, count) + .setSql("stock = stock - " + count)); + } + + /** + * 更新活动库存(增加) + * + * @param id 活动编号 + * @param count 增加的库存数量(正数) + * @return 影响的行数 + */ + default int updateStockIncr(Long id, int count) { + Assert.isTrue(count > 0); + return update(null, new LambdaUpdateWrapper() + .eq(PointActivityDO::getId, id) + .setSql("stock = stock + " + count)); + } + } \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointProductMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointProductMapper.java index 1169d8f53..3c6d469a2 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointProductMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/point/PointProductMapper.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.promotion.dal.mysql.point; +import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointProductDO; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; @@ -34,4 +35,32 @@ public interface PointProductMapper extends BaseMapperX { PointProductDO::getSkuId, skuId); } + /** + * 更新活动库存(减少) + * + * @param id 活动编号 + * @param count 扣减的库存数量(减少库存) + * @return 影响的行数 + */ + default int updateStockDecr(Long id, int count) { + Assert.isTrue(count > 0); + return update(null, new LambdaUpdateWrapper() + .eq(PointProductDO::getId, id) + .ge(PointProductDO::getStock, count) + .setSql("stock = stock - " + count)); + } + + /** + * 更新活动库存(增加) + * + * @param id 活动编号 + * @param count 需要增加的库存(增加库存) + * @return 影响的行数 + */ + default int updateStockIncr(Long id, int count) { + Assert.isTrue(count > 0); + return update(null, new LambdaUpdateWrapper() + .eq(PointProductDO::getId, id) + .setSql("stock = stock + " + count)); + } } \ No newline at end of file diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityService.java index f93a89f3e..b2b039570 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityService.java @@ -33,6 +33,24 @@ public interface PointActivityService { */ void updatePointActivity(@Valid PointActivitySaveReqVO updateReqVO); + /** + * 更新积分商城商品库存(减少) + * + * @param id 活动编号 + * @param skuId sku 编号 + * @param count 数量(正数) + */ + void updatePointStockDecr(Long id, Long skuId, Integer count); + + /** + * 更新积分商城商品库存(增加) + * + * @param id 活动编号 + * @param skuId sku 编号 + * @param count 数量(正数) + */ + void updatePointStockIncr(Long id, Long skuId, Integer count); + /** * 关闭积分商城活动 * diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityServiceImpl.java index cd03bc45d..8e88c5026 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityServiceImpl.java @@ -103,6 +103,43 @@ public class PointActivityServiceImpl implements PointActivityService { updateSeckillProduct(updateObj, updateReqVO.getProducts()); } + @Override + @Transactional(rollbackFor = Exception.class) + public void updatePointStockDecr(Long id, Long skuId, Integer count) { + // 1.1 校验活动库存是否充足 + PointActivityDO activity = validatePointActivityExists(id); + if (count > activity.getStock()) { + throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL); + } + // 1.2 校验商品库存是否充足 + PointProductDO product = pointProductMapper.selectListByActivityIdAndSkuId(id, skuId); + if (product == null || count > product.getStock()) { + throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL); + } + + // 2.1 更新活动商品库存 + int updateCount = pointProductMapper.updateStockDecr(product.getId(), count); + if (updateCount == 0) { + throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL); + } + + // 2.2 更新活动库存 + updateCount = pointActivityMapper.updateStockDecr(id, count); + if (updateCount == 0) { + throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL); + } + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updatePointStockIncr(Long id, Long skuId, Integer count) { + PointProductDO product = pointProductMapper.selectListByActivityIdAndSkuId(id, skuId); + // 更新活动商品库存 + pointProductMapper.updateStockIncr(product.getId(), count); + // 更新活动库存 + pointActivityMapper.updateStockIncr(id, count); + } + @Override @Transactional(rollbackFor = Exception.class) public void closePointActivity(Long id) { diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java index 3b7ab3d64..dd8495ae5 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java @@ -160,7 +160,7 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { public void updateSeckillStockDecr(Long id, Long skuId, Integer count) { // 1.1 校验活动库存是否充足 SeckillActivityDO seckillActivity = validateSeckillActivityExists(id); - if (count > seckillActivity.getTotalStock()) { + if (count > seckillActivity.getStock()) { throw exception(SECKILL_ACTIVITY_UPDATE_STOCK_FAIL); } // 1.2 校验商品库存是否充足 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 0e6aad735..0fccebb47 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 @@ -55,4 +55,8 @@ public enum TradeOrderTypeEnum implements IntArrayValuable { return ObjectUtil.equal(type, COMBINATION.getType()); } + public static boolean isPoint(Integer type) { + return ObjectUtil.equal(type, POINT.getType()); + } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java index 399b692ed..4c06c8013 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java @@ -353,4 +353,11 @@ public class TradeOrderDO extends BaseDO { */ private Long combinationRecordId; + /** + * 积分商城活动的编号 + * + * 关联 PointActivityDO 的 id 字段 + */ + private Long pointActivityId; + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradePointOrderHandler.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradePointOrderHandler.java new file mode 100644 index 000000000..11ca73f6b --- /dev/null +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradePointOrderHandler.java @@ -0,0 +1,64 @@ +package cn.iocoder.yudao.module.trade.service.order.handler; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.Assert; +import cn.iocoder.yudao.module.promotion.api.point.PointActivityApi; +import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO; +import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO; +import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 积分商城活动订单的 {@link TradeOrderHandler} 实现类 + * + * @author HUIHUI + */ +@Component +public class TradePointOrderHandler implements TradeOrderHandler { + + @Resource + private PointActivityApi pointActivityApi; + + @Override + public void beforeOrderCreate(TradeOrderDO order, List orderItems) { + if (!TradeOrderTypeEnum.isPoint(order.getType())) { + return; + } + // 明确校验一下 + Assert.isTrue(orderItems.size() == 1, "积分商城活动兑换商品兑换时,只允许选择一个商品"); + + // 扣减积分商城活动的库存 + pointActivityApi.updatePointStockDecr(order.getPointActivityId(), + orderItems.get(0).getSkuId(), orderItems.get(0).getCount()); + } + + @Override + public void afterCancelOrder(TradeOrderDO order, List orderItems) { + if (!TradeOrderTypeEnum.isPoint(order.getType())) { + return; + } + // 明确校验一下 + Assert.isTrue(orderItems.size() == 1, "积分商城活动兑换商品兑换时,只允许选择一个商品"); + + // 售后的订单项,已经在 afterCancelOrderItem 回滚库存,所以这里不需要重复回滚 + orderItems = filterOrderItemListByNoneAfterSale(orderItems); + if (CollUtil.isEmpty(orderItems)) { + return; + } + afterCancelOrderItem(order, orderItems.get(0)); + } + + @Override + public void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) { + if (!TradeOrderTypeEnum.isPoint(order.getType())) { + return; + } + // 恢复积分商城活动的库存 + pointActivityApi.updatePointStockIncr(order.getPointActivityId(), + orderItem.getSkuId(), orderItem.getCount()); + } + +} diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointActivityPriceCalculator.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointActivityPriceCalculator.java index 3bec8e343..158eaa3d9 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointActivityPriceCalculator.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointActivityPriceCalculator.java @@ -63,15 +63,18 @@ public class TradePointActivityPriceCalculator implements TradePriceCalculator { // 3.1 记录优惠明细 int discountPrice = orderItem.getPayPrice(); // 情况一:单使用积分兑换 Assert.isTrue(activity.getPoint() >= 1, "积分商城商品兑换积分必须大于 1"); - result.setUsePoint(activity.getPoint()); + result.setUsePoint(activity.getPoint() * orderItem.getCount()); + orderItem.setUsePoint(activity.getPoint() * orderItem.getCount()); if (activity.getPrice() != null && activity.getPrice() > 0) { // 情况二:积分 + 金额 discountPrice = orderItem.getPayPrice() - activity.getPrice() * orderItem.getCount(); } + // 3.2 记录优惠明细 TradePriceCalculatorHelper.addPromotion(result, orderItem, param.getPointActivityId(), "积分商城活动", PromotionTypeEnum.POINT.getType(), StrUtil.format("积分商城活动:省 {} 元", TradePriceCalculatorHelper.formatPrice(discountPrice)), discountPrice); - // 3.2 更新 SKU 优惠金额 + + // 3.3 更新 SKU 优惠金额 orderItem.setDiscountPrice(orderItem.getDiscountPrice() + discountPrice); TradePriceCalculatorHelper.recountPayPrice(orderItem); TradePriceCalculatorHelper.recountAllPrice(result);