feat: 会员商品收藏

This commit is contained in:
dongshanshan 2023-10-31 16:51:56 +08:00
parent 48b1c19f28
commit 98aae5ed1a
12 changed files with 314 additions and 2 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, @Param("resultTypeClass_Eg1sG") Class<DTO> var2, @Param("ew") MPJBaseJoin<T> queryWrapper) {
IPage<DTO> mpPage = MyBatisUtils.buildPage(pageParam);
selectJoinPage(mpPage, var2, queryWrapper);
// 转换返回
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
}
}

View File

@ -0,0 +1,104 @@
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.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.product.controller.admin.favorite.vo.ProductFavoriteBatchReqVO;
import cn.iocoder.yudao.module.product.controller.admin.favorite.vo.ProductFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.favorite.vo.ProductFavoriteReqVO;
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.favorite.ProductFavoriteDetailDO;
import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - 商品收藏")
@RestController
@RequestMapping("/product/favorite")
@Validated
public class ProductFavoriteController {
@Resource
private ProductFavoriteService productFavoriteService;
@PostMapping("/create")
@Operation(summary = "添加单个商品收藏")
@PreAuthorize("@ss.hasPermission('product:favorite:create')")
public CommonResult<Long> createFavorite(@Valid @RequestBody ProductFavoriteReqVO reqVO) {
return success(productFavoriteService.createFavorite(reqVO.getUserId(), reqVO.getSpuId()));
}
@PostMapping("/create-list")
@Operation(summary = "添加多个商品收藏")
@PreAuthorize("@ss.hasPermission('product:favorite:create')")
public CommonResult<Boolean> createFavoriteList(@Valid @RequestBody ProductFavoriteBatchReqVO reqVO) {
// todo @jason待实现如果有已经收藏的不用报错忽略即可
return success(Boolean.TRUE);
}
@DeleteMapping("/delete")
@Operation(summary = "取消单个商品收藏")
@PreAuthorize("@ss.hasPermission('product:favorite:delete')")
public CommonResult<Boolean> deleteFavorite(@Valid @RequestBody ProductFavoriteReqVO reqVO) {
productFavoriteService.deleteFavorite(reqVO.getUserId(), reqVO.getSpuId());
return success(Boolean.TRUE);
}
@DeleteMapping("/delete-list")
@Operation(summary = "取消单个商品收藏")
@PreAuthorize("@ss.hasPermission('product:favorite:delete')")
public CommonResult<Boolean> deleteFavoriteList(@Valid @RequestBody ProductFavoriteBatchReqVO reqVO) {
// todo @jason待实现
// productFavoriteService.deleteFavorite(getLoginUserId(), reqVO.getSpuId());
return success(Boolean.TRUE);
}
@GetMapping("/page")
@Operation(summary = "获得商品收藏分页")
@PreAuthorize("@ss.hasPermission('product:favorite:query')")
public CommonResult<PageResult<ProductFavoriteRespVO>> getFavoritePage(@Valid ProductFavoritePageReqVO pageVO) {
PageResult<ProductFavoriteDetailDO> favoritePage = productFavoriteService.getFavoritePageByFilter(pageVO);
if (CollUtil.isEmpty(favoritePage.getList())) {
return success(PageResult.empty());
}
// 得到商品 spu 信息
List<ProductFavoriteRespVO> favorites = ProductFavoriteConvert.INSTANCE.convertList2admin(favoritePage.getList());
// 转换 VO 结果
PageResult<ProductFavoriteRespVO> pageResult = new PageResult<>(favoritePage.getTotal());
pageResult.setList(favorites);
return success(pageResult);
}
@PostMapping(value = "/exits")
@Operation(summary = "检查是否收藏过商品")
@PreAuthenticated
public CommonResult<Boolean> isFavoriteExists(@Valid @RequestBody ProductFavoriteReqVO reqVO) {
ProductFavoriteDO favorite = productFavoriteService.getFavorite(reqVO.getUserId(), reqVO.getSpuId());
return success(favorite != null);
}
@GetMapping(value = "/get-count")
@Operation(summary = "获得商品收藏数量")
@Parameter(name = "userId", description = "用户编号", required = true)
@PreAuthenticated
public CommonResult<Long> getFavoriteCount(@RequestParam("userId") Long userId) {
return success(productFavoriteService.getFavoriteCount(userId));
}
}

View File

@ -0,0 +1,21 @@
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,21 @@
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;
import java.util.List;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
@Schema(description = "管理后台 - 商品收藏的批量 Request VO")
@Data
@ToString(callSuper = true)
public class ProductFavoriteBatchReqVO extends ProductFavoriteBaseVO{
@Schema(description = "商品 SPU 编号数组", requiredMode = REQUIRED, example = "29502")
@NotNull(message = "商品 SPU 编号数组不能为空")
private List<Long> spuIds;
}

View File

@ -0,0 +1,35 @@
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;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 商品收藏分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ProductFavoritePageReqVO extends PageParam {
@Schema(description = "用户编号", example = "5036")
private Long userId;
@Schema(description = "商品 SPU 编号", example = "32734")
private Long spuId;
@Schema(description = "收藏时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
@Schema(description = "商品名称", example = "5036")
private String name;
@Schema(description = "关键字", example = "5036")
private String keyword;
}

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,22 @@
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;
@Schema(description = "收藏状态", example = "1")
private Integer favoriteStatus;
}

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;
@ -34,4 +37,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")
@Mapping(target = "favoriteStatus", constant = "1")
ProductFavoriteRespVO convert2admin(ProductSpuDO spu, ProductFavoriteDO favorite);
default List<ProductFavoriteRespVO> convertList2admin(List<ProductFavoriteDetailDO> favorites) {
List<ProductFavoriteRespVO> resultList = new ArrayList<>(favorites.size());
for (ProductFavoriteDetailDO favorite : favorites) {
resultList.add(convert2admin(favorite.getSpuDO(), favorite));
}
return resultList;
}
PageResult<ProductFavoriteRespVO> convertPage(PageResult<ProductFavoriteDetailDO> page);
}

View File

@ -0,0 +1,16 @@
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,15 @@ 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 cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDetailDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
@Mapper
@ -21,6 +27,36 @@ public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
.orderByDesc(ProductFavoriteDO::getId));
}
default PageResult<ProductFavoriteDetailDO> selectPageByUserAndFields(ProductFavoritePageReqVO reqVO) {
MPJLambdaWrapper<ProductFavoriteDO> wrapper = new MPJLambdaWrapper<ProductFavoriteDO>()
.selectAll(ProductFavoriteDO.class)
.eq(ProductFavoriteDO::getUserId, reqVO.getUserId())
.selectAssociation(ProductSpuDO.class, ProductFavoriteDetailDO::getSpuDO);
if(StringUtils.isNotEmpty(reqVO.getName())){
wrapper.likeRight(ProductSpuDO::getName, reqVO.getName());
}
if(StringUtils.isNotEmpty(reqVO.getName()) && StringUtils.isNotEmpty(reqVO.getKeyword())){
wrapper.or();
}
if(StringUtils.isNotEmpty(reqVO.getKeyword())){
wrapper.likeRight(ProductSpuDO::getKeyword, reqVO.getKeyword());
}
if(reqVO.getCreateTime() != null){
if (reqVO.getCreateTime()[0] != null && reqVO.getCreateTime()[1] != null) {
wrapper.between(ProductFavoriteDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
}
if (reqVO.getCreateTime()[0] != null) {
wrapper.ge(ProductFavoriteDO::getCreateTime, reqVO.getCreateTime()[0]);
}
if (reqVO.getCreateTime()[1] != null) {
wrapper.le(ProductFavoriteDO::getCreateTime, reqVO.getCreateTime()[1]);
}
}
return selectJoinPage(reqVO, ProductFavoriteDetailDO.class, wrapper);
}
default Long selectCountByUserId(Long userId) {
return selectCount(ProductFavoriteDO::getUserId, userId);
}

View File

@ -1,8 +1,10 @@
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;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDetailDO;
import javax.validation.Valid;
@ -37,6 +39,13 @@ public interface ProductFavoriteService {
*/
PageResult<ProductFavoriteDO> getFavoritePage(Long userId, @Valid AppFavoritePageReqVO reqVO);
/**
* 分页查询用户收藏列表
*
* @param reqVO 请求 vo
*/
PageResult<ProductFavoriteDetailDO> getFavoritePageByFilter(@Valid ProductFavoritePageReqVO reqVO);
/**
* 获取收藏过商品
*
@ -52,5 +61,4 @@ public interface ProductFavoriteService {
* @return 数量
*/
Long getFavoriteCount(Long userId);
}

View File

@ -1,9 +1,11 @@
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;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDetailDO;
import cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -33,7 +35,6 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
if (favorite != null) {
throw exception(FAVORITE_EXISTS);
}
ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, spuId);
productFavoriteMapper.insert(entity);
return entity.getId();
@ -54,6 +55,11 @@ public class ProductFavoriteServiceImpl implements ProductFavoriteService {
return productFavoriteMapper.selectPageByUserAndType(userId, reqVO);
}
@Override
public PageResult<ProductFavoriteDetailDO> getFavoritePageByFilter(@Valid ProductFavoritePageReqVO reqVO) {
return productFavoriteMapper.selectPageByUserAndFields(reqVO);
}
@Override
public ProductFavoriteDO getFavorite(Long userId, Long spuId) {
return productFavoriteMapper.selectByUserIdAndSpuId(userId, spuId);