分类名称不能重复

master
LGH 6 years ago
parent b74dddea4a
commit affc3d2170

@ -16,6 +16,7 @@ import java.util.Objects;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.util.SecurityUtils; import com.yami.shop.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -83,6 +84,11 @@ public class CategoryController {
public ResponseEntity<Void> save(@RequestBody Category category){ public ResponseEntity<Void> save(@RequestBody Category category){
category.setShopId(SecurityUtils.getSysUser().getShopId()); category.setShopId(SecurityUtils.getSysUser().getShopId());
category.setRecTime(new Date()); category.setRecTime(new Date());
Category categoryName = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getCategoryName,category.getCategoryName())
.eq(Category::getShopId,category.getShopId()));
if(categoryName != null){
throw new YamiShopBindException("类目名称已存在!");
}
categoryService.saveCategroy(category); categoryService.saveCategroy(category);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
@ -98,6 +104,11 @@ public class CategoryController {
if (Objects.equals(category.getParentId(),category.getCategoryId())) { if (Objects.equals(category.getParentId(),category.getCategoryId())) {
return ResponseEntity.badRequest().body("分类的上级不能是自己本身"); return ResponseEntity.badRequest().body("分类的上级不能是自己本身");
} }
Category categoryName = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getCategoryName,category.getCategoryName())
.eq(Category::getShopId,category.getShopId()).ne(Category::getCategoryId,category.getCategoryId()));
if(categoryName != null){
throw new YamiShopBindException("类目名称已存在!");
}
categoryService.updateCategroy(category); categoryService.updateCategroy(category);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }

Loading…
Cancel
Save