Update CategoryController.java

cyj
pbvfus8to 2 months ago
parent b2c499d142
commit 1789270f80

@ -25,10 +25,10 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
/** /**
* *
*
*
* @author lgh * @author lgh
* *
*/ */
@ -36,22 +36,32 @@ import java.util.Objects;
@RequestMapping("/prod/category") @RequestMapping("/prod/category")
public class CategoryController { public class CategoryController {
// 注入CategoryService用于与商品分类相关的业务逻辑处理例如查询、保存、更新、删除分类等操作。
@Autowired @Autowired
private CategoryService categoryService; private CategoryService categoryService;
/** /**
* * CategoryService
* @return * ID
* @PreAuthorize"prod:category:page"访
*
* @return ServerResponseEntity
*/ */
@GetMapping("/table") @GetMapping("/table")
@PreAuthorize("@pms.hasPermission('prod:category:page')") @PreAuthorize("@pms.hasPermission('prod:category:page')")
public ServerResponseEntity<List<Category>> table() { public ServerResponseEntity<List<Category>> table() {
// 调用CategoryService的tableCategory方法传入当前登录用户所属的店铺ID获取用于菜单页面展示的分类列表信息。
List<Category> categoryMenuList = categoryService.tableCategory(SecurityUtils.getSysUser().getShopId()); List<Category> categoryMenuList = categoryService.tableCategory(SecurityUtils.getSysUser().getShopId());
// 将获取到的分类列表信息封装在成功的响应实体中返回给前端。
return ServerResponseEntity.success(categoryMenuList); return ServerResponseEntity.success(categoryMenuList);
} }
/** /**
* * IDIDCategoryServicegetById
*
*
* @param categoryId
* @return ServerResponseEntity
*/ */
@GetMapping("/info/{categoryId}") @GetMapping("/info/{categoryId}")
public ServerResponseEntity<Category> info(@PathVariable("categoryId") Long categoryId) { public ServerResponseEntity<Category> info(@PathVariable("categoryId") Long categoryId) {
@ -59,10 +69,15 @@ public class CategoryController {
return ServerResponseEntity.success(category); return ServerResponseEntity.success(category);
} }
/** /**
* * Category
* IDID
* CategoryServicesaveCategory
* @PreAuthorize"prod:category:save"访
* 使@SysLog便
*
* @param category ID
* @return ServerResponseEntityVoid
*/ */
@SysLog("保存分类") @SysLog("保存分类")
@PostMapping @PostMapping
@ -70,7 +85,9 @@ public class CategoryController {
public ServerResponseEntity<Void> save(@RequestBody Category category) { public ServerResponseEntity<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()) // 通过LambdaQueryWrapper构建查询条件查询在当前店铺下分类名称与要保存的分类名称相同的分类记录用于判断名称是否已存在。
Category categoryName = categoryService.getOne(new LambdaQueryWrapper<Category>()
.eq(Category::getCategoryName, category.getCategoryName())
.eq(Category::getShopId, category.getShopId())); .eq(Category::getShopId, category.getShopId()));
if (Objects.nonNull(categoryName)) { if (Objects.nonNull(categoryName)) {
throw new YamiShopBindException("类目名称已存在!"); throw new YamiShopBindException("类目名称已存在!");
@ -80,7 +97,15 @@ public class CategoryController {
} }
/** /**
* * Category
* IDID
* 线
* CategoryServiceupdateCategory
* @PreAuthorize"prod:category:update"访
* 使@SysLog便
*
* @param category ID
* @return ServerResponseEntity
*/ */
@SysLog("更新分类") @SysLog("更新分类")
@PutMapping @PutMapping
@ -90,15 +115,19 @@ public class CategoryController {
if (Objects.equals(category.getParentId(), category.getCategoryId())) { if (Objects.equals(category.getParentId(), category.getCategoryId())) {
return ServerResponseEntity.showFailMsg("分类的上级不能是自己本身"); return ServerResponseEntity.showFailMsg("分类的上级不能是自己本身");
} }
Category categoryName = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getCategoryName,category.getCategoryName()) // 通过LambdaQueryWrapper构建查询条件查询在当前店铺下分类名称与要更新的分类名称相同且ID不同排除自身的分类记录用于判断名称是否已存在。
.eq(Category::getShopId,category.getShopId()).ne(Category::getCategoryId,category.getCategoryId())); 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) { if (categoryName!= null) {
throw new YamiShopBindException("类目名称已存在!"); throw new YamiShopBindException("类目名称已存在!");
} }
Category categoryDb = categoryService.getById(category.getCategoryId()); Category categoryDb = categoryService.getById(category.getCategoryId());
// 如果从下线改成正常,则需要判断上级的状态 // 如果从下线状态为0改成正常状态为1则需要判断上级的状态确保上级分类存在且为正常状态。
if (Objects.equals(categoryDb.getStatus(), 0) && Objects.equals(category.getStatus(), 1) &&!Objects.equals(category.getParentId(), 0L)) { if (Objects.equals(categoryDb.getStatus(), 0) && Objects.equals(category.getStatus(), 1) &&!Objects.equals(category.getParentId(), 0L)) {
Category parentCategory = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getCategoryId, category.getParentId())); Category parentCategory = categoryService.getOne(new LambdaQueryWrapper<Category>()
.eq(Category::getCategoryId, category.getParentId()));
if (Objects.isNull(parentCategory) || Objects.equals(parentCategory.getStatus(), 0)) { if (Objects.isNull(parentCategory) || Objects.equals(parentCategory.getStatus(), 0)) {
// 修改失败,上级分类不存在或者不为正常状态 // 修改失败,上级分类不存在或者不为正常状态
throw new YamiShopBindException("修改失败,上级分类不存在或者不为正常状态"); throw new YamiShopBindException("修改失败,上级分类不存在或者不为正常状态");
@ -109,12 +138,19 @@ public class CategoryController {
} }
/** /**
* * ID
* CategoryServicedeleteCategory
* @PreAuthorize"prod:category:delete"访
* 使@SysLog便
*
* @param categoryId
* @return ServerResponseEntity
*/ */
@SysLog("删除分类") @SysLog("删除分类")
@DeleteMapping("/{categoryId}") @DeleteMapping("/{categoryId}")
@PreAuthorize("@pms.hasPermission('prod:category:delete')") @PreAuthorize("@pms.hasPermission('prod:category:delete')")
public ServerResponseEntity<String> delete(@PathVariable("categoryId") Long categoryId) { public ServerResponseEntity<String> delete(@PathVariable("categoryId") Long categoryId) {
// 通过LambdaQueryWrapper构建查询条件统计以当前分类ID为上级分类的子分类数量若数量大于0则说明还有子分类不允许删除。
if (categoryService.count(new LambdaQueryWrapper<Category>().eq(Category::getParentId, categoryId)) > 0) { if (categoryService.count(new LambdaQueryWrapper<Category>().eq(Category::getParentId, categoryId)) > 0) {
return ServerResponseEntity.showFailMsg("请删除子分类,再删除该分类"); return ServerResponseEntity.showFailMsg("请删除子分类,再删除该分类");
} }
@ -123,7 +159,11 @@ public class CategoryController {
} }
/** /**
* * LambdaQueryWrapper
* 2ID
*
*
* @return ServerResponseEntity
*/ */
@GetMapping("/listCategory") @GetMapping("/listCategory")
public ServerResponseEntity<List<Category>> listCategory() { public ServerResponseEntity<List<Category>> listCategory() {
@ -135,7 +175,11 @@ public class CategoryController {
} }
/** /**
* * CategoryServicetreeSelect
* IDservice
*
*
* @return ServerResponseEntity
*/ */
@GetMapping("/listProdCategory") @GetMapping("/listProdCategory")
public ServerResponseEntity<List<Category>> listProdCategory() { public ServerResponseEntity<List<Category>> listProdCategory() {

Loading…
Cancel
Save