code review:拼团记录流程

This commit is contained in:
YunaiV 2023-10-11 11:04:22 +08:00
parent 708af92a0f
commit 670a098aa8
15 changed files with 37 additions and 62 deletions

View File

@ -6,8 +6,6 @@ import cn.iocoder.yudao.module.promotion.api.combination.dto.CombinationValidate
import javax.validation.Valid;
// TODO @芋艿后面也再撸撸这几个接口
/**
* 拼团记录 API 接口
*
@ -30,7 +28,7 @@ public interface CombinationRecordApi {
* 创建开团记录
*
* @param reqDTO 请求 DTO
* @return key 开团记录编号 value 团长编号
* @return key 开团记录编号value 团长编号
*/
KeyValue<Long, Long> createCombinationRecord(@Valid CombinationRecordCreateReqDTO reqDTO);

View File

@ -29,6 +29,7 @@ public class CombinationRecordApiImpl implements CombinationRecordApi {
recordService.validateCombinationRecord(userId, activityId, headId, skuId, count);
}
// TODO @puhui999搞个创建的 RespDTO
@Override
public KeyValue<Long, Long> createCombinationRecord(CombinationRecordCreateReqDTO reqDTO) {
return recordService.createCombinationRecord(reqDTO);

View File

@ -40,6 +40,9 @@ public class CombinationRecordController {
@Lazy
private CombinationRecordService combinationRecordService;
// TODO @puhui999getBargainRecordPage getBargainRecordPage 是不是可以合并然后 CombinationRecordReqPageVO 加一个 headId
// 然后如果 headId 非空并且第一页单独多查询一条 head 放到第 0 个位置相当于说第一页特殊一点
@GetMapping("/page")
@Operation(summary = "获得拼团记录分页")
@PreAuthorize("@ss.hasPermission('promotion:combination-record:query')")
@ -47,6 +50,7 @@ public class CombinationRecordController {
PageResult<CombinationRecordDO> recordPage = combinationRecordService.getCombinationRecordPage(pageVO);
List<CombinationActivityDO> activities = combinationActivityService.getCombinationActivityListByIds(
convertSet(recordPage.getList(), CombinationRecordDO::getActivityId));
// TODO @puhui999商品没读取
return success(CombinationActivityConvert.INSTANCE.convert(recordPage, activities));
}

View File

@ -27,7 +27,6 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -49,20 +48,15 @@ public class AppBargainRecordController {
@Resource
private BargainHelpService bargainHelpService;
@Resource
@Lazy
private BargainRecordService bargainRecordService;
@Resource
@Lazy
private BargainActivityService bargainActivityService;
@Resource
private TradeOrderApi tradeOrderApi;
@Resource
@Lazy
private MemberUserApi memberUserApi;
@Resource
@Lazy
private ProductSpuApi productSpuApi;
@GetMapping("/get-summary")

View File

@ -9,7 +9,6 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityD
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
import java.util.Collection;
@ -87,6 +86,7 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> {
.last("LIMIT " + count));
}
// TODO @puhui999是不是返回 BargainActivityDO 更干净哈
/**
* 查询出指定 spuId spu 参加的活动最接近现在的一条记录多个的话一个 spuId 对应一个最近的活动编号
*
@ -94,7 +94,7 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> {
* @param status 状态
* @return 包含 spuId activityId map 对象列表
*/
default List<Map<String, Object>> selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(@Param("spuIds") Collection<Long> spuIds, @Param("status") Integer status) {
default List<Map<String, Object>> selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(Collection<Long> spuIds, Integer status) {
return selectMaps(new QueryWrapper<BargainActivityDO>()
.select("spu_id AS spuId, MAX(DISTINCT(id)) AS activityId") // 时间越大 id 也越大 直接用 id
.in("spu_id", spuIds)

View File

@ -120,6 +120,7 @@ public interface CombinationRecordMapper extends BaseMapperX<CombinationRecordDO
*/
default Long selectCountByHeadAndStatusAndVirtualGroup(Integer status, Boolean virtualGroup, Long headId) {
return selectCount(new QueryWrapper<CombinationRecordDO>()
// TODO @puhui999这种偏逻辑性的不要给 mapper 可以考虑拆成 2 mapper上层也是 2 service
.select(status == null && virtualGroup == null && headId == null, "DISTINCT (user_id)")
.eq(status != null, "status", status)
.eq(virtualGroup != null, "virtual_group", virtualGroup)

View File

@ -22,7 +22,7 @@ public class CombinationRecordExpireJob implements JobHandler {
@Override
@TenantJob
public String execute(String param) throws Exception {
public String execute(String param) {
KeyValue<Integer, Integer> keyValue = combinationRecordService.expireCombinationRecord();
return StrUtil.format("过期拼团 {} 个, 虚拟成团 {} 个", keyValue.getKey(), keyValue.getValue());
}

View File

@ -94,7 +94,6 @@ public class BargainActivityServiceImpl implements BargainActivityService {
} else if (count > 0) {
bargainActivityMapper.updateStock(id, count);
}
}
private void validateBargainConflict(Long spuId, Long activityId) {
@ -184,12 +183,13 @@ public class BargainActivityServiceImpl implements BargainActivityService {
@Override
public List<BargainActivityDO> getBargainActivityBySpuIdsAndStatus(Collection<Long> spuIds, Integer status) {
// 1.查询出指定 spuId spu 参加的活动最接近现在的一条记录多个的话一个 spuId 对应一个最近的活动编号
// 1. 查询出指定 spuId spu 参加的活动最接近现在的一条记录多个的话一个 spuId 对应一个最近的活动编号
// TODO @puhui999我想了下这种是不是只展示当前正在进行中的已经结束或者未开始的可能没啥意义
List<Map<String, Object>> spuIdAndActivityIdMaps = bargainActivityMapper.selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(spuIds, status);
if (CollUtil.isEmpty(spuIdAndActivityIdMaps)) {
return Collections.emptyList();
}
// 2.查询活动详情
// 2. 查询活动详情
return bargainActivityMapper.selectListByIds(convertSet(spuIdAndActivityIdMaps, map -> MapUtil.getLong(map, "activityId")));
}

View File

@ -146,7 +146,6 @@ public interface CombinationRecordService {
*/
PageResult<CombinationRecordDO> getCombinationRecordPage2(CombinationRecordReqPage2VO pageVO);
/**
* 拼团活动获得拼团记录数量 Map
*
@ -159,7 +158,6 @@ public interface CombinationRecordService {
@Nullable Integer status,
@Nullable Long headId);
/**
* 获取拼团记录
*

View File

@ -23,7 +23,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 org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -52,7 +51,6 @@ import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
public class CombinationRecordServiceImpl implements CombinationRecordService {
@Resource
@Lazy
private CombinationActivityService combinationActivityService;
@Resource
private CombinationRecordMapper combinationRecordMapper;
@ -60,16 +58,13 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
@Resource
private MemberUserApi memberUserApi;
@Resource
@Lazy
private ProductSpuApi productSpuApi;
@Resource
@Lazy
private ProductSkuApi productSkuApi;
@Resource
private TradeOrderApi tradeOrderApi;
// TODO @芋艿在详细预览下
@Override
@Transactional(rollbackFor = Exception.class)
public void updateCombinationRecordStatusByUserIdAndOrderId(Integer status, Long userId, Long orderId) {
@ -77,6 +72,7 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
CombinationRecordDO record = validateCombinationRecord(userId, orderId);
// 更新状态
// TODO @puhui999不要整个更新new 一个出来why例如说两个线程都去更新这样存在相互覆盖的问题
record.setStatus(status);
combinationRecordMapper.updateById(record);
}
@ -169,16 +165,16 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
@Override
@Transactional(rollbackFor = Exception.class)
public KeyValue<Long, Long> createCombinationRecord(CombinationRecordCreateReqDTO reqDTO) {
// 1.校验拼团活动
// 1. 校验拼团活动
KeyValue<CombinationActivityDO, CombinationProductDO> keyValue = validateCombinationRecord(reqDTO.getUserId(),
reqDTO.getActivityId(), reqDTO.getHeadId(), reqDTO.getSkuId(), reqDTO.getCount());
// 2.组合数据创建拼团记录
// 2. 组合数据创建拼团记录
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
CombinationRecordDO record = CombinationActivityConvert.INSTANCE.convert(reqDTO, keyValue.getKey(), user, spu, sku);
// 2.1.如果是团长需要设置 headId CombinationRecordDO#HEAD_ID_GROUP
// 2.1. 如果是团长需要设置 headId CombinationRecordDO#HEAD_ID_GROUP
if (record.getHeadId() == null) {
record.setStartTime(LocalDateTime.now())
.setExpireTime(keyValue.getKey().getStartTime().plusHours(keyValue.getKey().getLimitDuration()))
@ -191,12 +187,10 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
}
combinationRecordMapper.insert(record);
if (ObjUtil.equal(CombinationRecordDO.HEAD_ID_GROUP, record.getHeadId())) {
return new KeyValue<>(record.getId(), record.getHeadId());
// 3. 更新拼团记录
if (ObjUtil.notEqual(CombinationRecordDO.HEAD_ID_GROUP, record.getHeadId())) {
updateCombinationRecordWhenCreate(reqDTO.getHeadId(), keyValue.getKey());
}
// 3更新拼团记录
updateCombinationRecordWhenCreate(reqDTO.getHeadId(), keyValue.getKey());
return new KeyValue<>(record.getId(), record.getHeadId());
}
@ -349,6 +343,7 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
@Override
public KeyValue<Integer, Integer> expireCombinationRecord() {
// TODO @puhui999数字一般是 1. 2. 这种格式哈
// 1获取所有正在进行中的过期的父拼团
List<CombinationRecordDO> headExpireRecords = combinationRecordMapper.selectListByHeadIdAndStatusAndExpireTimeLt(
CombinationRecordDO.HEAD_ID_GROUP, CombinationRecordStatusEnum.IN_PROGRESS.getStatus(), LocalDateTime.now());
@ -356,18 +351,21 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
return new KeyValue<>(0, 0);
}
// 2.获取拼团活动
// 2. 获取拼团活动
// TODO @puhui999在自己模块里变量可以简略点例如说 activityList
List<CombinationActivityDO> combinationActivities = combinationActivityService.getCombinationActivityListByIds(
convertSet(headExpireRecords, CombinationRecordDO::getActivityId));
Map<Long, CombinationActivityDO> activityMap = convertMap(combinationActivities, CombinationActivityDO::getId);
// 3.校验是否虚拟成团
// TODO @puhui999job 一般不建议异步跑因为可能下次跑结果上次还没跑完
// TODO 这里我们可以每个 record 处理下然后按照是否需要虚拟拼团各搞一个方法逻辑 + 事务这样保证 job 里面尽量不要大事务而是 n 个独立小事务的处理
// 3. 校验是否虚拟成团
List<CombinationRecordDO> virtualGroupHeadRecords = new ArrayList<>(); // 虚拟成团
for (Iterator<CombinationRecordDO> iterator = headExpireRecords.iterator(); iterator.hasNext(); ) {
CombinationRecordDO record = iterator.next();
// 3.1 不匹配则直接跳过
CombinationActivityDO activityDO = activityMap.get(record.getActivityId());
if (activityDO == null || !activityDO.getVirtualGroup()) { // 取不到活动的或者不是虚拟拼团的
CombinationActivityDO activity = activityMap.get(record.getActivityId());
if (activity == null || !activity.getVirtualGroup()) { // 取不到活动的或者不是虚拟拼团的
continue;
}
// 3.2 匹配则移除添加到虚拟成团中并结束寻找
@ -380,7 +378,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
getSelf().handleExpireRecord(headExpireRecords);
// 5.虚拟成团
getSelf().handleVirtualGroupRecord(virtualGroupHeadRecords);
return new KeyValue<>(headExpireRecords.size(), virtualGroupHeadRecords.size());
}
@ -401,7 +398,6 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
headsAndRecords.forEach(item -> {
tradeOrderApi.cancelPaidOrder(item.getUserId(), item.getOrderId());
});
}
@Async
@ -410,9 +406,9 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
return;
}
// 1.团员补齐
// 1. 团员补齐
combinationRecordMapper.insertBatch(CombinationActivityConvert.INSTANCE.convertVirtualGroupList(virtualGroupHeadRecords));
// 2.更新拼团记录
// 2. 更新拼团记录
updateBatchCombinationRecords(virtualGroupHeadRecords, CombinationRecordStatusEnum.SUCCESS);
}

View File

@ -39,16 +39,7 @@ 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);
// TODO 芋艿需要优化下
/**
* 取消支付订单
*

View File

@ -5,7 +5,6 @@ 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.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -26,7 +25,6 @@ public class TradeOrderApiImpl implements TradeOrderApi {
@Resource
private TradeOrderUpdateService tradeOrderUpdateService;
@Resource
@Lazy
private TradeOrderQueryService tradeOrderQueryService;
@Override
@ -44,11 +42,6 @@ public class TradeOrderApiImpl implements TradeOrderApi {
return tradeOrderQueryService.getOrderSummary(beginTime, endTime);
}
@Override
public void updateOrderCombinationInfo(Long orderId, Long activityId, Long combinationRecordId, Long headId) {
tradeOrderUpdateService.updateOrderCombinationInfo(orderId, activityId, combinationRecordId, headId);
}
@Override
public void cancelPaidOrder(Long userId, Long orderId) {
tradeOrderUpdateService.cancelPaidOrder(userId, orderId);

View File

@ -110,6 +110,7 @@ public interface TradeOrderMapper extends BaseMapperX<TradeOrderDO> {
return selectOne(TradeOrderDO::getPickUpVerifyCode, pickUpVerifyCode);
}
// TODO @hui999是不是只针对 combinationActivityId 的查询呀
default TradeOrderDO selectByUserIdAndActivityIdAndStatus(Long userId, Long activityId, Integer status) {
return selectOne(new LambdaQueryWrapperX<TradeOrderDO>()
.and(q -> q.eq(TradeOrderDO::getUserId, userId)

View File

@ -657,7 +657,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
public void updateOrderAddress(TradeOrderUpdateAddressReqVO reqVO) {
// 校验交易订单
TradeOrderDO order = validateOrderExists(reqVO.getId());
// 只有待发货状态才可以修改订单收货地址
// 只有待发货状态才可以修改订单收货地址
if (!TradeOrderStatusEnum.isUndelivered(order.getStatus())) {
throw exception(ORDER_UPDATE_ADDRESS_FAIL_STATUS_NOT_DELIVERED);
}
@ -823,11 +823,11 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelPaidOrder(Long userId, Long orderId) {
// TODO 芋艿这里实现要优化下
TradeOrderDO order = tradeOrderMapper.selectOrderByIdAndUserId(orderId, userId);
if (order == null) {
throw exception(ORDER_NOT_FOUND);
}
cancelOrder0(order, TradeOrderCancelTypeEnum.MEMBER_CANCEL);
}

View File

@ -10,7 +10,6 @@ import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@ -29,14 +28,11 @@ import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.ORDER_DELIV
public class TradeCombinationOrderHandler implements TradeOrderHandler {
@Resource
@Lazy
private TradeOrderUpdateService orderUpdateService;
@Resource
@Lazy
private TradeOrderQueryService orderQueryService;
@Resource
@Lazy
private CombinationRecordApi combinationRecordApi;
@Override
@ -51,7 +47,9 @@ public class TradeCombinationOrderHandler implements TradeOrderHandler {
TradeOrderItemDO item = orderItems.get(0);
combinationRecordApi.validateCombinationRecord(order.getUserId(), order.getCombinationActivityId(),
order.getCombinationHeadId(), item.getSkuId(), item.getCount());
// 2. 校验该用户是否存在未支付的拼团活动订单就是还没支付的时候重复下单了需要校验下不然的话一个拼团可以下多个单子了
// 2. 校验该用户是否存在未支付的拼团活动订单避免一个拼团可以下多个单子了
// TODO @puhui999只校验未支付的拼团订单噢
TradeOrderDO activityOrder = orderQueryService.getActivityOrderByUserIdAndActivityIdAndStatus(
order.getUserId(), order.getCombinationActivityId(), TradeOrderStatusEnum.UNPAID.getStatus());
if (activityOrder != null) {