mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-08-11 16:51:53 +08:00
trade:完善砍价的下单流程
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
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.service.bargain.BargainRecordService;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -18,13 +17,14 @@ public class BargainRecordApiImpl implements BargainRecordApi {
|
||||
@Resource
|
||||
private BargainRecordService bargainRecordService;
|
||||
|
||||
@Override
|
||||
public void createBargainRecord(BargainRecordCreateReqDTO reqDTO) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) {
|
||||
return bargainRecordService.validateJoinBargain(userId, bargainRecordId, skuId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateBargainRecordOrderId(Long id, Long orderId) {
|
||||
bargainRecordService.updateBargainRecordOrderId(id, orderId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -39,6 +39,14 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> {
|
||||
* @return 影响的行数
|
||||
*/
|
||||
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>()
|
||||
.eq(BargainActivityDO::getId, id)
|
||||
.ge(BargainActivityDO::getStock, count)
|
||||
|
@@ -93,4 +93,18 @@ public interface BargainRecordMapper extends BaseMapperX<BargainRecordDO> {
|
||||
.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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -36,6 +36,8 @@ public interface BargainActivityService {
|
||||
/**
|
||||
* 更新砍价活动库存
|
||||
*
|
||||
* 如果更新失败(库存不足),则抛出业务异常
|
||||
*
|
||||
* @param id 砍价活动编号
|
||||
* @param count 购买数量
|
||||
*/
|
||||
|
@@ -83,16 +83,7 @@ public class BargainActivityServiceImpl implements BargainActivityService {
|
||||
|
||||
@Override
|
||||
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);
|
||||
if (updateCount == 0) {
|
||||
throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH);
|
||||
|
@@ -55,6 +55,16 @@ public interface BargainRecordService {
|
||||
*/
|
||||
BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId);
|
||||
|
||||
/**
|
||||
* 更新砍价记录的订单编号
|
||||
*
|
||||
* 在砍价成功后,用户发起订单后,会记录该订单编号
|
||||
*
|
||||
* @param id 砍价记录编号
|
||||
* @param orderId 订单编号
|
||||
*/
|
||||
void updateBargainRecordOrderId(Long id, Long orderId);
|
||||
|
||||
/**
|
||||
* 获得砍价记录
|
||||
*
|
||||
|
@@ -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.enums.bargain.BargainRecordStatusEnum;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -42,7 +43,7 @@ public class BargainRecordServiceImpl implements BargainRecordService {
|
||||
|
||||
@Override
|
||||
public Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO) {
|
||||
// 1. 校验砍价活动
|
||||
// 1. 校验砍价活动(包括库存)
|
||||
BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(reqVO.getActivityId());
|
||||
|
||||
// 2.1 校验当前是否已经有参与中的砍价活动
|
||||
@@ -77,23 +78,37 @@ public class BargainRecordServiceImpl implements BargainRecordService {
|
||||
|
||||
@Override
|
||||
public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) {
|
||||
// 1.1 拼团记录不存在
|
||||
// 1.1 砍价记录不存在
|
||||
BargainRecordDO record = bargainRecordMapper.selectByIdAndUserId(bargainRecordId, userId);
|
||||
if (record == null) {
|
||||
throw exception(BARGAIN_RECORD_NOT_EXISTS);
|
||||
}
|
||||
// 1.2 拼团记录未在进行中
|
||||
if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.IN_PROGRESS)) {
|
||||
// 1.2 砍价记录未在进行中
|
||||
if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.SUCCESS.getStatus())) {
|
||||
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());
|
||||
Assert.isTrue(Objects.equals(skuId, activity.getSkuId()), "砍价商品不匹配"); // 防御性校验
|
||||
return new BargainValidateJoinRespDTO().setActivityId(activity.getId()).setName(activity.getName())
|
||||
.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
|
||||
public BargainRecordDO getBargainRecord(Long id) {
|
||||
return bargainRecordMapper.selectById(id);
|
||||
|
Reference in New Issue
Block a user