trade:完善砍价的下单流程

This commit is contained in:
YunaiV 2023-10-05 23:24:44 +08:00
parent 1bda3c8b53
commit aec8e853e4
11 changed files with 90 additions and 22 deletions

View File

@ -21,4 +21,14 @@ public interface BargainRecordApi {
*/ */
BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId); BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId);
/**
* 更新砍价记录的订单编号
*
* 在砍价成功后用户发起订单后会记录该订单编号
*
* @param id 砍价记录编号
* @param orderId 订单编号
*/
void updateBargainRecordOrderId(Long id, Long orderId);
} }

View File

@ -99,6 +99,7 @@ public interface ErrorCodeConstants {
ErrorCode BARGAIN_RECORD_CREATE_FAIL_EXISTS = new ErrorCode(1_013_013_001, "参与失败,您已经参与当前砍价活动"); ErrorCode BARGAIN_RECORD_CREATE_FAIL_EXISTS = new ErrorCode(1_013_013_001, "参与失败,您已经参与当前砍价活动");
ErrorCode BARGAIN_RECORD_CREATE_FAIL_LIMIT = new ErrorCode(1_013_013_002, "参与失败,您已达到当前砍价活动的参与上限"); ErrorCode BARGAIN_RECORD_CREATE_FAIL_LIMIT = new ErrorCode(1_013_013_002, "参与失败,您已达到当前砍价活动的参与上限");
ErrorCode BARGAIN_JOIN_RECORD_NOT_SUCCESS = new ErrorCode(1_013_013_004, "下单失败,砍价未成功"); ErrorCode BARGAIN_JOIN_RECORD_NOT_SUCCESS = new ErrorCode(1_013_013_004, "下单失败,砍价未成功");
ErrorCode BARGAIN_JOIN_RECORD_ALREADY_ORDER = new ErrorCode(1_013_013_005, "下单失败,该砍价已经下单");
// ========== 砍价助力 1-013-014-000 ========== // ========== 砍价助力 1-013-014-000 ==========
ErrorCode BARGAIN_HELP_CREATE_FAIL_RECORD_NOT_IN_PROCESS = new ErrorCode(1_013_014_000, "助力失败,砍价记录不处于进行中"); ErrorCode BARGAIN_HELP_CREATE_FAIL_RECORD_NOT_IN_PROCESS = new ErrorCode(1_013_014_000, "助力失败,砍价记录不处于进行中");

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.promotion.api.bargain; package cn.iocoder.yudao.module.promotion.api.bargain;
import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainRecordCreateReqDTO;
import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO; import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.service.bargain.BargainRecordService; import cn.iocoder.yudao.module.promotion.service.bargain.BargainRecordService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -18,13 +17,14 @@ public class BargainRecordApiImpl implements BargainRecordApi {
@Resource @Resource
private BargainRecordService bargainRecordService; private BargainRecordService bargainRecordService;
@Override
public void createBargainRecord(BargainRecordCreateReqDTO reqDTO) {
}
@Override @Override
public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) { public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) {
return bargainRecordService.validateJoinBargain(userId, bargainRecordId, skuId); return bargainRecordService.validateJoinBargain(userId, bargainRecordId, skuId);
} }
@Override
public void updateBargainRecordOrderId(Long id, Long orderId) {
bargainRecordService.updateBargainRecordOrderId(id, orderId);
}
} }

View File

@ -39,6 +39,14 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> {
* @return 影响的行数 * @return 影响的行数
*/ */
default int updateStock(Long id, int count) { default int updateStock(Long id, int count) {
// 情况一增加库存
if (count > 0) {
return update(null, new LambdaUpdateWrapper<BargainActivityDO>()
.eq(BargainActivityDO::getId, id)
.setSql("stock = stock + " + count));
}
// 情况二扣减库存
count = -count; // 取正
return update(null, new LambdaUpdateWrapper<BargainActivityDO>() return update(null, new LambdaUpdateWrapper<BargainActivityDO>()
.eq(BargainActivityDO::getId, id) .eq(BargainActivityDO::getId, id)
.ge(BargainActivityDO::getStock, count) .ge(BargainActivityDO::getStock, count)

View File

@ -93,4 +93,18 @@ public interface BargainRecordMapper extends BaseMapperX<BargainRecordDO> {
.last("LIMIT " + count)); .last("LIMIT " + count));
} }
/**
* 更新砍价的订单编号前提是 orderId 原本是空的
*
* @param id 砍价记录编号
* @param orderId 订单编号
* @return 更新数量
*/
default int updateOrderIdById(Long id, Long orderId) {
return update(new BargainRecordDO().setOrderId(orderId),
new LambdaQueryWrapper<>(BargainRecordDO.class)
.eq(BargainRecordDO::getId, id)
.isNull(BargainRecordDO::getOrderId));
}
} }

View File

@ -36,6 +36,8 @@ public interface BargainActivityService {
/** /**
* 更新砍价活动库存 * 更新砍价活动库存
* *
* 如果更新失败库存不足则抛出业务异常
*
* @param id 砍价活动编号 * @param id 砍价活动编号
* @param count 购买数量 * @param count 购买数量
*/ */

View File

@ -83,16 +83,7 @@ public class BargainActivityServiceImpl implements BargainActivityService {
@Override @Override
public void updateBargainActivityStock(Long id, Integer count) { 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_STOCK_NOT_ENOUGH);
}
// 更新砍价库存
int updateCount = bargainActivityMapper.updateStock(id, count); int updateCount = bargainActivityMapper.updateStock(id, count);
if (updateCount == 0) { if (updateCount == 0) {
throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH); throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH);

View File

@ -55,6 +55,16 @@ public interface BargainRecordService {
*/ */
BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId); BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId);
/**
* 更新砍价记录的订单编号
*
* 在砍价成功后用户发起订单后会记录该订单编号
*
* @param id 砍价记录编号
* @param orderId 订单编号
*/
void updateBargainRecordOrderId(Long id, Long orderId);
/** /**
* 获得砍价记录 * 获得砍价记录
* *

View File

@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainRecordMapper; import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainRecordMapper;
import cn.iocoder.yudao.module.promotion.enums.bargain.BargainRecordStatusEnum; import cn.iocoder.yudao.module.promotion.enums.bargain.BargainRecordStatusEnum;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -42,7 +43,7 @@ public class BargainRecordServiceImpl implements BargainRecordService {
@Override @Override
public Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO) { public Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO) {
// 1. 校验砍价活动 // 1. 校验砍价活动包括库存
BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(reqVO.getActivityId()); BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(reqVO.getActivityId());
// 2.1 校验当前是否已经有参与中的砍价活动 // 2.1 校验当前是否已经有参与中的砍价活动
@ -77,23 +78,37 @@ public class BargainRecordServiceImpl implements BargainRecordService {
@Override @Override
public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) { public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) {
// 1.1 拼团记录不存在 // 1.1 砍价记录不存在
BargainRecordDO record = bargainRecordMapper.selectByIdAndUserId(bargainRecordId, userId); BargainRecordDO record = bargainRecordMapper.selectByIdAndUserId(bargainRecordId, userId);
if (record == null) { if (record == null) {
throw exception(BARGAIN_RECORD_NOT_EXISTS); throw exception(BARGAIN_RECORD_NOT_EXISTS);
} }
// 1.2 拼团记录未在进行中 // 1.2 砍价记录未在进行中
if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.IN_PROGRESS)) { if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.SUCCESS.getStatus())) {
throw exception(BARGAIN_JOIN_RECORD_NOT_SUCCESS); throw exception(BARGAIN_JOIN_RECORD_NOT_SUCCESS);
} }
// 1.3 砍价记录已经下单
if (record.getOrderId() != null) {
throw exception(BARGAIN_JOIN_RECORD_ALREADY_ORDER);
}
// 2. 校验砍价活动 // 2.1 校验砍价活动包括库存
BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(record.getActivityId()); BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(record.getActivityId());
Assert.isTrue(Objects.equals(skuId, activity.getSkuId()), "砍价商品不匹配"); // 防御性校验 Assert.isTrue(Objects.equals(skuId, activity.getSkuId()), "砍价商品不匹配"); // 防御性校验
return new BargainValidateJoinRespDTO().setActivityId(activity.getId()).setName(activity.getName()) return new BargainValidateJoinRespDTO().setActivityId(activity.getId()).setName(activity.getName())
.setBargainPrice(record.getBargainPrice()); .setBargainPrice(record.getBargainPrice());
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void updateBargainRecordOrderId(Long id, Long orderId) {
// 更新失败说明已经下单
int updateCount = bargainRecordMapper.updateOrderIdById(id, orderId);
if (updateCount == 0) {
throw exception(BARGAIN_JOIN_RECORD_ALREADY_ORDER);
}
}
@Override @Override
public BargainRecordDO getBargainRecord(Long id) { public BargainRecordDO getBargainRecord(Long id) {
return bargainRecordMapper.selectById(id); return bargainRecordMapper.selectById(id);

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.promotion.api.bargain.BargainActivityApi; 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; 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.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum; import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
@ -20,6 +21,8 @@ public class TradeBargainHandler implements TradeOrderHandler {
@Resource @Resource
private BargainActivityApi bargainActivityApi; private BargainActivityApi bargainActivityApi;
@Resource
private BargainRecordApi bargainRecordApi;
@Override @Override
public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
@ -28,7 +31,18 @@ public class TradeBargainHandler implements TradeOrderHandler {
} }
// 扣减砍价活动的库存 // 扣减砍价活动的库存
bargainActivityApi.updateBargainActivityStock(order.getBargainActivityId(), bargainActivityApi.updateBargainActivityStock(order.getBargainActivityId(),
orderItems.get(0).getCount()); -orderItems.get(0).getCount());
} }
@Override
public void afterOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (ObjectUtil.notEqual(TradeOrderTypeEnum.BARGAIN.getType(), order.getType())) {
return;
}
// 记录砍价记录对应的订单编号
bargainRecordApi.updateBargainRecordOrderId(order.getBargainRecordId(), order.getId());
}
// TODO 芋艿取消订单时需要增加库存
} }

View File

@ -1,10 +1,12 @@
package cn.iocoder.yudao.module.trade.service.price.calculator; package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.promotion.api.bargain.BargainRecordApi; import cn.iocoder.yudao.module.promotion.api.bargain.BargainRecordApi;
import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO; import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
@ -28,7 +30,7 @@ public class TradeBargainActivityPriceCalculator implements TradePriceCalculator
@Override @Override
public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) { public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
// 1. 判断订单类型和是否具有拼团记录编号 // 1. 判断订单类型和是否具有拼团记录编号
if (param.getBargainRecordId() != null) { if (ObjectUtil.notEqual(result.getType(), TradeOrderTypeEnum.BARGAIN.getType())) {
return; return;
} }
Assert.isTrue(param.getItems().size() == 1, "砍价时,只允许选择一个商品"); Assert.isTrue(param.getItems().size() == 1, "砍价时,只允许选择一个商品");
@ -40,6 +42,7 @@ public class TradeBargainActivityPriceCalculator implements TradePriceCalculator
// 3.1 记录优惠明细 // 3.1 记录优惠明细
Integer discountPrice = orderItem.getPayPrice() - bargainActivity.getBargainPrice() * orderItem.getCount(); Integer discountPrice = orderItem.getPayPrice() - bargainActivity.getBargainPrice() * orderItem.getCount();
// TODO 芋艿极端情况优惠金额为负数需要处理
TradePriceCalculatorHelper.addPromotion(result, orderItem, TradePriceCalculatorHelper.addPromotion(result, orderItem,
param.getSeckillActivityId(), bargainActivity.getName(), PromotionTypeEnum.BARGAIN_ACTIVITY.getType(), param.getSeckillActivityId(), bargainActivity.getName(), PromotionTypeEnum.BARGAIN_ACTIVITY.getType(),
StrUtil.format("砍价活动:省 {} 元", TradePriceCalculatorHelper.formatPrice(discountPrice)), StrUtil.format("砍价活动:省 {} 元", TradePriceCalculatorHelper.formatPrice(discountPrice)),