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

This commit is contained in:
YunaiV 2022-09-05 20:03:13 +08:00
parent 1bd1c79ff3
commit 9632c33d27
13 changed files with 58 additions and 28 deletions

View File

@ -39,7 +39,7 @@ CREATE TABLE `market_activity` (
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号', `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE 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 -- Records of market_activity
@ -66,7 +66,7 @@ CREATE TABLE `market_banner` (
`sort` tinyint NULL DEFAULT NULL COMMENT '排序', `sort` tinyint NULL DEFAULT NULL COMMENT '排序',
`memo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '描述', `memo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '描述',
PRIMARY KEY (`id`) USING BTREE 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 -- Records of market_banner

View File

@ -19,6 +19,8 @@ public interface ErrorCodeConstants {
// ========== 商品品牌相关编号 1008002000 ========== // ========== 商品品牌相关编号 1008002000 ==========
ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在"); ErrorCode BRAND_NOT_EXISTS = new ErrorCode(1008002000, "品牌不存在");
ErrorCode BRAND_DISABLED = new ErrorCode(1008002001, "品牌不存在");
ErrorCode BRAND_NAME_EXISTS = new ErrorCode(1008002002, "品牌名称已存在");
// ========== 商品规格名称 1008003000 ========== // ========== 商品规格名称 1008003000 ==========
ErrorCode PROPERTY_NOT_EXISTS = new ErrorCode(1008003000, "规格名称不存在"); ErrorCode PROPERTY_NOT_EXISTS = new ErrorCode(1008003000, "规格名称不存在");

View File

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

View File

@ -20,7 +20,8 @@ public class ProductBrandBaseVO {
@NotNull(message = "品牌图片不能为空") @NotNull(message = "品牌图片不能为空")
private String picUrl; private String picUrl;
@ApiModelProperty(value = "品牌排序", example = "1") @ApiModelProperty(value = "品牌排序", required = true, example = "1")
@NotNull(message = "品牌排序不能为空")
private Integer sort; private Integer sort;
@ApiModelProperty(value = "品牌描述", example = "描述") @ApiModelProperty(value = "品牌描述", example = "描述")

View File

@ -21,7 +21,7 @@ public class ProductBrandPageReqVO extends PageParam {
@ApiModelProperty(value = "品牌名称", example = "芋道") @ApiModelProperty(value = "品牌名称", example = "芋道")
private String name; private String name;
@ApiModelProperty(value = "状态", example = "0") @ApiModelProperty(value = "状态", example = "0", notes = "参考 CommonStatusEnum 枚举")
private Integer status; private Integer status;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)

View File

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

View File

@ -27,4 +27,8 @@ public interface ProductBrandMapper extends BaseMapperX<ProductBrandDO> {
.likeIfPresent(ProductBrandDO::getName, reqVO.getName())); .likeIfPresent(ProductBrandDO::getName, reqVO.getName()));
} }
default ProductBrandDO selectByName(String name) {
return selectOne(ProductBrandDO::getName, name);
}
} }

View File

@ -21,7 +21,7 @@ public interface ProductBrandService {
* @param createReqVO 创建信息 * @param createReqVO 创建信息
* @return 编号 * @return 编号
*/ */
Long createProductBrand(@Valid ProductBrandCreateReqVO createReqVO); Long createBrand(@Valid ProductBrandCreateReqVO createReqVO);
/** /**
* 更新品牌 * 更新品牌

View File

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

View File

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

View File

@ -43,7 +43,7 @@ public class ProductBrandServiceImplTest extends BaseDbUnitTest {
ProductBrandCreateReqVO reqVO = randomPojo(ProductBrandCreateReqVO.class); ProductBrandCreateReqVO reqVO = randomPojo(ProductBrandCreateReqVO.class);
// 调用 // 调用
Long brandId = brandService.createProductBrand(reqVO); Long brandId = brandService.createBrand(reqVO);
// 断言 // 断言
assertNotNull(brandId); assertNotNull(brandId);
// 校验记录的属性是否正确 // 校验记录的属性是否正确

View File

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

View File

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