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