|
|
|
@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.promotion.service.combination;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
import cn.hutool.core.util.ObjUtil;
|
|
|
|
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.core.KeyValue;
|
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
|
@ -22,7 +21,6 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationR
|
|
|
|
import cn.iocoder.yudao.module.promotion.dal.mysql.combination.CombinationRecordMapper;
|
|
|
|
import cn.iocoder.yudao.module.promotion.dal.mysql.combination.CombinationRecordMapper;
|
|
|
|
import cn.iocoder.yudao.module.promotion.enums.combination.CombinationRecordStatusEnum;
|
|
|
|
import cn.iocoder.yudao.module.promotion.enums.combination.CombinationRecordStatusEnum;
|
|
|
|
import cn.iocoder.yudao.module.trade.api.order.TradeOrderApi;
|
|
|
|
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.context.annotation.Lazy;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
@ -30,7 +28,7 @@ import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
@ -80,30 +78,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
|
|
|
|
recordMapper.updateById(record);
|
|
|
|
recordMapper.updateById(record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void updateRecordStatusAndStartTimeByUserIdAndOrderId(Integer status, Long userId, Long orderId, LocalDateTime startTime) {
|
|
|
|
|
|
|
|
CombinationRecordDO record = validateCombinationRecord(userId, orderId);
|
|
|
|
|
|
|
|
// 更新状态
|
|
|
|
|
|
|
|
record.setStatus(status);
|
|
|
|
|
|
|
|
// 更新开始时间
|
|
|
|
|
|
|
|
record.setStartTime(startTime);
|
|
|
|
|
|
|
|
recordMapper.updateById(record);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新拼团参入人数
|
|
|
|
|
|
|
|
List<CombinationRecordDO> records = recordMapper.selectListByHeadIdAndStatus(record.getHeadId(), status);
|
|
|
|
|
|
|
|
if (CollUtil.isNotEmpty(records)) {
|
|
|
|
|
|
|
|
records.forEach(item -> {
|
|
|
|
|
|
|
|
item.setUserCount(records.size());
|
|
|
|
|
|
|
|
// 校验拼团是否满足要求
|
|
|
|
|
|
|
|
if (ObjectUtil.equal(records.size(), record.getUserSize())) {
|
|
|
|
|
|
|
|
item.setStatus(CombinationRecordStatusEnum.SUCCESS.getStatus());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
recordMapper.updateBatch(records);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private CombinationRecordDO validateCombinationRecord(Long userId, Long orderId) {
|
|
|
|
private CombinationRecordDO validateCombinationRecord(Long userId, Long orderId) {
|
|
|
|
// 校验拼团是否存在
|
|
|
|
// 校验拼团是否存在
|
|
|
|
CombinationRecordDO recordDO = recordMapper.selectByUserIdAndOrderId(userId, orderId);
|
|
|
|
CombinationRecordDO recordDO = recordMapper.selectByUserIdAndOrderId(userId, orderId);
|
|
|
|
@ -143,15 +117,15 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
|
|
|
|
if (ObjUtil.equal(record.getUserCount(), record.getUserSize())) {
|
|
|
|
if (ObjUtil.equal(record.getUserCount(), record.getUserSize())) {
|
|
|
|
throw exception(COMBINATION_RECORD_USER_FULL);
|
|
|
|
throw exception(COMBINATION_RECORD_USER_FULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 2.3、校验拼团是否过期
|
|
|
|
// 2.3、校验拼团是否过期(有父拼团的时候只校验父拼团的过期时间)
|
|
|
|
if (beforeNow(record.getExpireTime())) {
|
|
|
|
if (beforeNow(record.getExpireTime())) {
|
|
|
|
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
|
|
|
|
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 3、校验当前活动是否结束(自己是父拼团的时候才校验活动是否结束)
|
|
|
|
// 3、校验当前活动是否结束
|
|
|
|
if (beforeNow(activity.getEndTime())) {
|
|
|
|
if (beforeNow(activity.getEndTime())) {
|
|
|
|
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
|
|
|
|
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4、校验活动商品是否存在
|
|
|
|
// 4、校验活动商品是否存在
|
|
|
|
@ -187,19 +161,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
|
|
|
|
throw exception(COMBINATION_RECORD_FAILED_TOTAL_LIMIT_COUNT_EXCEED);
|
|
|
|
throw exception(COMBINATION_RECORD_FAILED_TOTAL_LIMIT_COUNT_EXCEED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 7、校验拼团记录是否存在未支付的订单(如果存在未支付的订单则不允许发起新的拼团)
|
|
|
|
|
|
|
|
CombinationRecordDO record = findFirst(recordList, item -> ObjectUtil.equals(item.getStatus(), null));
|
|
|
|
|
|
|
|
if (record == null) {
|
|
|
|
|
|
|
|
return new KeyValue<>(activity, product);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 7.1、查询关联的订单是否已经支付
|
|
|
|
|
|
|
|
// 当前 activityId 已经有未支付的订单,不允许在发起新的;要么支付,要么去掉先;
|
|
|
|
|
|
|
|
// TODO 芋艿:看看是不是可以删除掉;
|
|
|
|
|
|
|
|
Integer orderStatus = tradeOrderApi.getOrderStatus(record.getOrderId());
|
|
|
|
|
|
|
|
if (ObjectUtil.equal(orderStatus, TradeOrderStatusEnum.UNPAID.getStatus())) {
|
|
|
|
|
|
|
|
throw exception(COMBINATION_RECORD_FAILED_ORDER_STATUS_UNPAID);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new KeyValue<>(activity, product);
|
|
|
|
return new KeyValue<>(activity, product);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -215,9 +176,62 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
|
|
|
|
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
|
|
|
|
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
|
|
|
|
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
|
|
|
|
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
|
|
|
|
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
|
|
|
|
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
|
|
|
|
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku));
|
|
|
|
CombinationRecordDO recordDO = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku);
|
|
|
|
// TODO @puhui999:status 未设置;headId 未设置
|
|
|
|
recordMapper.insert(recordDO);
|
|
|
|
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, activity, user, spu, sku));
|
|
|
|
|
|
|
|
|
|
|
|
// 3、如果是团长需要设置 headId 为它自己
|
|
|
|
|
|
|
|
if (reqDTO.getHeadId() == null) {
|
|
|
|
|
|
|
|
recordMapper.updateById(new CombinationRecordDO().setId(recordDO.getId()).setHeadId(recordDO.getId()));
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO 这里要不要弄成异步的
|
|
|
|
|
|
|
|
// 4、更新拼团相关信息到订单
|
|
|
|
|
|
|
|
updateOrderCombinationInfo(recordDO.getOrderId(), recordDO.getActivityId(), recordDO.getId(), recordDO.getHeadId());
|
|
|
|
|
|
|
|
// 4、更新拼团记录
|
|
|
|
|
|
|
|
updateCombinationRecords(keyValue.getKey(), reqDTO.getHeadId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新拼团相关信息到订单
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param orderId 订单编号
|
|
|
|
|
|
|
|
* @param activityId 拼团活动编号
|
|
|
|
|
|
|
|
* @param combinationRecordId 拼团记录编号
|
|
|
|
|
|
|
|
* @param headId 团长编号
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void updateOrderCombinationInfo(Long orderId, Long activityId, Long combinationRecordId, Long headId) {
|
|
|
|
|
|
|
|
tradeOrderApi.updateOrderCombinationInfo(orderId, activityId, combinationRecordId, headId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新拼团记录
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param activity 活动
|
|
|
|
|
|
|
|
* @param headId 团长编号
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void updateCombinationRecords(CombinationActivityDO activity, Long headId) {
|
|
|
|
|
|
|
|
List<CombinationRecordDO> records = recordMapper.selectList(CombinationRecordDO::getHeadId, headId);
|
|
|
|
|
|
|
|
List<CombinationRecordDO> updateRecords = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (CollUtil.isEmpty(records)) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
recordMapper.updateBatch(updateRecords);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
|