【代码优化】拼团活动和SPU详情分开查询

This commit is contained in:
卢越 2024-09-05 10:55:23 +08:00
parent efd0a86007
commit 39a6eb4792
3 changed files with 6 additions and 30 deletions

View File

@ -100,32 +100,19 @@ public class CombinationActivityController {
// 转换活动列表
List<CombinationActivityRespVO> activityVOs = CombinationActivityConvert.INSTANCE.convertList(activities);
// 获取商品SPU列表和拼团产品列表
Set<Long> spuIds = activities.stream().map(CombinationActivityDO::getSpuId).collect(Collectors.toSet());
List<ProductSpuRespDTO> spuList = productSpuApi.getSpuList(spuIds);
// 获取拼团产品列表
Set<Long> activityIds = activities.stream().map(CombinationActivityDO::getId).collect(Collectors.toSet());
List<CombinationProductDO> productList = combinationActivityService.getCombinationProductListByActivityIds(activityIds);
// 创建SPU和产品的映射
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
Map<Long, List<CombinationProductDO>> productMap = convertMultiMap(productList, CombinationProductDO::getActivityId);
// 更新VO列表
activityVOs.forEach(vo -> {
ProductSpuRespDTO spu = spuMap.get(vo.getSpuId());
if (spu != null) {
vo.setSpuName(spu.getName())
.setPicUrl(spu.getPicUrl())
.setMarketPrice(spu.getMarketPrice());
}
vo.setProducts(CombinationActivityConvert.INSTANCE.convertList2(productMap.get(vo.getId())));
});
// 往活动VO赋值产品列表
activityVOs.forEach(vo -> vo.setProducts(CombinationActivityConvert.INSTANCE.convertList2(productMap.get(vo.getId()))));
return success(activityVOs);
}
@GetMapping("/page")
@Operation(summary = "获得拼团活动分页")
@PreAuthorize("@ss.hasPermission('promotion:combination-activity:query')")

View File

@ -27,16 +27,4 @@ public class CombinationActivityRespVO extends CombinationActivityBaseVO {
@Schema(description = "拼团商品", requiredMode = Schema.RequiredMode.REQUIRED)
private List<CombinationProductRespVO> products;
// ========== 商品字段 ==========
@Schema(description = "商品名称", requiredMode = Schema.RequiredMode.REQUIRED, // SPU name 读取
example = "618大促")
private String spuName;
@Schema(description = "商品主图", requiredMode = Schema.RequiredMode.REQUIRED, // SPU picUrl 读取
example = "https://www.iocoder.cn/xx.png")
private String picUrl;
@Schema(description = "商品市场价,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, // SPU marketPrice 读取
example = "50")
private Integer marketPrice;
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.promotion.controller.app.combination;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
@ -126,9 +127,9 @@ public class AppCombinationActivityController {
!ObjectUtil.equal(combinationActivityDO.getStatus(), CommonStatusEnum.DISABLE.getStatus()))
.toList();
// 如果没有有效的活动返回 null 或者适当的错误信息
// 如果没有有效的活动返回空列表
if (validActivities.isEmpty()) {
return success(null);
return success(ListUtil.empty());
}
// 2. 构建结果列表