mall:优化商品收藏的分页接口

This commit is contained in:
YunaiV 2023-11-19 00:58:12 +08:00
parent 867293a4f9
commit c7a21b2a64
7 changed files with 23 additions and 46 deletions

View File

@ -139,4 +139,5 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> {
// 转换返回
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
}
}

View File

@ -41,20 +41,13 @@ public class ProductFavoriteController {
@Operation(summary = "获得商品收藏分页")
@PreAuthorize("@ss.hasPermission('product:favorite:query')")
public CommonResult<PageResult<ProductFavoriteRespVO>> getFavoritePage(@Valid ProductFavoritePageReqVO pageVO) {
PageResult<ProductFavoriteDO> favoritePage = productFavoriteService.getFavoritePage(pageVO);
if (CollUtil.isEmpty(favoritePage.getList())) {
PageResult<ProductFavoriteDO> pageResult = productFavoriteService.getFavoritePage(pageVO);
if (CollUtil.isEmpty(pageResult.getList())) {
return success(PageResult.empty());
}
List<ProductSpuDO> list = productSpuService.getSpuList(convertSet(favoritePage.getList(), ProductFavoriteDO::getSpuId));
// 得到商品 spu 信息
List<ProductFavoriteRespVO> favorites = ProductFavoriteConvert.INSTANCE.convertList2admin(favoritePage.getList(), list);
// 转换 VO 结果
PageResult<ProductFavoriteRespVO> pageResult = new PageResult<>(favoritePage.getTotal());
pageResult.setList(favorites);
return success(pageResult);
// 拼接数据
List<ProductSpuDO> spuList = productSpuService.getSpuList(convertSet(pageResult.getList(), ProductFavoriteDO::getSpuId));
return success(ProductFavoriteConvert.INSTANCE.convertPage(pageResult, spuList));
}
}

View File

@ -14,4 +14,5 @@ public class ProductFavoritePageReqVO extends PageParam {
@Schema(description = "用户编号", example = "5036")
private Long userId;
}

View File

@ -15,4 +15,5 @@ public class ProductFavoriteRespVO extends ProductSpuRespVO {
@Schema(description = "spuId", requiredMode = Schema.RequiredMode.REQUIRED, example = "111")
private Long spuId;
}

View File

@ -1,10 +1,10 @@
package cn.iocoder.yudao.module.product.convert.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.controller.admin.favorite.vo.ProductFavoriteRespVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDetailDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@ -13,7 +13,6 @@ import org.mapstruct.factory.Mappers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
@ -38,20 +37,18 @@ public interface ProductFavoriteConvert {
return resultList;
}
default PageResult<ProductFavoriteRespVO> convertPage(PageResult<ProductFavoriteDO> pageResult, List<ProductSpuDO> spuList) {
Map<Long, ProductSpuDO> spuMap = convertMap(spuList, ProductSpuDO::getId);
List<ProductFavoriteRespVO> voList = CollectionUtils.convertList(pageResult.getList(), favorite -> {
ProductSpuDO spu = spuMap.get(favorite.getSpuId());
return convert02(spu, favorite);
});
return new PageResult<>(voList, pageResult.getTotal());
}
@Mapping(target = "id", source = "favorite.id")
@Mapping(target = "userId", source = "favorite.userId")
@Mapping(target = "spuId", source = "favorite.spuId")
@Mapping(target = "createTime", source = "favorite.createTime")
ProductFavoriteRespVO convert2admin(ProductSpuDO spu, ProductFavoriteDO favorite);
ProductFavoriteRespVO convert02(ProductSpuDO spu, ProductFavoriteDO favorite);
default List<ProductFavoriteRespVO> convertList2admin(List<ProductFavoriteDO> favorites, List<ProductSpuDO> spus) {
List<ProductFavoriteRespVO> resultList = new ArrayList<>(spus.size());
for (ProductFavoriteDO favorite : favorites) {
Optional<ProductSpuDO> spu = spus.stream().filter(e -> e.getId().equals(favorite.getSpuId())).findFirst();
resultList.add(convert2admin(spu.get(), favorite));
}
return resultList;
}
PageResult<ProductFavoriteRespVO> convertPage(PageResult<ProductFavoriteDetailDO> page);
}

View File

@ -1,15 +0,0 @@
package cn.iocoder.yudao.module.product.dal.dataobject.favorite;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import lombok.Data;
/**
* 商品收藏 DO
*
* @author 芋道源码
*/
@Data
public class ProductFavoriteDetailDO extends ProductFavoriteDO {
ProductSpuDO spuDO;
}

View File

@ -2,11 +2,11 @@ package cn.iocoder.yudao.module.product.dal.mysql.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.product.controller.admin.favorite.vo.ProductFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
@ -24,10 +24,9 @@ public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
}
default PageResult<ProductFavoriteDO> selectPageByUserId(ProductFavoritePageReqVO reqVO) {
return selectPage(reqVO, new MPJLambdaWrapper<ProductFavoriteDO>()
.selectAll(ProductFavoriteDO.class)
.eq(ProductFavoriteDO::getUserId, reqVO.getUserId())
.orderByDesc(ProductFavoriteDO::getCreateTime));
return selectPage(reqVO, new LambdaQueryWrapperX<ProductFavoriteDO>()
.eqIfPresent(ProductFavoriteDO::getUserId, reqVO.getUserId())
.orderByDesc(ProductFavoriteDO::getId));
}
default Long selectCountByUserId(Long userId) {