拼团记录:完善拼团记录创建前的条件校验规则

This commit is contained in:
puhui999 2023-10-07 14:30:25 +08:00
parent c7b5df930e
commit f40bd23f6a
10 changed files with 117 additions and 71 deletions

View File

@ -18,12 +18,13 @@ public interface CombinationRecordApi {
/**
* 校验是否满足拼团条件
*
* @param activityId 活动编号
* @param userId 用户编号
* @param activityId 活动编号
* @param headId 团长编号
* @param skuId sku 编号
* @param count 数量
*/
void validateCombinationRecord(Long activityId, Long userId, Long skuId, Integer count);
void validateCombinationRecord(Long userId, Long activityId, Long headId, Long skuId, Integer count);
/**
* 创建开团记录
@ -71,13 +72,13 @@ public interface CombinationRecordApi {
*
* 如果校验失败则抛出业务异常
*
* @param activityId 活动编号
* @param userId 用户编号
* @param activityId 活动编号
* @param headId 团长编号
* @param skuId sku 编号
* @param count 数量
* @return 拼团信息
*/
// TODO @puhuiuserId 放最前面然后应该还有个 headId 参数
CombinationValidateJoinRespDTO validateJoinCombination(Long activityId, Long userId, Long skuId, Integer count);
CombinationValidateJoinRespDTO validateJoinCombination(Long userId, Long activityId, Long headId, Long skuId, Integer count);
}

View File

@ -80,7 +80,7 @@ public interface ErrorCodeConstants {
ErrorCode COMBINATION_RECORD_EXISTS = new ErrorCode(1_013_011_001, "拼团失败,已参与过该拼团");
ErrorCode COMBINATION_RECORD_HEAD_NOT_EXISTS = new ErrorCode(1_013_011_002, "拼团失败,父拼团不存在");
ErrorCode COMBINATION_RECORD_USER_FULL = new ErrorCode(1_013_011_003, "拼团失败,拼团人数已满");
ErrorCode COMBINATION_RECORD_FAILED_HAVE_JOINED = new ErrorCode(1_013_011_004, "拼团失败,已参与其它拼团");
ErrorCode COMBINATION_RECORD_FAILED_HAVE_JOINED = new ErrorCode(1_013_011_004, "拼团失败,原因:存在该活动正在进行的拼团记录");
ErrorCode COMBINATION_RECORD_FAILED_TIME_NOT_START = new ErrorCode(1_013_011_005, "拼团失败,活动未开始");
ErrorCode COMBINATION_RECORD_FAILED_TIME_END = new ErrorCode(1_013_011_006, "拼团失败,活动已经结束");
ErrorCode COMBINATION_RECORD_FAILED_SINGLE_LIMIT_COUNT_EXCEED = new ErrorCode(1_013_011_007, "拼团失败,原因:单次限购超出");

View File

@ -40,4 +40,8 @@ public enum CombinationRecordStatusEnum implements IntArrayValuable {
return ObjectUtil.equal(status, SUCCESS.getStatus());
}
public static boolean isInProgress(Integer status) {
return ObjectUtil.equal(status, IN_PROGRESS.getStatus());
}
}

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.promotion.api.combination;
import cn.iocoder.yudao.module.promotion.api.combination.dto.CombinationRecordCreateReqDTO;
import cn.iocoder.yudao.module.promotion.api.combination.dto.CombinationValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationRecordDO;
import cn.iocoder.yudao.module.promotion.enums.combination.CombinationRecordStatusEnum;
import cn.iocoder.yudao.module.promotion.service.combination.CombinationRecordService;
import org.springframework.stereotype.Service;
@ -9,6 +10,9 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COMBINATION_RECORD_NOT_EXISTS;
/**
* 拼团活动 API 实现类
*
@ -21,8 +25,8 @@ public class CombinationRecordApiImpl implements CombinationRecordApi {
private CombinationRecordService recordService;
@Override
public void validateCombinationRecord(Long activityId, Long userId, Long skuId, Integer count) {
recordService.validateCombinationRecord(activityId, userId, skuId, count);
public void validateCombinationRecord(Long userId, Long activityId, Long headId, Long skuId, Integer count) {
recordService.validateCombinationRecord(userId, activityId, headId, skuId, count);
}
@Override
@ -32,7 +36,12 @@ public class CombinationRecordApiImpl implements CombinationRecordApi {
@Override
public boolean isCombinationRecordSuccess(Long userId, Long orderId) {
return CombinationRecordStatusEnum.isSuccess(recordService.getCombinationRecord(userId, orderId).getStatus());
CombinationRecordDO combinationRecord = recordService.getCombinationRecord(userId, orderId);
if (combinationRecord == null) {
throw exception(COMBINATION_RECORD_NOT_EXISTS);
}
return CombinationRecordStatusEnum.isSuccess(combinationRecord.getStatus());
}
@Override
@ -52,8 +61,8 @@ public class CombinationRecordApiImpl implements CombinationRecordApi {
}
@Override
public CombinationValidateJoinRespDTO validateJoinCombination(Long activityId, Long userId, Long skuId, Integer count) {
return recordService.validateJoinCombination(activityId, userId, skuId, count);
public CombinationValidateJoinRespDTO validateJoinCombination(Long userId, Long activityId, Long headId, Long skuId, Integer count) {
return recordService.validateJoinCombination(userId, activityId, headId, skuId, count);
}
}

View File

@ -48,8 +48,9 @@ public class CombinationActivityBaseVO {
@NotNull(message = "开团人数不能为空")
private Integer userSize;
@Schema(description = "虚拟成团", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Boolean virtualGroup = false; // TODO @puhui999这个字段界面没做呀
@Schema(description = "虚拟成团", requiredMode = Schema.RequiredMode.REQUIRED, example = "false")
@NotNull(message = "虚拟成团不能为空")
private Boolean virtualGroup;
@Schema(description = "限制时长(小时)", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
@NotNull(message = "限制时长不能为空")

View File

@ -113,7 +113,7 @@ public interface CombinationRecordMapper extends BaseMapperX<CombinationRecordDO
// 转换数据
return CollectionUtils.convertMap(result,
record -> MapUtil.getLong(record, "activityId"),
record -> MapUtil.getInt(record, "recordCount" ));
record -> MapUtil.getInt(record, "recordCount"));
}
static LocalDateTime[] builderQueryTime(Integer dateType) {
@ -172,4 +172,20 @@ public interface CombinationRecordMapper extends BaseMapperX<CombinationRecordDO
.betweenIfPresent(CombinationRecordDO::getCreateTime, builderQueryTime(dateType)));
}
/**
* 查询指定团长的拼团记录包括团长
*
* @param activityId 活动编号
* @param headId 团长编号
* @return 拼团记录
*/
default List<CombinationRecordDO> selectList(Long activityId, Long headId) {
return selectList(new LambdaQueryWrapperX<CombinationRecordDO>()
.eq(CombinationRecordDO::getActivityId, activityId)
.eq(CombinationRecordDO::getHeadId, headId)
.or()
.eq(CombinationRecordDO::getId, headId));
}
}

View File

@ -32,16 +32,18 @@ public interface CombinationRecordService {
void updateCombinationRecordStatusByUserIdAndOrderId(Integer status, Long userId, Long orderId);
/**
* 校验是否满足拼团条件
* 如果不满足会抛出异常
* 下单前校验是否满足拼团活动条件
*
* 如果校验失败则抛出业务异常
*
* @param activityId 活动编号
* @param userId 用户编号
* @param activityId 活动编号
* @param headId 团长编号
* @param skuId sku 编号
* @param count 数量
* @return 返回拼团活动和拼团活动商品
* @return 拼团信息
*/
KeyValue<CombinationActivityDO, CombinationProductDO> validateCombinationRecord(Long activityId, Long userId, Long skuId, Integer count);
KeyValue<CombinationActivityDO, CombinationProductDO> validateCombinationRecord(Long userId, Long activityId, Long headId, Long skuId, Integer count);
/**
* 创建拼团记录
@ -83,13 +85,14 @@ public interface CombinationRecordService {
*
* 如果校验失败则抛出业务异常
*
* @param activityId 活动编号
* @param userId 用户编号
* @param activityId 活动编号
* @param headId 团长编号
* @param skuId sku 编号
* @param count 数量
* @return 拼团信息
*/
CombinationValidateJoinRespDTO validateJoinCombination(Long activityId, Long userId, Long skuId, Integer count);
CombinationValidateJoinRespDTO validateJoinCombination(Long userId, Long activityId, Long headId, Long skuId, Integer count);
/**
* 获取所有拼团记录数

View File

@ -1,11 +1,11 @@
package cn.iocoder.yudao.module.promotion.service.combination;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
@ -37,6 +37,8 @@ import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.afterNow;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.beforeNow;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
// TODO 芋艿等拼团记录做完完整 review
@ -114,48 +116,83 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
// TODO @芋艿在详细预览下
@Override
public KeyValue<CombinationActivityDO, CombinationProductDO> validateCombinationRecord(
Long activityId, Long userId, Long skuId, Integer count) {
// 1.1 校验拼团活动是否存在
Long userId, Long activityId, Long headId, Long skuId, Integer count) {
// 1 校验拼团活动是否存在
CombinationActivityDO activity = combinationActivityService.validateCombinationActivityExists(activityId);
// 1.2 校验活动是否开启
if (ObjectUtil.equal(activity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
// 1.1 校验活动是否开启
if (ObjUtil.equal(activity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
throw exception(COMBINATION_ACTIVITY_STATUS_DISABLE);
}
// 2 校验是否超出单次限购数量
// 1.2校验活动开始时间
if (afterNow(activity.getStartTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_NOT_START);
}
// 1.3 校验是否超出单次限购数量
if (count > activity.getSingleLimitCount()) {
throw exception(COMBINATION_RECORD_FAILED_SINGLE_LIMIT_COUNT_EXCEED);
}
// 2.1校验活动商品是否存在
// 2父拼团是否存在,是否已经满了
if (headId != null) {
// 2.1查询进行中的父拼团
CombinationRecordDO record = recordMapper.selectOneByHeadId(headId, CombinationRecordStatusEnum.IN_PROGRESS.getStatus());
if (record == null) {
throw exception(COMBINATION_RECORD_HEAD_NOT_EXISTS);
}
// 2.2校验拼团是否满足要求
if (ObjUtil.equal(record.getUserCount(), record.getUserSize())) {
throw exception(COMBINATION_RECORD_USER_FULL);
}
// 2.3校验拼团是否过期
if (beforeNow(record.getExpireTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
}
}
// 3校验当前活动是否结束
if (beforeNow(activity.getEndTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
}
// 4校验活动商品是否存在
CombinationProductDO product = combinationActivityService.selectByActivityIdAndSkuId(activityId, skuId);
if (product == null) {
throw exception(COMBINATION_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS);
}
// 2.2校验 sku 是否存在
// 5校验 sku 是否存在
ProductSkuRespDTO sku = productSkuApi.getSku(skuId);
if (sku == null) {
throw exception(COMBINATION_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS);
}
// 2.3 校验库存是否充足
// 5.1校验库存是否充足
if (count > sku.getStock()) {
throw exception(COMBINATION_ACTIVITY_UPDATE_STOCK_FAIL);
}
// 3校验是否有拼团记录
// 6校验是否有拼团记录
List<CombinationRecordDO> recordList = getCombinationRecordListByUserIdAndActivityId(userId, activityId);
if (CollUtil.isEmpty(recordList)) {
return new KeyValue<>(activity, product);
}
// 4校验是否超出总限购数量
// 6.1校验用户是否有该活动正在进行的拼团
List<CombinationRecordDO> filtered = filterList(recordList, record -> CombinationRecordStatusEnum.isInProgress(record.getStatus()));
if (CollUtil.isNotEmpty(filtered)) {
throw exception(COMBINATION_RECORD_FAILED_HAVE_JOINED);
}
// 6.2校验是否超出总限购数量
Integer sumValue = getSumValue(convertList(recordList, CombinationRecordDO::getCount,
item -> ObjectUtil.equals(item.getStatus(), CombinationRecordStatusEnum.SUCCESS.getStatus())), i -> i, Integer::sum);
item -> CombinationRecordStatusEnum.isSuccess(item.getStatus())), i -> i, Integer::sum);
if ((sumValue + count) > activity.getTotalLimitCount()) {
throw exception(COMBINATION_RECORD_FAILED_TOTAL_LIMIT_COUNT_EXCEED);
}
// 5校验拼团记录是否存在未支付的订单如果存在未支付的订单则不允许发起新的拼团
// 7校验拼团记录是否存在未支付的订单如果存在未支付的订单则不允许发起新的拼团
CombinationRecordDO record = findFirst(recordList, item -> ObjectUtil.equals(item.getStatus(), null));
if (record == null) {
return new KeyValue<>(activity, product);
}
// 5.1查询关联的订单是否已经支付
// 7.1查询关联的订单是否已经支付
// 当前 activityId 已经有未支付的订单不允许在发起新的要么支付要么去掉先
// TODO 芋艿看看是不是可以删除掉
Integer orderStatus = tradeOrderApi.getOrderStatus(record.getOrderId());
@ -171,46 +208,19 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
@Transactional(rollbackFor = Exception.class)
public void createCombinationRecord(CombinationRecordCreateReqDTO reqDTO) {
// 1校验拼团活动
KeyValue<CombinationActivityDO, CombinationProductDO> keyValue = validateCombinationRecord(
reqDTO.getActivityId(), reqDTO.getUserId(), reqDTO.getSkuId(), reqDTO.getCount());
CombinationActivityDO activity = keyValue.getKey();
// 2校验用户是否参加了其它拼团
List<CombinationRecordDO> recordDOList = recordMapper.selectListByUserIdAndStatus(reqDTO.getUserId(), CombinationRecordStatusEnum.IN_PROGRESS.getStatus());
if (CollUtil.isNotEmpty(recordDOList)) {
throw exception(COMBINATION_RECORD_FAILED_HAVE_JOINED);
}
// 3校验活动是否开启
if (!LocalDateTimeUtils.beforeNow(activity.getStartTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_NOT_START);
}
// 4校验当前活动是否过期
if (LocalDateTime.now().isAfter(activity.getEndTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
}
// 5父拼团是否存在,是否已经满了
if (reqDTO.getHeadId() != null) {
// 5.1查询进行中的父拼团
CombinationRecordDO record = recordMapper.selectOneByHeadId(reqDTO.getHeadId(), CombinationRecordStatusEnum.IN_PROGRESS.getStatus());
if (record == null) {
throw exception(COMBINATION_RECORD_HEAD_NOT_EXISTS);
}
// 5.2校验拼团是否满足要求
if (ObjectUtil.equal(record.getUserCount(), record.getUserSize())) {
throw exception(COMBINATION_RECORD_USER_FULL);
}
}
KeyValue<CombinationActivityDO, CombinationProductDO> keyValue = validateCombinationRecord(reqDTO.getUserId(),
reqDTO.getActivityId(), reqDTO.getHeadId(), reqDTO.getSkuId(), reqDTO.getCount());
// 6. 创建拼团记录
// 2. 组合数据创建拼团记录
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, activity, user, spu, sku));
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku));
}
@Override
public CombinationRecordDO getCombinationRecord(Long userId, Long orderId) {
// TODO puhui999:这里直接获得不适合调用校验的接口
return validateCombinationRecord(userId, orderId);
return recordMapper.selectByUserIdAndOrderId(userId, orderId);
}
@Override
@ -219,8 +229,8 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
}
@Override
public CombinationValidateJoinRespDTO validateJoinCombination(Long activityId, Long userId, Long skuId, Integer count) {
KeyValue<CombinationActivityDO, CombinationProductDO> keyValue = validateCombinationRecord(activityId, userId, skuId, count);
public CombinationValidateJoinRespDTO validateJoinCombination(Long userId, Long activityId, Long headId, Long skuId, Integer count) {
KeyValue<CombinationActivityDO, CombinationProductDO> keyValue = validateCombinationRecord(userId, activityId, headId, skuId, count);
return new CombinationValidateJoinRespDTO()
.setActivityId(keyValue.getKey().getId())
.setName(keyValue.getKey().getName())

View File

@ -34,7 +34,8 @@ public class TradeCombinationHandler implements TradeOrderHandler {
// 获取商品信息
TradeOrderItemDO item = orderItems.get(0);
// 校验是否满足拼团活动相关限制
combinationRecordApi.validateCombinationRecord(order.getCombinationActivityId(), order.getUserId(), item.getSkuId(), item.getCount());
combinationRecordApi.validateCombinationRecord(order.getUserId(), order.getCombinationActivityId(),
order.getCombinationHeadId(), item.getSkuId(), item.getCount());
}
@Override

View File

@ -13,6 +13,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
// TODO @puhui999单测可以后补下
/**
* 拼团活动的 {@link TradePriceCalculator} 实现类
*
@ -35,7 +36,7 @@ public class TradeCombinationActivityPriceCalculator implements TradePriceCalcul
// 2. 校验是否可以参与拼团
TradePriceCalculateRespBO.OrderItem orderItem = result.getItems().get(0);
CombinationValidateJoinRespDTO combinationActivity = combinationRecordApi.validateJoinCombination(
param.getCombinationActivityId(), param.getUserId(),
param.getUserId(), param.getCombinationActivityId(), param.getCombinationHeadId(),
orderItem.getSkuId(), orderItem.getCount());
// 3.1 记录优惠明细