|
|
@@ -0,0 +1,133 @@
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.AdminRoles;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.service.UnifiedKnowledgeBaseService;
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 统一知识库管理 Controller
|
|
|
+ * 管理端在线编辑 knowledge_base_section 各模块 JSON,写库后热重载(无需重启)。
|
|
|
+ * 权限:平台角色(admin/operator/finance) + 知识管理员 + 管家。
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Tag(name = "统一知识库管理")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/admin/knowledge-base/unified")
|
|
|
+public class AdminUnifiedKnowledgeController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UnifiedKnowledgeBaseService unifiedKnowledgeBaseService;
|
|
|
+
|
|
|
+ /** 允许维护统一知识库的角色(knowledge_admin 门户中知识库归其管理;admin/operator 平台角色放行) */
|
|
|
+ private static final Set<String> ALLOWED_ROLES = new HashSet<>(Arrays.asList(
|
|
|
+ "admin", "operator", "finance", "knowledge_admin", "butler"));
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private List<String> roles(HttpServletRequest request) {
|
|
|
+ Object rolesAttr = request.getAttribute("roles");
|
|
|
+ if (rolesAttr instanceof List) {
|
|
|
+ return (List<String>) rolesAttr;
|
|
|
+ }
|
|
|
+ Object roleAttr = request.getAttribute("role");
|
|
|
+ if (roleAttr != null) {
|
|
|
+ return Collections.singletonList(roleAttr.toString());
|
|
|
+ }
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean allowed(HttpServletRequest request) {
|
|
|
+ List<String> r = roles(request);
|
|
|
+ if (r == null || r.isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (String raw : r) {
|
|
|
+ if (ALLOWED_ROLES.contains(AdminRoles.normalize(raw))) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "统一知识库-模块概览(管理端:键/名称/大小/版本/更新时间/加载状态)")
|
|
|
+ @PostMapping("/overview")
|
|
|
+ public Result<List<Map<String, Object>>> overview(HttpServletRequest request) {
|
|
|
+ if (!allowed(request)) {
|
|
|
+ return Result.error("无权限维护统一知识库");
|
|
|
+ }
|
|
|
+ return Result.success(unifiedKnowledgeBaseService.getModuleOverviewDetailed());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "统一知识库-按模块获取原始内容")
|
|
|
+ @PostMapping("/section")
|
|
|
+ public Result<Map<String, Object>> section(@RequestBody Map<String, String> params, HttpServletRequest request) {
|
|
|
+ if (!allowed(request)) {
|
|
|
+ return Result.error("无权限维护统一知识库");
|
|
|
+ }
|
|
|
+ String moduleKey = params.get("moduleKey");
|
|
|
+ if (moduleKey == null || moduleKey.isEmpty()) {
|
|
|
+ return Result.error("moduleKey不能为空");
|
|
|
+ }
|
|
|
+ Map<String, Object> data = unifiedKnowledgeBaseService.getSection(moduleKey);
|
|
|
+ if (data == null) {
|
|
|
+ return Result.error("模块不存在或内容为空: " + moduleKey);
|
|
|
+ }
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "统一知识库-更新模块内容(写库+热重载)")
|
|
|
+ @PostMapping("/update")
|
|
|
+ public Result<Map<String, Object>> update(@RequestBody Map<String, Object> params, HttpServletRequest request) {
|
|
|
+ if (!allowed(request)) {
|
|
|
+ return Result.error("无权限维护统一知识库");
|
|
|
+ }
|
|
|
+ String moduleKey = params.get("moduleKey") != null ? params.get("moduleKey").toString() : null;
|
|
|
+ String moduleName = params.get("moduleName") != null ? params.get("moduleName").toString() : null;
|
|
|
+ Object content = params.get("content");
|
|
|
+ if (moduleKey == null || moduleKey.isEmpty()) {
|
|
|
+ return Result.error("moduleKey不能为空");
|
|
|
+ }
|
|
|
+ if (content == null) {
|
|
|
+ return Result.error("content不能为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Map<String, Object> r = unifiedKnowledgeBaseService.updateModule(moduleKey, moduleName, content);
|
|
|
+ log.info("统一知识库模块更新: {} 成功, size={}", moduleKey, r.get("size"));
|
|
|
+ return Result.success("模块已更新并热加载生效", r);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return Result.error(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("统一知识库模块更新异常: " + moduleKey, e);
|
|
|
+ return Result.error("更新失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "统一知识库-从classpath种子重置模块(写库+热重载)")
|
|
|
+ @PostMapping("/reset")
|
|
|
+ public Result<Map<String, Object>> reset(@RequestBody Map<String, String> params, HttpServletRequest request) {
|
|
|
+ if (!allowed(request)) {
|
|
|
+ return Result.error("无权限维护统一知识库");
|
|
|
+ }
|
|
|
+ String moduleKey = params.get("moduleKey");
|
|
|
+ if (moduleKey == null || moduleKey.isEmpty()) {
|
|
|
+ return Result.error("moduleKey不能为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Map<String, Object> r = unifiedKnowledgeBaseService.resetModuleFromClasspath(moduleKey);
|
|
|
+ log.info("统一知识库模块重置: {} 成功, size={}", moduleKey, r.get("size"));
|
|
|
+ return Result.success("模块已从种子重置并热加载生效", r);
|
|
|
+ } catch (IllegalArgumentException | IllegalStateException e) {
|
|
|
+ return Result.error(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("统一知识库模块重置异常: " + moduleKey, e);
|
|
|
+ return Result.error("重置失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|