Skip to content

Commit

Permalink
修复分类名称是否重复校验
Browse files Browse the repository at this point in the history
  • Loading branch information
hcy6100 committed Aug 2, 2021
1 parent 48655e9 commit 614492b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ public interface CategoryMapper {
int updateBatchOfStatus(@Param("categoryIds") List<Long> categoryIds, @Param("status") Integer status);

/**
* 根据分类名获取分类的数量
* 查询分类名是否存在
*
* @param categoryId
* @param name
* @param category
* @return
*/
int getCountByName(@Param("categoryId") Long categoryId, @Param("name") String name);
int existCategoryName(@Param("category") Category category);

/**
* 根据分类id列表,获取分类列表
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CategoryVO getById(Long categoryId) {

@Override
public void save(Category category) {
checkName(null, category.getName());
existCategoryName(category);
category.setShopId(AuthUserContext.get().getTenantId());
String path = "";
if (!Objects.equals(CategoryLevel.First.value(), category.getLevel())) {
Expand All @@ -76,7 +76,7 @@ public void update(Category category) {
if (Objects.equals(dbCategory.getCategoryId(), category.getParentId())) {
throw new Mall4cloudException("分类不能成为本身的上级分类");
}
checkName(category.getCategoryId(), category.getName());
existCategoryName(category);
int updateCount = categoryMapper.update(category);
}

Expand Down Expand Up @@ -251,11 +251,11 @@ private void setChildCategory(List<CategoryVO> categories, List<CategoryVO> chil
/**
* 校验分类名是否已存在
*
* @param categoryId
* @param name
* @param category
*/
private void checkName(Long categoryId, String name) {
int countByName = categoryMapper.getCountByName(categoryId, name);
private void existCategoryName(Category category) {
category.setShopId(AuthUserContext.get().getTenantId());
int countByName = categoryMapper.existCategoryName(category);
if (countByName > 0) {
throw new Mall4cloudException("分类名已存在,请重新输入");
}
Expand Down
11 changes: 7 additions & 4 deletions mall4cloud-product/src/main/resources/mapper/CategoryMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@
</foreach>
</update>

<select id="getCountByName" resultType="int">
SELECT COUNT(*) FROM category WHERE `name` = #{name}
<if test="categoryId != null">
AND category_id != #{categoryId}
<select id="existCategoryName" resultType="int">
SELECT COUNT(*) FROM category WHERE `name` = #{category.name} AND parent_id = #{category.parentId}
<if test="category.categoryId != null">
AND category_id != #{category.categoryId}
</if>
<if test="category.shopId != null">
AND shop_id = #{category.shopId}
</if>
</select>

Expand Down

0 comments on commit 614492b

Please sign in to comment.