kongdeqiang
2026-10-10 ad24c871d74a417db5412fd9d1fa60411c33ae7f
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
98
99
100
101
102
103
104
105
106
package com.custom.webui.controller;
 
import com.custom.webui.dto.resp.CategoryDTO;
import com.custom.webui.dto.resp.Result;
import com.custom.webui.security.CurrentUser;
import com.custom.webui.service.CategoryService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
/**
 * 知识库分类接口。
 *
 * <p>分类的作用是让「绑定知识库」从逐个勾选变成整类绑定。
 *
 * <p>权限模型与文件一致:可见 = 自己的 + 全局的;可管理 = 自己的,或本人是管理员。
 */
@Slf4j
@RestController
@RequestMapping("/api/categories")
@RequiredArgsConstructor
public class CategoryController {
 
    private final CategoryService categoryService;
 
    /**
     * 可见分类列表,带文档数、已入库数与集合名。
     *
     * @param chatId 传入时会标记每个分类是否已绑定到该会话,前端可显示选中态
     */
    @GetMapping
    public Result<List<CategoryDTO>> list(@RequestParam(value = "chatId", required = false) Long chatId) {
        return Result.success(categoryService.listVisible(CurrentUser.requireUserId(), chatId));
    }
 
    /** 创建分类;管理员可传 global=true 创建全员可见的分类 */
    @PostMapping
    public Result<CategoryDTO> create(@Validated @RequestBody CategoryRequest request) {
        return Result.success(categoryService.create(
                CurrentUser.requireUserId(),
                request.getName(),
                request.getDescription(),
                Boolean.TRUE.equals(request.getGlobal())));
    }
 
    /** 重命名 / 修改说明 */
    @PutMapping("/{id}")
    public Result<CategoryDTO> update(@PathVariable Long id,
                                     @Validated @RequestBody CategoryRequest request) {
        return Result.success(categoryService.update(
                CurrentUser.requireUserId(), id, request.getName(), request.getDescription()));
    }
 
    /**
     * 删除分类。
     *
     * <p>分类内的**文档不会被删除**,只会变成「未分类」。
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        categoryService.delete(CurrentUser.requireUserId(), id);
        return Result.success();
    }
 
    /**
     * 把文档归入分类 / 移出分类。
     *
     * @param categoryId 传 null 表示移出分类
     */
    @PostMapping("/{id}/files/{fileId}")
    public Result<Void> assignFile(@PathVariable Long id,
                                  @PathVariable Long fileId,
                                  @RequestParam(value = "assign", defaultValue = "true") boolean assign) {
        categoryService.assignFile(CurrentUser.requireUserId(), fileId, assign ? id : null);
        return Result.success();
    }
 
    /** 分类请求体 */
    @Data
    public static class CategoryRequest {
 
        @NotBlank(message = "分类名称不能为空")
        @Size(max = 64, message = "分类名称不能超过 64 个字符")
        private String name;
 
        @Size(max = 255, message = "分类说明不能超过 255 个字符")
        private String description;
 
        /** 是否创建为全局分类(仅管理员) */
        private Boolean global;
    }
}