|
@@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.promotion.service.combination;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
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;
|
|
@@ -14,6 +15,8 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationA
|
|
|
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationRecordDO;
|
|
|
import cn.iocoder.yudao.module.promotion.dal.mysql.combination.CombinationRecordMapper;
|
|
|
import cn.iocoder.yudao.module.promotion.enums.combination.CombinationRecordStatusEnum;
|
|
|
+import cn.iocoder.yudao.module.trade.api.order.TradeOrderApi;
|
|
|
+import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -24,6 +27,7 @@ import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
|
|
|
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.module.promotion.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
// TODO 芋艿:等拼团记录做完,完整 review 下
|
|
@@ -51,6 +55,8 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
|
|
|
@Resource
|
|
|
@Lazy
|
|
|
private ProductSkuApi productSkuApi;
|
|
|
+ @Resource
|
|
|
+ private TradeOrderApi tradeOrderApi;
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@@ -96,30 +102,77 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
|
|
|
return recordDO;
|
|
|
}
|
|
|
|
|
|
- // TODO @puhui999:有一个应该在创建那要做下;就是当前 activityId 已经有未支付的订单,不允许在发起新的;要么支付,要么去掉先;
|
|
|
+ @Override
|
|
|
+ public void validateCombinationRecord(Long activityId, Long userId, Long skuId, Integer count) {
|
|
|
+ // 1.1 校验拼团活动是否存在
|
|
|
+ CombinationActivityDO activity = combinationActivityService.validateCombinationActivityExists(activityId);
|
|
|
+ // 1.2 校验活动是否开启
|
|
|
+ if (ObjectUtil.equal(activity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
|
|
|
+ throw exception(COMBINATION_ACTIVITY_STATUS_DISABLE);
|
|
|
+ }
|
|
|
+ // 2 校验是否超出单次限购数量
|
|
|
+ if (count > activity.getSingleLimitCount()) {
|
|
|
+ throw exception(COMBINATION_RECORD_FAILED_SINGLE_LIMIT_COUNT_EXCEED);
|
|
|
+ }
|
|
|
+ // 3、校验是否有拼团记录
|
|
|
+ List<CombinationRecordDO> recordList = getRecordListByUserIdAndActivityId(userId, activityId);
|
|
|
+ if (CollUtil.isEmpty(recordList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 4、校验是否超出总限购数量
|
|
|
+ Integer sumValue = getSumValue(convertList(recordList, CombinationRecordDO::getCount,
|
|
|
+ item -> ObjectUtil.equals(item.getStatus(), CombinationRecordStatusEnum.SUCCESS.getStatus())), i -> i, Integer::sum);
|
|
|
+ if ((sumValue + count) > activity.getTotalLimitCount()) {
|
|
|
+ throw exception(COMBINATION_RECORD_FAILED_TOTAL_LIMIT_COUNT_EXCEED);
|
|
|
+ }
|
|
|
+ // 5、校验拼团记录是否存在未支付的订单(如果存在未支付的订单则不允许发起新的拼团)
|
|
|
+ CombinationRecordDO record = findFirst(recordList, item -> ObjectUtil.equals(item.getStatus(), null));
|
|
|
+ if (record == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 5.1、查询关联的订单是否已经支付
|
|
|
+ // 当前 activityId 已经有未支付的订单,不允许在发起新的;要么支付,要么去掉先;
|
|
|
+ Integer orderStatus = tradeOrderApi.getOrderStatus(record.getOrderId());
|
|
|
+ if (ObjectUtil.equal(orderStatus, TradeOrderStatusEnum.UNPAID.getStatus())) {
|
|
|
+ throw exception(COMBINATION_RECORD_FAILED_ORDER_STATUS_UNPAID);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void createCombinationRecord(CombinationRecordCreateReqDTO reqDTO) {
|
|
|
- // 1.1 校验拼团活动
|
|
|
+
|
|
|
+ // 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);
|
|
|
+ // 1.2 校验是否超出单次限购数量
|
|
|
+ if (reqDTO.getCount() > activity.getSingleLimitCount()) {
|
|
|
+ throw exception(COMBINATION_RECORD_FAILED_SINGLE_LIMIT_COUNT_EXCEED);
|
|
|
+ }
|
|
|
+ // 1.3、校验是否有拼团记录
|
|
|
+ List<CombinationRecordDO> records = getRecordListByUserIdAndActivityId(reqDTO.getUserId(), reqDTO.getActivityId());
|
|
|
+ if (CollUtil.isEmpty(records)) {
|
|
|
+ return;
|
|
|
}
|
|
|
- // 1.3 校验用户是否参加了其它拼团
|
|
|
+ // 1.4、校验是否超出总限购数量
|
|
|
+ Integer sumValue = getSumValue(convertList(records, CombinationRecordDO::getCount,
|
|
|
+ item -> ObjectUtil.equals(item.getStatus(), CombinationRecordStatusEnum.SUCCESS.getStatus())), i -> i, Integer::sum);
|
|
|
+ if ((sumValue + reqDTO.getCount()) > activity.getTotalLimitCount()) {
|
|
|
+ throw exception(COMBINATION_RECORD_FAILED_TOTAL_LIMIT_COUNT_EXCEED);
|
|
|
+ }
|
|
|
+ // 2、 校验用户是否参加了其它拼团
|
|
|
List<CombinationRecordDO> recordDOList = recordMapper.selectListByUserIdAndStatus(reqDTO.getUserId(), CombinationRecordStatusEnum.IN_PROGRESS.getStatus());
|
|
|
if (CollUtil.isNotEmpty(recordDOList)) {
|
|
|
throw exception(COMBINATION_RECORD_FAILED_HAVE_JOINED);
|
|
|
}
|
|
|
- // TODO @puhui999:有个开始时间未校验
|
|
|
- // 1.4 校验当前活动是否过期
|
|
|
+ // 3、 校验活动是否开启
|
|
|
+ if (LocalDateTime.now().isAfter(activity.getStartTime())) {
|
|
|
+ throw exception(COMBINATION_RECORD_FAILED_TIME_NOT_START);
|
|
|
+ }
|
|
|
+ // 4、 校验当前活动是否过期
|
|
|
if (LocalDateTime.now().isAfter(activity.getEndTime())) {
|
|
|
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
|
|
|
}
|
|
|
- // 1.5 父拼团是否存在,是否已经满了
|
|
|
+ // 5、 父拼团是否存在,是否已经满了
|
|
|
if (reqDTO.getHeadId() != null) {
|
|
|
// 查询进行中的父拼团
|
|
|
CombinationRecordDO record = recordMapper.selectOneByHeadId(reqDTO.getHeadId(), CombinationRecordStatusEnum.IN_PROGRESS.getStatus());
|
|
@@ -132,8 +185,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // TODO @puhui999:单次限购
|
|
|
-
|
|
|
// 2. 创建拼团记录
|
|
|
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
|
|
|
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
|