mall:优化商品收藏的分页接口
This commit is contained in:
parent
867293a4f9
commit
c7a21b2a64
@ -139,4 +139,5 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> {
|
|||||||
// 转换返回
|
// 转换返回
|
||||||
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
|
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -41,20 +41,13 @@ public class ProductFavoriteController {
|
|||||||
@Operation(summary = "获得商品收藏分页")
|
@Operation(summary = "获得商品收藏分页")
|
||||||
@PreAuthorize("@ss.hasPermission('product:favorite:query')")
|
@PreAuthorize("@ss.hasPermission('product:favorite:query')")
|
||||||
public CommonResult<PageResult<ProductFavoriteRespVO>> getFavoritePage(@Valid ProductFavoritePageReqVO pageVO) {
|
public CommonResult<PageResult<ProductFavoriteRespVO>> getFavoritePage(@Valid ProductFavoritePageReqVO pageVO) {
|
||||||
PageResult<ProductFavoriteDO> favoritePage = productFavoriteService.getFavoritePage(pageVO);
|
PageResult<ProductFavoriteDO> pageResult = productFavoriteService.getFavoritePage(pageVO);
|
||||||
if (CollUtil.isEmpty(favoritePage.getList())) {
|
if (CollUtil.isEmpty(pageResult.getList())) {
|
||||||
return success(PageResult.empty());
|
return success(PageResult.empty());
|
||||||
}
|
}
|
||||||
|
// 拼接数据
|
||||||
List<ProductSpuDO> list = productSpuService.getSpuList(convertSet(favoritePage.getList(), ProductFavoriteDO::getSpuId));
|
List<ProductSpuDO> spuList = productSpuService.getSpuList(convertSet(pageResult.getList(), ProductFavoriteDO::getSpuId));
|
||||||
|
return success(ProductFavoriteConvert.INSTANCE.convertPage(pageResult, spuList));
|
||||||
// 得到商品 spu 信息
|
|
||||||
List<ProductFavoriteRespVO> favorites = ProductFavoriteConvert.INSTANCE.convertList2admin(favoritePage.getList(), list);
|
|
||||||
|
|
||||||
// 转换 VO 结果
|
|
||||||
PageResult<ProductFavoriteRespVO> pageResult = new PageResult<>(favoritePage.getTotal());
|
|
||||||
pageResult.setList(favorites);
|
|
||||||
|
|
||||||
return success(pageResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,4 +14,5 @@ public class ProductFavoritePageReqVO extends PageParam {
|
|||||||
|
|
||||||
@Schema(description = "用户编号", example = "5036")
|
@Schema(description = "用户编号", example = "5036")
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,4 +15,5 @@ public class ProductFavoriteRespVO extends ProductSpuRespVO {
|
|||||||
|
|
||||||
@Schema(description = "spuId", requiredMode = Schema.RequiredMode.REQUIRED, example = "111")
|
@Schema(description = "spuId", requiredMode = Schema.RequiredMode.REQUIRED, example = "111")
|
||||||
private Long spuId;
|
private Long spuId;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package cn.iocoder.yudao.module.product.convert.favorite;
|
package cn.iocoder.yudao.module.product.convert.favorite;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
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.admin.favorite.vo.ProductFavoriteRespVO;
|
||||||
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
|
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.ProductFavoriteDO;
|
||||||
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDetailDO;
|
|
||||||
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
|
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
@ -13,7 +13,6 @@ import org.mapstruct.factory.Mappers;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||||
|
|
||||||
@ -38,20 +37,18 @@ public interface ProductFavoriteConvert {
|
|||||||
return resultList;
|
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 = "id", source = "favorite.id")
|
||||||
@Mapping(target = "userId", source = "favorite.userId")
|
@Mapping(target = "userId", source = "favorite.userId")
|
||||||
@Mapping(target = "spuId", source = "favorite.spuId")
|
@Mapping(target = "spuId", source = "favorite.spuId")
|
||||||
@Mapping(target = "createTime", source = "favorite.createTime")
|
@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);
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
}
|
|
@ -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.common.pojo.PageResult;
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
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.admin.favorite.vo.ProductFavoritePageReqVO;
|
||||||
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
|
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
|
||||||
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
|
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
@ -24,10 +24,9 @@ public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default PageResult<ProductFavoriteDO> selectPageByUserId(ProductFavoritePageReqVO reqVO) {
|
default PageResult<ProductFavoriteDO> selectPageByUserId(ProductFavoritePageReqVO reqVO) {
|
||||||
return selectPage(reqVO, new MPJLambdaWrapper<ProductFavoriteDO>()
|
return selectPage(reqVO, new LambdaQueryWrapperX<ProductFavoriteDO>()
|
||||||
.selectAll(ProductFavoriteDO.class)
|
.eqIfPresent(ProductFavoriteDO::getUserId, reqVO.getUserId())
|
||||||
.eq(ProductFavoriteDO::getUserId, reqVO.getUserId())
|
.orderByDesc(ProductFavoriteDO::getId));
|
||||||
.orderByDesc(ProductFavoriteDO::getCreateTime));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default Long selectCountByUserId(Long userId) {
|
default Long selectCountByUserId(Long userId) {
|
||||||
|
Loading…
Reference in New Issue
Block a user