Browse Source

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

YunaiV 2 years ago
parent
commit
9632c33d27
13 changed files with 58 additions and 28 deletions
  1. 2 2
      sql/optional/mall/mall.sql
  2. 2 0
      yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/enums/ErrorCodeConstants.java
  3. 2 2
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/brand/ProductBrandController.java
  4. 2 1
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/brand/vo/ProductBrandBaseVO.java
  5. 1 1
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/brand/vo/ProductBrandPageReqVO.java
  6. 5 5
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/category/ProductCategoryController.java
  7. 4 0
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/brand/ProductBrandMapper.java
  8. 1 1
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/brand/ProductBrandService.java
  9. 29 4
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/brand/ProductBrandServiceImpl.java
  10. 3 3
      yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/category/ProductCategoryServiceImpl.java
  11. 1 1
      yudao-module-mall/yudao-module-product-biz/src/test/java/cn/iocoder/yudao/module/product/service/brand/ProductBrandServiceImplTest.java
  12. 4 4
      yudao-server/src/main/resources/application-local.yaml
  13. 2 4
      yudao-ui-admin/src/views/mall/product/category/index.vue

+ 2 - 2
sql/optional/mall/mall.sql

@@ -39,7 +39,7 @@ CREATE TABLE `market_activity`  (
                                     `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                     `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
                                     PRIMARY KEY (`id`) USING BTREE
-) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '促销活动';
+) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '促销活动';
 
 -- ----------------------------
 -- Records of market_activity
@@ -66,7 +66,7 @@ CREATE TABLE `market_banner`  (
                                   `sort` tinyint NULL DEFAULT NULL COMMENT '排序',
                                   `memo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '描述',
                                   PRIMARY KEY (`id`) USING BTREE
-) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'Banner管理';
+) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'Banner管理';
 
 -- ----------------------------
 -- Records of market_banner

+ 2 - 0
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 {
 
     // ========== 商品品牌相关编号 1008002000 ==========
     ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在");
+    ErrorCode BRAND_DISABLED = new ErrorCode(1008002001, "品牌不存在");
+    ErrorCode BRAND_NAME_EXISTS = new ErrorCode(1008002002, "品牌名称已存在");
 
     // ========== 商品规格名称 1008003000 ==========
     ErrorCode PROPERTY_NOT_EXISTS = new ErrorCode(1008003000, "规格名称不存在");

+ 2 - 2
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 {
     @ApiOperation("创建品牌")
     @PreAuthorize("@ss.hasPermission('product:brand:create')")
     public CommonResult<Long> createBrand(@Valid @RequestBody ProductBrandCreateReqVO createReqVO) {
-        return success(brandService.createProductBrand(createReqVO));
+        return success(brandService.createBrand(createReqVO));
     }
 
     @PutMapping("/update")
@@ -73,7 +73,7 @@ public class ProductBrandController {
     @GetMapping("/list")
     @ApiOperation("获得品牌列表")
     @PreAuthorize("@ss.hasPermission('product:brand:query')")
-    public CommonResult<List<ProductBrandRespVO>> getProductCategoryList(@Valid ProductBrandListReqVO listVO) {
+    public CommonResult<List<ProductBrandRespVO>> getBrandList(@Valid ProductBrandListReqVO listVO) {
         List<ProductBrandDO> list = brandService.getBrandList(listVO);
         list.sort(Comparator.comparing(ProductBrandDO::getSort));
         return success(ProductBrandConvert.INSTANCE.convertList(list));

+ 2 - 1
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 {
     @NotNull(message = "品牌图片不能为空")
     private String picUrl;
 
-    @ApiModelProperty(value = "品牌排序", example = "1")
+    @ApiModelProperty(value = "品牌排序", required = true, example = "1")
+    @NotNull(message = "品牌排序不能为空")
     private Integer sort;
 
     @ApiModelProperty(value = "品牌描述", example = "描述")

+ 1 - 1
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 {
     @ApiModelProperty(value = "品牌名称", example = "芋道")
     private String name;
 
-    @ApiModelProperty(value = "状态", example = "0")
+    @ApiModelProperty(value = "状态", example = "0", notes = "参考 CommonStatusEnum 枚举")
     private Integer status;
 
     @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)

+ 5 - 5
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 {
     @PostMapping("/create")
     @ApiOperation("创建商品分类")
     @PreAuthorize("@ss.hasPermission('product:category:create')")
-    public CommonResult<Long> createProductCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
+    public CommonResult<Long> createCategory(@Valid @RequestBody ProductCategoryCreateReqVO createReqVO) {
         return success(categoryService.createCategory(createReqVO));
     }
 
     @PutMapping("/update")
     @ApiOperation("更新商品分类")
     @PreAuthorize("@ss.hasPermission('product:category:update')")
-    public CommonResult<Boolean> updateProductCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
+    public CommonResult<Boolean> updateCategory(@Valid @RequestBody ProductCategoryUpdateReqVO updateReqVO) {
         categoryService.updateCategory(updateReqVO);
         return success(true);
     }
@@ -50,7 +50,7 @@ public class ProductCategoryController {
     @ApiOperation("删除商品分类")
     @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
     @PreAuthorize("@ss.hasPermission('product:category:delete')")
-    public CommonResult<Boolean> deleteProductCategory(@RequestParam("id") Long id) {
+    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
         categoryService.deleteCategory(id);
         return success(true);
     }
@@ -59,7 +59,7 @@ public class ProductCategoryController {
     @ApiOperation("获得商品分类")
     @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
     @PreAuthorize("@ss.hasPermission('product:category:query')")
-    public CommonResult<ProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
+    public CommonResult<ProductCategoryRespVO> getCategory(@RequestParam("id") Long id) {
         ProductCategoryDO category = categoryService.getCategory(id);
         return success(ProductCategoryConvert.INSTANCE.convert(category));
     }
@@ -67,7 +67,7 @@ public class ProductCategoryController {
     @GetMapping("/list")
     @ApiOperation("获得商品分类列表")
     @PreAuthorize("@ss.hasPermission('product:category:query')")
-    public CommonResult<List<ProductCategoryRespVO>> getProductCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
+    public CommonResult<List<ProductCategoryRespVO>> getCategoryList(@Valid ProductCategoryListReqVO treeListReqVO) {
         List<ProductCategoryDO> list = categoryService.getEnableCategoryList(treeListReqVO);
         list.sort(Comparator.comparing(ProductCategoryDO::getSort));
         return success(ProductCategoryConvert.INSTANCE.convertList(list));

+ 4 - 0
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<ProductBrandDO> {
                 .likeIfPresent(ProductBrandDO::getName, reqVO.getName()));
     }
 
+    default ProductBrandDO selectByName(String name) {
+        return selectOne(ProductBrandDO::getName, name);
+    }
+
 }

+ 1 - 1
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 {
      * @param createReqVO 创建信息
      * @return 编号
      */
-    Long createProductBrand(@Valid ProductBrandCreateReqVO createReqVO);
+    Long createBrand(@Valid ProductBrandCreateReqVO createReqVO);
 
     /**
      * 更新品牌

+ 29 - 4
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/brand/ProductBrandServiceImpl.java

@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.module.product.service.brand;
 
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandCreateReqVO;
 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
 import cn.iocoder.yudao.module.product.convert.brand.ProductBrandConvert;
 import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
 import cn.iocoder.yudao.module.product.dal.mysql.brand.ProductBrandMapper;
+import com.google.common.annotations.VisibleForTesting;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
@@ -16,7 +18,7 @@ import java.util.Collection;
 import java.util.List;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
-import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.BRAND_NOT_EXISTS;
+import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
 
 /**
  * 品牌 Service 实现类
@@ -31,7 +33,10 @@ public class ProductBrandServiceImpl implements ProductBrandService {
     private ProductBrandMapper brandMapper;
 
     @Override
-    public Long createProductBrand(ProductBrandCreateReqVO createReqVO) {
+    public Long createBrand(ProductBrandCreateReqVO createReqVO) {
+        // 校验
+        validateBrandNameUnique(null, createReqVO.getName());
+
         // 插入
         ProductBrandDO brand = ProductBrandConvert.INSTANCE.convert(createReqVO);
         brandMapper.insert(brand);
@@ -43,6 +48,7 @@ public class ProductBrandServiceImpl implements ProductBrandService {
     public void updateBrand(ProductBrandUpdateReqVO updateReqVO) {
         // 校验存在
         validateBrandExists(updateReqVO.getId());
+        validateBrandNameUnique(updateReqVO.getId(), updateReqVO.getName());
         // 更新
         ProductBrandDO updateObj = ProductBrandConvert.INSTANCE.convert(updateReqVO);
         brandMapper.updateById(updateObj);
@@ -51,7 +57,7 @@ public class ProductBrandServiceImpl implements ProductBrandService {
     @Override
     public void deleteBrand(Long id) {
         // 校验存在
-        this.validateBrandExists(id);
+        validateBrandExists(id);
         // 删除
         brandMapper.deleteById(id);
     }
@@ -62,6 +68,21 @@ public class ProductBrandServiceImpl implements ProductBrandService {
         }
     }
 
+    @VisibleForTesting
+    public void validateBrandNameUnique(Long id, String name) {
+        ProductBrandDO brand = brandMapper.selectByName(name);
+        if (brand == null) {
+            return;
+        }
+        // 如果 id 为空,说明不用比较是否为相同 id 的字典类型
+        if (id == null) {
+            throw exception(BRAND_NAME_EXISTS);
+        }
+        if (!brand.getId().equals(id)) {
+            throw exception(BRAND_NAME_EXISTS);
+        }
+    }
+
     @Override
     public ProductBrandDO getBrand(Long id) {
         return brandMapper.selectById(id);
@@ -79,9 +100,13 @@ public class ProductBrandServiceImpl implements ProductBrandService {
 
     @Override
     public void validateProductBrand(Long id) {
-        if(getBrand(id) == null){
+        ProductBrandDO brand = brandMapper.selectById(id);
+        if (brand == null) {
             throw exception(BRAND_NOT_EXISTS);
         }
+        if (brand.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) {
+            throw exception(BRAND_DISABLED);
+        }
     }
 
     @Override

+ 3 - 3
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 {
         if (productCategoryMapper.selectCountByParentId(id) > 0) {
             throw exception(CATEGORY_EXISTS_CHILDREN);
         }
-
+        // TODO 芋艿 补充只有不存在商品才可以删除
         // 删除
         productCategoryMapper.deleteById(id);
     }
@@ -94,8 +94,8 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
     @Override
     public void validateCategoryLevel(Long id) {
         Integer level = getProductCategoryLevel(id, 1);
-        if(level < 3){
-          throw exception(CATEGORY_LEVEL_ERROR);
+        if (level < 3){
+            throw exception(CATEGORY_LEVEL_ERROR);
         }
     }
 

+ 1 - 1
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 {
         ProductBrandCreateReqVO reqVO = randomPojo(ProductBrandCreateReqVO.class);
 
         // 调用
-        Long brandId = brandService.createProductBrand(reqVO);
+        Long brandId = brandService.createBrand(reqVO);
         // 断言
         assertNotNull(brandId);
         // 校验记录的属性是否正确

+ 4 - 4
yudao-server/src/main/resources/application-local.yaml

@@ -45,7 +45,7 @@ spring:
       datasource:
         master:
           name: ruoyi-vue-pro
-          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 连接的示例
+          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 连接的示例
           #          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 连接的示例
           #          url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
           #          url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
@@ -56,7 +56,7 @@ spring:
         #          password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
         slave: # 模拟从库,可根据自己需要修改
           name: ruoyi-vue-pro
-          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 连接的示例
+          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 连接的示例
           #          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 连接的示例
           #          url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
           #          url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
@@ -68,10 +68,10 @@ spring:
 
   # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
   redis:
-    host: 127.0.0.1 # 地址
+    host: 139.9.196.247 # 地址
     port: 6379 # 端口
     database: 0 # 数据库索引
-#    password: 123456 # 密码,建议生产环境开启
+    password: 123456 # 密码,建议生产环境开启
 
 jasypt:
   encryptor:

+ 2 - 4
yudao-ui-admin/src/views/mall/product/category/index.vue

@@ -61,10 +61,8 @@
     <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
       <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="上级分类" prop="parentId">
-          <Treeselect v-model="form.parentId" :options="parentCategoryOptions" :normalizer="normalizer"
-                      :show-count="true"
-                      :defaultExpandLevel="1"
-                      placeholder="上级分类"/>
+          <Treeselect v-model="form.parentId" :options="parentCategoryOptions" :normalizer="normalizer" :show-count="true"
+                      :defaultExpandLevel="1" placeholder="上级分类"/>
         </el-form-item>
         <el-form-item label="分类名称" prop="name">
           <el-input v-model="form.name" placeholder="请输入分类名称"/>