From aa5525d1f58aa2e39731cbd6461a1e18c9c57b81 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 23 Sep 2023 22:21:23 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=EF=BC=9A=E8=90=A5=E9=94=80?= =?UTF-8?q?=E6=B4=BB=E5=8A=A8=20&=20=E4=BA=A4=E6=98=93=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/bargain/BargainActivityApi.java | 2 +- .../mysql/bargain/BargainActivityMapper.java | 2 +- .../SeckillActivityMapper.java | 2 +- .../seckillactivity/SeckillProductMapper.java | 11 ++++++-- .../bargain/BargainActivityServiceImpl.java | 6 ++-- .../CombinationRecordServiceImpl.java | 6 ++++ .../seckill/SeckillActivityServiceImpl.java | 28 ++++++++----------- .../order/TradeOrderQueryServiceImpl.java | 4 +-- .../order/handler/TradeBargainHandler.java | 7 +++-- .../order/handler/TradeOrderHandler.java | 3 ++ .../order/handler/TradeSeckillHandler.java | 6 +++- 11 files changed, 47 insertions(+), 30 deletions(-) diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/bargain/BargainActivityApi.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/bargain/BargainActivityApi.java index eb912e417..0f9812629 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/bargain/BargainActivityApi.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/bargain/BargainActivityApi.java @@ -11,7 +11,7 @@ public interface BargainActivityApi { * 更新砍价活动库存 * * @param id 砍价活动编号 - * @param count 购买数量 + * @param count 购买数量 */ void updateBargainActivityStock(Long id, Integer count); diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainActivityMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainActivityMapper.java index a7fe3e58d..de518f60c 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainActivityMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainActivityMapper.java @@ -38,7 +38,7 @@ public interface BargainActivityMapper extends BaseMapperX { * @param count 扣减的库存数量 * @return 影响的行数 */ - default int updateActivityStock(Long id, int count) { + default int updateStock(Long id, int count) { return update(null, new LambdaUpdateWrapper() .eq(BargainActivityDO::getId, id) .ge(BargainActivityDO::getStock, count) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java index 8ce12092f..7e2afdc85 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java @@ -41,7 +41,7 @@ public interface SeckillActivityMapper extends BaseMapperX { * @param count 扣减的库存数量 * @return 影响的行数 */ - default int updateActivityStock(Long id, int count) { + default int updateStock(Long id, int count) { return update(null, new LambdaUpdateWrapper() .eq(SeckillActivityDO::getId, id) .gt(SeckillActivityDO::getTotalStock, 0) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java index 5ca2dae24..db34aa744 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java @@ -16,8 +16,13 @@ import java.util.List; @Mapper public interface SeckillProductMapper extends BaseMapperX { - default List selectListByActivityId(Long id) { - return selectList(SeckillProductDO::getActivityId, id); + default List selectListByActivityId(Long activityId) { + return selectList(SeckillProductDO::getActivityId, activityId); + } + + default SeckillProductDO selectByActivityIdAndSkuId(Long activityId, Long skuId) { + return selectOne(SeckillProductDO::getActivityId, activityId, + SeckillProductDO::getSkuId, skuId); } default List selectListByActivityId(Collection ids) { @@ -31,7 +36,7 @@ public interface SeckillProductMapper extends BaseMapperX { * @param count 扣减的库存数量 * @return 影响的行数 */ - default int updateActivityStock(Long id, int count) { + default int updateStock(Long id, int count) { return update(null, new LambdaUpdateWrapper() .eq(SeckillProductDO::getId, id) .gt(SeckillProductDO::getStock, count) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityServiceImpl.java index 95e50eff5..546b95041 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityServiceImpl.java @@ -75,16 +75,18 @@ public class BargainActivityServiceImpl implements BargainActivityService { } @Override - @Transactional(rollbackFor = Exception.class) public void updateBargainActivityStock(Long id, Integer count) { // 查询砍价活动 BargainActivityDO activity = getBargainActivity(id); if (activity == null) { throw exception(BARGAIN_ACTIVITY_NOT_EXISTS); } + if (count > activity.getStock()) { + throw exception(BARGAIN_ACTIVITY_UPDATE_STOCK_FAIL); + } // 更新砍价库存 - int updateCount = bargainActivityMapper.updateActivityStock(id, count); + int updateCount = bargainActivityMapper.updateStock(id, count); if (updateCount == 0) { throw exception(BARGAIN_ACTIVITY_UPDATE_STOCK_FAIL); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationRecordServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationRecordServiceImpl.java index bb62295b8..047e060a9 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationRecordServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationRecordServiceImpl.java @@ -96,12 +96,15 @@ public class CombinationRecordServiceImpl implements CombinationRecordService { return recordDO; } + // TODO @puhui999:有一个应该在创建那要做下;就是当前 activityId 已经有未支付的订单,不允许在发起新的;要么支付,要么去掉先; + @Override @Transactional(rollbackFor = Exception.class) public void createCombinationRecord(CombinationRecordCreateReqDTO reqDTO) { // 1.1 校验拼团活动 CombinationActivityDO activity = combinationActivityService.validateCombinationActivityExists(reqDTO.getActivityId()); // 1.2 需要校验下,他当前是不是已经参加了该拼团; + // TODO @puhui999:拼团应该可以重复参加;应该去校验总共的上限哈,就是 activity.totalLimitCount CombinationRecordDO recordDO = recordMapper.selectByUserIdAndOrderId(reqDTO.getUserId(), reqDTO.getOrderId()); if (recordDO != null) { throw exception(COMBINATION_RECORD_EXISTS); @@ -111,6 +114,7 @@ public class CombinationRecordServiceImpl implements CombinationRecordService { if (CollUtil.isNotEmpty(recordDOList)) { throw exception(COMBINATION_RECORD_FAILED_HAVE_JOINED); } + // TODO @puhui999:有个开始时间未校验 // 1.4 校验当前活动是否过期 if (LocalDateTime.now().isAfter(activity.getEndTime())) { throw exception(COMBINATION_RECORD_FAILED_TIME_END); @@ -128,6 +132,8 @@ public class CombinationRecordServiceImpl implements CombinationRecordService { } } + // TODO @puhui999:单次限购 + // 2. 创建拼团记录 MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId()); ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId()); 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 0092cf1b8..6e05191bf 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 @@ -149,31 +149,25 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { @Override @Transactional(rollbackFor = Exception.class) public void updateSeckillStock(Long id, Long skuId, Integer count) { - // 1、校验秒杀活动是否存在 + // 1.1 校验活动库存是否充足 SeckillActivityDO seckillActivity = getSeckillActivity(id); - // 1.1、校验库存是否充足 - if (seckillActivity.getTotalStock() < count) { + if (count > seckillActivity.getTotalStock()) { + throw exception(SECKILL_ACTIVITY_UPDATE_STOCK_FAIL); + } + // 1.2 校验商品库存是否充足 + SeckillProductDO product = seckillProductMapper.selectByActivityIdAndSkuId(id, skuId); + if (product == null || count > product.getStock()) { throw exception(SECKILL_ACTIVITY_UPDATE_STOCK_FAIL); } - // 2、获取活动商品 - List products = getSeckillProductListByActivityId(id); - // 2.1、过滤出购买的商品 - SeckillProductDO product = findFirst(products, item -> ObjectUtil.equal(skuId, item.getSkuId())); - // 2.2、检查活动商品库存是否充足 - boolean isSufficient = product == null || (product.getStock() == 0 || (product.getStock() < count) || (product.getStock() - count) < 0); - if (isSufficient) { - throw exception(SECKILL_ACTIVITY_UPDATE_STOCK_FAIL); - } - - // 3、更新活动商品库存 - int updateCount = seckillProductMapper.updateActivityStock(product.getId(), count); + // 2.1 更新活动商品库存 + int updateCount = seckillProductMapper.updateStock(product.getId(), count); if (updateCount == 0) { throw exception(SECKILL_ACTIVITY_UPDATE_STOCK_FAIL); } - // 4、更新活动库存 - updateCount = seckillActivityMapper.updateActivityStock(seckillActivity.getId(), count); + // 2.2 更新活动库存 + updateCount = seckillActivityMapper.updateStock(seckillActivity.getId(), count); if (updateCount == 0) { throw exception(SECKILL_ACTIVITY_UPDATE_STOCK_FAIL); } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java index 733ec9780..910e8c911 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java @@ -106,7 +106,7 @@ public class TradeOrderQueryServiceImpl implements TradeOrderQueryService { if (order == null) { throw exception(ORDER_NOT_FOUND); } - + // 查询物流 return getExpressTrackList(order); } @@ -117,7 +117,7 @@ public class TradeOrderQueryServiceImpl implements TradeOrderQueryService { if (order == null) { throw exception(ORDER_NOT_FOUND); } - + // 查询物流 return getExpressTrackList(order); } 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 72def213d..18a276fde 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 @@ -20,14 +20,17 @@ public class TradeBargainHandler implements TradeOrderHandler { @Resource private BargainActivityApi bargainActivityApi; + // TODO @puhui999:先临时写在这里;在价格计算时,如果是秒杀商品,需要校验如下条件: + // 1. 商品存在、库存充足、单次限购; + // 2. 活动进行中、时间段符合 + @Override public void beforeOrderCreate(TradeBeforeOrderCreateReqBO reqBO) { // 如果是砍价订单 if (ObjectUtil.notEqual(TradeOrderTypeEnum.BARGAIN.getType(), reqBO.getOrderType())) { return; } - - // 额外扣减砍价的库存 + // 扣减砍价活动的库存 bargainActivityApi.updateBargainActivityStock(reqBO.getBargainActivityId(), reqBO.getCount()); } 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 b747e35d4..47426dd9b 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 @@ -24,9 +24,12 @@ public interface TradeOrderHandler { */ void afterOrderCreate(TradeAfterOrderCreateReqBO reqBO); + // TODO @puhui999:这个搞成订单取消 /** * 回滚 */ void rollback(); + // TODO @puhui999:再搞个订单项取消哈 + } 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 ce1dfdb05..652717b77 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 @@ -20,13 +20,17 @@ public class TradeSeckillHandler implements TradeOrderHandler { @Resource private SeckillActivityApi seckillActivityApi; + // TODO @puhui999:先临时写在这里;在价格计算时,如果是秒杀商品,需要校验如下条件: + // 1. 商品存在、库存充足、单次限购; + // 2. 活动进行中、时间段符合 + @Override public void beforeOrderCreate(TradeBeforeOrderCreateReqBO reqBO) { // 如果是秒杀订单:额外扣减秒杀的库存; if (ObjectUtil.notEqual(TradeOrderTypeEnum.SECKILL.getType(), reqBO.getOrderType())) { return; } - + // 扣减秒杀活动的库存 seckillActivityApi.updateSeckillStock(reqBO.getSeckillActivityId(), reqBO.getSkuId(), reqBO.getCount()); }