wuyouming666
2023-12-12 69d6f1711a32ba4d1a989fef6b3640ff5ae4f2e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.example.springboot.controller;
 
import cn.hutool.core.collection.CollectionUtil;
import com.example.springboot.security.util.SecurityUtil;
import com.example.springboot.service.CategoryService;
import com.example.springboot.entity.Category;
import com.example.springboot.entity.vo.CategoryVo;
import com.example.springboot.entity.vo.Result;
import com.example.springboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
@Slf4j
@Api(tags = "分类")
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private UserService userService;
 
    @ApiOperation(value = "分页查询分类")
    @GetMapping("/selectPage")
    @RequiresPermissions({"category:select"})
    public Result selectPage(CategoryVo categoryVO) {
        return Result.success(categoryService.selectPage(categoryVO));
    }
 
    @ApiOperation(value = "单个删除分类")
    @PostMapping("/removeById")
    @RequiresPermissions({"category:delete"})
    public Result removeById(@RequestBody CategoryVo categoryVO) {
        List<Category> categories = categoryService.selectList(categoryVO.getId(), false);
        if (CollectionUtil.isNotEmpty(categories)) return Result.fail("该分类下含有子集,不可以删除");
        List<Long> longs = userService.selectChild(SecurityUtil.getCurrentUser().getId(), true);
        boolean remove = categoryService.lambdaUpdate()
                .in(Category::getCreator, longs)
                .eq(Category::getId, categoryVO.getId())
                .remove();
        if (remove) log.info("用户id{}删除分类{}", SecurityUtil.getCurrentUser().getId(), categoryVO.getName());
        return Result.success();
    }
 
    @ApiOperation(value = "修改或者更新分类")
    @PostMapping("/saveOrUpdate")
    @RequiresPermissions({"category:update", "category:add"})
    public Result saveOrUpdate(@RequestBody Category category) {
        List<Long> longs = userService.selectChild(SecurityUtil.getCurrentUser().getId(), true);
        Integer count = categoryService.lambdaQuery()
                .eq(Category::getName, category.getName())
                .in(Category::getCreator, longs)
                .ne(category.getId() != null, Category::getId, category.getId())
                .count();
        if (count > 0) return Result.fail("该分类名称已存在");
        category.setCreator(category.getId() == null ? SecurityUtil.getCurrentUser().getId() : null);
        categoryService.saveOrUpdate(category);
        return Result.success();
    }
 
    @ApiOperation(value = "查询某人创建的分类,但是排除当前选中的分类,用户修改分类使用")
    @GetMapping("/select")
    @RequiresPermissions({"category:select"})
    public Result select(Category category) {
        List<Category> categories = categoryService.lambdaQuery()
                .ne(category.getId() != null, Category::getId, category.getId())
                .eq(Category::getCreator, SecurityUtil.getCurrentUser().getId())
                .list();
        return Result.success(categories);
    }
 
    @ApiOperation(value = "查询分类,嵌套数据结构")
    @GetMapping("/selectChilds")
    @RequiresPermissions({"category:select"})
    public Result selectChilds(Category category) {
        return Result.success(categoryService.selectChilds(SecurityUtil.getCurrentUser().getId()));
    }
 
    @ApiOperation(value = "通过id查询分类")
    @GetMapping("/getById")
    @RequiresPermissions({"category:select"})
    public Result getById(Category category) {
        Category one = categoryService.lambdaQuery().eq(Category::getCreator, SecurityUtil.getCurrentUser().getId())
                .eq(Category::getId, category.getId()).one();
        return Result.success(one);
    }
}