Commit 14d02bdfb3931be8f98ebdcb4bfd6737e27d876b
1 parent
96e2bf02
商品收藏修改
Showing
10 changed files
with
117 additions
and
98 deletions
yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java
| @@ -49,7 +49,7 @@ public interface ErrorCodeConstants { | @@ -49,7 +49,7 @@ public interface ErrorCodeConstants { | ||
| 49 | ErrorCode COMMENT_ADDITIONAL_EXISTS = new ErrorCode(1008007003, "商品追加评价已存在"); | 49 | ErrorCode COMMENT_ADDITIONAL_EXISTS = new ErrorCode(1008007003, "商品追加评价已存在"); |
| 50 | 50 | ||
| 51 | // ========== 商品 收藏 1008008000 ========== | 51 | // ========== 商品 收藏 1008008000 ========== |
| 52 | - ErrorCode COLLECTION_EXISTS = new ErrorCode(1008008000, "该商品已经被收藏"); | ||
| 53 | - ErrorCode COLLECTION_NOT_EXISTS = new ErrorCode(1008008001, "商品收藏不存在"); | 52 | + ErrorCode PRODUCT_FAVORITE_EXISTS = new ErrorCode(1008008000, "该商品已经被收藏"); |
| 53 | + ErrorCode PRODUCT_FAVORITE_NOT_EXISTS = new ErrorCode(1008008001, "商品收藏不存在"); | ||
| 54 | 54 | ||
| 55 | } | 55 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/AppFavoriteController.java
| 1 | package cn.iocoder.yudao.module.product.controller.app.favorite; | 1 | package cn.iocoder.yudao.module.product.controller.app.favorite; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.lang.Assert; | ||
| 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; | 5 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; |
| 7 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; | 6 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; |
| 8 | -import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService; | ||
| 9 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; | 7 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; |
| 8 | +import cn.iocoder.yudao.module.product.service.favorite.ProductFavoriteService; | ||
| 10 | import io.swagger.v3.oas.annotations.Operation; | 9 | import io.swagger.v3.oas.annotations.Operation; |
| 11 | import io.swagger.v3.oas.annotations.tags.Tag; | 10 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 12 | import org.springframework.web.bind.annotation.*; | 11 | import org.springframework.web.bind.annotation.*; |
| 13 | 12 | ||
| 14 | import javax.annotation.Resource; | 13 | import javax.annotation.Resource; |
| 15 | import javax.validation.Valid; | 14 | import javax.validation.Valid; |
| 16 | -import java.util.Objects; | ||
| 17 | 15 | ||
| 18 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 16 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 19 | -import static cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum.COLLECT; | 17 | +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| 20 | 18 | ||
| 21 | @Tag(name = "用户 APP - 商品收藏") | 19 | @Tag(name = "用户 APP - 商品收藏") |
| 22 | @RestController | 20 | @RestController |
| @@ -26,30 +24,31 @@ public class AppFavoriteController { | @@ -26,30 +24,31 @@ public class AppFavoriteController { | ||
| 26 | @Resource | 24 | @Resource |
| 27 | private ProductFavoriteService productFavoriteService; | 25 | private ProductFavoriteService productFavoriteService; |
| 28 | 26 | ||
| 29 | - // TODO @jason:创建;create | ||
| 30 | - @PostMapping(value = "/collect") | 27 | + @PostMapping(value = "/create") |
| 31 | @Operation(summary = "商品收藏") | 28 | @Operation(summary = "商品收藏") |
| 32 | - public CommonResult<Boolean> collect(@RequestBody @Valid AppFavoriteReqVO reqVO) { | ||
| 33 | - Assert.isTrue(Objects.equals(COLLECT.getType(), reqVO.getType()), "参数type 不匹配"); | ||
| 34 | - return success(productFavoriteService.collect(reqVO)); | 29 | + public CommonResult<Boolean> create(@RequestBody @Valid AppFavoriteReqVO reqVO) { |
| 30 | + Long loginUserId = getLoginUserId(); | ||
| 31 | + return success(productFavoriteService.create(loginUserId, reqVO)); | ||
| 35 | } | 32 | } |
| 36 | 33 | ||
| 37 | - // TODO @jason:创建;delete;使用 @DeleteMapping | ||
| 38 | - @PostMapping(value = "/cancelCollect") | 34 | + @DeleteMapping(value = "/delete") |
| 39 | @Operation(summary = "取消商品收藏(通过商品详情)") | 35 | @Operation(summary = "取消商品收藏(通过商品详情)") |
| 40 | - public CommonResult<Boolean> cancelCollect(@RequestBody @Valid AppFavoriteReqVO reqVO) { | ||
| 41 | - // TODO @jason:是不是不用校验呀? | ||
| 42 | - Assert.isTrue(Objects.equals(COLLECT.getType(), reqVO.getType()), "参数type 不匹配"); | ||
| 43 | - return success(productFavoriteService.cancelCollect(reqVO)); | 36 | + public CommonResult<Boolean> delete(@RequestBody @Valid AppFavoriteReqVO reqVO) { |
| 37 | + Long loginUserId = getLoginUserId(); | ||
| 38 | + return success(productFavoriteService.delete(loginUserId,reqVO)); | ||
| 44 | } | 39 | } |
| 45 | 40 | ||
| 46 | - // TODO @jason:page;分页 | ||
| 47 | - @GetMapping(value = "/collectList") | ||
| 48 | - @Operation(summary = "商品收藏列表") | ||
| 49 | - public CommonResult<PageResult<AppFavoriteRespVO>> pageCollectList(AppFavoritePageReqVO reqVO) { | ||
| 50 | - return success(productFavoriteService.pageCollectList(reqVO)); | 41 | + @GetMapping(value = "/page") |
| 42 | + @Operation(summary = "分页获取商品收藏列表") | ||
| 43 | + public CommonResult<PageResult<AppFavoriteRespVO>> page(AppFavoritePageReqVO reqVO) { | ||
| 44 | + Long userId = getLoginUserId(); | ||
| 45 | + return success(productFavoriteService.page(userId,reqVO)); | ||
| 51 | } | 46 | } |
| 52 | 47 | ||
| 53 | - // TODO @json:需要在给一个,用户查询某个商品是否收藏;详情页要用 | ||
| 54 | - | 48 | + @GetMapping(value = "/checkFavorite") |
| 49 | + @Operation(summary = "检查是否收藏过商品") | ||
| 50 | + public CommonResult<Boolean> checkFavorite(AppFavoriteReqVO reqVO) { | ||
| 51 | + Long userId = getLoginUserId(); | ||
| 52 | + return success(productFavoriteService.checkFavorite(userId,reqVO)); | ||
| 53 | + } | ||
| 55 | } | 54 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteReqVO.java
| @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull; | @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull; | ||
| 9 | 9 | ||
| 10 | import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; | 10 | import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; |
| 11 | 11 | ||
| 12 | -@Schema(description = "用户 APP - 商品收藏创建 Request VO") | 12 | +@Schema(description = "用户 APP - 商品收藏 Request VO") |
| 13 | @Data | 13 | @Data |
| 14 | public class AppFavoriteReqVO { | 14 | public class AppFavoriteReqVO { |
| 15 | 15 |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/favorite/vo/AppFavoriteRespVO.java
| @@ -3,24 +3,22 @@ package cn.iocoder.yudao.module.product.controller.app.favorite.vo; | @@ -3,24 +3,22 @@ package cn.iocoder.yudao.module.product.controller.app.favorite.vo; | ||
| 3 | import io.swagger.v3.oas.annotations.media.Schema; | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | import lombok.Data; | 4 | import lombok.Data; |
| 5 | 5 | ||
| 6 | +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; | ||
| 7 | + | ||
| 6 | @Schema(description = "用户APP - 商品收藏 Response VO") | 8 | @Schema(description = "用户APP - 商品收藏 Response VO") |
| 7 | @Data | 9 | @Data |
| 8 | public class AppFavoriteRespVO { | 10 | public class AppFavoriteRespVO { |
| 9 | 11 | ||
| 10 | - // TODO @jason:required true 哈 | ||
| 11 | - @Schema(description = "编号", example = "1") | 12 | + @Schema(description = "编号", requiredMode = REQUIRED, example = "1") |
| 12 | private Long id; | 13 | private Long id; |
| 13 | 14 | ||
| 14 | - // TODO @jason:required true 哈 | ||
| 15 | - @Schema(description = "商品 SPU 编号", example = "29502") | 15 | + @Schema(description = "商品 SPU 编号", requiredMode = REQUIRED, example = "29502") |
| 16 | private Long spuId; | 16 | private Long spuId; |
| 17 | 17 | ||
| 18 | - // TODO @jason:required true 哈 | ||
| 19 | - @Schema(description = "类型", example = "1") | 18 | + @Schema(description = "类型", requiredMode = REQUIRED, example = "1") |
| 20 | private Integer type; | 19 | private Integer type; |
| 21 | 20 | ||
| 22 | // ========== 商品相关字段 ========== | 21 | // ========== 商品相关字段 ========== |
| 23 | - | ||
| 24 | @Schema(description = "商品 SPU 名称", example = "赵六") | 22 | @Schema(description = "商品 SPU 名称", example = "赵六") |
| 25 | private String spuName; | 23 | private String spuName; |
| 26 | 24 |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/favorite/ProductFavoriteConvert.java
| 1 | package cn.iocoder.yudao.module.product.convert.favorite; | 1 | package cn.iocoder.yudao.module.product.convert.favorite; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; | 3 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; |
| 4 | +import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; | ||
| 4 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; | 5 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; |
| 6 | +import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; | ||
| 5 | import org.mapstruct.Mapper; | 7 | import org.mapstruct.Mapper; |
| 8 | +import org.mapstruct.Mapping; | ||
| 6 | import org.mapstruct.factory.Mappers; | 9 | import org.mapstruct.factory.Mappers; |
| 7 | 10 | ||
| 8 | @Mapper | 11 | @Mapper |
| @@ -11,4 +14,7 @@ public interface ProductFavoriteConvert { | @@ -11,4 +14,7 @@ public interface ProductFavoriteConvert { | ||
| 11 | ProductFavoriteConvert INSTANCE = Mappers.getMapper(ProductFavoriteConvert.class); | 14 | ProductFavoriteConvert INSTANCE = Mappers.getMapper(ProductFavoriteConvert.class); |
| 12 | 15 | ||
| 13 | ProductFavoriteDO convert(Long userId, AppFavoriteReqVO reqVO); | 16 | ProductFavoriteDO convert(Long userId, AppFavoriteReqVO reqVO); |
| 17 | + @Mapping(target = "id", source = "favoriteDO.id") | ||
| 18 | + @Mapping(target = "spuName", source = "spuDO.name") | ||
| 19 | + AppFavoriteRespVO convert(ProductSpuDO spuDO, ProductFavoriteDO favoriteDO); | ||
| 14 | } | 20 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/favorite/ProductFavoriteDO.java
| 1 | package cn.iocoder.yudao.module.product.dal.dataobject.favorite; | 1 | package cn.iocoder.yudao.module.product.dal.dataobject.favorite; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; | 3 | +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; |
| 4 | import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; | 4 | import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; |
| 5 | +import cn.iocoder.yudao.module.product.enums.favorite.ProductFavoriteTypeEnum; | ||
| 5 | import com.baomidou.mybatisplus.annotation.KeySequence; | 6 | import com.baomidou.mybatisplus.annotation.KeySequence; |
| 6 | import com.baomidou.mybatisplus.annotation.TableId; | 7 | import com.baomidou.mybatisplus.annotation.TableId; |
| 7 | import com.baomidou.mybatisplus.annotation.TableName; | 8 | import com.baomidou.mybatisplus.annotation.TableName; |
| @@ -20,7 +21,7 @@ import lombok.*; | @@ -20,7 +21,7 @@ import lombok.*; | ||
| 20 | @Builder | 21 | @Builder |
| 21 | @NoArgsConstructor | 22 | @NoArgsConstructor |
| 22 | @AllArgsConstructor | 23 | @AllArgsConstructor |
| 23 | -public class ProductFavoriteDO extends TenantBaseDO { // TODO @jason:如无必要,使用 BaseDO 哈。例如说 tenant_id 在业务里,是否需要使用到 | 24 | +public class ProductFavoriteDO extends BaseDO { |
| 24 | 25 | ||
| 25 | /** | 26 | /** |
| 26 | * 编号,主键自增 | 27 | * 编号,主键自增 |
| @@ -40,7 +41,9 @@ public class ProductFavoriteDO extends TenantBaseDO { // TODO @jason:如无必 | @@ -40,7 +41,9 @@ public class ProductFavoriteDO extends TenantBaseDO { // TODO @jason:如无必 | ||
| 40 | */ | 41 | */ |
| 41 | private Long spuId; | 42 | private Long spuId; |
| 42 | /** | 43 | /** |
| 43 | - * 类型 1 收藏;2 点赞 // TODO @jason:不要注释 1 收藏 2 点赞;而是注释好,它对应的枚举类 | 44 | + * 类型 |
| 45 | + * | ||
| 46 | + * 枚举 {@link ProductFavoriteTypeEnum} | ||
| 44 | */ | 47 | */ |
| 45 | private Integer type; | 48 | private Integer type; |
| 46 | 49 |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/favorite/ProductFavoriteMapper.java
| @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | ||
| 8 | import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; | 8 | import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; |
| 9 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; | 9 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; |
| 10 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; | 10 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; |
| 11 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
| 11 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 12 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 12 | import org.apache.ibatis.annotations.Mapper; | 13 | import org.apache.ibatis.annotations.Mapper; |
| 13 | import org.apache.ibatis.annotations.Param; | 14 | import org.apache.ibatis.annotations.Param; |
| @@ -15,24 +16,23 @@ import org.apache.ibatis.annotations.Param; | @@ -15,24 +16,23 @@ import org.apache.ibatis.annotations.Param; | ||
| 15 | @Mapper | 16 | @Mapper |
| 16 | public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> { | 17 | public interface ProductFavoriteMapper extends BaseMapperX<ProductFavoriteDO> { |
| 17 | 18 | ||
| 18 | - default ProductFavoriteDO selectByUserAndSpuAndType(Long userId, Long spuId, Integer type) { | ||
| 19 | - Assert.notNull(userId, "the userId argument must not be null"); | ||
| 20 | - Assert.notNull(spuId, "the spuId argument must not be null"); | ||
| 21 | - Assert.notNull(type, "the type argument must not be null"); | 19 | + default ProductFavoriteDO selectByUserAndSpuAndType(Long userId, Long spuId, Integer type) { |
| 20 | + Assert.notNull(userId, "the userId must not be null"); | ||
| 21 | + Assert.notNull(spuId, "the spuId must not be null"); | ||
| 22 | + Assert.notNull(type, "the type must not be null"); | ||
| 22 | return selectOne(new LambdaQueryWrapperX<ProductFavoriteDO>() | 23 | return selectOne(new LambdaQueryWrapperX<ProductFavoriteDO>() |
| 23 | .eq(ProductFavoriteDO::getUserId, userId) | 24 | .eq(ProductFavoriteDO::getUserId, userId) |
| 24 | .eq(ProductFavoriteDO::getSpuId, spuId) | 25 | .eq(ProductFavoriteDO::getSpuId, spuId) |
| 25 | .eq(ProductFavoriteDO::getType, type)); | 26 | .eq(ProductFavoriteDO::getType, type)); |
| 26 | } | 27 | } |
| 27 | 28 | ||
| 28 | - default PageResult<AppFavoriteRespVO> selectPageByUserAndType(Long userId, Integer type, PageParam pageParam){ | ||
| 29 | - Page<AppFavoriteRespVO> page = MyBatisUtils.buildPage(pageParam); | ||
| 30 | - page = selectFavoriteProductList(page, userId, type); | ||
| 31 | - return new PageResult<>(page.getRecords(), page.getTotal()); | 29 | + default PageResult<ProductFavoriteDO> selectPageByUserAndType(Long userId, Integer type, PageParam pageParam) { |
| 30 | + Assert.notNull(userId, "the userId must not be null"); | ||
| 31 | + Assert.notNull(type, "the type must not be null"); | ||
| 32 | + Assert.notNull(pageParam, "the pageParam must not be null"); | ||
| 33 | + return selectPage(pageParam, new LambdaQueryWrapper<ProductFavoriteDO>() | ||
| 34 | + .eq(ProductFavoriteDO::getUserId, userId) | ||
| 35 | + .eq(ProductFavoriteDO::getType, type) | ||
| 36 | + .orderByDesc(ProductFavoriteDO::getId)); | ||
| 32 | } | 37 | } |
| 33 | - | ||
| 34 | - // TODO @jason:内存中拼接哈。这样好兼容更多的 db 类型; | ||
| 35 | - Page<AppFavoriteRespVO> selectFavoriteProductList(Page<AppFavoriteRespVO> page, | ||
| 36 | - @Param("userId") Long userId, | ||
| 37 | - @Param("type") Integer type); | ||
| 38 | } | 38 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteService.java
| @@ -15,20 +15,31 @@ import javax.validation.Valid; | @@ -15,20 +15,31 @@ import javax.validation.Valid; | ||
| 15 | public interface ProductFavoriteService { | 15 | public interface ProductFavoriteService { |
| 16 | 16 | ||
| 17 | /** | 17 | /** |
| 18 | - * 商品收藏 | 18 | + * 创建商品收藏 |
| 19 | + * @param userId 用户id | ||
| 19 | * @param reqVO 请求vo | 20 | * @param reqVO 请求vo |
| 20 | */ | 21 | */ |
| 21 | - Boolean collect(@Valid AppFavoriteReqVO reqVO); | 22 | + Boolean create(Long userId, @Valid AppFavoriteReqVO reqVO); |
| 22 | 23 | ||
| 23 | /** | 24 | /** |
| 24 | * 取消商品收藏 (通过商品详情页面) | 25 | * 取消商品收藏 (通过商品详情页面) |
| 26 | + * @param userId 用户id | ||
| 25 | * @param reqVO 请求vo | 27 | * @param reqVO 请求vo |
| 26 | */ | 28 | */ |
| 27 | - Boolean cancelCollect(@Valid AppFavoriteReqVO reqVO); | 29 | + Boolean delete(Long userId, @Valid AppFavoriteReqVO reqVO); |
| 28 | 30 | ||
| 29 | /** | 31 | /** |
| 30 | * 分页查询用户收藏列表 | 32 | * 分页查询用户收藏列表 |
| 33 | + * @param userId 用户id | ||
| 31 | * @param reqVO 请求 vo | 34 | * @param reqVO 请求 vo |
| 32 | */ | 35 | */ |
| 33 | - PageResult<AppFavoriteRespVO> pageCollectList(@Valid AppFavoritePageReqVO reqVO); | 36 | + PageResult<AppFavoriteRespVO> page(Long userId, @Valid AppFavoritePageReqVO reqVO); |
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * 检查是否收藏过商品 | ||
| 40 | + * @param userId 用户id | ||
| 41 | + * @param reqVO 请求 vo | ||
| 42 | + * @return true: 已收藏 false: 未收藏 | ||
| 43 | + */ | ||
| 44 | + Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO); | ||
| 34 | } | 45 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/favorite/ProductFavoriteServiceImpl.java
| 1 | package cn.iocoder.yudao.module.product.service.favorite; | 1 | package cn.iocoder.yudao.module.product.service.favorite; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.lang.Assert; | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 4 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; | 6 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO; |
| 5 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; | 7 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO; |
| 6 | import cn.iocoder.yudao.module.product.convert.favorite.ProductFavoriteConvert; | 8 | import cn.iocoder.yudao.module.product.convert.favorite.ProductFavoriteConvert; |
| 7 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; | 9 | import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO; |
| 10 | +import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; | ||
| 8 | import cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper; | 11 | import cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper; |
| 9 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; | 12 | import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO; |
| 13 | +import cn.iocoder.yudao.module.product.service.spu.ProductSpuService; | ||
| 10 | import org.springframework.stereotype.Service; | 14 | import org.springframework.stereotype.Service; |
| 11 | import org.springframework.validation.annotation.Validated; | 15 | import org.springframework.validation.annotation.Validated; |
| 12 | 16 | ||
| 13 | import javax.annotation.Resource; | 17 | import javax.annotation.Resource; |
| 14 | import javax.validation.Valid; | 18 | import javax.validation.Valid; |
| 19 | +import java.util.ArrayList; | ||
| 20 | +import java.util.List; | ||
| 21 | +import java.util.Map; | ||
| 15 | import java.util.Objects; | 22 | import java.util.Objects; |
| 16 | 23 | ||
| 17 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 24 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 18 | -import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | ||
| 19 | -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTION_EXISTS; | ||
| 20 | -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTION_NOT_EXISTS; | 25 | +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_EXISTS; |
| 26 | +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_NOT_EXISTS; | ||
| 21 | 27 | ||
| 22 | /** | 28 | /** |
| 23 | * 商品收藏 Service 实现类 | 29 | * 商品收藏 Service 实现类 |
| @@ -27,43 +33,61 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTIO | @@ -27,43 +33,61 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTIO | ||
| 27 | @Service | 33 | @Service |
| 28 | @Validated | 34 | @Validated |
| 29 | public class ProductFavoriteServiceImpl implements ProductFavoriteService { | 35 | public class ProductFavoriteServiceImpl implements ProductFavoriteService { |
| 30 | - | 36 | + @Resource |
| 37 | + private ProductSpuService productSpuService; | ||
| 31 | @Resource | 38 | @Resource |
| 32 | private ProductFavoriteMapper mapper; | 39 | private ProductFavoriteMapper mapper; |
| 33 | 40 | ||
| 34 | @Override | 41 | @Override |
| 35 | - public Boolean collect(@Valid AppFavoriteReqVO reqVO) { | ||
| 36 | - // TODO @jason:userId 要从 Controller 传递过来,Service 不能有转台 | ||
| 37 | - Long userId = getLoginUserId(); | ||
| 38 | - // TODO @jason:代码缩进不对; | 42 | + public Boolean create(Long userId, @Valid AppFavoriteReqVO reqVO) { |
| 43 | + Assert.notNull(userId, "the userId must not be null"); | ||
| 39 | ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | 44 | ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); |
| 40 | if (Objects.nonNull(favoriteDO)) { | 45 | if (Objects.nonNull(favoriteDO)) { |
| 41 | - throw exception(COLLECTION_EXISTS); | 46 | + throw exception(PRODUCT_FAVORITE_EXISTS); |
| 42 | } | 47 | } |
| 43 | - | ||
| 44 | - // TODO @jason:插入只有成功,不用判断 1 | ||
| 45 | ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO); | 48 | ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO); |
| 46 | - int count = mapper.insert(entity); | ||
| 47 | - return count == 1; | 49 | + mapper.insert(entity); |
| 50 | + return Boolean.TRUE; | ||
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | @Override | 53 | @Override |
| 51 | - public Boolean cancelCollect(@Valid AppFavoriteReqVO reqVO) { | ||
| 52 | - // TODO @jason:代码缩进不对; | ||
| 53 | - Long loginUserId = getLoginUserId(); | ||
| 54 | - ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(loginUserId, reqVO.getSpuId(), reqVO.getType()); | 54 | + public Boolean delete(Long userId, @Valid AppFavoriteReqVO reqVO) { |
| 55 | + Assert.notNull(userId, "the userId must not be null "); | ||
| 56 | + ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | ||
| 55 | if (Objects.isNull(favoriteDO)) { | 57 | if (Objects.isNull(favoriteDO)) { |
| 56 | - throw exception(COLLECTION_NOT_EXISTS); | 58 | + throw exception(PRODUCT_FAVORITE_NOT_EXISTS); |
| 57 | } | 59 | } |
| 58 | - // TODO @jason:插入只有成功,不用判断 1 | ||
| 59 | - int count = mapper.deleteById(favoriteDO.getId()); | ||
| 60 | - return count == 1; | 60 | + mapper.deleteById(favoriteDO.getId()); |
| 61 | + return Boolean.TRUE; | ||
| 61 | } | 62 | } |
| 62 | 63 | ||
| 63 | @Override | 64 | @Override |
| 64 | - public PageResult<AppFavoriteRespVO> pageCollectList(@Valid AppFavoritePageReqVO reqVO) { | ||
| 65 | - Long userId = getLoginUserId(); | ||
| 66 | - return mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); | 65 | + public PageResult<AppFavoriteRespVO> page(Long userId, @Valid AppFavoritePageReqVO reqVO) { |
| 66 | + Assert.notNull(userId, "the userId must not be null "); | ||
| 67 | + PageResult<ProductFavoriteDO> favorites = mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO); | ||
| 68 | + if (favorites.getTotal() > 0) { | ||
| 69 | + PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal()); | ||
| 70 | + List<ProductFavoriteDO> list = favorites.getList(); | ||
| 71 | + //得到商品spu 信息 | ||
| 72 | + List<Long> spuIds = CollectionUtils.convertList(list, ProductFavoriteDO::getSpuId); | ||
| 73 | + Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(productSpuService.getSpuList(spuIds), ProductSpuDO::getId, val -> val); | ||
| 74 | + List<AppFavoriteRespVO> resultList = new ArrayList<>(list.size()); | ||
| 75 | + for (ProductFavoriteDO item : list) { | ||
| 76 | + ProductSpuDO spuDO = spuMap.get(item.getSpuId()); | ||
| 77 | + resultList.add(ProductFavoriteConvert.INSTANCE.convert(spuDO, item)); | ||
| 78 | + } | ||
| 79 | + pageResult.setList(resultList); | ||
| 80 | + return pageResult; | ||
| 81 | + }else { | ||
| 82 | + return PageResult.empty(); | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + @Override | ||
| 87 | + public Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) { | ||
| 88 | + Assert.notNull(userId, "the userId must not be null "); | ||
| 89 | + ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType()); | ||
| 90 | + return Objects.nonNull(favoriteDO); | ||
| 67 | } | 91 | } |
| 68 | 92 | ||
| 69 | } | 93 | } |
yudao-module-mall/yudao-module-product-biz/src/main/resources/mapper/favorite/ProductFavoriteMapper.xml deleted
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
| 3 | -<mapper namespace="cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper"> | ||
| 4 | - | ||
| 5 | - <!-- | ||
| 6 | - 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。 | ||
| 7 | - 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。 | ||
| 8 | - 代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。 | ||
| 9 | - 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ | ||
| 10 | - --> | ||
| 11 | - | ||
| 12 | - <select id="selectFavoriteProductList" | ||
| 13 | - resultType="cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO"> | ||
| 14 | - SELECT f.id, f.type, f.spu_id as spuId , p.name as spuName, p.pic_url as picUrl , p.price | ||
| 15 | - FROM product_favorite f | ||
| 16 | - INNER JOIN product_spu p ON f.spu_id = p.id and f.deleted = 0 | ||
| 17 | - where f.type = #{type} and f.user_id = #{userId} | ||
| 18 | - ORDER BY f.id DESC | ||
| 19 | - | ||
| 20 | - </select> | ||
| 21 | - | ||
| 22 | -</mapper> | ||
| 23 | \ No newline at end of file | 0 | \ No newline at end of file |