Commit 9632c33d279f33348276c27d592ed8241bd827c6

Authored by YunaiV
1 parent 1bd1c79f

mall:优化商品的分类、品牌的代码

Showing 13 changed files with 58 additions and 28 deletions
sql/optional/mall/mall.sql
... ... @@ -39,7 +39,7 @@ CREATE TABLE `market_activity` (
39 39 `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
40 40 `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
41 41 PRIMARY KEY (`id`) USING BTREE
42   -) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '促销活动';
  42 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '促销活动';
43 43  
44 44 -- ----------------------------
45 45 -- Records of market_activity
... ... @@ -66,7 +66,7 @@ CREATE TABLE `market_banner` (
66 66 `sort` tinyint NULL DEFAULT NULL COMMENT '排序',
67 67 `memo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '描述',
68 68 PRIMARY KEY (`id`) USING BTREE
69   -) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'Banner管理';
  69 +) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'Banner管理';
70 70  
71 71 -- ----------------------------
72 72 -- Records of market_banner
... ...
yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java
... ... @@ -19,6 +19,8 @@ public interface ErrorCodeConstants {
19 19  
20 20 // ========== 商品品牌相关编号 1008002000 ==========
21 21 ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在");
  22 + ErrorCode BRAND_DISABLED = new ErrorCode(1008002001, "品牌不存在");
  23 + ErrorCode BRAND_NAME_EXISTS = new ErrorCode(1008002002, "品牌名称已存在");
22 24  
23 25 // ========== 商品规格名称 1008003000 ==========
24 26 ErrorCode PROPERTY_NOT_EXISTS = new ErrorCode(1008003000, "规格名称不存在");
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/brand/ProductBrandController.java
... ... @@ -33,7 +33,7 @@ public class ProductBrandController {
33 33 @ApiOperation("创建品牌")
34 34 @PreAuthorize("@ss.hasPermission('product:brand:create')")
35 35 public CommonResult<Long> createBrand(@Valid @RequestBody ProductBrandCreateReqVO createReqVO) {
36   - return success(brandService.createProductBrand(createReqVO));
  36 + return success(brandService.createBrand(createReqVO));
37 37 }
38 38  
39 39 @PutMapping("/update")
... ... @@ -73,7 +73,7 @@ public class ProductBrandController {
73 73 @GetMapping("/list")
74 74 @ApiOperation("获得品牌列表")
75 75 @PreAuthorize("@ss.hasPermission('product:brand:query')")
76   - public CommonResult<List<ProductBrandRespVO>> getProductCategoryList(@Valid ProductBrandListReqVO listVO) {
  76 + public CommonResult<List<ProductBrandRespVO>> getBrandList(@Valid ProductBrandListReqVO listVO) {
77 77 List<ProductBrandDO> list = brandService.getBrandList(listVO);
78 78 list.sort(Comparator.comparing(ProductBrandDO::getSort));
79 79 return success(ProductBrandConvert.INSTANCE.convertList(list));
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/brand/vo/ProductBrandBaseVO.java
... ... @@ -20,7 +20,8 @@ public class ProductBrandBaseVO {
20 20 @NotNull(message = "品牌图片不能为空")
21 21 private String picUrl;
22 22  
23   - @ApiModelProperty(value = "品牌排序", example = "1")
  23 + @ApiModelProperty(value = "品牌排序", required = true, example = "1")
  24 + @NotNull(message = "品牌排序不能为空")
24 25 private Integer sort;
25 26  
26 27 @ApiModelProperty(value = "品牌描述", example = "描述")
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/brand/vo/ProductBrandPageReqVO.java
... ... @@ -21,7 +21,7 @@ public class ProductBrandPageReqVO extends PageParam {
21 21 @ApiModelProperty(value = "品牌名称", example = "芋道")
22 22 private String name;
23 23  
24   - @ApiModelProperty(value = "状态", example = "0")
  24 + @ApiModelProperty(value = "状态", example = "0", notes = "参考 CommonStatusEnum 枚举")
25 25 private Integer status;
26 26  
27 27 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/ProductCategoryController.java
... ... @@ -34,14 +34,14 @@ public class ProductCategoryController {
34 34 @PostMapping("/create")
35 35 @ApiOperation("创建商品分类")
36 36 @PreAuthorize("@ss.hasPermission('product:category:create')")
37   - public CommonResult<Long> createProductCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
  37 + public CommonResult<Long> createCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
38 38 return success(categoryService.createCategory(createReqVO));
39 39 }
40 40  
41 41 @PutMapping("/update")
42 42 @ApiOperation("更新商品分类")
43 43 @PreAuthorize("@ss.hasPermission('product:category:update')")
44   - public CommonResult<Boolean> updateProductCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
  44 + public CommonResult<Boolean> updateCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
45 45 categoryService.updateCategory(updateReqVO);
46 46 return success(true);
47 47 }
... ... @@ -50,7 +50,7 @@ public class ProductCategoryController {
50 50 @ApiOperation("删除商品分类")
51 51 @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
52 52 @PreAuthorize("@ss.hasPermission('product:category:delete')")
53   - public CommonResult<Boolean> deleteProductCategory(@RequestParam("id") Long id) {
  53 + public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
54 54 categoryService.deleteCategory(id);
55 55 return success(true);
56 56 }
... ... @@ -59,7 +59,7 @@ public class ProductCategoryController {
59 59 @ApiOperation("获得商品分类")
60 60 @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
61 61 @PreAuthorize("@ss.hasPermission('product:category:query')")
62   - public CommonResult<ProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
  62 + public CommonResult<ProductCategoryRespVO> getCategory(@RequestParam("id") Long id) {
63 63 ProductCategoryDO category = categoryService.getCategory(id);
64 64 return success(ProductCategoryConvert.INSTANCE.convert(category));
65 65 }
... ... @@ -67,7 +67,7 @@ public class ProductCategoryController {
67 67 @GetMapping("/list")
68 68 @ApiOperation("获得商品分类列表")
69 69 @PreAuthorize("@ss.hasPermission('product:category:query')")
70   - public CommonResult<List<ProductCategoryRespVO>> getProductCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
  70 + public CommonResult<List<ProductCategoryRespVO>> getCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
71 71 List<ProductCategoryDO> list = categoryService.getEnableCategoryList(treeListReqVO);
72 72 list.sort(Comparator.comparing(ProductCategoryDO::getSort));
73 73 return success(ProductCategoryConvert.INSTANCE.convertList(list));
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/brand/ProductBrandMapper.java
... ... @@ -27,4 +27,8 @@ public interface ProductBrandMapper extends BaseMapperX&lt;ProductBrandDO&gt; {
27 27 .likeIfPresent(ProductBrandDO::getName, reqVO.getName()));
28 28 }
29 29  
  30 + default ProductBrandDO selectByName(String name) {
  31 + return selectOne(ProductBrandDO::getName, name);
  32 + }
  33 +
30 34 }
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/brand/ProductBrandService.java
... ... @@ -21,7 +21,7 @@ public interface ProductBrandService {
21 21 * @param createReqVO 创建信息
22 22 * @return 编号
23 23 */
24   - Long createProductBrand(@Valid ProductBrandCreateReqVO createReqVO);
  24 + Long createBrand(@Valid ProductBrandCreateReqVO createReqVO);
25 25  
26 26 /**
27 27 * 更新品牌
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/brand/ProductBrandServiceImpl.java
1 1 package cn.iocoder.yudao.module.product.service.brand;
2 2  
  3 +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
3 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 5 import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandCreateReqVO;
5 6 import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandListReqVO;
... ... @@ -8,6 +9,7 @@ import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandUpd
8 9 import cn.iocoder.yudao.module.product.convert.brand.ProductBrandConvert;
9 10 import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
10 11 import cn.iocoder.yudao.module.product.dal.mysql.brand.ProductBrandMapper;
  12 +import com.google.common.annotations.VisibleForTesting;
11 13 import org.springframework.stereotype.Service;
12 14 import org.springframework.validation.annotation.Validated;
13 15  
... ... @@ -16,7 +18,7 @@ import java.util.Collection;
16 18 import java.util.List;
17 19  
18 20 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
19   -import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.BRAND_NOT_EXISTS;
  21 +import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
20 22  
21 23 /**
22 24 * 品牌 Service 实现类
... ... @@ -31,7 +33,10 @@ public class ProductBrandServiceImpl implements ProductBrandService {
31 33 private ProductBrandMapper brandMapper;
32 34  
33 35 @Override
34   - public Long createProductBrand(ProductBrandCreateReqVO createReqVO) {
  36 + public Long createBrand(ProductBrandCreateReqVO createReqVO) {
  37 + // 校验
  38 + validateBrandNameUnique(null, createReqVO.getName());
  39 +
35 40 // 插入
36 41 ProductBrandDO brand = ProductBrandConvert.INSTANCE.convert(createReqVO);
37 42 brandMapper.insert(brand);
... ... @@ -43,6 +48,7 @@ public class ProductBrandServiceImpl implements ProductBrandService {
43 48 public void updateBrand(ProductBrandUpdateReqVO updateReqVO) {
44 49 // 校验存在
45 50 validateBrandExists(updateReqVO.getId());
  51 + validateBrandNameUnique(updateReqVO.getId(), updateReqVO.getName());
46 52 // 更新
47 53 ProductBrandDO updateObj = ProductBrandConvert.INSTANCE.convert(updateReqVO);
48 54 brandMapper.updateById(updateObj);
... ... @@ -51,7 +57,7 @@ public class ProductBrandServiceImpl implements ProductBrandService {
51 57 @Override
52 58 public void deleteBrand(Long id) {
53 59 // 校验存在
54   - this.validateBrandExists(id);
  60 + validateBrandExists(id);
55 61 // 删除
56 62 brandMapper.deleteById(id);
57 63 }
... ... @@ -62,6 +68,21 @@ public class ProductBrandServiceImpl implements ProductBrandService {
62 68 }
63 69 }
64 70  
  71 + @VisibleForTesting
  72 + public void validateBrandNameUnique(Long id, String name) {
  73 + ProductBrandDO brand = brandMapper.selectByName(name);
  74 + if (brand == null) {
  75 + return;
  76 + }
  77 + // 如果 id 为空,说明不用比较是否为相同 id 的字典类型
  78 + if (id == null) {
  79 + throw exception(BRAND_NAME_EXISTS);
  80 + }
  81 + if (!brand.getId().equals(id)) {
  82 + throw exception(BRAND_NAME_EXISTS);
  83 + }
  84 + }
  85 +
65 86 @Override
66 87 public ProductBrandDO getBrand(Long id) {
67 88 return brandMapper.selectById(id);
... ... @@ -79,9 +100,13 @@ public class ProductBrandServiceImpl implements ProductBrandService {
79 100  
80 101 @Override
81 102 public void validateProductBrand(Long id) {
82   - if(getBrand(id) == null){
  103 + ProductBrandDO brand = brandMapper.selectById(id);
  104 + if (brand == null) {
83 105 throw exception(BRAND_NOT_EXISTS);
84 106 }
  107 + if (brand.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) {
  108 + throw exception(BRAND_DISABLED);
  109 + }
85 110 }
86 111  
87 112 @Override
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImpl.java
... ... @@ -63,7 +63,7 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
63 63 if (productCategoryMapper.selectCountByParentId(id) > 0) {
64 64 throw exception(CATEGORY_EXISTS_CHILDREN);
65 65 }
66   -
  66 + // TODO 芋艿 补充只有不存在商品才可以删除
67 67 // 删除
68 68 productCategoryMapper.deleteById(id);
69 69 }
... ... @@ -94,8 +94,8 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
94 94 @Override
95 95 public void validateCategoryLevel(Long id) {
96 96 Integer level = getProductCategoryLevel(id, 1);
97   - if(level < 3){
98   - throw exception(CATEGORY_LEVEL_ERROR);
  97 + if (level < 3){
  98 + throw exception(CATEGORY_LEVEL_ERROR);
99 99 }
100 100 }
101 101  
... ...
yudao-module-mall/yudao-module-product-biz/src/test/java/cn/iocoder/yudao/module/product/service/brand/ProductBrandServiceImplTest.java
... ... @@ -43,7 +43,7 @@ public class ProductBrandServiceImplTest extends BaseDbUnitTest {
43 43 ProductBrandCreateReqVO reqVO = randomPojo(ProductBrandCreateReqVO.class);
44 44  
45 45 // 调用
46   - Long brandId = brandService.createProductBrand(reqVO);
  46 + Long brandId = brandService.createBrand(reqVO);
47 47 // 断言
48 48 assertNotNull(brandId);
49 49 // 校验记录的属性是否正确
... ...
yudao-server/src/main/resources/application-local.yaml
... ... @@ -45,7 +45,7 @@ spring:
45 45 datasource:
46 46 master:
47 47 name: ruoyi-vue-pro
48   - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
  48 + url: jdbc:mysql://139.9.196.247:3306/${spring.datasource.dynamic.datasource.master.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
49 49 # url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT # MySQL Connector/J 5.X 连接的示例
50 50 # url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
51 51 # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
... ... @@ -56,7 +56,7 @@ spring:
56 56 # password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
57 57 slave: # 模拟从库,可根据自己需要修改
58 58 name: ruoyi-vue-pro
59   - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
  59 + url: jdbc:mysql://139.9.196.247:3306/${spring.datasource.dynamic.datasource.slave.name}?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
60 60 # url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT # MySQL Connector/J 5.X 连接的示例
61 61 # url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
62 62 # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
... ... @@ -68,10 +68,10 @@ spring:
68 68  
69 69 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
70 70 redis:
71   - host: 127.0.0.1 # 地址
  71 + host: 139.9.196.247 # 地址
72 72 port: 6379 # 端口
73 73 database: 0 # 数据库索引
74   -# password: 123456 # 密码,建议生产环境开启
  74 + password: 123456 # 密码,建议生产环境开启
75 75  
76 76 jasypt:
77 77 encryptor:
... ...
yudao-ui-admin/src/views/mall/product/category/index.vue
... ... @@ -61,10 +61,8 @@
61 61 <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
62 62 <el-form ref="form" :model="form" :rules="rules" label-width="80px">
63 63 <el-form-item label="上级分类" prop="parentId">
64   - <Treeselect v-model="form.parentId" :options="parentCategoryOptions" :normalizer="normalizer"
65   - :show-count="true"
66   - :defaultExpandLevel="1"
67   - placeholder="上级分类"/>
  64 + <Treeselect v-model="form.parentId" :options="parentCategoryOptions" :normalizer="normalizer" :show-count="true"
  65 + :defaultExpandLevel="1" placeholder="上级分类"/>
68 66 </el-form-item>
69 67 <el-form-item label="分类名称" prop="name">
70 68 <el-input v-model="form.name" placeholder="请输入分类名称"/>
... ...