Commit 6a0f7134524569843391070db148510da58f3c31

Authored by YunaiV
1 parent 1976571a

mall:完善商品分类的后端接口

Showing 23 changed files with 407 additions and 574 deletions
yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java
... ... @@ -3,16 +3,18 @@ package cn.iocoder.yudao.module.product.enums;
3 3 import cn.iocoder.yudao.framework.common.exception.ErrorCode;
4 4  
5 5 /**
6   - * product 错误码枚举类
7   - * <p>
  6 + * Product 错误码枚举类
  7 + *
8 8 * product 系统,使用 1-008-000-000 段
9 9 */
10 10 public interface ErrorCodeConstants {
11 11  
12   - // ========== 商品分类相关 1008001000============
13   - ErrorCode CATEGORY_NOT_EXISTS = new ErrorCode(1008001000, "商品分类不存在");
14   - ErrorCode CATEGORY_PARENT_NOT_EXISTS = new ErrorCode(1008001001, "父分类不存在");
15   - ErrorCode CATEGORY_EXISTS_CHILDREN = new ErrorCode(1008001002, "存在子分类,无法删除");
  12 + // ========== 商品分类相关 1008001000 ============
  13 + ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1008001000, "商品分类不存在");
  14 + ErrorCode PRODUCT_CATEGORY_PARENT_NOT_EXISTS = new ErrorCode(1008001001, "父分类不存在");
  15 + ErrorCode PRODUCT_CATEGORY_PARENT_NOT_FIRST_LEVEL = new ErrorCode(1008001002, "父分类不能是二级分类");
  16 + ErrorCode PRODUCT_CATEGORY_EXISTS_CHILDREN = new ErrorCode(1008001003, "存在子分类,无法删除");
  17 + ErrorCode PRODUCT_CATEGORY_DISABLED = new ErrorCode(1008001004, "商品分类({})已禁用,无法使用");
16 18  
17 19 // ========== 品牌相关编号 1008002000 ==========
18 20 ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在");
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/CategoryController.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/ProductCategoryController.java
1 1 package cn.iocoder.yudao.module.product.controller.admin.category;
2 2  
3 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
4   -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
5   -import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
6   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
  4 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryRespVO;
  5 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
  6 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
  7 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
7 8 import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
8   -import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
9   -import cn.iocoder.yudao.module.product.service.category.CategoryService;
  9 +import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
  10 +import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
10 11 import io.swagger.annotations.Api;
11 12 import io.swagger.annotations.ApiImplicitParam;
12 13 import io.swagger.annotations.ApiOperation;
... ... @@ -15,36 +16,33 @@ import org.springframework.validation.annotation.Validated;
15 16 import org.springframework.web.bind.annotation.*;
16 17  
17 18 import javax.annotation.Resource;
18   -import javax.servlet.http.HttpServletResponse;
19 19 import javax.validation.Valid;
20   -import java.io.IOException;
21 20 import java.util.Comparator;
22 21 import java.util.List;
23 22  
24 23 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
25   -import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
26 24  
27 25 @Api(tags = "管理后台 - 商品分类")
28 26 @RestController
29 27 @RequestMapping("/product/category")
30 28 @Validated
31   -public class CategoryController {
  29 +public class ProductCategoryController {
32 30  
33 31 @Resource
34   - private CategoryService categoryService;
  32 + private ProductCategoryService categoryService;
35 33  
36 34 @PostMapping("/create")
37 35 @ApiOperation("创建商品分类")
38 36 @PreAuthorize("@ss.hasPermission('product:category:create')")
39   - public CommonResult<Long> createCategory(@Valid @RequestBody CategoryCreateReqVO createReqVO) {
40   - return success(categoryService.createCategory(createReqVO));
  37 + public CommonResult<Long> createProductCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
  38 + return success(categoryService.createProductCategory(createReqVO));
41 39 }
42 40  
43 41 @PutMapping("/update")
44 42 @ApiOperation("更新商品分类")
45 43 @PreAuthorize("@ss.hasPermission('product:category:update')")
46   - public CommonResult<Boolean> updateCategory(@Valid @RequestBody CategoryUpdateReqVO updateReqVO) {
47   - categoryService.updateCategory(updateReqVO);
  44 + public CommonResult<Boolean> updateProductCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
  45 + categoryService.updateProductCategory(updateReqVO);
48 46 return success(true);
49 47 }
50 48  
... ... @@ -52,8 +50,8 @@ public class CategoryController {
52 50 @ApiOperation("删除商品分类")
53 51 @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
54 52 @PreAuthorize("@ss.hasPermission('product:category:delete')")
55   - public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
56   - categoryService.deleteCategory(id);
  53 + public CommonResult<Boolean> deleteProductCategory(@RequestParam("id") Long id) {
  54 + categoryService.deleteProductCategory(id);
57 55 return success(true);
58 56 }
59 57  
... ... @@ -61,32 +59,18 @@ public class CategoryController {
61 59 @ApiOperation("获得商品分类")
62 60 @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
63 61 @PreAuthorize("@ss.hasPermission('product:category:query')")
64   - public CommonResult<CategoryRespVO> getCategory(@RequestParam("id") Long id) {
65   - CategoryDO category = categoryService.getCategory(id);
  62 + public CommonResult<ProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
  63 + ProductCategoryDO category = categoryService.getProductCategory(id);
66 64 return success(CategoryConvert.INSTANCE.convert(category));
67 65 }
68 66  
69   - // TODO @JeromeSoar:这应该是个 app 的接口,提供商品分类的树结构。这个调整下,后端只返回列表,前端构建 tree。注意,不需要返回创建时间、是否开启等无关字段。
70   - // TODO @YunaiV: 这个是在管理端展示了一个类似菜单的分类树列表, treeListReqVO 只是查询参数的封装命名,返给前端的是列表数据。PS: 这里 /page 接口没有使用到。
71   - @GetMapping("/listByQuery")
  67 + @GetMapping("/list")
72 68 @ApiOperation("获得商品分类列表")
73 69 @PreAuthorize("@ss.hasPermission('product:category:query')")
74   - public CommonResult<List<CategoryRespVO>> listByQuery(@Valid CategoryTreeListReqVO treeListReqVO) {
75   - List<CategoryDO> list = categoryService.getCategoryTreeList(treeListReqVO);
76   - list.sort(Comparator.comparing(CategoryDO::getSort));
  70 + public CommonResult<List<ProductCategoryRespVO>> getProductCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
  71 + List<ProductCategoryDO> list = categoryService.getEnableProductCategoryList(treeListReqVO);
  72 + list.sort(Comparator.comparing(ProductCategoryDO::getSort));
77 73 return success(CategoryConvert.INSTANCE.convertList(list));
78 74 }
79   -
80   - @GetMapping("/export-excel")
81   - @ApiOperation("导出商品分类 Excel")
82   - @PreAuthorize("@ss.hasPermission('product:category:export')")
83   - @OperateLog(type = EXPORT)
84   - public void exportCategoryExcel(@Valid CategoryExportReqVO exportReqVO,
85   - HttpServletResponse response) throws IOException {
86   - List<CategoryDO> list = categoryService.getCategoryList(exportReqVO);
87   - // 导出 Excel
88   - List<CategoryExcelVO> datas = CategoryConvert.INSTANCE.convertList02(list);
89   - ExcelUtils.write(response, "商品分类.xls", "数据", CategoryExcelVO.class, datas);
90   - }
91 75  
92 76 }
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryExcelVO.java deleted
1   -package cn.iocoder.yudao.module.product.controller.admin.category.vo;
2   -
3   -import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
4   -import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
5   -import com.alibaba.excel.annotation.ExcelProperty;
6   -import lombok.Data;
7   -
8   -import java.util.Date;
9   -
10   -
11   -/**
12   - * 商品分类 Excel VO
13   - *
14   - * @author 芋道源码
15   - */
16   -@Data
17   -public class CategoryExcelVO {
18   -
19   - @ExcelProperty("分类编号")
20   - private Long id;
21   -
22   - @ExcelProperty("父分类编号")
23   - private Long parentId;
24   -
25   - @ExcelProperty("分类名称")
26   - private String name;
27   -
28   - @ExcelProperty("分类图标")
29   - private String icon;
30   -
31   - @ExcelProperty("分类图片")
32   - private String bannerUrl;
33   -
34   - @ExcelProperty("分类排序")
35   - private Integer sort;
36   -
37   - @ExcelProperty("分类描述")
38   - private String description;
39   -
40   - @ExcelProperty(value = "开启状态", converter = DictConvert.class)
41   - @DictFormat("common_status") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中
42   - private Integer status;
43   -
44   - @ExcelProperty("创建时间")
45   - private Date createTime;
46   -
47   -}
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryPageReqVO.java deleted
1   -package cn.iocoder.yudao.module.product.controller.admin.category.vo;
2   -
3   -import cn.iocoder.yudao.framework.common.pojo.PageParam;
4   -import io.swagger.annotations.ApiModel;
5   -import io.swagger.annotations.ApiModelProperty;
6   -import lombok.Data;
7   -import lombok.EqualsAndHashCode;
8   -import lombok.ToString;
9   -import org.springframework.format.annotation.DateTimeFormat;
10   -
11   -import java.util.Date;
12   -
13   -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
14   -
15   -@ApiModel("管理后台 - 商品分类分页 Request VO")
16   -@Data
17   -@EqualsAndHashCode(callSuper = true)
18   -@ToString(callSuper = true)
19   -public class CategoryPageReqVO extends PageParam {
20   -
21   - @ApiModelProperty(value = "分类名称", example = "办公文具")
22   - private String name;
23   -
24   - @ApiModelProperty(value = "开启状态", example = "0")
25   - private Integer status;
26   -
27   - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
28   - @ApiModelProperty(value = "创建时间")
29   - private Date[] createTime;
30   -
31   -}
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryTreeListReqVO.java deleted
1   -package cn.iocoder.yudao.module.product.controller.admin.category.vo;
2   -
3   -import io.swagger.annotations.ApiModel;
4   -import io.swagger.annotations.ApiModelProperty;
5   -import lombok.Data;
6   -import org.springframework.format.annotation.DateTimeFormat;
7   -
8   -import java.util.Date;
9   -
10   -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
11   -
12   -@Data
13   -@ApiModel(value = "管理后台 - 商品分类列表查询 Request VO", description = "参数和 CategoryPageReqVO 是一致的")
14   -public class CategoryTreeListReqVO extends CategoryExportReqVO {
15   -
16   - @ApiModelProperty(value = "分类名称", example = "办公文具")
17   - private String name;
18   -
19   - @ApiModelProperty(value = "开启状态", example = "0")
20   - private Integer status;
21   -
22   - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
23   - @ApiModelProperty(value = "创建时间")
24   - private Date[] createTime;
25   -}
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryBaseVO.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/ProductCategoryBaseVO.java
... ... @@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull;
11 11 * 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成
12 12 */
13 13 @Data
14   -public class CategoryBaseVO {
  14 +public class ProductCategoryBaseVO {
15 15  
16 16 @ApiModelProperty(value = "父分类编号", required = true, example = "1")
17 17 @NotNull(message = "父分类编号不能为空")
... ... @@ -21,13 +21,9 @@ public class CategoryBaseVO {
21 21 @NotBlank(message = "分类名称不能为空")
22 22 private String name;
23 23  
24   - @ApiModelProperty(value = "分类图标")
25   - @NotBlank(message = "分类图标不能为空")
26   - private String icon;
27   -
28 24 @ApiModelProperty(value = "分类图片", required = true)
29 25 @NotBlank(message = "分类图片不能为空")
30   - private String bannerUrl;
  26 + private String picUrl;
31 27  
32 28 @ApiModelProperty(value = "分类排序", required = true, example = "1")
33 29 private Integer sort;
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryCreateReqVO.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/ProductCategoryCreateReqVO.java
1 1 package cn.iocoder.yudao.module.product.controller.admin.category.vo;
2 2  
3 3 import lombok.*;
4   -import java.util.*;
5 4 import io.swagger.annotations.*;
6   -import javax.validation.constraints.*;
7 5  
8 6 @ApiModel("管理后台 - 商品分类创建 Request VO")
9 7 @Data
10 8 @EqualsAndHashCode(callSuper = true)
11 9 @ToString(callSuper = true)
12   -public class CategoryCreateReqVO extends CategoryBaseVO {
  10 +public class ProductCategoryCreateReqVO extends ProductCategoryBaseVO {
13 11  
14 12 }
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryExportReqVO.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/ProductCategoryListReqVO.java
... ... @@ -3,24 +3,12 @@ package cn.iocoder.yudao.module.product.controller.admin.category.vo;
3 3 import io.swagger.annotations.ApiModel;
4 4 import io.swagger.annotations.ApiModelProperty;
5 5 import lombok.Data;
6   -import org.springframework.format.annotation.DateTimeFormat;
7 6  
8   -import java.util.Date;
9   -
10   -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
11   -
12   -@ApiModel(value = "管理后台 - 商品分类 Excel 导出 Request VO", description = "参数和 CategoryPageReqVO 是一致的")
  7 +@ApiModel(value = "管理后台 - 商品分类列表查询 Request VO")
13 8 @Data
14   -public class CategoryExportReqVO {
  9 +public class ProductCategoryListReqVO {
15 10  
16 11 @ApiModelProperty(value = "分类名称", example = "办公文具")
17 12 private String name;
18 13  
19   - @ApiModelProperty(value = "开启状态", example = "0")
20   - private Integer status;
21   -
22   - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
23   - @ApiModelProperty(value = "创建时间")
24   - private Date[] createTime;
25   -
26 14 }
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryRespVO.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/ProductCategoryRespVO.java
... ... @@ -8,7 +8,7 @@ import io.swagger.annotations.*;
8 8 @Data
9 9 @EqualsAndHashCode(callSuper = true)
10 10 @ToString(callSuper = true)
11   -public class CategoryRespVO extends CategoryBaseVO {
  11 +public class ProductCategoryRespVO extends ProductCategoryBaseVO {
12 12  
13 13 @ApiModelProperty(value = "分类编号", required = true, example = "2")
14 14 private Long id;
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/CategoryUpdateReqVO.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/vo/ProductCategoryUpdateReqVO.java
1 1 package cn.iocoder.yudao.module.product.controller.admin.category.vo;
2 2  
3 3 import lombok.*;
4   -import java.util.*;
5 4 import io.swagger.annotations.*;
6 5 import javax.validation.constraints.*;
7 6  
... ... @@ -9,7 +8,7 @@ import javax.validation.constraints.*;
9 8 @Data
10 9 @EqualsAndHashCode(callSuper = true)
11 10 @ToString(callSuper = true)
12   -public class CategoryUpdateReqVO extends CategoryBaseVO {
  11 +public class ProductCategoryUpdateReqVO extends ProductCategoryBaseVO {
13 12  
14 13 @ApiModelProperty(value = "分类编号", required = true, example = "2")
15 14 @NotNull(message = "分类编号不能为空")
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/category/AppCategoryController.java
... ... @@ -3,8 +3,8 @@ package cn.iocoder.yudao.module.product.controller.app.category;
3 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
4 4 import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryListRespVO;
5 5 import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
6   -import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
7   -import cn.iocoder.yudao.module.product.service.category.CategoryService;
  6 +import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
  7 +import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
8 8 import io.swagger.annotations.Api;
9 9 import io.swagger.annotations.ApiOperation;
10 10 import org.springframework.validation.annotation.Validated;
... ... @@ -25,13 +25,13 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
25 25 public class AppCategoryController {
26 26  
27 27 @Resource
28   - private CategoryService categoryService;
  28 + private ProductCategoryService categoryService;
29 29  
30 30 @GetMapping("/list")
31 31 @ApiOperation("获得商品分类列表")
32 32 public CommonResult<List<AppCategoryListRespVO>> listByQuery() {
33   - List<CategoryDO> list = categoryService.getCategoryList();
34   - list.sort(Comparator.comparing(CategoryDO::getSort));
  33 + List<ProductCategoryDO> list = categoryService.getEnableProductCategoryList();
  34 + list.sort(Comparator.comparing(ProductCategoryDO::getSort));
35 35 return success(CategoryConvert.INSTANCE.convertList03(list));
36 36 }
37 37  
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/category/CategoryConvert.java
1 1 package cn.iocoder.yudao.module.product.convert.category;
2 2  
3   -import java.util.*;
4   -
5   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
6   -
  3 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
  4 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryRespVO;
  5 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
7 6 import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryListRespVO;
  7 +import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
8 8 import org.mapstruct.Mapper;
9 9 import org.mapstruct.factory.Mappers;
10   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
11   -import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
  10 +
  11 +import java.util.List;
12 12  
13 13 /**
14 14 * 商品分类 Convert
... ... @@ -20,17 +20,13 @@ public interface CategoryConvert {
20 20  
21 21 CategoryConvert INSTANCE = Mappers.getMapper(CategoryConvert.class);
22 22  
23   - CategoryDO convert(CategoryCreateReqVO bean);
24   -
25   - CategoryDO convert(CategoryUpdateReqVO bean);
26   -
27   - CategoryRespVO convert(CategoryDO bean);
  23 + ProductCategoryDO convert(ProductCategoryCreateReqVO bean);
28 24  
29   - List<CategoryRespVO> convertList(List<CategoryDO> list);
  25 + ProductCategoryDO convert(ProductCategoryUpdateReqVO bean);
30 26  
31   - PageResult<CategoryRespVO> convertPage(PageResult<CategoryDO> page);
  27 + ProductCategoryRespVO convert(ProductCategoryDO bean);
32 28  
33   - List<CategoryExcelVO> convertList02(List<CategoryDO> list);
  29 + List<ProductCategoryRespVO> convertList(List<ProductCategoryDO> list);
34 30  
35   - List<AppCategoryListRespVO> convertList03(List<CategoryDO> list);
  31 + List<AppCategoryListRespVO> convertList03(List<ProductCategoryDO> list);
36 32 }
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/category/CategoryDO.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/category/ProductCategoryDO.java
... ... @@ -6,10 +6,13 @@ import com.baomidou.mybatisplus.annotation.TableId;
6 6 import com.baomidou.mybatisplus.annotation.TableName;
7 7 import lombok.*;
8 8  
9   -// TODO @JeromeSoar:Product 前缀
10 9 /**
11 10 * 商品分类 DO
12 11 *
  12 + * 商品分类一共两级:
  13 + * 1)一级分类:{@link #parentId} 等于 0
  14 + * 2)二级分类:{@link #parentId} 等于父分类
  15 + *
13 16 * @author 芋道源码
14 17 */
15 18 @TableName("product_category")
... ... @@ -19,7 +22,12 @@ import lombok.*;
19 22 @Builder
20 23 @NoArgsConstructor
21 24 @AllArgsConstructor
22   -public class CategoryDO extends BaseDO {
  25 +public class ProductCategoryDO extends BaseDO {
  26 +
  27 + /**
  28 + * 父分类编号 - 根分类
  29 + */
  30 + public static final Long PARENT_ID_NULL = 0L;
23 31  
24 32 /**
25 33 * 分类编号
... ... @@ -35,15 +43,12 @@ public class CategoryDO extends BaseDO {
35 43 */
36 44 private String name;
37 45 /**
38   - * 分类图标
39   - */
40   - private String icon;
41   - /**
42   - * 分类 Banner 图片
  46 + * 分类图片
43 47 *
44   - * 第一层的商品分类,会有该字段,用于用户 App 展示
  48 + * 一级分类:推荐 200 x 100 分辨率
  49 + * 二级分类:推荐 100 x 100 分辨率
45 50 */
46   - private String bannerUrl;
  51 + private String picUrl;
47 52 /**
48 53 * 分类排序
49 54 */
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/category/CategoryMapper.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/category/ProductCategoryMapper.java
1 1 package cn.iocoder.yudao.module.product.dal.mysql.category;
2 2  
3   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 3 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
5 4 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
6   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryExportReqVO;
7   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryPageReqVO;
8   -import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
  5 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
  6 +import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
9 7 import org.apache.ibatis.annotations.Mapper;
10 8  
11 9 import java.util.List;
... ... @@ -16,25 +14,20 @@ import java.util.List;
16 14 * @author 芋道源码
17 15 */
18 16 @Mapper
19   -public interface CategoryMapper extends BaseMapperX<CategoryDO> {
  17 +public interface ProductCategoryMapper extends BaseMapperX<ProductCategoryDO> {
20 18  
21   - default PageResult<CategoryDO> selectPage(CategoryPageReqVO reqVO) {
22   - return selectPage(reqVO, new LambdaQueryWrapperX<CategoryDO>()
23   - .likeIfPresent(CategoryDO::getName, reqVO.getName())
24   - .eqIfPresent(CategoryDO::getStatus, reqVO.getStatus())
25   - .betweenIfPresent(CategoryDO::getCreateTime, reqVO.getCreateTime())
26   - .orderByDesc(CategoryDO::getId));
  19 + default List<ProductCategoryDO> selectList(ProductCategoryListReqVO listReqVO) {
  20 + return selectList(new LambdaQueryWrapperX<ProductCategoryDO>()
  21 + .likeIfPresent(ProductCategoryDO::getName, listReqVO.getName())
  22 + .orderByDesc(ProductCategoryDO::getId));
27 23 }
28 24  
29   - default List<CategoryDO> selectList(CategoryExportReqVO reqVO) {
30   - return selectList(new LambdaQueryWrapperX<CategoryDO>()
31   - .likeIfPresent(CategoryDO::getName, reqVO.getName())
32   - .eqIfPresent(CategoryDO::getStatus, reqVO.getStatus())
33   - .betweenIfPresent(CategoryDO::getCreateTime, reqVO.getCreateTime())
34   - .orderByDesc(CategoryDO::getId));
  25 + default Long selectCountByParentId(Long parentId) {
  26 + return selectCount(ProductCategoryDO::getParentId, parentId);
35 27 }
36 28  
37   - default Long selectCountByParentId(Long parentId) {
38   - return selectCount(CategoryDO::getParentId, parentId);
  29 + default List<ProductCategoryDO> selectListByStatus(Integer status) {
  30 + return selectList(ProductCategoryDO::getStatus, status);
39 31 }
  32 +
40 33 }
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/CategoryServiceImpl.java deleted
1   -package cn.iocoder.yudao.module.product.service.category;
2   -
3   -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
4   -import cn.iocoder.yudao.framework.common.exception.ErrorCode;
5   -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
6   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
7   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
8   -import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
9   -import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
10   -import cn.iocoder.yudao.module.product.dal.mysql.category.CategoryMapper;
11   -import org.springframework.stereotype.Service;
12   -import org.springframework.validation.annotation.Validated;
13   -
14   -import javax.annotation.Resource;
15   -import java.util.Collection;
16   -import java.util.List;
17   -import java.util.stream.Collectors;
18   -
19   -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
20   -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
21   -
22   -/**
23   - * 商品分类 Service 实现类
24   - *
25   - * @author 芋道源码
26   - */
27   -@Service
28   -@Validated
29   -public class CategoryServiceImpl implements CategoryService {
30   -
31   - @Resource
32   - private CategoryMapper categoryMapper;
33   -
34   - @Override
35   - public Long createCategory(CategoryCreateReqVO createReqVO) {
36   - // 校验父分类存在
37   - this.validateCategoryExists(createReqVO.getParentId(), CATEGORY_PARENT_NOT_EXISTS);
38   - // 插入
39   - CategoryDO category = CategoryConvert.INSTANCE.convert(createReqVO);
40   - categoryMapper.insert(category);
41   - // 返回
42   - return category.getId();
43   - }
44   -
45   - @Override
46   - public void updateCategory(CategoryUpdateReqVO updateReqVO) {
47   - // 校验父分类存在
48   - this.validateCategoryExists(updateReqVO.getParentId(), CATEGORY_PARENT_NOT_EXISTS);
49   - // 校验分类是否存在
50   - this.validateCategoryExists(updateReqVO.getId(), CATEGORY_NOT_EXISTS);
51   - // 更新
52   - CategoryDO updateObj = CategoryConvert.INSTANCE.convert(updateReqVO);
53   - categoryMapper.updateById(updateObj);
54   - }
55   -
56   - @Override
57   - public void deleteCategory(Long id) {
58   - // TODO 芋艿 补充只有不存在商品才可以删除
59   - // 校验分类是否存在
60   - CategoryDO categoryDO = this.validateCategoryExists(id, CATEGORY_NOT_EXISTS);
61   - // 校验是否还有子分类
62   - if (categoryMapper.selectCountByParentId(categoryDO.getParentId()) > 0) {
63   - throw ServiceExceptionUtil.exception(CATEGORY_EXISTS_CHILDREN);
64   - }
65   - // 删除
66   - categoryMapper.deleteById(id);
67   - }
68   -
69   - private CategoryDO validateCategoryExists(Long id, ErrorCode errorCode) {
70   - // TODO franky:0 要枚举哈
71   - if (id == 0) {
72   - return new CategoryDO().setId(id);
73   - }
74   - CategoryDO categoryDO = categoryMapper.selectById(id);
75   - if (categoryDO == null) {
76   - throw exception(errorCode);
77   - }
78   - return categoryDO;
79   - }
80   -
81   - @Override
82   - public void validatedCategoryById(Long categoryId) {
83   - this.validateCategoryExists(categoryId, CATEGORY_NOT_EXISTS);
84   - }
85   -
86   - @Override
87   - public CategoryDO getCategory(Long id) {
88   - return categoryMapper.selectById(id);
89   - }
90   -
91   - @Override
92   - public List<CategoryDO> getCategoryList(Collection<Long> ids) {
93   - return categoryMapper.selectBatchIds(ids);
94   - }
95   -
96   - @Override
97   - public PageResult<CategoryDO> getCategoryPage(CategoryPageReqVO pageReqVO) {
98   - return categoryMapper.selectPage(pageReqVO);
99   - }
100   -
101   - @Override
102   - public List<CategoryDO> getCategoryList(CategoryExportReqVO exportReqVO) {
103   - return categoryMapper.selectList(exportReqVO);
104   - }
105   -
106   - @Override
107   - public List<CategoryDO> getCategoryTreeList(CategoryTreeListReqVO treeListReqVO) {
108   - return categoryMapper.selectList(treeListReqVO);
109   - }
110   -
111   - @Override
112   - public List<CategoryDO> getCategoryList() {
113   - return categoryMapper.selectList()
114   - .stream()
115   - .filter(v->v.getStatus().equals(CommonStatusEnum.ENABLE.getStatus()))
116   - .collect(Collectors.toList());
117   - }
118   -
119   -
120   -}
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/CategoryService.java renamed to yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryService.java
1 1 package cn.iocoder.yudao.module.product.service.category;
2 2  
3   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
4   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.*;
5   -import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryListRespVO;
6   -import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
  3 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
  4 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
  5 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
  6 +import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
7 7  
8 8 import javax.validation.Valid;
9 9 import java.util.Collection;
10 10 import java.util.List;
11 11  
12   -// TODO @JeromeSoar:需要 Product 前缀
13 12 /**
14 13 * 商品分类 Service 接口
15 14 *
16 15 * @author 芋道源码
17 16 */
18   -public interface CategoryService {
  17 +public interface ProductCategoryService {
19 18  
20 19 /**
21 20 * 创建商品分类
... ... @@ -23,21 +22,21 @@ public interface CategoryService {
23 22 * @param createReqVO 创建信息
24 23 * @return 编号
25 24 */
26   - Long createCategory(@Valid CategoryCreateReqVO createReqVO);
  25 + Long createProductCategory(@Valid ProductCategoryCreateReqVO createReqVO);
27 26  
28 27 /**
29 28 * 更新商品分类
30 29 *
31 30 * @param updateReqVO 更新信息
32 31 */
33   - void updateCategory(@Valid CategoryUpdateReqVO updateReqVO);
  32 + void updateProductCategory(@Valid ProductCategoryUpdateReqVO updateReqVO);
34 33  
35 34 /**
36 35 * 删除商品分类
37 36 *
38 37 * @param id 编号
39 38 */
40   - void deleteCategory(Long id);
  39 + void deleteProductCategory(Long id);
41 40  
42 41 /**
43 42 * 获得商品分类
... ... @@ -45,7 +44,7 @@ public interface CategoryService {
45 44 * @param id 编号
46 45 * @return 商品分类
47 46 */
48   - CategoryDO getCategory(Long id);
  47 + ProductCategoryDO getProductCategory(Long id);
49 48  
50 49 /**
51 50 * 获得商品分类列表
... ... @@ -53,42 +52,28 @@ public interface CategoryService {
53 52 * @param ids 编号
54 53 * @return 商品分类列表
55 54 */
56   - List<CategoryDO> getCategoryList(Collection<Long> ids);
57   -
58   - /**
59   - * 获得商品分类分页
60   - *
61   - * @param pageReqVO 分页查询
62   - * @return 商品分类分页
63   - */
64   - PageResult<CategoryDO> getCategoryPage(CategoryPageReqVO pageReqVO);
65   -
66   - /**
67   - * 获得商品分类列表, 用于 Excel 导出
68   - *
69   - * @param exportReqVO 查询条件
70   - * @return 商品分类列表
71   - */
72   - List<CategoryDO> getCategoryList(CategoryExportReqVO exportReqVO);
  55 + List<ProductCategoryDO> getEnableProductCategoryList(Collection<Long> ids);
73 56  
74 57 /**
75 58 * 获得商品分类列表
76 59 *
77   - * @param treeListReqVO 查询条件
  60 + * @param listReqVO 查询条件
78 61 * @return 商品分类列表
79 62 */
80   - List<CategoryDO> getCategoryTreeList(CategoryTreeListReqVO treeListReqVO);
  63 + List<ProductCategoryDO> getEnableProductCategoryList(ProductCategoryListReqVO listReqVO);
81 64  
82 65 /**
83   - * 验证选择的分类的合法性
84   - * @param categoryId 分类id
  66 + * 验证选择的商品分类是否合法
  67 + *
  68 + * @param id 分类编号
85 69 */
86   - void validatedCategoryById(Long categoryId);
  70 + void validateProductCategory(Long id);
87 71  
88 72 /**
89   - * app端获得商品分类列表
  73 + * 获得开启状态的商品分类列表
90 74 *
91 75 * @return 商品分类列表
92 76 */
93   - List<CategoryDO> getCategoryList();
  77 + List<ProductCategoryDO> getEnableProductCategoryList();
  78 +
94 79 }
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.product.service.category;
  2 +
  3 +import cn.hutool.core.util.ObjectUtil;
  4 +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
  5 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
  6 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
  7 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
  8 +import cn.iocoder.yudao.module.product.convert.category.CategoryConvert;
  9 +import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
  10 +import cn.iocoder.yudao.module.product.dal.mysql.category.ProductCategoryMapper;
  11 +import org.springframework.stereotype.Service;
  12 +import org.springframework.validation.annotation.Validated;
  13 +
  14 +import javax.annotation.Resource;
  15 +import java.util.Collection;
  16 +import java.util.List;
  17 +import java.util.Objects;
  18 +
  19 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  20 +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
  21 +
  22 +/**
  23 + * 商品分类 Service 实现类
  24 + *
  25 + * @author 芋道源码
  26 + */
  27 +@Service
  28 +@Validated
  29 +public class ProductCategoryServiceImpl implements ProductCategoryService {
  30 +
  31 + @Resource
  32 + private ProductCategoryMapper productCategoryMapper;
  33 +
  34 + @Override
  35 + public Long createProductCategory(ProductCategoryCreateReqVO createReqVO) {
  36 + // 校验父分类存在
  37 + validateParentProductCategory(createReqVO.getParentId());
  38 +
  39 + // 插入
  40 + ProductCategoryDO category = CategoryConvert.INSTANCE.convert(createReqVO);
  41 + productCategoryMapper.insert(category);
  42 + // 返回
  43 + return category.getId();
  44 + }
  45 +
  46 + @Override
  47 + public void updateProductCategory(ProductCategoryUpdateReqVO updateReqVO) {
  48 + // 校验分类是否存在
  49 + validateProductCategoryExists(updateReqVO.getId());
  50 + // 校验父分类存在
  51 + validateParentProductCategory(updateReqVO.getParentId());
  52 +
  53 + // 更新
  54 + ProductCategoryDO updateObj = CategoryConvert.INSTANCE.convert(updateReqVO);
  55 + productCategoryMapper.updateById(updateObj);
  56 + }
  57 +
  58 + @Override
  59 + public void deleteProductCategory(Long id) {
  60 + // 校验分类是否存在
  61 + validateProductCategoryExists(id);
  62 + // 校验是否还有子分类
  63 + if (productCategoryMapper.selectCountByParentId(id) > 0) {
  64 + throw exception(PRODUCT_CATEGORY_EXISTS_CHILDREN);
  65 + }
  66 +
  67 + // 删除
  68 + productCategoryMapper.deleteById(id);
  69 + }
  70 +
  71 + private void validateParentProductCategory(Long id) {
  72 + // 如果是根分类,无需验证
  73 + if (Objects.equals(id, ProductCategoryDO.PARENT_ID_NULL)) {
  74 + return;
  75 + }
  76 + // 父分类不存在
  77 + ProductCategoryDO category = productCategoryMapper.selectById(id);
  78 + if (category == null) {
  79 + throw exception(PRODUCT_CATEGORY_PARENT_NOT_EXISTS);
  80 + }
  81 + // 父分类不能是二级分类
  82 + if (Objects.equals(id, ProductCategoryDO.PARENT_ID_NULL)) {
  83 + throw exception(PRODUCT_CATEGORY_PARENT_NOT_FIRST_LEVEL);
  84 + }
  85 + }
  86 +
  87 + private void validateProductCategoryExists(Long id) {
  88 + ProductCategoryDO category = productCategoryMapper.selectById(id);
  89 + if (category == null) {
  90 + throw exception(PRODUCT_CATEGORY_NOT_EXISTS);
  91 + }
  92 + }
  93 +
  94 + @Override
  95 + public void validateProductCategory(Long id) {
  96 + ProductCategoryDO category = productCategoryMapper.selectById(id);
  97 + if (category == null) {
  98 + throw exception(PRODUCT_CATEGORY_NOT_EXISTS);
  99 + }
  100 + if (ObjectUtil.notEqual(category.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
  101 + throw exception(PRODUCT_CATEGORY_DISABLED);
  102 + }
  103 + }
  104 +
  105 + @Override
  106 + public ProductCategoryDO getProductCategory(Long id) {
  107 + return productCategoryMapper.selectById(id);
  108 + }
  109 +
  110 + @Override
  111 + public List<ProductCategoryDO> getEnableProductCategoryList(Collection<Long> ids) {
  112 + return productCategoryMapper.selectBatchIds(ids);
  113 + }
  114 +
  115 + @Override
  116 + public List<ProductCategoryDO> getEnableProductCategoryList(ProductCategoryListReqVO listReqVO) {
  117 + return productCategoryMapper.selectList(listReqVO);
  118 + }
  119 +
  120 + @Override
  121 + public List<ProductCategoryDO> getEnableProductCategoryList() {
  122 + return productCategoryMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus());
  123 + }
  124 +
  125 +}
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/spu/ProductSpuServiceImpl.java
... ... @@ -15,7 +15,7 @@ import cn.iocoder.yudao.module.product.convert.spu.ProductSpuConvert;
15 15 import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
16 16 import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
17 17 import cn.iocoder.yudao.module.product.dal.mysql.spu.ProductSpuMapper;
18   -import cn.iocoder.yudao.module.product.service.category.CategoryService;
  18 +import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
19 19 import cn.iocoder.yudao.module.product.service.property.ProductPropertyService;
20 20 import cn.iocoder.yudao.module.product.service.sku.ProductSkuService;
21 21 import org.springframework.stereotype.Service;
... ... @@ -42,7 +42,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
42 42 private ProductSpuMapper ProductSpuMapper;
43 43  
44 44 @Resource
45   - private CategoryService categoryService;
  45 + private ProductCategoryService categoryService;
46 46  
47 47 @Resource
48 48 private ProductSkuService productSkuService;
... ... @@ -54,7 +54,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
54 54 @Transactional
55 55 public Long createSpu(ProductSpuCreateReqVO createReqVO) {
56 56 // 校验分类
57   - categoryService.validatedCategoryById(createReqVO.getCategoryId());
  57 + categoryService.validateProductCategory(createReqVO.getCategoryId());
58 58 // 校验SKU
59 59 List<ProductSkuCreateReqVO> skuCreateReqList = createReqVO.getSkus();
60 60 productSkuService.validateSkus(skuCreateReqList);
... ... @@ -78,7 +78,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
78 78 // 校验 spu 是否存在
79 79 this.validateSpuExists(updateReqVO.getId());
80 80 // 校验分类
81   - categoryService.validatedCategoryById(updateReqVO.getCategoryId());
  81 + categoryService.validateProductCategory(updateReqVO.getCategoryId());
82 82 // 校验SKU
83 83 List<ProductSkuCreateReqVO> skuCreateReqList = updateReqVO.getSkus();
84 84 productSkuService.validateSkus(skuCreateReqList);
... ... @@ -142,7 +142,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
142 142 Long parentId = spuVO.getCategoryId();
143 143 categoryArray.addFirst(parentId);
144 144 while (parentId != 0) {
145   - parentId = categoryService.getCategory(parentId).getParentId();
  145 + parentId = categoryService.getProductCategory(parentId).getParentId();
146 146 if (parentId > 0) {
147 147 categoryArray.addFirst(parentId);
148 148 }
... ...
yudao-module-mall/yudao-module-product-biz/src/test/java/cn/iocoder/yudao/module/product/service/category/CategoryServiceImplTest.java deleted
1   -package cn.iocoder.yudao.module.product.service.category;
2   -
3   -import cn.iocoder.yudao.framework.common.pojo.PageResult;
4   -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
5   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryCreateReqVO;
6   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryExportReqVO;
7   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryPageReqVO;
8   -import cn.iocoder.yudao.module.product.controller.admin.category.vo.CategoryUpdateReqVO;
9   -import cn.iocoder.yudao.module.product.dal.dataobject.category.CategoryDO;
10   -import cn.iocoder.yudao.module.product.dal.mysql.category.CategoryMapper;
11   -import org.junit.jupiter.api.Disabled;
12   -import org.junit.jupiter.api.Test;
13   -import org.springframework.context.annotation.Import;
14   -
15   -import javax.annotation.Resource;
16   -import java.util.List;
17   -
18   -import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
19   -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
20   -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
21   -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
22   -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
23   -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS;
24   -import static org.junit.jupiter.api.Assertions.*;
25   -
26   -/**
27   - * {@link CategoryServiceImpl} 的单元测试类
28   - *
29   - * @author 芋道源码
30   - */
31   -@Import(CategoryServiceImpl.class)
32   -public class CategoryServiceImplTest extends BaseDbUnitTest {
33   -
34   - @Resource
35   - private CategoryServiceImpl categoryService;
36   -
37   - @Resource
38   - private CategoryMapper categoryMapper;
39   -
40   - @Test
41   - public void testCreateCategory_success() {
42   - // 准备参数
43   - CategoryCreateReqVO reqVO = randomPojo(CategoryCreateReqVO.class);
44   -
45   - // 调用
46   - Long categoryId = categoryService.createCategory(reqVO);
47   - // 断言
48   - assertNotNull(categoryId);
49   - // 校验记录的属性是否正确
50   - CategoryDO category = categoryMapper.selectById(categoryId);
51   - assertPojoEquals(reqVO, category);
52   - }
53   -
54   - @Test
55   - public void testUpdateCategory_success() {
56   - // mock 数据
57   - CategoryDO dbCategory = randomPojo(CategoryDO.class);
58   - categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
59   - // 准备参数
60   - CategoryUpdateReqVO reqVO = randomPojo(CategoryUpdateReqVO.class, o -> {
61   - o.setId(dbCategory.getId()); // 设置更新的 ID
62   - });
63   -
64   - // 调用
65   - categoryService.updateCategory(reqVO);
66   - // 校验是否更新正确
67   - CategoryDO category = categoryMapper.selectById(reqVO.getId()); // 获取最新的
68   - assertPojoEquals(reqVO, category);
69   - }
70   -
71   - @Test
72   - public void testUpdateCategory_notExists() {
73   - // 准备参数
74   - CategoryUpdateReqVO reqVO = randomPojo(CategoryUpdateReqVO.class);
75   -
76   - // 调用, 并断言异常
77   - assertServiceException(() -> categoryService.updateCategory(reqVO), CATEGORY_NOT_EXISTS);
78   - }
79   -
80   - @Test
81   - public void testDeleteCategory_success() {
82   - // mock 数据
83   - CategoryDO dbCategory = randomPojo(CategoryDO.class);
84   - categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
85   - // 准备参数
86   - Long id = dbCategory.getId();
87   -
88   - // 调用
89   - categoryService.deleteCategory(id);
90   - // 校验数据不存在了
91   - assertNull(categoryMapper.selectById(id));
92   - }
93   -
94   - @Test
95   - public void testDeleteCategory_notExists() {
96   - // 准备参数
97   - Long id = randomLongId();
98   -
99   - // 调用, 并断言异常
100   - assertServiceException(() -> categoryService.deleteCategory(id), CATEGORY_NOT_EXISTS);
101   - }
102   -
103   - @Test
104   - @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
105   - public void testGetCategoryPage() {
106   - // mock 数据
107   - CategoryDO dbCategory = randomPojo(CategoryDO.class, o -> { // 等会查询到
108   - o.setParentId(null);
109   - o.setName(null);
110   - o.setIcon(null);
111   - o.setBannerUrl(null);
112   - o.setSort(null);
113   - o.setDescription(null);
114   - o.setStatus(null);
115   - o.setCreateTime(null);
116   - });
117   - categoryMapper.insert(dbCategory);
118   - // 测试 pid 不匹配
119   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setParentId(null)));
120   - // 测试 name 不匹配
121   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName(null)));
122   - // 测试 icon 不匹配
123   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setIcon(null)));
124   - // 测试 bannerUrl 不匹配
125   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setBannerUrl(null)));
126   - // 测试 sort 不匹配
127   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setSort(null)));
128   - // 测试 description 不匹配
129   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setDescription(null)));
130   - // 测试 status 不匹配
131   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setStatus(null)));
132   - // 测试 createTime 不匹配
133   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setCreateTime(null)));
134   - // 准备参数
135   - CategoryPageReqVO reqVO = new CategoryPageReqVO();
136   - reqVO.setName(null);
137   - reqVO.setStatus(null);
138   - reqVO.setCreateTime(null);
139   -
140   - // 调用
141   - PageResult<CategoryDO> pageResult = categoryService.getCategoryPage(reqVO);
142   - // 断言
143   - assertEquals(1, pageResult.getTotal());
144   - assertEquals(1, pageResult.getList().size());
145   - assertPojoEquals(dbCategory, pageResult.getList().get(0));
146   - }
147   -
148   - @Test
149   - @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
150   - public void testGetCategoryList() {
151   - // mock 数据
152   - CategoryDO dbCategory = randomPojo(CategoryDO.class, o -> { // 等会查询到
153   - o.setParentId(null);
154   - o.setName(null);
155   - o.setIcon(null);
156   - o.setBannerUrl(null);
157   - o.setSort(null);
158   - o.setDescription(null);
159   - o.setStatus(null);
160   - o.setCreateTime(null);
161   - });
162   - categoryMapper.insert(dbCategory);
163   - // 测试 pid 不匹配
164   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setParentId(null)));
165   - // 测试 name 不匹配
166   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName(null)));
167   - // 测试 icon 不匹配
168   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setIcon(null)));
169   - // 测试 bannerUrl 不匹配
170   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setBannerUrl(null)));
171   - // 测试 sort 不匹配
172   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setSort(null)));
173   - // 测试 description 不匹配
174   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setDescription(null)));
175   - // 测试 status 不匹配
176   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setStatus(null)));
177   - // 测试 createTime 不匹配
178   - categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setCreateTime(null)));
179   - // 准备参数
180   - CategoryExportReqVO reqVO = new CategoryExportReqVO();
181   - reqVO.setName(null);
182   - reqVO.setStatus(null);
183   - reqVO.setCreateTime(null);
184   -
185   - // 调用
186   - List<CategoryDO> list = categoryService.getCategoryList(reqVO);
187   - // 断言
188   - assertEquals(1, list.size());
189   - assertPojoEquals(dbCategory, list.get(0));
190   - }
191   -
192   -}
yudao-module-mall/yudao-module-product-biz/src/test/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImplTest.java 0 → 100644
  1 +package cn.iocoder.yudao.module.product.service.category;
  2 +
  3 +import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
  4 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
  5 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
  6 +import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
  7 +import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
  8 +import cn.iocoder.yudao.module.product.dal.mysql.category.ProductCategoryMapper;
  9 +import org.junit.jupiter.api.Test;
  10 +import org.springframework.context.annotation.Import;
  11 +
  12 +import javax.annotation.Resource;
  13 +import java.util.List;
  14 +
  15 +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
  16 +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
  17 +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
  18 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
  19 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
  20 +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_CATEGORY_NOT_EXISTS;
  21 +import static org.junit.jupiter.api.Assertions.*;
  22 +
  23 +/**
  24 + * {@link ProductCategoryServiceImpl} 的单元测试类
  25 + *
  26 + * @author 芋道源码
  27 + */
  28 +@Import(ProductCategoryServiceImpl.class)
  29 +public class ProductCategoryServiceImplTest extends BaseDbUnitTest {
  30 +
  31 + @Resource
  32 + private ProductCategoryServiceImpl productCategoryService;
  33 +
  34 + @Resource
  35 + private ProductCategoryMapper productCategoryMapper;
  36 +
  37 + @Test
  38 + public void testCreateCategory_success() {
  39 + // 准备参数
  40 + ProductCategoryCreateReqVO reqVO = randomPojo(ProductCategoryCreateReqVO.class);
  41 + // mock 父类
  42 + ProductCategoryDO parentProductCategory = randomPojo(ProductCategoryDO.class, o -> o.setId(reqVO.getParentId()));
  43 + productCategoryMapper.insert(parentProductCategory);
  44 +
  45 + // 调用
  46 + Long categoryId = productCategoryService.createProductCategory(reqVO);
  47 + // 断言
  48 + assertNotNull(categoryId);
  49 + // 校验记录的属性是否正确
  50 + ProductCategoryDO category = productCategoryMapper.selectById(categoryId);
  51 + assertPojoEquals(reqVO, category);
  52 + }
  53 +
  54 + @Test
  55 + public void testUpdateCategory_success() {
  56 + // mock 数据
  57 + ProductCategoryDO dbCategory = randomPojo(ProductCategoryDO.class);
  58 + productCategoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
  59 + // 准备参数
  60 + ProductCategoryUpdateReqVO reqVO = randomPojo(ProductCategoryUpdateReqVO.class, o -> {
  61 + o.setId(dbCategory.getId()); // 设置更新的 ID
  62 + });
  63 + // mock 父类
  64 + ProductCategoryDO parentProductCategory = randomPojo(ProductCategoryDO.class, o -> o.setId(reqVO.getParentId()));
  65 + productCategoryMapper.insert(parentProductCategory);
  66 +
  67 + // 调用
  68 + productCategoryService.updateProductCategory(reqVO);
  69 + // 校验是否更新正确
  70 + ProductCategoryDO category = productCategoryMapper.selectById(reqVO.getId()); // 获取最新的
  71 + assertPojoEquals(reqVO, category);
  72 + }
  73 +
  74 + @Test
  75 + public void testUpdateCategory_notExists() {
  76 + // 准备参数
  77 + ProductCategoryUpdateReqVO reqVO = randomPojo(ProductCategoryUpdateReqVO.class);
  78 +
  79 + // 调用, 并断言异常
  80 + assertServiceException(() -> productCategoryService.updateProductCategory(reqVO), PRODUCT_CATEGORY_NOT_EXISTS);
  81 + }
  82 +
  83 + @Test
  84 + public void testDeleteCategory_success() {
  85 + // mock 数据
  86 + ProductCategoryDO dbCategory = randomPojo(ProductCategoryDO.class);
  87 + productCategoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
  88 + // 准备参数
  89 + Long id = dbCategory.getId();
  90 +
  91 + // 调用
  92 + productCategoryService.deleteProductCategory(id);
  93 + // 校验数据不存在了
  94 + assertNull(productCategoryMapper.selectById(id));
  95 + }
  96 +
  97 + @Test
  98 + public void testDeleteCategory_notExists() {
  99 + // 准备参数
  100 + Long id = randomLongId();
  101 +
  102 + // 调用, 并断言异常
  103 + assertServiceException(() -> productCategoryService.deleteProductCategory(id), PRODUCT_CATEGORY_NOT_EXISTS);
  104 + }
  105 +
  106 + @Test
  107 + public void testGetCategoryList() {
  108 + // mock 数据
  109 + ProductCategoryDO dbCategory = randomPojo(ProductCategoryDO.class, o -> { // 等会查询到
  110 + o.setName("奥特曼");
  111 + });
  112 + productCategoryMapper.insert(dbCategory);
  113 + // 测试 name 不匹配
  114 + productCategoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName("奥特块")));
  115 + // 准备参数
  116 + ProductCategoryListReqVO reqVO = new ProductCategoryListReqVO();
  117 + reqVO.setName("特曼");
  118 +
  119 + // 调用
  120 + List<ProductCategoryDO> list = productCategoryService.getEnableProductCategoryList(reqVO);
  121 + // 断言
  122 + assertEquals(1, list.size());
  123 + assertPojoEquals(dbCategory, list.get(0));
  124 + }
  125 +
  126 +}
... ...
yudao-module-mall/yudao-module-product-biz/src/test/resources/application-unit-test.yaml 0 → 100644
  1 +spring:
  2 + main:
  3 + lazy-initialization: true # 开启懒加载,加快速度
  4 + banner-mode: off # 单元测试,禁用 Banner
  5 +
  6 +--- #################### 数据库相关配置 ####################
  7 +
  8 +spring:
  9 + # 数据源配置项
  10 + datasource:
  11 + name: ruoyi-vue-pro
  12 + url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写
  13 + driver-class-name: org.h2.Driver
  14 + username: sa
  15 + password:
  16 + druid:
  17 + async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度
  18 + initial-size: 1 # 单元测试,配置为 1,提升启动速度
  19 + sql:
  20 + init:
  21 + schema-locations: classpath:/sql/create_tables.sql
  22 +
  23 + # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
  24 + redis:
  25 + host: 127.0.0.1 # 地址
  26 + port: 16379 # 端口(单元测试,使用 16379 端口)
  27 + database: 0 # 数据库索引
  28 +
  29 +mybatis-plus:
  30 + lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试
  31 + type-aliases-package: ${yudao.info.base-package}.module.*.dal.dataobject
  32 +
  33 +--- #################### 定时任务相关配置 ####################
  34 +
  35 +--- #################### 配置中心相关配置 ####################
  36 +
  37 +--- #################### 服务保障相关配置 ####################
  38 +
  39 +# Lock4j 配置项(单元测试,禁用 Lock4j)
  40 +
  41 +# Resilience4j 配置项
  42 +
  43 +--- #################### 监控相关配置 ####################
  44 +
  45 +--- #################### 芋道相关配置 ####################
  46 +
  47 +# 芋道配置项,设置当前项目所有自定义的配置
  48 +yudao:
  49 + info:
  50 + base-package: cn.iocoder.yudao
... ...
yudao-module-mall/yudao-module-product-biz/src/test/resources/logback.xml 0 → 100644
  1 +<configuration>
  2 + <!-- 引用 Spring Boot 的 logback 基础配置 -->
  3 + <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  4 +</configuration>
... ...
yudao-module-mall/yudao-module-product-biz/src/test/resources/sql/create_tables.sql
... ... @@ -2,8 +2,7 @@ CREATE TABLE IF NOT EXISTS &quot;product_category&quot; (
2 2 "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY,
3 3 "parent_id" bigint(20) NOT NULL,
4 4 "name" varchar(255) NOT NULL,
5   - "icon" varchar(100),
6   - "banner_url" varchar(255) NOT NULL,
  5 + "pic_url" varchar(255) NOT NULL,
7 6 "sort" int(11) NOT NULL,
8 7 "description" varchar(1024) NOT NULL,
9 8 "status" tinyint(4) NOT NULL,
... ... @@ -12,7 +11,6 @@ CREATE TABLE IF NOT EXISTS &quot;product_category&quot; (
12 11 "updater" varchar(64) DEFAULT '',
13 12 "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
14 13 "deleted" bit NOT NULL DEFAULT FALSE,
15   - "tenant_id" bigint(20) NOT NULL,
16 14 PRIMARY KEY ("id")
17 15 ) COMMENT '商品分类';
18 16  
... ... @@ -29,6 +27,5 @@ CREATE TABLE IF NOT EXISTS &quot;product_brand&quot; (
29 27 "updater" varchar(64) DEFAULT '',
30 28 "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
31 29 "deleted" bit NOT NULL DEFAULT FALSE,
32   - "tenant_id" bigint(20) NOT NULL,
33 30 PRIMARY KEY ("id")
34 31 ) COMMENT '品牌';
... ...