promotion:优化拼团记录的详情代码

This commit is contained in:
YunaiV 2023-10-07 13:58:12 +08:00
parent c7b5df930e
commit ec2c3e508a
6 changed files with 49 additions and 48 deletions

View File

@ -1,8 +1,6 @@
package cn.iocoder.yudao.module.promotion.controller.app.combination;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.record.AppCombinationRecordDetailRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.record.AppCombinationRecordRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.record.AppCombinationRecordSummaryRespVO;
@ -23,6 +21,7 @@ import javax.annotation.Resource;
import javax.validation.constraints.Max;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@ -61,7 +60,7 @@ public class AppCombinationRecordController {
@Operation(summary = "获得最近 n 条拼团记录(团长发起的)")
@Parameters({
@Parameter(name = "activityId", description = "拼团活动编号"),
@Parameter(name = "status", description = "状态"),
@Parameter(name = "status", description = "拼团状态"), // 对应 CombinationRecordStatusEnum 枚举
@Parameter(name = "count", description = "数量")
})
public CommonResult<List<AppCombinationRecordRespVO>> getHeadCombinationRecordList(
@ -69,44 +68,37 @@ public class AppCombinationRecordController {
@RequestParam("status") Integer status,
@RequestParam(value = "count", defaultValue = "20") @Max(20) Integer count) {
return success(CombinationActivityConvert.INSTANCE.convertList3(
combinationRecordService.getCombinationRecordListWithHead(activityId, status, count)));
combinationRecordService.getHeadCombinationRecordList(activityId, status, count)));
}
@GetMapping("/get-detail")
@Operation(summary = "获得拼团记录明细")
@Parameter(name = "id", description = "拼团记录编号", required = true, example = "1024")
public CommonResult<AppCombinationRecordDetailRespVO> getCombinationRecordDetail(@RequestParam("id") Long id) {
// 1查询这条记录
// 1. 查找这条拼团记录
CombinationRecordDO record = combinationRecordService.getCombinationRecordById(id);
if (record == null) {
return success(null);
}
AppCombinationRecordDetailRespVO detail = new AppCombinationRecordDetailRespVO();
List<CombinationRecordDO> records;
// 2判断是否为团长
if (record.getHeadId() == null) {
detail.setHeadRecord(CombinationActivityConvert.INSTANCE.convert(record));
// 2.1查找团员拼团记录
records = combinationRecordService.getCombinationRecordListByHeadId(record.getId());
} else {
// 2.2查找团长拼团记录
CombinationRecordDO headRecord = combinationRecordService.getCombinationRecordById(record.getHeadId());
if (headRecord == null) {
return success(null);
}
detail.setHeadRecord(CombinationActivityConvert.INSTANCE.convert(headRecord));
// 2.3查找团员拼团记录
records = combinationRecordService.getCombinationRecordListByHeadId(headRecord.getId());
// 2. 查找该拼团的参团记录
CombinationRecordDO headRecord;
List<CombinationRecordDO> memberRecords;
if (Objects.equals(record.getHeadId(), CombinationRecordDO.HEAD_ID_GROUP)) { // 情况一团长
headRecord = record;
memberRecords = combinationRecordService.getCombinationRecordListByHeadId(record.getId());
} else { // 情况二团员
headRecord = combinationRecordService.getCombinationRecordById(record.getHeadId());
memberRecords = combinationRecordService.getCombinationRecordListByHeadId(headRecord.getId());
}
detail.setMemberRecords(CombinationActivityConvert.INSTANCE.convertList3(records));
// 3获取当前用户参团记录订单编号
CombinationRecordDO userRecord = CollectionUtils.findFirst(records, r -> ObjectUtil.equal(r.getUserId(), getLoginUserId()));
detail.setOrderId(userRecord == null ? null : userRecord.getOrderId()); // 如果没参团返回 null
return success(detail);
// 3. 拼接数据
return success(CombinationActivityConvert.INSTANCE.convert(getLoginUserId(), headRecord, memberRecords));
}
// TODO @puhui新增一个取消拼团的接口cancel
// 1. 需要先校验拼团记录未完成
// 2. Order 那增加一个 cancelPaidOrder 接口用于取消已支付的订单
// 3. order 完成后取消拼团记录另外如果它是团长则顺序下单时间继承
}

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.convert.combination;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
@ -16,6 +17,7 @@ import cn.iocoder.yudao.module.promotion.controller.admin.combination.vo.product
import cn.iocoder.yudao.module.promotion.controller.admin.combination.vo.recrod.CombinationRecordRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.activity.AppCombinationActivityDetailRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.activity.AppCombinationActivityRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.record.AppCombinationRecordDetailRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.combination.vo.record.AppCombinationRecordRespVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationProductDO;
@ -168,4 +170,16 @@ public interface CombinationActivityConvert {
PageResult<CombinationRecordRespVO> convert(PageResult<CombinationRecordDO> result);
default AppCombinationRecordDetailRespVO convert(Long userId, CombinationRecordDO headRecord, List<CombinationRecordDO> memberRecords) {
AppCombinationRecordDetailRespVO respVO = new AppCombinationRecordDetailRespVO()
.setHeadRecord(convert(headRecord)).setMemberRecords(convertList3(memberRecords));
// 处理自己参与拼团的 orderId
CombinationRecordDO userRecord = CollectionUtils.findFirst(memberRecords, r -> ObjectUtil.equal(r.getUserId(), userId));
if (userRecord == null && ObjectUtil.equal(headRecord.getUserId(), userId)) {
userRecord = headRecord;
}
respVO.setOrderId(userRecord == null ? null : userRecord.getOrderId());
return respVO;
}
}

View File

@ -31,7 +31,7 @@ public class CombinationRecordDO extends BaseDO {
/**
* 团长编号 - 团长
*/
public static final Integer HEAD_ID_GROUP = 0;
public static final Long HEAD_ID_GROUP = 0L;
/**
* 编号主键自增

View File

@ -81,25 +81,18 @@ public interface CombinationRecordMapper extends BaseMapperX<CombinationRecordDO
.last("LIMIT " + count));
}
/**
* 获得最近 count 条拼团记录团长发起的
*
* @param activityId 拼团活动编号
* @param status 记录状态
* @param count 数量
* @return 拼团记录列表
*/
default List<CombinationRecordDO> selectList(Long activityId, Integer status, Integer count) {
default List<CombinationRecordDO> selectListByActivityIdAndStatusAndHeadId(Long activityId, Integer status,
Long headId, Integer count) {
return selectList(new LambdaQueryWrapperX<CombinationRecordDO>()
.eqIfPresent(CombinationRecordDO::getActivityId, activityId)
.eqIfPresent(CombinationRecordDO::getStatus, status)
.eq(CombinationRecordDO::getHeadId, null) // TODO 团长的 headId 是不是 null 还是自己的记录编号来着
.orderByDesc(CombinationRecordDO::getCreateTime)
.eq(CombinationRecordDO::getActivityId, activityId)
.eq(CombinationRecordDO::getStatus, status)
.eq(CombinationRecordDO::getHeadId, headId)
.orderByDesc(CombinationRecordDO::getId)
.last("LIMIT " + count));
}
default Map<Long, Integer> selectCombinationRecordCountMapByActivityIdAndStatusAndHeadId(Collection<Long> activityIds,
Integer status, Integer headId) {
Integer status, Long headId) {
// SQL count 查询
List<Map<String, Object>> result = selectMaps(new QueryWrapper<CombinationRecordDO>()
.select("COUNT(DISTINCT(user_id)) AS recordCount, activity_id AS activityId")

View File

@ -136,7 +136,7 @@ public interface CombinationRecordService {
* @param count 数量
* @return 拼团记录列表
*/
List<CombinationRecordDO> getCombinationRecordListWithHead(Long activityId, Integer status, Integer count);
List<CombinationRecordDO> getHeadCombinationRecordList(Long activityId, Integer status, Integer count);
/**
* 获取指定编号的拼团记录
@ -172,6 +172,6 @@ public interface CombinationRecordService {
*/
Map<Long, Integer> getCombinationRecordCountMapByActivity(Collection<Long> activityIds,
@Nullable Integer status,
@Nullable Integer headId);
@Nullable Long headId);
}

View File

@ -204,6 +204,7 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
// TODO @puhui999status 未设置headId 未设置
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, activity, user, spu, sku));
}
@ -253,8 +254,9 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
}
@Override
public List<CombinationRecordDO> getCombinationRecordListWithHead(Long activityId, Integer status, Integer count) {
return recordMapper.selectList(activityId, status, count);
public List<CombinationRecordDO> getHeadCombinationRecordList(Long activityId, Integer status, Integer count) {
return recordMapper.selectListByActivityIdAndStatusAndHeadId(activityId, status,
CombinationRecordDO.HEAD_ID_GROUP, count);
}
@Override
@ -274,7 +276,7 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
@Override
public Map<Long, Integer> getCombinationRecordCountMapByActivity(Collection<Long> activityIds,
@Nullable Integer status, @Nullable Integer headId) {
@Nullable Integer status, @Nullable Long headId) {
return recordMapper.selectCombinationRecordCountMapByActivityIdAndStatusAndHeadId(activityIds, status, headId);
}