diff --git a/yami-shop-admin/src/main/java/com/yami/shop/admin/controller/CategoryController.java b/yami-shop-admin/src/main/java/com/yami/shop/admin/controller/CategoryController.java new file mode 100644 index 0000000..f42c71e --- /dev/null +++ b/yami-shop-admin/src/main/java/com/yami/shop/admin/controller/CategoryController.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. + * + * https://www.mall4j.com/ + * + * 未经允许,不可做商业用途! + * + * 版权所有,侵权必究! + */ + +package com.yami.shop.admin.controller; // 定义类所在的包 + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; // 引入MyBatis-Plus的条件查询包装器 +import com.yami.shop.bean.model.Category; // 引入商品分类模型类 +import com.yami.shop.common.annotation.SysLog; // 引入自定义日志注解 +import com.yami.shop.common.exception.YamiShopBindException; // 引入自定义异常类 +import com.yami.shop.security.admin.util.SecurityUtils; // 引入安全工具类 +import com.yami.shop.service.CategoryService; // 引入商品分类服务类 +import org.springframework.beans.factory.annotation.Autowired; // 引入Spring的@Autowired注解 +import com.yami.shop.common.response.ServerResponseEntity; // 引入服务器响应实体类 +import org.springframework.security.access.prepost.PreAuthorize; // 引入Spring Security的PreAuthorize注解 +import org.springframework.web.bind.annotation.*; + +import java.util.Date; // 引入Java的Date类 +import java.util.List; // 引入Java的List接口 +import java.util.Objects; // 引入Java的Objects工具类 + +/** + * 商品分类管理控制器 + * 该类包含分页获取、获取详细信息、保存、修改和删除商品分类的方法。 + * @作者 lgh + */ +@RestController // 标注这是一个控制器类,并且其返回结果直接写入HTTP响应体中,而不是视图名称 +@RequestMapping("/prod/category") // 定义请求路径的根地址为/prod/category +public class CategoryController { + + @Autowired + private CategoryService categoryService; // 自动注入商品分类服务类 + + /** + * 获取菜单页面的表 + * @return 服务器响应实体,包含商品分类列表 + */ + @GetMapping("/table") + @PreAuthorize("@pms.hasPermission('prod:category:page')") // 权限检查 + public ServerResponseEntity> table() { + List categoryMenuList = categoryService.tableCategory(SecurityUtils.getSysUser().getShopId()); + return ServerResponseEntity.success(categoryMenuList); + } + + /** + * 获取分类信息 + * @param categoryId 商品分类ID + * @return 服务器响应实体,包含商品分类的详细信息 + */ + @GetMapping("/info/{categoryId}") + public ServerResponseEntity info(@PathVariable("categoryId") Long categoryId) { + Category category = categoryService.getById(categoryId); + return ServerResponseEntity.success(category); + } + + /** + * 保存分类 + * @param category 商品分类信息 + * @return 服务器响应实体 + */ + @SysLog("保存分类") // 自定义日志注解 + @PostMapping + @PreAuthorize("@pms.hasPermission('prod:category:save')") // 权限检查 + public ServerResponseEntity save(@RequestBody Category category) { + category.setShopId(SecurityUtils.getSysUser().getShopId()); + category.setRecTime(new Date()); + Category categoryName = categoryService.getOne(new LambdaQueryWrapper().eq(Category::getCategoryName, category.getCategoryName()) + .eq(Category::getShopId, category.getShopId())); + if (Objects.nonNull(categoryName)) { + throw new YamiShopBindException("类目名称已存在!"); + } + categoryService.saveCategory(category); + return ServerResponseEntity.success(); + } + + /** + * 更新分类 + * @param category 商品分类信息 + * @return 服务器响应实体 + */ + @SysLog("更新分类") // 自定义日志注解 + @PutMapping + @PreAuthorize("@pms.hasPermission('prod:category:update')") // 权限检查 + public ServerResponseEntity update(@RequestBody Category category) { + category.setShopId(SecurityUtils.getSysUser().getShopId()); + if (Objects.equals(category.getParentId(), category.getCategoryId())) { + return ServerResponseEntity.showFailMsg("分类的上级不能是自己本身"); + } + Category categoryName = categoryService.getOne(new LambdaQueryWrapper().eq(Category::getCategoryName, category.getCategoryName()) + .eq(Category::getShopId, category.getShopId()).ne(Category::getCategoryId, category.getCategoryId())); + if (categoryName != null) { + throw new YamiShopBindException("类目名称已存在!"); + } + Category categoryDb = categoryService.getById(category.getCategoryId()); + // 如果从下线改成正常,则需要判断上级的状态 + if (Objects.equals(categoryDb.getStatus(), 0) && Objects.equals(category.getStatus(), 1) && !Objects.equals(category.getParentId(), 0L)) { + Category parentCategory = categoryService.getOne(new LambdaQueryWrapper().eq(Category::getCategoryId, category.getParentId())); + if (Objects.isNull(parentCategory) || Objects.equals(parentCategory.getStatus(), 0)) { + // 修改失败,上级分类不存在或者不为正常状态 + throw new YamiShopBindException("修改失败,上级分类不存在或者不为正常状态"); + } + } + categoryService.updateCategory(category); + return ServerResponseEntity.success(); + } + + /** + * 删除分类 + * @param categoryId 商品分类ID + * @return 服务器响应实体 + */ + @SysLog("删除分类") // 自定义日志注解 + @DeleteMapping("/{categoryId}") + @PreAuthorize("@pms.hasPermission('prod:category:delete')") // 权限检查 + public ServerResponseEntity delete(@PathVariable("categoryId") Long categoryId) { + if (categoryService.count(new LambdaQueryWrapper().eq(Category::getParentId, categoryId)) > 0) { + return ServerResponseEntity.showFailMsg("请删除子分类,再删除该分类"); + } + categoryService.deleteCategory(categoryId); + return ServerResponseEntity.success(); + } + + /** + * 获取所有的分类 + * @return 服务器响应实体,包含商品分类列表 + */ + @GetMapping("/listCategory") + public ServerResponseEntity> listCategory() { + return ServerResponseEntity.success(categoryService.list(new LambdaQueryWrapper() + .le(Category::getGrade, 2) + .eq(Category::getShopId, SecurityUtils.getSysUser().getShopId()) + .orderByAsc(Category::getSeq))); + } + + /** + * 获取所有的产品分类 + * @return 服务器响应实体,包含产品分类列表 + */ + @GetMapping("/listProdCategory") + public ServerResponseEntity> listProdCategory() { + List categories = categoryService.treeSelect(SecurityUtils.getSysUser().getShopId(), 2); + return ServerResponseEntity.success(categories); + } +}