新增商品收藏功能

This commit is contained in:
jason 2023-05-04 15:52:42 +08:00
parent 19ceea2c6c
commit 7345d80b53
13 changed files with 378 additions and 5 deletions

View File

@ -289,6 +289,25 @@ CREATE TABLE `product_spu` (
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='商品spu';
-- ----------------------------
-- Table structure for product_favorite
-- ----------------------------
DROP TABLE IF EXISTS `product_favorite`;
CREATE TABLE `product_favorite` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号自增',
`spu_id` bigint NOT NULL COMMENT '商品 SPU 编号',
`user_id` bigint NOT NULL COMMENT '用户id',
`type` int(10) NOT NULL DEFAULT 1 COMMENT '类型1:收藏 2:点赞',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='喜欢的商品表';
-- ----------------------------
-- Records of product_spu
-- ----------------------------

View File

@ -48,4 +48,8 @@ public interface ErrorCodeConstants {
ErrorCode COMMENT_ERROR_OPT = new ErrorCode(1008007002, "商品评价非法操作");
ErrorCode COMMENT_ADDITIONAL_EXISTS = new ErrorCode(1008007003, "商品追加评价已存在");
// ========== 喜爱商品 1008008000 ==========
ErrorCode COLLECTION_EXISTS = new ErrorCode(1008008000, "该商品已经被收藏");
ErrorCode COLLECTION_NOT_EXISTS = new ErrorCode(1008008001, "商品收藏不存在");
}

View File

@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.product.enums.favorite;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* 喜爱商品类型 枚举
*
* @author jason
*/
@Getter
@AllArgsConstructor
public enum ProductFavoriteTypeEnum implements IntArrayValuable {
COLLECT(1,"收藏"),
THUMBS_UP(2, "点赞");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(ProductFavoriteTypeEnum::getType).toArray();
/**
* 类型
*/
private final Integer type;
/**
* 描述
*/
private final String desc;
@Override
public int[] array() {
return ARRAYS;
}
}

View File

@ -0,0 +1,51 @@
package cn.iocoder.yudao.module.product.controller.app.favorite;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum.COLLECT;
/**
* @author jason
*/
@Tag(name = "用户 APP - 喜爱商品")
@RestController
@RequestMapping("/product/favorite")
public class AppFavoriteController {
@Resource
private ProductFavoriteService productFavoriteService;
@PostMapping(value = "/collect")
@Operation(summary = "商品收藏")
public CommonResult<Boolean> collect(@RequestBody @Valid AppFavoriteReqVO reqVO) {
Assert.isTrue(Objects.equals(COLLECT.getType(), reqVO.getType()), "参数type 不匹配");
return success(productFavoriteService.collect(reqVO));
}
@PostMapping(value = "/cancelCollect")
@Operation(summary = "取消商品收藏(通过商品详情)")
public CommonResult<Boolean> cancelCollect(@RequestBody @Valid AppFavoriteReqVO reqVO) {
Assert.isTrue(Objects.equals(COLLECT.getType(), reqVO.getType()), "参数type 不匹配");
return success(productFavoriteService.cancelCollect(reqVO));
}
@GetMapping(value = "/collectList")
@Operation(summary = "商品收藏列表")
public CommonResult<PageResult<AppFavoriteRespVO>> pageCollectList(AppFavoritePageReqVO reqVO) {
return success(productFavoriteService.pageCollectList(reqVO));
}
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.product.controller.app.favorite.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotNull;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
/**
* @author jason
*/
@Schema(description = "用户APP - 喜爱商品分页查询 Request VO")
@Data
public class AppFavoritePageReqVO extends PageParam {
@Schema(description = "类型 1:收藏 2点赞", requiredMode = REQUIRED, example = "1")
@NotNull(message = "类型不能为空")
@InEnum(ProductFavoriteTypeEnum.class)
private Integer type;
}

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.module.product.controller.app.favorite.vo;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotNull;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
/**
* @author jason
*/
@Schema(description = "用户APP - 喜爱商品创建 Request VO")
@Data
public class AppFavoriteReqVO {
@Schema(description = "商品SPU编号", requiredMode = REQUIRED, example = "29502")
@NotNull(message = "商品SPU编号不能为空")
private Long spuId;
@Schema(description = "类型 1:收藏 2点赞", requiredMode = REQUIRED, example = "1")
@NotNull(message = "类型不能为空")
@InEnum(ProductFavoriteTypeEnum.class)
private Integer type;
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.product.controller.app.favorite.vo;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* @author jason
*/
@Schema(description = "用户APP - 喜爱商品 Response VO")
@Data
public class AppFavoriteRespVO {
@Schema(description = "编号", example = "1")
private Long id;
@Schema(description = "商品SPU编号", example = "29502")
private Long spuId;
@Schema(description = "商品SPU名称", example = "赵六")
private String spuName;
@Schema(description = "商品封面图", example = "https://domain/pic.png")
private String picUrl;
@Schema(description = "商品单价", example = "100")
private Integer price;
@Schema(description = "类型 1:收藏 2点赞", example = "1")
private Integer type;
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.product.convert.favorite;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* 喜爱商品 Convert
*
* @author jason
*/
@Mapper
public interface ProductFavoriteConvert {
ProductFavoriteConvert INSTANCE = Mappers.getMapper(ProductFavoriteConvert.class);
ProductFavoriteDO convert(Long userId, AppFavoriteReqVO reqVO);
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.product.dal.dataobject.favorite;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
@ -8,7 +8,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
/**
* 商品收藏 DO
* 喜爱商品 DO
*
* @author 芋道源码
*/
@ -20,7 +20,7 @@ import lombok.*;
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductFavoriteDO extends BaseDO {
public class ProductFavoriteDO extends TenantBaseDO {
/**
* 编号主键自增
@ -39,7 +39,9 @@ public class ProductFavoriteDO extends BaseDO {
* 关联 {@link ProductSpuDO#getId()}
*/
private Long spuId;
// TODO 芋艿type 1 收藏2 点赞
/**
* 类型 1 收藏2 点赞
*/
private Integer type;
}

View File

@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.product.dal.mysql.favorite;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 喜爱商品 Mapper
*
* @author jason
*/
@Mapper
public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> {
default ProductFavoriteDO selectByUserAndSpuAndType(Long userId, Long spuId, Integer type){
Assert.notNull(userId, "the userId argument must not be null");
Assert.notNull(spuId, "the spuId argument must not be null");
Assert.notNull(type, "the type argument must not be null");
return selectOne(new LambdaQueryWrapperX<ProductFavoriteDO>()
.eq(ProductFavoriteDO::getUserId, userId)
.eq(ProductFavoriteDO::getSpuId, spuId)
.eq(ProductFavoriteDO::getType, type));
}
default PageResult<AppFavoriteRespVO> selectPageByUserAndType(Long userId, Integer type, PageParam pageParam){
Page<AppFavoriteRespVO> page = MyBatisUtils.buildPage(pageParam);
page = selectFavoriteProductList(page, userId, type);
return new PageResult<>(page.getRecords(), page.getTotal());
}
Page<AppFavoriteRespVO> selectFavoriteProductList(Page<AppFavoriteRespVO> page,
@Param("userId") Long userId,
@Param("type") Integer type);
}

View File

@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.product.service.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import javax.validation.Valid;
/**
* 喜爱商品 Service 接口
*
* @author jason
*/
public interface ProductFavoriteService {
/**
* 商品收藏
* @param reqVO 请求vo
*/
Boolean collect(@Valid AppFavoriteReqVO reqVO);
/**
* 取消商品收藏 (通过商品详情页面)
* @param reqVO 请求vo
*/
Boolean cancelCollect(@Valid AppFavoriteReqVO reqVO);
/**
* 分页查询用户收藏列表
* @param reqVO 请求 vo
*/
PageResult<AppFavoriteRespVO> pageCollectList(@Valid AppFavoritePageReqVO reqVO);
}

View File

@ -0,0 +1,62 @@
package cn.iocoder.yudao.module.product.service.favorite;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
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.mysql.favorite.ProductFavoriteMapper;
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTION_EXISTS;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTION_NOT_EXISTS;
/**
* 喜爱商品 Service 实现类
*
* @author jason
*/
@Service
@Validated
public class ProductFavoriteServiceImpl implements ProductFavoriteService {
@Resource
private ProductFavoriteMapper mapper;
@Override
public Boolean collect(@Valid AppFavoriteReqVO reqVO) {
Long userId = getLoginUserId();
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
if (Objects.nonNull(favoriteDO)) {
throw exception(COLLECTION_EXISTS);
}
ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO);
int count = mapper.insert(entity);
return count == 1;
}
@Override
public Boolean cancelCollect(@Valid AppFavoriteReqVO reqVO) {
Long loginUserId = getLoginUserId();
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(loginUserId, reqVO.getSpuId(), reqVO.getType());
if (Objects.isNull(favoriteDO)) {
throw exception(COLLECTION_NOT_EXISTS);
}
int count = mapper.deleteById(favoriteDO.getId());
return count == 1;
}
@Override
public PageResult<AppFavoriteRespVO> pageCollectList(@Valid AppFavoritePageReqVO reqVO) {
Long userId = getLoginUserId();
return mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO);
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
<select id="selectFavoriteProductList"
resultType="cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO">
SELECT f.id, f.type, f.spu_id as spuId , p.name as spuName, p.pic_url as picUrl , p.price
FROM product_favorite f
INNER JOIN product_spu p ON f.spu_id = p.id and f.deleted = 0
where f.type = #{type} and f.user_id = #{userId}
ORDER BY f.id DESC
</select>
</mapper>