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; /** * 知识库分类接口。 * *

分类的作用是让「绑定知识库」从逐个勾选变成整类绑定。 * *

权限模型与文件一致:可见 = 自己的 + 全局的;可管理 = 自己的,或本人是管理员。 */ @Slf4j @RestController @RequestMapping("/api/categories") @RequiredArgsConstructor public class CategoryController { private final CategoryService categoryService; /** * 可见分类列表,带文档数、已入库数与集合名。 * * @param chatId 传入时会标记每个分类是否已绑定到该会话,前端可显示选中态 */ @GetMapping public Result> list(@RequestParam(value = "chatId", required = false) Long chatId) { return Result.success(categoryService.listVisible(CurrentUser.requireUserId(), chatId)); } /** 创建分类;管理员可传 global=true 创建全员可见的分类 */ @PostMapping public Result 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 update(@PathVariable Long id, @Validated @RequestBody CategoryRequest request) { return Result.success(categoryService.update( CurrentUser.requireUserId(), id, request.getName(), request.getDescription())); } /** * 删除分类。 * *

分类内的**文档不会被删除**,只会变成「未分类」。 */ @DeleteMapping("/{id}") public Result delete(@PathVariable Long id) { categoryService.delete(CurrentUser.requireUserId(), id); return Result.success(); } /** * 把文档归入分类 / 移出分类。 * * @param categoryId 传 null 表示移出分类 */ @PostMapping("/{id}/files/{fileId}") public Result 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; } }