wangjinhao_branch
wangjinhao 9 months ago
parent 29790ef1e4
commit c78536267d

@ -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<List<Category>> table() {
List<Category> categoryMenuList = categoryService.tableCategory(SecurityUtils.getSysUser().getShopId());
return ServerResponseEntity.success(categoryMenuList);
}
/**
*
* @param categoryId ID
* @return
*/
@GetMapping("/info/{categoryId}")
public ServerResponseEntity<Category> 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<Void> save(@RequestBody Category category) {
category.setShopId(SecurityUtils.getSysUser().getShopId());
category.setRecTime(new Date());
Category categoryName = categoryService.getOne(new LambdaQueryWrapper<Category>().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<String> 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<Category>().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<Category>().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<String> delete(@PathVariable("categoryId") Long categoryId) {
if (categoryService.count(new LambdaQueryWrapper<Category>().eq(Category::getParentId, categoryId)) > 0) {
return ServerResponseEntity.showFailMsg("请删除子分类,再删除该分类");
}
categoryService.deleteCategory(categoryId);
return ServerResponseEntity.success();
}
/**
*
* @return
*/
@GetMapping("/listCategory")
public ServerResponseEntity<List<Category>> listCategory() {
return ServerResponseEntity.success(categoryService.list(new LambdaQueryWrapper<Category>()
.le(Category::getGrade, 2)
.eq(Category::getShopId, SecurityUtils.getSysUser().getShopId())
.orderByAsc(Category::getSeq)));
}
/**
*
* @return
*/
@GetMapping("/listProdCategory")
public ServerResponseEntity<List<Category>> listProdCategory() {
List<Category> categories = categoryService.treeSelect(SecurityUtils.getSysUser().getShopId(), 2);
return ServerResponseEntity.success(categories);
}
}
Loading…
Cancel
Save