订单:重构拼团下单流程更改为订单支付后再创建拼团记录

This commit is contained in:
puhui999 2023-10-07 16:30:28 +08:00
parent c95317577d
commit c485c6e04d
14 changed files with 118 additions and 141 deletions

View File

@ -4,7 +4,6 @@ import cn.iocoder.yudao.module.promotion.api.combination.dto.CombinationRecordCr
import cn.iocoder.yudao.module.promotion.api.combination.dto.CombinationValidateJoinRespDTO;
import javax.validation.Valid;
import java.time.LocalDateTime;
// TODO @芋艿后面也再撸撸这几个接口
@ -42,14 +41,6 @@ public interface CombinationRecordApi {
*/
boolean isCombinationRecordSuccess(Long userId, Long orderId);
/**
* 更新拼团状态为成功
*
* @param userId 用户编号
* @param orderId 订单编号
*/
void updateRecordStatusToSuccess(Long userId, Long orderId);
/**
* 更新拼团状态为失败
*
@ -58,15 +49,6 @@ public interface CombinationRecordApi {
*/
void updateRecordStatusToFailed(Long userId, Long orderId);
/**
* 更新拼团状态为 进行中
*
* @param userId 用户编号
* @param orderId 订单编号
* @param startTime 开始时间
*/
void updateRecordStatusToInProgress(Long userId, Long orderId, LocalDateTime startTime);
/**
* 下单前校验是否满足拼团活动条件
*

View File

@ -8,7 +8,6 @@ import cn.iocoder.yudao.module.promotion.service.combination.CombinationRecordSe
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;
@ -44,22 +43,11 @@ public class CombinationRecordApiImpl implements CombinationRecordApi {
return CombinationRecordStatusEnum.isSuccess(combinationRecord.getStatus());
}
@Override
public void updateRecordStatusToSuccess(Long userId, Long orderId) {
recordService.updateCombinationRecordStatusByUserIdAndOrderId(CombinationRecordStatusEnum.SUCCESS.getStatus(), userId, orderId);
}
@Override
public void updateRecordStatusToFailed(Long userId, Long orderId) {
recordService.updateCombinationRecordStatusByUserIdAndOrderId(CombinationRecordStatusEnum.FAILED.getStatus(), userId, orderId);
}
@Override
public void updateRecordStatusToInProgress(Long userId, Long orderId, LocalDateTime startTime) {
recordService.updateRecordStatusAndStartTimeByUserIdAndOrderId(CombinationRecordStatusEnum.IN_PROGRESS.getStatus(),
userId, orderId, startTime);
}
@Override
public CombinationValidateJoinRespDTO validateJoinCombination(Long userId, Long activityId, Long headId, Long skuId, Integer count) {
return recordService.validateJoinCombination(userId, activityId, headId, skuId, count);

View File

@ -22,11 +22,13 @@ import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.record.Ap
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationProductDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationRecordDO;
import cn.iocoder.yudao.module.promotion.enums.combination.CombinationRecordStatusEnum;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@ -110,14 +112,19 @@ public interface CombinationActivityConvert {
CombinationActivityDO activity, MemberUserRespDTO user,
ProductSpuRespDTO spu, ProductSkuRespDTO sku) {
return convert(reqDTO)
.setCount(reqDTO.getCount()).setUserCount(1)
.setHeadId(reqDTO.getHeadId()) // 显示性再设置一下
.setCount(reqDTO.getCount())
.setVirtualGroup(false)
.setStatus(CombinationRecordStatusEnum.IN_PROGRESS.getStatus()) // 创建后默认状态为进行中
.setStartTime(LocalDateTime.now())
.setExpireTime(activity.getStartTime().plusHours(activity.getLimitDuration()))
.setUserSize(activity.getUserSize())
.setUserCount(1) // 默认就是 1 插入后会接着更新一次所有的拼团记录
.setNickname(user.getNickname())
.setAvatar(user.getAvatar())
.setSpuName(spu.getName())
.setPicUrl(sku.getPicUrl());
}
List<AppCombinationActivityRespVO> convertAppList(List<CombinationActivityDO> list);

View File

@ -32,12 +32,6 @@ public interface CombinationRecordMapper extends BaseMapperX<CombinationRecordDO
CombinationRecordDO::getOrderId, orderId);
}
default List<CombinationRecordDO> selectListByUserIdAndStatus(Long userId, Integer status) {
return selectList(new LambdaQueryWrapperX<CombinationRecordDO>()
.eq(CombinationRecordDO::getUserId, userId)
.eq(CombinationRecordDO::getStatus, status));
}
/**
* 查询拼团记录
*

View File

@ -10,7 +10,6 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationP
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationRecordDO;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -52,16 +51,6 @@ public interface CombinationRecordService {
*/
void createCombinationRecord(CombinationRecordCreateReqDTO reqDTO);
/**
* 更新拼团状态和开始时间
*
* @param status 状态
* @param userId 用户编号
* @param orderId 订单编号
* @param startTime 开始时间
*/
void updateRecordStatusAndStartTimeByUserIdAndOrderId(Integer status, Long userId, Long orderId, LocalDateTime startTime);
/**
* 获得拼团记录
*

View File

@ -2,7 +2,6 @@ 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;
@ -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.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;
@ -30,7 +28,7 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -80,30 +78,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
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) {
// 校验拼团是否存在
CombinationRecordDO recordDO = recordMapper.selectByUserIdAndOrderId(userId, orderId);
@ -143,16 +117,16 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
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);
}
}
// 3校验当前活动是否结束
} else {
// 3校验当前活动是否结束(自己是父拼团的时候才校验活动是否结束)
if (beforeNow(activity.getEndTime())) {
throw exception(COMBINATION_RECORD_FAILED_TIME_END);
}
}
// 4校验活动商品是否存在
CombinationProductDO product = combinationActivityService.selectByActivityIdAndSkuId(activityId, skuId);
@ -187,19 +161,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
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);
}
@ -215,9 +176,62 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku));
// TODO @puhui999status 未设置headId 未设置
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, activity, user, spu, sku));
CombinationRecordDO recordDO = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku);
recordMapper.insert(recordDO);
// 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

View File

@ -30,15 +30,6 @@ public interface TradeOrderApi {
*/
TradeOrderRespDTO getOrder(Long id);
// TODO 芋艿看看是不是可以删除掉
/**
* 获取订单状态
*
* @param id 订单编号
* @return 订单状态
*/
Integer getOrderStatus(Long id);
/**
* 获取订单统计
*
@ -48,4 +39,14 @@ public interface TradeOrderApi {
*/
TradeOrderSummaryRespDTO getOrderSummary(LocalDateTime beginTime, LocalDateTime endTime);
/**
* 更新拼团相关信息到订单
*
* @param orderId 订单编号
* @param activityId 拼团活动编号
* @param combinationRecordId 拼团记录编号
* @param headId 团长编号
*/
void updateOrderCombinationInfo(Long orderId, Long activityId, Long combinationRecordId, Long headId);
}

View File

@ -3,20 +3,16 @@ package cn.iocoder.yudao.module.trade.api.order;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderRespDTO;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderSummaryRespDTO;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.ORDER_NOT_FOUND;
/**
* 订单 API 接口实现类
*
@ -28,6 +24,8 @@ public class TradeOrderApiImpl implements TradeOrderApi {
@Resource
private TradeOrderQueryService tradeOrderQueryService;
@Resource
private TradeOrderUpdateService tradeOrderUpdateService;
@Override
public List<TradeOrderRespDTO> getOrderList(Collection<Long> ids) {
@ -39,18 +37,14 @@ public class TradeOrderApiImpl implements TradeOrderApi {
return TradeOrderConvert.INSTANCE.convert(tradeOrderQueryService.getOrder(id));
}
@Override
public Integer getOrderStatus(Long id) {
TradeOrderDO order = tradeOrderQueryService.getOrder(id);
if (order == null) {
throw exception(ORDER_NOT_FOUND);
}
return order.getStatus();
}
@Override
public TradeOrderSummaryRespDTO getOrderSummary(LocalDateTime beginTime, LocalDateTime endTime) {
return tradeOrderQueryService.getOrderSummary(beginTime, endTime);
}
@Override
public void updateOrderCombinationInfo(Long orderId, Long activityId, Long combinationRecordId, Long headId) {
tradeOrderUpdateService.updateOrderCombinationInfo(orderId, activityId, combinationRecordId, headId);
}
}

View File

@ -52,7 +52,6 @@ public class AppTradeOrderSettlementReqVO {
private Long seckillActivityId;
// ========== 拼团活动相关字段 ==========
// TODO @puhui999是不是拼团记录的编号哈
@Schema(description = "拼团活动编号", example = "1024")
private Long combinationActivityId;

View File

@ -170,4 +170,14 @@ public interface TradeOrderUpdateService {
*/
int createOrderItemCommentBySystem();
/**
* 更新拼团相关信息到订单
*
* @param orderId 订单编号
* @param activityId 拼团活动编号
* @param combinationRecordId 拼团记录编号
* @param headId 团长编号
*/
void updateOrderCombinationInfo(Long orderId, Long activityId, Long combinationRecordId, Long headId);
}

View File

@ -331,7 +331,8 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
}
// 3订单支付成功后
tradeOrderHandlers.forEach(handler -> handler.afterPayOrder(order));
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(id);
tradeOrderHandlers.forEach(handler -> handler.afterPayOrder(order, orderItems));
// 4.1 增加用户积分赠送
addUserPoint(order.getUserId(), order.getGivePoint(), MemberPointBizTypeEnum.ORDER_GIVE, order.getId());
@ -909,6 +910,14 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
return count;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateOrderCombinationInfo(Long orderId, Long activityId, Long combinationRecordId, Long headId) {
tradeOrderMapper.updateById(
new TradeOrderDO().setId(orderId).setCombinationActivityId(activityId)
.setCombinationRecordId(combinationRecordId).setCombinationHeadId(headId));
}
/**
* 创建单个订单的评论
*

View File

@ -39,29 +39,18 @@ public class TradeCombinationHandler implements TradeOrderHandler {
}
@Override
public void afterOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 如果不是拼团订单则结束
if (ObjectUtil.notEqual(TradeOrderTypeEnum.COMBINATION.getType(), order.getType())) {
return;
}
Assert.isTrue(orderItems.size() == 1, "拼团时,只允许选择一个商品");
// 获取商品信息
TradeOrderItemDO item = orderItems.get(0);
// 创建拼团记录
// TODO puhui这里应该先不创建等支付好才去创建另外创建好后需要更新编号到订单
combinationRecordApi.createCombinationRecord(TradeOrderConvert.INSTANCE.convert(order, item));
}
@Override
public void afterPayOrder(TradeOrderDO order) {
// 如果不是拼团订单则结束
if (ObjectUtil.notEqual(TradeOrderTypeEnum.COMBINATION.getType(), order.getType())) {
return;
}
// 更新拼团状态 TODO puhui999订单支付失败或订单支付过期删除这条拼团记录
combinationRecordApi.updateRecordStatusToInProgress(order.getUserId(), order.getId(), order.getPayTime());
}
}

View File

@ -33,8 +33,10 @@ public interface TradeOrderHandler {
* 支付订单后
*
* @param order 订单
* @param orderItems 订单项
*/
default void afterPayOrder(TradeOrderDO order) {}
default void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
}
/**
* 订单取消

View File

@ -68,7 +68,6 @@ public class TradePriceCalculateReqBO {
private Long seckillActivityId;
// ========== 拼团活动相关字段 ==========
// TODO @puhui999是不是拼团记录的编号哈
/**
* 拼团活动编号
*/