diff --git a/sql/mysql/optional/mall.sql b/sql/mysql/optional/mall.sql index 6a4b73e544..77356cf65d 100644 --- a/sql/mysql/optional/mall.sql +++ b/sql/mysql/optional/mall.sql @@ -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 -- ---------------------------- diff --git a/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java index fe9319dae1..4338103b48 100644 --- a/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java @@ -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, "商品收藏不存在"); + } diff --git a/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/favorite/ProductFavoriteTypeEnum.java b/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/favorite/ProductFavoriteTypeEnum.java new file mode 100644 index 0000000000..3dee472fb9 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/favorite/ProductFavoriteTypeEnum.java @@ -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; + } +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java new file mode 100644 index 0000000000..385cde3ec3 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java @@ -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 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 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> pageCollectList(AppFavoritePageReqVO reqVO) { + return success(productFavoriteService.pageCollectList(reqVO)); + } +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoritePageReqVO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoritePageReqVO.java new file mode 100644 index 0000000000..ef68cb7364 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoritePageReqVO.java @@ -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; +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java new file mode 100644 index 0000000000..b611922cc7 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java @@ -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; +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java new file mode 100644 index 0000000000..b15c49a628 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java @@ -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; +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/favorite/ProductFavoriteConvert.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/favorite/ProductFavoriteConvert.java new file mode 100644 index 0000000000..04b6dc56af --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/favorite/ProductFavoriteConvert.java @@ -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); +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/favorite/ProductFavoriteDO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/favorite/ProductFavoriteDO.java index 54d4b31ac2..20b712affb 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/favorite/ProductFavoriteDO.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/favorite/ProductFavoriteDO.java @@ -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; } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java new file mode 100644 index 0000000000..e9ee46a7f5 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java @@ -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 { + + 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() + .eq(ProductFavoriteDO::getUserId, userId) + .eq(ProductFavoriteDO::getSpuId, spuId) + .eq(ProductFavoriteDO::getType, type)); + } + + default PageResult selectPageByUserAndType(Long userId, Integer type, PageParam pageParam){ + Page page = MyBatisUtils.buildPage(pageParam); + page = selectFavoriteProductList(page, userId, type); + return new PageResult<>(page.getRecords(), page.getTotal()); + } + + Page selectFavoriteProductList(Page page, + @Param("userId") Long userId, + @Param("type") Integer type); +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java new file mode 100644 index 0000000000..c1b85af176 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java @@ -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 pageCollectList(@Valid AppFavoritePageReqVO reqVO); +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java new file mode 100644 index 0000000000..1b295157c3 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java @@ -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 pageCollectList(@Valid AppFavoritePageReqVO reqVO) { + Long userId = getLoginUserId(); + return mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); + } +} diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/resources/mapper/favorite/ProductFavoriteMapper.xml b/yudao-module-mall/yudao-module-product-biz/src/main/resources/mapper/favorite/ProductFavoriteMapper.xml new file mode 100644 index 0000000000..8d2741e035 --- /dev/null +++ b/yudao-module-mall/yudao-module-product-biz/src/main/resources/mapper/favorite/ProductFavoriteMapper.xml @@ -0,0 +1,22 @@ + + + + + + + + + \ No newline at end of file