Merge remote-tracking branch 'yudao/feature/mall_product' into feature/mall_product

# Conflicts:
#	yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationRecordServiceImpl.java
This commit is contained in:
puhui999 2023-10-08 17:33:21 +08:00
commit 2e559ebe06
6 changed files with 68 additions and 66 deletions

View File

@ -232,7 +232,8 @@ public class CollectionUtils {
return valueFunc.apply(t);
}
public static <T, V extends Comparable<? super V>> V getSumValue(List<T> from, Function<T, V> valueFunc, BinaryOperator<V> accumulator) {
public static <T, V extends Comparable<? super V>> V getSumValue(List<T> from, Function<T, V> valueFunc,
BinaryOperator<V> accumulator) {
if (CollUtil.isEmpty(from)) {
return null;
}

View File

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

View File

@ -35,7 +35,7 @@ public interface CombinationRecordMapper extends BaseMapperX<CombinationRecordDO
* @param headId 团长编号
* @return 拼团记录
*/
default CombinationRecordDO selectOneByHeadId(Long headId, Integer status) {
default CombinationRecordDO selectByHeadId(Long headId, Integer status) {
return selectOne(new LambdaQueryWrapperX<CombinationRecordDO>()
.eq(CombinationRecordDO::getId, headId)
.eq(CombinationRecordDO::getStatus, status));

View File

@ -42,7 +42,8 @@ public interface CombinationRecordService {
* @param count 数量
* @return 拼团信息
*/
KeyValue<CombinationActivityDO, CombinationProductDO> validateCombinationRecord(Long userId, Long activityId, Long headId, Long skuId, Integer count);
KeyValue<CombinationActivityDO, CombinationProductDO> validateCombinationRecord(Long userId, Long activityId, Long headId,
Long skuId, Integer count);
/**
* 创建拼团记录

View File

@ -89,13 +89,13 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
@Override
public KeyValue<CombinationActivityDO, CombinationProductDO> validateCombinationRecord(
Long userId, Long activityId, Long headId, Long skuId, Integer count) {
// 1 校验拼团活动是否存在
// 1. 校验拼团活动是否存在
CombinationActivityDO activity = combinationActivityService.validateCombinationActivityExists(activityId);
// 1.1 校验活动是否开启
if (ObjUtil.equal(activity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
throw exception(COMBINATION_ACTIVITY_STATUS_DISABLE);
}
// 1.2校验活动开始时间
// 1.2. 校验活动开始时间
if (afterNow(activity.getStartTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_NOT_START);
}
@ -104,69 +104,67 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
throw exception(COMBINATION_RECORD_FAILED_SINGLE_LIMIT_COUNT_EXCEED);
}
// 2父拼团是否存在,是否已经满了
// 2. 父拼团是否存在,是否已经满了
if (headId != null) {
// 2.1查询进行中的父拼团
CombinationRecordDO record = recordMapper.selectOneByHeadId(headId, CombinationRecordStatusEnum.IN_PROGRESS.getStatus());
// 2.1. 查询进行中的父拼团
CombinationRecordDO record = recordMapper.selectByHeadId(headId, CombinationRecordStatusEnum.IN_PROGRESS.getStatus());
if (record == null) {
throw exception(COMBINATION_RECORD_HEAD_NOT_EXISTS);
}
// 2.2校验拼团是否满足要求
// 2.2. 校验拼团是否已满
if (ObjUtil.equal(record.getUserCount(), record.getUserSize())) {
throw exception(COMBINATION_RECORD_USER_FULL);
}
// 2.3校验拼团是否过期有父拼团的时候只校验父拼团的过期时间
// 2.3 校验拼团是否过期有父拼团的时候只校验父拼团的过期时间
if (beforeNow(record.getExpireTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
}
} else {
// 3校验当前活动是否结束(自己是父拼团的时候才校验活动是否结束)
// 3. 校验当前活动是否结束(自己是父拼团的时候才校验活动是否结束)
if (beforeNow(activity.getEndTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
}
}
// 4校验活动商品是否存在
// 4.1 校验活动商品是否存在
CombinationProductDO product = combinationActivityService.selectByActivityIdAndSkuId(activityId, skuId);
if (product == null) {
throw exception(COMBINATION_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS);
}
// 5校验 sku 是否存在
// 4.2 校验 sku 是否存在
ProductSkuRespDTO sku = productSkuApi.getSku(skuId);
if (sku == null) {
throw exception(COMBINATION_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS);
}
// 5.1校验库存是否充足
// 4.3 校验库存是否充足
if (count > sku.getStock()) {
throw exception(COMBINATION_ACTIVITY_UPDATE_STOCK_FAIL);
}
// 6校验是否有拼团记录
List<CombinationRecordDO> recordList = getCombinationRecordListByUserIdAndActivityId(userId, activityId);
if (CollUtil.isEmpty(recordList)) {
// 6.1 校验是否有拼团记录
List<CombinationRecordDO> recordList = recordMapper.selectListByUserIdAndActivityId(userId, activityId);
recordList.removeIf(record -> CombinationRecordStatusEnum.isFailed(record.getStatus())); // 取消的订单不算数
if (CollUtil.isEmpty(recordList)) { // 如果为空说明可以参与直接返回
return new KeyValue<>(activity, product);
}
// 6.1校验用户是否有该活动正在进行的拼团
List<CombinationRecordDO> filtered = filterList(recordList, record -> CombinationRecordStatusEnum.isInProgress(record.getStatus()));
if (CollUtil.isNotEmpty(filtered)) {
// 6.2 校验用户是否有该活动正在进行的拼团
CombinationRecordDO inProgressRecord = findFirst(recordList,
record -> CombinationRecordStatusEnum.isInProgress(record.getStatus()));
if (inProgressRecord != null) {
throw exception(COMBINATION_RECORD_FAILED_HAVE_JOINED);
}
// 6.2校验是否超出总限购数量
Integer sumValue = getSumValue(convertList(recordList, CombinationRecordDO::getCount,
item -> CombinationRecordStatusEnum.isSuccess(item.getStatus())), i -> i, Integer::sum);
if ((sumValue + count) > activity.getTotalLimitCount()) {
// 6.3 校验是否超出总限购数量
Integer sumValue = getSumValue(recordList, CombinationRecordDO::getCount, Integer::sum);
if (sumValue != null && sumValue + count > activity.getTotalLimitCount()) {
throw exception(COMBINATION_RECORD_FAILED_TOTAL_LIMIT_COUNT_EXCEED);
}
return new KeyValue<>(activity, product);
}
// TODO 芋艿在详细 review
@Override
@Transactional(rollbackFor = Exception.class)
public void createCombinationRecord(CombinationRecordCreateReqDTO reqDTO) {
// 1校验拼团活动
// 1. 校验拼团活动
KeyValue<CombinationActivityDO, CombinationProductDO> keyValue = validateCombinationRecord(reqDTO.getUserId(),
reqDTO.getActivityId(), reqDTO.getHeadId(), reqDTO.getSkuId(), reqDTO.getCount());
@ -174,23 +172,25 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
CombinationRecordDO recordDO = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku);
recordMapper.insert(recordDO);
CombinationRecordDO record = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku);
// TODO @puhui head 的情况下 head 结束为准
recordMapper.insert(record);
// 3如果是团长(CombinationRecordCreateReqDTO#headId null )需要设置 headId CombinationRecordDO#HEAD_ID_GROUP
if (reqDTO.getHeadId() == null) {
recordMapper.updateById(new CombinationRecordDO().setId(recordDO.getId()).setHeadId(CombinationRecordDO.HEAD_ID_GROUP));
// 3. 如果是团长需要设置 headId CombinationRecordDO#HEAD_ID_GROUP
// TODO @puhui999是不是只要是团长record 设置了就好啦不用 update
if (ObjUtil.equal(CombinationRecordDO.HEAD_ID_GROUP, reqDTO.getHeadId())) {
recordMapper.updateById(new CombinationRecordDO().setId(record.getId()).setHeadId(CombinationRecordDO.HEAD_ID_GROUP));
return;
}
// TODO 这里要不要弄成异步的
// TODO 这里要不要弄成异步的不用异步哈就是事务好了
// 4更新拼团相关信息到订单
updateOrderCombinationInfo(recordDO.getOrderId(), recordDO.getActivityId(), recordDO.getId(), recordDO.getHeadId());
updateOrderCombinationInfo(record.getOrderId(), record.getActivityId(), record.getId(), record.getHeadId());
// 4更新拼团记录
updateCombinationRecords(keyValue.getKey(), reqDTO.getHeadId());
updateCombinationRecordWhenCreate(reqDTO.getHeadId(), keyValue.getKey());
}
// TODO @puhui999这个更新放到 trade 那就好了createCombinationRecord 返回一个 recordId
/**
* 更新拼团相关信息到订单
*
@ -204,35 +204,31 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
}
/**
* 更新拼团记录
* 当新增拼团时更新拼团记录的进展
*
* @param activity 活动
* @param headId 团长编号
* @param activity 活动
*/
private void updateCombinationRecords(CombinationActivityDO activity, Long headId) {
// 团长
CombinationRecordDO recordHead = recordMapper.selectById(headId);
// 团员
private void updateCombinationRecordWhenCreate(Long headId, CombinationActivityDO activity) {
// 1. 团长 + 团员
List<CombinationRecordDO> records = getCombinationRecordListByHeadId(headId);
// 需要更新的记录
List<CombinationRecordDO> updateRecords = new ArrayList<>();
if (CollUtil.isEmpty(records)) {
return;
}
records.add(recordHead); // 加入团长团长也需要更新
boolean isEqual = ObjUtil.equal(records.size(), activity.getUserSize());
records.forEach(item -> {
CombinationRecordDO recordDO = new CombinationRecordDO();
recordDO.setId(item.getId());
recordDO.setUserCount(records.size());
// 校验拼团是否满足要求
if (isEqual) {
recordDO.setStatus(CombinationRecordStatusEnum.SUCCESS.getStatus());
}
updateRecords.add(recordDO);
});
CombinationRecordDO headRecord = recordMapper.selectById(headId);
// 2. 批量更新记录
List<CombinationRecordDO> updateRecords = new ArrayList<>();
records.add(headRecord); // 加入团长团长也需要更新
boolean isFull = records.size() >= activity.getUserSize();
records.forEach(item -> {
CombinationRecordDO updateRecord = new CombinationRecordDO();
updateRecord.setId(item.getId()).setUserCount(records.size());
if (isFull) {
updateRecord.setStatus(CombinationRecordStatusEnum.SUCCESS.getStatus());
}
updateRecords.add(updateRecord);
});
recordMapper.updateBatch(updateRecords);
}
@ -247,12 +243,12 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
}
@Override
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())
.setCombinationPrice(keyValue.getValue().getCombinationPrice());
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()).setCombinationPrice(keyValue.getValue().getCombinationPrice());
}
@Override

View File

@ -30,11 +30,11 @@ public class TradeCombinationHandler implements TradeOrderHandler {
}
Assert.isTrue(orderItems.size() == 1, "拼团时,只允许选择一个商品");
// 获取商品信息
TradeOrderItemDO item = orderItems.get(0);
// 校验是否满足拼团活动相关限制
TradeOrderItemDO item = orderItems.get(0);
combinationRecordApi.validateCombinationRecord(order.getUserId(), order.getCombinationActivityId(),
order.getCombinationHeadId(), item.getSkuId(), item.getCount());
// TODO @puhui999这里还要限制下是不是已经 createOrder就是还没支付的时候重复下单了需要校验下不然的话一个拼团可以下多个单子了
}
@Override