Commit f732e7a7505b531848149889dda97d80b8d358aa
Merge remote-tracking branch 'yudao/feature/mall_product' into feature/mall_product
# Conflicts: # yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/bargain/BargainRecordApiImpl.java # yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainRecordServiceImpl.java
Showing
11 changed files
with
88 additions
and
19 deletions
yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/bargain/BargainRecordApi.java
| ... | ... | @@ -21,4 +21,14 @@ public interface BargainRecordApi { |
| 21 | 21 | */ |
| 22 | 22 | BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId); |
| 23 | 23 | |
| 24 | + /** | |
| 25 | + * 更新砍价记录的订单编号 | |
| 26 | + * | |
| 27 | + * 在砍价成功后,用户发起订单后,会记录该订单编号 | |
| 28 | + * | |
| 29 | + * @param id 砍价记录编号 | |
| 30 | + * @param orderId 订单编号 | |
| 31 | + */ | |
| 32 | + void updateBargainRecordOrderId(Long id, Long orderId); | |
| 33 | + | |
| 24 | 34 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java
| ... | ... | @@ -101,6 +101,7 @@ public interface ErrorCodeConstants { |
| 101 | 101 | ErrorCode BARGAIN_RECORD_CREATE_FAIL_EXISTS = new ErrorCode(1_013_013_001, "参与失败,您已经参与当前砍价活动"); |
| 102 | 102 | ErrorCode BARGAIN_RECORD_CREATE_FAIL_LIMIT = new ErrorCode(1_013_013_002, "参与失败,您已达到当前砍价活动的参与上限"); |
| 103 | 103 | ErrorCode BARGAIN_JOIN_RECORD_NOT_SUCCESS = new ErrorCode(1_013_013_004, "下单失败,砍价未成功"); |
| 104 | + ErrorCode BARGAIN_JOIN_RECORD_ALREADY_ORDER = new ErrorCode(1_013_013_005, "下单失败,该砍价已经下单"); | |
| 104 | 105 | |
| 105 | 106 | // ========== 砍价助力 1-013-014-000 ========== |
| 106 | 107 | ErrorCode BARGAIN_HELP_CREATE_FAIL_RECORD_NOT_IN_PROCESS = new ErrorCode(1_013_014_000, "助力失败,砍价记录不处于进行中"); | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/bargain/BargainRecordApiImpl.java
| ... | ... | @@ -17,13 +17,14 @@ public class BargainRecordApiImpl implements BargainRecordApi { |
| 17 | 17 | @Resource |
| 18 | 18 | private BargainRecordService bargainRecordService; |
| 19 | 19 | |
| 20 | - //@Override | |
| 21 | - //public void createBargainRecord(BargainRecordCreateReqDTO reqDTO) { | |
| 22 | - //} | |
| 23 | - | |
| 24 | 20 | @Override |
| 25 | 21 | public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) { |
| 26 | 22 | return bargainRecordService.validateJoinBargain(userId, bargainRecordId, skuId); |
| 27 | 23 | } |
| 28 | 24 | |
| 25 | + @Override | |
| 26 | + public void updateBargainRecordOrderId(Long id, Long orderId) { | |
| 27 | + bargainRecordService.updateBargainRecordOrderId(id, orderId); | |
| 28 | + } | |
| 29 | + | |
| 29 | 30 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainActivityMapper.java
| ... | ... | @@ -39,6 +39,14 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> { |
| 39 | 39 | * @return 影响的行数 |
| 40 | 40 | */ |
| 41 | 41 | default int updateStock(Long id, int count) { |
| 42 | + // 情况一:增加库存 | |
| 43 | + if (count > 0) { | |
| 44 | + return update(null, new LambdaUpdateWrapper<BargainActivityDO>() | |
| 45 | + .eq(BargainActivityDO::getId, id) | |
| 46 | + .setSql("stock = stock + " + count)); | |
| 47 | + } | |
| 48 | + // 情况二:扣减库存 | |
| 49 | + count = -count; // 取正 | |
| 42 | 50 | return update(null, new LambdaUpdateWrapper<BargainActivityDO>() |
| 43 | 51 | .eq(BargainActivityDO::getId, id) |
| 44 | 52 | .ge(BargainActivityDO::getStock, count) | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainRecordMapper.java
| ... | ... | @@ -93,4 +93,18 @@ public interface BargainRecordMapper extends BaseMapperX<BargainRecordDO> { |
| 93 | 93 | .last("LIMIT " + count)); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | + /** | |
| 97 | + * 更新砍价的订单编号,前提是 orderId 原本是空的 | |
| 98 | + * | |
| 99 | + * @param id 砍价记录编号 | |
| 100 | + * @param orderId 订单编号 | |
| 101 | + * @return 更新数量 | |
| 102 | + */ | |
| 103 | + default int updateOrderIdById(Long id, Long orderId) { | |
| 104 | + return update(new BargainRecordDO().setOrderId(orderId), | |
| 105 | + new LambdaQueryWrapper<>(BargainRecordDO.class) | |
| 106 | + .eq(BargainRecordDO::getId, id) | |
| 107 | + .isNull(BargainRecordDO::getOrderId)); | |
| 108 | + } | |
| 109 | + | |
| 96 | 110 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityService.java
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityServiceImpl.java
| ... | ... | @@ -83,16 +83,7 @@ public class BargainActivityServiceImpl implements BargainActivityService { |
| 83 | 83 | |
| 84 | 84 | @Override |
| 85 | 85 | public void updateBargainActivityStock(Long id, Integer count) { |
| 86 | - // 查询砍价活动 | |
| 87 | - BargainActivityDO activity = getBargainActivity(id); | |
| 88 | - if (activity == null) { | |
| 89 | - throw exception(BARGAIN_ACTIVITY_NOT_EXISTS); | |
| 90 | - } | |
| 91 | - if (count > activity.getStock()) { | |
| 92 | - throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH); | |
| 93 | - } | |
| 94 | - | |
| 95 | - // 更新砍价库存 | |
| 86 | + // 更新库存。如果更新失败,则抛出异常 | |
| 96 | 87 | int updateCount = bargainActivityMapper.updateStock(id, count); |
| 97 | 88 | if (updateCount == 0) { |
| 98 | 89 | throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH); | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainRecordService.java
| ... | ... | @@ -56,6 +56,16 @@ public interface BargainRecordService { |
| 56 | 56 | BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId); |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | + * 更新砍价记录的订单编号 | |
| 60 | + * | |
| 61 | + * 在砍价成功后,用户发起订单后,会记录该订单编号 | |
| 62 | + * | |
| 63 | + * @param id 砍价记录编号 | |
| 64 | + * @param orderId 订单编号 | |
| 65 | + */ | |
| 66 | + void updateBargainRecordOrderId(Long id, Long orderId); | |
| 67 | + | |
| 68 | + /** | |
| 59 | 69 | * 获得砍价记录 |
| 60 | 70 | * |
| 61 | 71 | * @param id 砍价记录编号 | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainRecordServiceImpl.java
| ... | ... | @@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO; |
| 13 | 13 | import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainRecordMapper; |
| 14 | 14 | import cn.iocoder.yudao.module.promotion.enums.bargain.BargainRecordStatusEnum; |
| 15 | 15 | import org.springframework.stereotype.Service; |
| 16 | +import org.springframework.transaction.annotation.Transactional; | |
| 16 | 17 | import org.springframework.validation.annotation.Validated; |
| 17 | 18 | |
| 18 | 19 | import javax.annotation.Nullable; |
| ... | ... | @@ -42,7 +43,7 @@ public class BargainRecordServiceImpl implements BargainRecordService { |
| 42 | 43 | |
| 43 | 44 | @Override |
| 44 | 45 | public Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO) { |
| 45 | - // 1. 校验砍价活动 | |
| 46 | + // 1. 校验砍价活动(包括库存) | |
| 46 | 47 | BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(reqVO.getActivityId()); |
| 47 | 48 | |
| 48 | 49 | // 2.1 校验当前是否已经有参与中的砍价活动 |
| ... | ... | @@ -83,11 +84,15 @@ public class BargainRecordServiceImpl implements BargainRecordService { |
| 83 | 84 | throw exception(BARGAIN_RECORD_NOT_EXISTS); |
| 84 | 85 | } |
| 85 | 86 | // 1.2 砍价记录未在进行中 |
| 86 | - if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.IN_PROGRESS)) { | |
| 87 | + if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.SUCCESS.getStatus())) { | |
| 87 | 88 | throw exception(BARGAIN_JOIN_RECORD_NOT_SUCCESS); |
| 88 | 89 | } |
| 90 | + // 1.3 砍价记录已经下单 | |
| 91 | + if (record.getOrderId() != null) { | |
| 92 | + throw exception(BARGAIN_JOIN_RECORD_ALREADY_ORDER); | |
| 93 | + } | |
| 89 | 94 | |
| 90 | - // 2. 校验砍价活动 | |
| 95 | + // 2.1 校验砍价活动(包括库存) | |
| 91 | 96 | BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(record.getActivityId()); |
| 92 | 97 | Assert.isTrue(Objects.equals(skuId, activity.getSkuId()), "砍价商品不匹配"); // 防御性校验 |
| 93 | 98 | return new BargainValidateJoinRespDTO().setActivityId(activity.getId()).setName(activity.getName()) |
| ... | ... | @@ -95,6 +100,16 @@ public class BargainRecordServiceImpl implements BargainRecordService { |
| 95 | 100 | } |
| 96 | 101 | |
| 97 | 102 | @Override |
| 103 | + @Transactional(rollbackFor = Exception.class) | |
| 104 | + public void updateBargainRecordOrderId(Long id, Long orderId) { | |
| 105 | + // 更新失败,说明已经下单 | |
| 106 | + int updateCount = bargainRecordMapper.updateOrderIdById(id, orderId); | |
| 107 | + if (updateCount == 0) { | |
| 108 | + throw exception(BARGAIN_JOIN_RECORD_ALREADY_ORDER); | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + @Override | |
| 98 | 113 | public BargainRecordDO getBargainRecord(Long id) { |
| 99 | 114 | return bargainRecordMapper.selectById(id); |
| 100 | 115 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeBargainHandler.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.trade.service.order.handler; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.ObjectUtil; |
| 4 | 4 | import cn.iocoder.yudao.module.promotion.api.bargain.BargainActivityApi; |
| 5 | +import cn.iocoder.yudao.module.promotion.api.bargain.BargainRecordApi; | |
| 5 | 6 | import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO; |
| 6 | 7 | import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO; |
| 7 | 8 | import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum; |
| ... | ... | @@ -20,6 +21,8 @@ public class TradeBargainHandler implements TradeOrderHandler { |
| 20 | 21 | |
| 21 | 22 | @Resource |
| 22 | 23 | private BargainActivityApi bargainActivityApi; |
| 24 | + @Resource | |
| 25 | + private BargainRecordApi bargainRecordApi; | |
| 23 | 26 | |
| 24 | 27 | @Override |
| 25 | 28 | public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { |
| ... | ... | @@ -28,7 +31,18 @@ public class TradeBargainHandler implements TradeOrderHandler { |
| 28 | 31 | } |
| 29 | 32 | // 扣减砍价活动的库存 |
| 30 | 33 | bargainActivityApi.updateBargainActivityStock(order.getBargainActivityId(), |
| 31 | - orderItems.get(0).getCount()); | |
| 34 | + -orderItems.get(0).getCount()); | |
| 35 | + } | |
| 36 | + | |
| 37 | + @Override | |
| 38 | + public void afterOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { | |
| 39 | + if (ObjectUtil.notEqual(TradeOrderTypeEnum.BARGAIN.getType(), order.getType())) { | |
| 40 | + return; | |
| 41 | + } | |
| 42 | + // 记录砍价记录对应的订单编号 | |
| 43 | + bargainRecordApi.updateBargainRecordOrderId(order.getBargainRecordId(), order.getId()); | |
| 32 | 44 | } |
| 33 | 45 | |
| 46 | + // TODO 芋艿:取消订单时,需要增加库存 | |
| 47 | + | |
| 34 | 48 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeBargainActivityPriceCalculator.java
| 1 | 1 | package cn.iocoder.yudao.module.trade.service.price.calculator; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.lang.Assert; |
| 4 | +import cn.hutool.core.util.ObjectUtil; | |
| 4 | 5 | import cn.hutool.core.util.StrUtil; |
| 5 | 6 | import cn.iocoder.yudao.module.promotion.api.bargain.BargainRecordApi; |
| 6 | 7 | import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO; |
| 7 | 8 | import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; |
| 9 | +import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum; | |
| 8 | 10 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; |
| 9 | 11 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; |
| 10 | 12 | import org.springframework.core.annotation.Order; |
| ... | ... | @@ -28,7 +30,7 @@ public class TradeBargainActivityPriceCalculator implements TradePriceCalculator |
| 28 | 30 | @Override |
| 29 | 31 | public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) { |
| 30 | 32 | // 1. 判断订单类型和是否具有拼团记录编号 |
| 31 | - if (param.getBargainRecordId() != null) { | |
| 33 | + if (ObjectUtil.notEqual(result.getType(), TradeOrderTypeEnum.BARGAIN.getType())) { | |
| 32 | 34 | return; |
| 33 | 35 | } |
| 34 | 36 | Assert.isTrue(param.getItems().size() == 1, "砍价时,只允许选择一个商品"); |
| ... | ... | @@ -40,6 +42,7 @@ public class TradeBargainActivityPriceCalculator implements TradePriceCalculator |
| 40 | 42 | |
| 41 | 43 | // 3.1 记录优惠明细 |
| 42 | 44 | Integer discountPrice = orderItem.getPayPrice() - bargainActivity.getBargainPrice() * orderItem.getCount(); |
| 45 | + // TODO 芋艿:极端情况,优惠金额为负数,需要处理 | |
| 43 | 46 | TradePriceCalculatorHelper.addPromotion(result, orderItem, |
| 44 | 47 | param.getSeckillActivityId(), bargainActivity.getName(), PromotionTypeEnum.BARGAIN_ACTIVITY.getType(), |
| 45 | 48 | StrUtil.format("砍价活动:省 {} 元", TradePriceCalculatorHelper.formatPrice(discountPrice)), | ... | ... |