code review:营销活动 & 交易订单的对接

This commit is contained in:
YunaiV 2023-09-23 22:21:23 +08:00
parent 1da14a3f53
commit aa5525d1f5
11 changed files with 47 additions and 30 deletions

View File

@ -11,7 +11,7 @@ public interface BargainActivityApi {
* 更新砍价活动库存
*
* @param id 砍价活动编号
* @param count 购买数量
* @param count 购买数量
*/
void updateBargainActivityStock(Long id, Integer count);

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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);
}

View File

@ -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());

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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());
}

View File

@ -24,9 +24,12 @@ public interface TradeOrderHandler {
*/
void afterOrderCreate(TradeAfterOrderCreateReqBO reqBO);
// TODO @puhui999这个搞成订单取消
/**
* 回滚
*/
void rollback();
// TODO @puhui999再搞个订单项取消哈
}

View File

@ -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());
}