package com.wok.supportbot.controller; import com.wok.supportbot.entity.SystemConfig; import com.wok.supportbot.service.SystemConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; /** * 系统配置管理接口 *

* 管理后台编辑系统级配置项(SDK 保密声明等), * 管理接口需要 admin 角色。 */ @Slf4j @RestController @RequestMapping("/system-config") public class SystemConfigController { @Autowired private SystemConfigService systemConfigService; /** * 查询所有系统配置列表 */ @GetMapping("/list") @PreAuthorize("hasRole('admin')") public ResponseEntity> list() { try { List configs = systemConfigService.listAll(); return ResponseEntity.ok(Map.of( "success", true, "data", configs )); } catch (Exception e) { log.error("查询系统配置列表失败", e); return ResponseEntity.status(500).body(Map.of( "success", false, "message", "查询失败:" + e.getMessage() )); } } /** * 查询单条系统配置 * * @param key 配置键(如 "disclaimer") */ @GetMapping("/{key}") @PreAuthorize("hasRole('admin')") public ResponseEntity> getByKey(@PathVariable String key) { try { SystemConfig config = systemConfigService.getByKey(key); if (config == null) { return ResponseEntity.status(404).body(Map.of( "success", false, "message", "配置不存在:" + key )); } return ResponseEntity.ok(Map.of( "success", true, "data", config )); } catch (Exception e) { log.error("查询系统配置失败, key={}", key, e); return ResponseEntity.status(500).body(Map.of( "success", false, "message", "查询失败:" + e.getMessage() )); } } /** * 更新系统配置 * * @param key 配置键(如 "disclaimer") * @param body 请求体 { "configValue": "...", "description": "..." } */ @PutMapping("/{key}") @PreAuthorize("hasRole('admin')") public ResponseEntity> update( @PathVariable String key, @RequestBody Map body) { try { String configValue = body.getOrDefault("configValue", ""); String description = body.getOrDefault("description", ""); systemConfigService.saveOrUpdate(key, configValue, description); return ResponseEntity.ok(Map.of( "success", true, "message", "保存成功" )); } catch (Exception e) { log.error("更新系统配置失败", e); return ResponseEntity.status(500).body(Map.of( "success", false, "message", "保存失败:" + e.getMessage() )); } } /** * 删除系统配置(逻辑删除) * * @param key 配置键(如 "disclaimer") */ @DeleteMapping("/{key}") @PreAuthorize("hasRole('admin')") public ResponseEntity> delete(@PathVariable String key) { try { boolean deleted = systemConfigService.deleteByKey(key); if (!deleted) { return ResponseEntity.status(404).body(Map.of( "success", false, "message", "配置不存在:" + key )); } return ResponseEntity.ok(Map.of( "success", true, "message", "删除成功" )); } catch (Exception e) { log.error("删除系统配置失败, key={}", key, e); return ResponseEntity.status(500).body(Map.of( "success", false, "message", "删除失败:" + e.getMessage() )); } } }