mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 18:28:43 +08:00 
			
		
		
		
	code review:营销活动 & 交易订单的对接
This commit is contained in:
		| @@ -11,7 +11,7 @@ public interface BargainActivityApi { | ||||
|      * 更新砍价活动库存 | ||||
|      * | ||||
|      * @param id 砍价活动编号 | ||||
|      * @param count      购买数量 | ||||
|      * @param count 购买数量 | ||||
|      */ | ||||
|     void updateBargainActivityStock(Long id, Integer count); | ||||
|  | ||||
|   | ||||
| @@ -38,7 +38,7 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> { | ||||
|      * @param count 扣减的库存数量 | ||||
|      * @return 影响的行数 | ||||
|      */ | ||||
|     default int updateActivityStock(Long id, int count) { | ||||
|     default int updateStock(Long id, int count) { | ||||
|         return update(null, new LambdaUpdateWrapper<BargainActivityDO>() | ||||
|                 .eq(BargainActivityDO::getId, id) | ||||
|                 .ge(BargainActivityDO::getStock, count) | ||||
|   | ||||
| @@ -41,7 +41,7 @@ public interface SeckillActivityMapper extends BaseMapperX<SeckillActivityDO> { | ||||
|      * @param count 扣减的库存数量 | ||||
|      * @return 影响的行数 | ||||
|      */ | ||||
|     default int updateActivityStock(Long id, int count) { | ||||
|     default int updateStock(Long id, int count) { | ||||
|         return update(null, new LambdaUpdateWrapper<SeckillActivityDO>() | ||||
|                 .eq(SeckillActivityDO::getId, id) | ||||
|                 .gt(SeckillActivityDO::getTotalStock, 0) | ||||
|   | ||||
| @@ -16,8 +16,13 @@ import java.util.List; | ||||
| @Mapper | ||||
| public interface SeckillProductMapper extends BaseMapperX<SeckillProductDO> { | ||||
|  | ||||
|     default List<SeckillProductDO> selectListByActivityId(Long id) { | ||||
|         return selectList(SeckillProductDO::getActivityId, id); | ||||
|     default List<SeckillProductDO> 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<SeckillProductDO> selectListByActivityId(Collection<Long> ids) { | ||||
| @@ -31,7 +36,7 @@ public interface SeckillProductMapper extends BaseMapperX<SeckillProductDO> { | ||||
|      * @param count 扣减的库存数量 | ||||
|      * @return 影响的行数 | ||||
|      */ | ||||
|     default int updateActivityStock(Long id, int count) { | ||||
|     default int updateStock(Long id, int count) { | ||||
|         return update(null, new LambdaUpdateWrapper<SeckillProductDO>() | ||||
|                 .eq(SeckillProductDO::getId, id) | ||||
|                 .gt(SeckillProductDO::getStock, count) | ||||
|   | ||||
| @@ -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); | ||||
|         } | ||||
|   | ||||
| @@ -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()); | ||||
|   | ||||
| @@ -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<SeckillProductDO> 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); | ||||
|         } | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -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()); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -24,9 +24,12 @@ public interface TradeOrderHandler { | ||||
|      */ | ||||
|     void afterOrderCreate(TradeAfterOrderCreateReqBO reqBO); | ||||
|  | ||||
|     // TODO @puhui999:这个搞成订单取消 | ||||
|     /** | ||||
|      * 回滚 | ||||
|      */ | ||||
|     void rollback(); | ||||
|  | ||||
|     // TODO @puhui999:再搞个订单项取消哈 | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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()); | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV