!724 会员中心商品收藏

Merge pull request !724 from niou233/feature/mall_product_org
This commit is contained in:
芋道源码 2023-11-18 16:40:00 +00:00 committed by Gitee
commit 867293a4f9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 196 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.interfaces.MPJBaseJoin;
import org.apache.ibatis.annotations.Param;
import java.util.Collection;
@ -132,4 +133,10 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> {
Db.saveOrUpdateBatch(collection);
}
default <DTO> PageResult<DTO> selectJoinPage(PageParam pageParam, Class<DTO> resultTypeClass, MPJBaseJoin<T> joinQueryWrapper) {
IPage<DTO> mpPage = MyBatisUtils.buildPage(pageParam);
selectJoinPage(mpPage, resultTypeClass, joinQueryWrapper);
// 转换返回
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
}
}

View File

@ -0,0 +1,60 @@
package cn.iocoder.yudao.module.product.controller.admin.favorite;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.favorite.vo.ProductFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.favorite.vo.ProductFavoriteRespVO;
import cn.iocoder.yudao.module.product.convert.favorite.ProductFavoriteConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService;
import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
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.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
@Tag(name = "管理后台 - 商品收藏")
@RestController
@RequestMapping("/product/favorite")
@Validated
public class ProductFavoriteController {
@Resource
private ProductFavoriteService productFavoriteService;
@Resource
private ProductSpuService productSpuService;
@GetMapping("/page")
@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())) {
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);
}
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.product.controller.admin.favorite.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 商品收藏 Base VO提供给添加修改详细的子 VO 使用
* 如果子 VO 存在差异的字段请不要添加到这里影响 Swagger 文档生成
*/
@Data
public class ProductFavoriteBaseVO {
@Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "5036")
@NotNull(message = "用户编号不能为空")
private Long userId;
}

View File

@ -0,0 +1,17 @@
package cn.iocoder.yudao.module.product.controller.admin.favorite.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@Schema(description = "管理后台 - 商品收藏分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ProductFavoritePageReqVO extends PageParam {
@Schema(description = "用户编号", example = "5036")
private Long userId;
}

View File

@ -0,0 +1,17 @@
package cn.iocoder.yudao.module.product.controller.admin.favorite.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;
import javax.validation.constraints.NotNull;
@Schema(description = "管理后台 - 商品收藏的单个 Response VO")
@Data
@ToString(callSuper = true)
public class ProductFavoriteReqVO extends ProductFavoriteBaseVO {
@Schema(description = "商品 SPU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "32734")
@NotNull(message = "商品 SPU 编号不能为空")
private Long spuId;
}

View File

@ -0,0 +1,18 @@
package cn.iocoder.yudao.module.product.controller.admin.favorite.vo;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuRespVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;
@Schema(description = "管理后台 - 商品收藏 Response VO")
@Data
@ToString(callSuper = true)
public class ProductFavoriteRespVO extends ProductSpuRespVO {
@Schema(description = "userId", requiredMode = Schema.RequiredMode.REQUIRED, example = "111")
private Long userId;
@Schema(description = "spuId", requiredMode = Schema.RequiredMode.REQUIRED, example = "111")
private Long spuId;
}

View File

@ -1,7 +1,10 @@
package cn.iocoder.yudao.module.product.convert.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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;
@ -10,6 +13,7 @@ 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;
@ -34,4 +38,20 @@ public interface ProductFavoriteConvert {
return resultList;
}
@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);
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

@ -0,0 +1,15 @@
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,9 +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.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
@ -21,6 +23,13 @@ public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
.orderByDesc(ProductFavoriteDO::getId));
}
default PageResult<ProductFavoriteDO> selectPageByUserId(ProductFavoritePageReqVO reqVO) {
return selectPage(reqVO, new MPJLambdaWrapper<ProductFavoriteDO>()
.selectAll(ProductFavoriteDO.class)
.eq(ProductFavoriteDO::getUserId, reqVO.getUserId())
.orderByDesc(ProductFavoriteDO::getCreateTime));
}
default Long selectCountByUserId(Long userId) {
return selectCount(ProductFavoriteDO::getUserId, userId);
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.product.service.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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;
@ -37,6 +38,13 @@ public interface ProductFavoriteService {
*/
PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO);
/**
* 分页查询用户收藏列表
*
* @param reqVO 请求 vo
*/
PageResult<ProductFavoriteDO> getFavoritePage(@Valid ProductFavoritePageReqVO reqVO);
/**
* 获取收藏过商品
*

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.product.service.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.convert.favorite.ProductFavoriteConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
@ -54,6 +55,11 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
return productFavoriteMapper.selectPageByUserAndType(userId, reqVO);
}
@Override
public PageResult<ProductFavoriteDO> getFavoritePage(@Valid ProductFavoritePageReqVO reqVO) {
return productFavoriteMapper.selectPageByUserId(reqVO);
}
@Override
public ProductFavoriteDO getFavorite(Long userId, Long spuId) {
return productFavoriteMapper.selectByUserIdAndSpuId(userId, spuId);