mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 07:11:52 +08:00
【代码优化】拼团活动:优化拼团活动列表的获取
This commit is contained in:
parent
6d00b02795
commit
73a7ccbd75
@ -1,6 +1,7 @@
|
||||
package cn.iocoder.yudao.module.promotion.controller.admin.combination;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
|
||||
@ -22,15 +23,15 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.hutool.core.collection.CollectionUtil.newArrayList;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||
|
||||
@Tag(name = "管理后台 - 拼团活动")
|
||||
@RestController
|
||||
@ -89,28 +90,21 @@ public class CombinationActivityController {
|
||||
return success(CombinationActivityConvert.INSTANCE.convert(activity, products));
|
||||
}
|
||||
|
||||
@GetMapping("/detail-list")
|
||||
@Operation(summary = "获得拼团活动详情列表")
|
||||
@Parameter(name = "ids", description = "拼团活动编号列表", required = true, example = "[1,2,3]")
|
||||
@PreAuthorize("@ss.hasPermission('product:spu:query')")
|
||||
public CommonResult<List<CombinationActivityRespVO>> getCombinationActivityDetailList(@RequestParam("ids") Collection<Long> ids) {
|
||||
// 查询拼团活动列表
|
||||
List<CombinationActivityDO> activities = combinationActivityService.getCombinationActivityListByIds(ids);
|
||||
|
||||
// 转换活动列表
|
||||
List<CombinationActivityRespVO> activityVOs = CombinationActivityConvert.INSTANCE.convertList(activities);
|
||||
|
||||
// 获取拼团产品列表
|
||||
Set<Long> activityIds = activities.stream().map(CombinationActivityDO::getId).collect(Collectors.toSet());
|
||||
List<CombinationProductDO> productList = combinationActivityService.getCombinationProductListByActivityIds(activityIds);
|
||||
|
||||
// 创建SPU和产品的映射
|
||||
Map<Long, List<CombinationProductDO>> productMap = convertMultiMap(productList, CombinationProductDO::getActivityId);
|
||||
|
||||
// 往活动VO赋值产品列表
|
||||
activityVOs.forEach(vo -> vo.setProducts(CombinationActivityConvert.INSTANCE.convertList2(productMap.get(vo.getId()))));
|
||||
|
||||
return success(activityVOs);
|
||||
@GetMapping("/list-by-ids")
|
||||
@Operation(summary = "获得拼团活动列表,基于活动编号数组")
|
||||
@Parameter(name = "ids", description = "活动编号数组", required = true, example = "[1024, 1025]")
|
||||
public CommonResult<List<CombinationActivityRespVO>> getCombinationActivityListByIds(@RequestParam("ids") List<Long> ids) {
|
||||
// 1. 获得开启的活动列表
|
||||
List<CombinationActivityDO> activityList = combinationActivityService.getCombinationActivityListByIds(ids);
|
||||
activityList.removeIf(activity -> CommonStatusEnum.isDisable(activity.getStatus()));
|
||||
if (CollUtil.isEmpty(activityList)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
// 2. 拼接返回
|
||||
List<CombinationProductDO> productList = combinationActivityService.getCombinationProductListByActivityIds(
|
||||
convertList(activityList, CombinationActivityDO::getId));
|
||||
List<ProductSpuRespDTO> spuList = productSpuApi.getSpuList(convertList(activityList, CombinationActivityDO::getSpuId));
|
||||
return success(CombinationActivityConvert.INSTANCE.convertList(activityList, productList, spuList));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
|
@ -27,4 +27,14 @@ public class CombinationActivityRespVO extends CombinationActivityBaseVO {
|
||||
@Schema(description = "拼团商品", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private List<CombinationProductRespVO> products;
|
||||
|
||||
@Schema(description = "商品 SPU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "一个白菜")
|
||||
private String spuName; // 从 SPU 的 name 读取
|
||||
@Schema(description = "商品图片", requiredMode = Schema.RequiredMode.REQUIRED, example = "4096")
|
||||
private String picUrl; // 从 SPU 的 picUrl 读取
|
||||
@Schema(description = "商品市场价,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "50")
|
||||
private Integer marketPrice; // 从 SPU 的 marketPrice 读取
|
||||
|
||||
@Schema(description = "拼团金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
|
||||
private Integer combinationPrice; // 从 products 获取最小 price 读取
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
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;
|
||||
@ -15,28 +14,20 @@ import cn.iocoder.yudao.module.promotion.convert.combination.CombinationActivity
|
||||
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.service.combination.CombinationActivityService;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
|
||||
@Tag(name = "用户 APP - 拼团活动")
|
||||
@ -45,45 +36,12 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
|
||||
@Validated
|
||||
public class AppCombinationActivityController {
|
||||
|
||||
/**
|
||||
* {@link AppCombinationActivityRespVO} 缓存,通过它异步刷新 {@link #getCombinationActivityList0(Integer)} 所要的首页数据
|
||||
*/
|
||||
private final LoadingCache<Integer, List<AppCombinationActivityRespVO>> combinationActivityListCache = buildAsyncReloadingCache(Duration.ofSeconds(10L),
|
||||
new CacheLoader<Integer, List<AppCombinationActivityRespVO>>() {
|
||||
|
||||
@Override
|
||||
public List<AppCombinationActivityRespVO> load(Integer count) {
|
||||
return getCombinationActivityList0(count);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@Resource
|
||||
private CombinationActivityService activityService;
|
||||
|
||||
@Resource
|
||||
private ProductSpuApi spuApi;
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "获得拼团活动列表", description = "用于小程序首页")
|
||||
@Parameter(name = "count", description = "需要展示的数量", example = "6")
|
||||
public CommonResult<List<AppCombinationActivityRespVO>> getCombinationActivityList(
|
||||
@RequestParam(name = "count", defaultValue = "6") Integer count) {
|
||||
return success(combinationActivityListCache.getUnchecked(count));
|
||||
}
|
||||
|
||||
private List<AppCombinationActivityRespVO> getCombinationActivityList0(Integer count) {
|
||||
List<CombinationActivityDO> activityList = activityService.getCombinationActivityListByCount(count);
|
||||
if (CollUtil.isEmpty(activityList)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 拼接返回
|
||||
List<CombinationProductDO> productList = activityService.getCombinationProductListByActivityIds(
|
||||
convertList(activityList, CombinationActivityDO::getId));
|
||||
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(activityList, CombinationActivityDO::getSpuId));
|
||||
return CombinationActivityConvert.INSTANCE.convertAppList(activityList, productList, spuList);
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得拼团活动分页")
|
||||
public CommonResult<PageResult<AppCombinationActivityRespVO>> getCombinationActivityPage(PageParam pageParam) {
|
||||
@ -98,6 +56,23 @@ public class AppCombinationActivityController {
|
||||
return success(CombinationActivityConvert.INSTANCE.convertAppPage(pageResult, productList, spuList));
|
||||
}
|
||||
|
||||
@GetMapping("/list-by-ids")
|
||||
@Operation(summary = "获得拼团活动列表,基于活动编号数组")
|
||||
@Parameter(name = "ids", description = "活动编号数组", required = true, example = "[1024, 1025]")
|
||||
public CommonResult<List<AppCombinationActivityRespVO>> getCombinationActivityListByIds(@RequestParam("ids") List<Long> ids) {
|
||||
// 1. 获得开启的活动列表
|
||||
List<CombinationActivityDO> activityList = activityService.getCombinationActivityListByIds(ids);
|
||||
activityList.removeIf(activity -> CommonStatusEnum.isDisable(activity.getStatus()));
|
||||
if (CollUtil.isEmpty(activityList)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
// 2. 拼接返回
|
||||
List<CombinationProductDO> productList = activityService.getCombinationProductListByActivityIds(
|
||||
convertList(activityList, CombinationActivityDO::getId));
|
||||
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(activityList, CombinationActivityDO::getSpuId));
|
||||
return success(CombinationActivityConvert.INSTANCE.convertAppList(activityList, productList, spuList));
|
||||
}
|
||||
|
||||
@GetMapping("/get-detail")
|
||||
@Operation(summary = "获得拼团活动明细")
|
||||
@Parameter(name = "id", description = "活动编号", required = true, example = "1024")
|
||||
@ -114,38 +89,4 @@ public class AppCombinationActivityController {
|
||||
return success(CombinationActivityConvert.INSTANCE.convert3(activity, products));
|
||||
}
|
||||
|
||||
@GetMapping("/detail-list")
|
||||
@Operation(summary = "获得拼团活动明细")
|
||||
@Parameter(name = "ids", description = "活动编号列表", required = true, example = "[1024, 1025]")
|
||||
public CommonResult<List<AppCombinationActivityDetailRespVO>> getCombinationActivityDetailList(@RequestParam("ids") Collection<Long> ids) {
|
||||
// 1. 获取活动
|
||||
List<CombinationActivityDO> combinationActivityDOList = activityService.getCombinationActivityListByIds(ids);
|
||||
|
||||
// 过滤掉无效的活动
|
||||
List<CombinationActivityDO> validActivities = combinationActivityDOList.stream()
|
||||
.filter(combinationActivityDO -> combinationActivityDO != null &&
|
||||
!ObjectUtil.equal(combinationActivityDO.getStatus(), CommonStatusEnum.DISABLE.getStatus()))
|
||||
.toList();
|
||||
|
||||
// 如果没有有效的活动,返回空列表
|
||||
if (validActivities.isEmpty()) {
|
||||
return success(ListUtil.empty());
|
||||
}
|
||||
|
||||
// 2. 构建结果列表
|
||||
List<AppCombinationActivityDetailRespVO> detailRespVOList = new ArrayList<>();
|
||||
for (CombinationActivityDO activity : validActivities) {
|
||||
// 获取活动商品
|
||||
List<CombinationProductDO> products = activityService.getCombinationProductsByActivityId(activity.getId());
|
||||
|
||||
// 调用转换方法并添加到结果列表
|
||||
AppCombinationActivityDetailRespVO detailRespVO = CombinationActivityConvert.INSTANCE.convert3(activity, products);
|
||||
detailRespVOList.add(detailRespVO);
|
||||
}
|
||||
|
||||
// 3. 返回转换后的结果
|
||||
return success(detailRespVOList);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -19,15 +19,14 @@ public class AppCombinationActivityRespVO {
|
||||
@Schema(description = "商品 SPU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
|
||||
private Long spuId;
|
||||
|
||||
@Schema(description = "商品 SPU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "一个白菜")
|
||||
private String spuName; // 从 SPU 的 name 读取
|
||||
@Schema(description = "商品图片", requiredMode = Schema.RequiredMode.REQUIRED, example = "4096")
|
||||
// 从 SPU 的 picUrl 读取
|
||||
private String picUrl;
|
||||
|
||||
private String picUrl; // 从 SPU 的 picUrl 读取
|
||||
@Schema(description = "商品市场价,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "50")
|
||||
// 从 SPU 的 marketPrice 读取
|
||||
private Integer marketPrice;
|
||||
private Integer marketPrice; // 从 SPU 的 marketPrice 读取
|
||||
|
||||
@Schema(description = "拼团金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
|
||||
private Integer combinationPrice;
|
||||
private Integer combinationPrice; // 从 products 获取最小 price 读取
|
||||
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ 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;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
|
||||
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
|
||||
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
|
||||
@ -127,40 +128,42 @@ public interface CombinationActivityConvert {
|
||||
.setSpuName(spu.getName()).setPicUrl(sku.getPicUrl());
|
||||
}
|
||||
|
||||
List<AppCombinationActivityRespVO> convertAppList(List<CombinationActivityDO> list);
|
||||
|
||||
default List<AppCombinationActivityRespVO> convertAppList(List<CombinationActivityDO> list,
|
||||
List<CombinationProductDO> productList,
|
||||
List<ProductSpuRespDTO> spuList) {
|
||||
List<AppCombinationActivityRespVO> activityList = convertAppList(list);
|
||||
default List<CombinationActivityRespVO> convertList(List<CombinationActivityDO> list,
|
||||
List<CombinationProductDO> productList,
|
||||
List<ProductSpuRespDTO> spuList) {
|
||||
List<CombinationActivityRespVO> activityList = BeanUtils.toBean(list, CombinationActivityRespVO.class);
|
||||
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
|
||||
Map<Long, List<CombinationProductDO>> productMap = convertMultiMap(productList, CombinationProductDO::getActivityId);
|
||||
return CollectionUtils.convertList(activityList, item -> {
|
||||
// 设置 product 信息
|
||||
item.setCombinationPrice(getMinValue(productMap.get(item.getId()), CombinationProductDO::getCombinationPrice));
|
||||
// 设置 SPU 信息
|
||||
findAndThen(spuMap, item.getSpuId(), spu -> item.setPicUrl(spu.getPicUrl()).setMarketPrice(spu.getMarketPrice()));
|
||||
findAndThen(spuMap, item.getSpuId(), spu -> item.setSpuName(spu.getName())
|
||||
.setPicUrl(spu.getPicUrl()).setMarketPrice(spu.getMarketPrice()));
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
PageResult<AppCombinationActivityRespVO> convertAppPage(PageResult<CombinationActivityDO> result);
|
||||
default List<AppCombinationActivityRespVO> convertAppList(List<CombinationActivityDO> list,
|
||||
List<CombinationProductDO> productList,
|
||||
List<ProductSpuRespDTO> spuList) {
|
||||
List<AppCombinationActivityRespVO> activityList = BeanUtils.toBean(list, AppCombinationActivityRespVO.class);
|
||||
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
|
||||
Map<Long, List<CombinationProductDO>> productMap = convertMultiMap(productList, CombinationProductDO::getActivityId);
|
||||
return CollectionUtils.convertList(activityList, item -> {
|
||||
// 设置 product 信息
|
||||
item.setCombinationPrice(getMinValue(productMap.get(item.getId()), CombinationProductDO::getCombinationPrice));
|
||||
// 设置 SPU 信息
|
||||
findAndThen(spuMap, item.getSpuId(), spu -> item.setSpuName(spu.getName())
|
||||
.setPicUrl(spu.getPicUrl()).setMarketPrice(spu.getMarketPrice()));
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
default PageResult<AppCombinationActivityRespVO> convertAppPage(PageResult<CombinationActivityDO> result,
|
||||
List<CombinationProductDO> productList,
|
||||
List<ProductSpuRespDTO> spuList) {
|
||||
PageResult<AppCombinationActivityRespVO> appPage = convertAppPage(result);
|
||||
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
|
||||
Map<Long, List<CombinationProductDO>> productMap = convertMultiMap(productList, CombinationProductDO::getActivityId);
|
||||
List<AppCombinationActivityRespVO> list = CollectionUtils.convertList(appPage.getList(), item -> {
|
||||
// 设置 product 信息
|
||||
item.setCombinationPrice(getMinValue(productMap.get(item.getId()), CombinationProductDO::getCombinationPrice));
|
||||
// 设置 SPU 信息
|
||||
findAndThen(spuMap, item.getSpuId(), spu -> item.setPicUrl(spu.getPicUrl()).setMarketPrice(spu.getMarketPrice()));
|
||||
return item;
|
||||
});
|
||||
appPage.setList(list);
|
||||
return appPage;
|
||||
return new PageResult<>(convertAppList(result.getList(), productList, spuList), result.getTotal());
|
||||
}
|
||||
|
||||
AppCombinationActivityDetailRespVO convert2(CombinationActivityDO combinationActivity);
|
||||
|
@ -100,14 +100,6 @@ public interface CombinationActivityService {
|
||||
*/
|
||||
List<CombinationActivityDO> getCombinationActivityListByIds(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获取正在进行的活动分页数据
|
||||
*
|
||||
* @param count 需要的数量
|
||||
* @return 拼团活动分页
|
||||
*/
|
||||
List<CombinationActivityDO> getCombinationActivityListByCount(Integer count);
|
||||
|
||||
/**
|
||||
* 获取正在进行的活动分页数据
|
||||
*
|
||||
|
@ -225,11 +225,6 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic
|
||||
return combinationActivityMapper.selectList(CombinationActivityDO::getId, ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CombinationActivityDO> getCombinationActivityListByCount(Integer count) {
|
||||
return combinationActivityMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus(), count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<CombinationActivityDO> getCombinationActivityPage(PageParam pageParam) {
|
||||
return combinationActivityMapper.selectPage(pageParam, CommonStatusEnum.ENABLE.getStatus());
|
||||
|
Loading…
Reference in New Issue
Block a user