""" 系统配置相关路由 - 增强版 """ from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from typing import List, Optional from ..core.database import get_db from ..schemas.system import ( SystemConfigCreate, SystemConfigUpdate, SystemConfigResponse, SystemConfigGroup, SystemConfigSummary, ConfigType, ConfigCategory ) from ..services.system_service import SystemService from ..utils.deps import get_current_admin router = APIRouter(prefix="/api/system", tags=["system"]) @router.get("/configs", response_model=List[SystemConfigResponse]) def get_all_configs( db: Session = Depends(get_db), current_user = Depends(get_current_admin), category: Optional[ConfigCategory] = Query(None, description="配置分类筛选"), editable_only: bool = Query(False, description="仅显示可编辑配置") ): """ 获取系统配置(仅管理员) """ if category: configs = SystemService.get_configs_by_category(db, category) else: configs = SystemService.get_all_configs(db) if editable_only: configs = [c for c in configs if c.is_editable] return configs @router.get("/configs/groups", response_model=List[SystemConfigGroup]) def get_config_groups( db: Session = Depends(get_db), current_user = Depends(get_current_admin) ): """ 获取配置分组(仅管理员) """ return SystemService.get_config_groups(db) @router.get("/configs/summary", response_model=SystemConfigSummary) def get_config_summary( db: Session = Depends(get_db), current_user = Depends(get_current_admin) ): """ 获取配置概览(仅管理员) """ return SystemService.get_config_summary(db) @router.get("/configs/{key}", response_model=SystemConfigResponse) def get_config( key: str, db: Session = Depends(get_db), current_user = Depends(get_current_admin) ): """ 根据键获取系统配置(仅管理员) """ config = SystemService.get_config(db, key) if not config: raise HTTPException(status_code=404, detail="配置不存在") return config @router.post("/configs", response_model=SystemConfigResponse) def create_config( config_data: SystemConfigCreate, db: Session = Depends(get_db), current_user = Depends(get_current_admin) ): """ 创建系统配置(仅管理员) """ try: return SystemService.create_config(db, config_data) except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) @router.put("/configs/{key}", response_model=SystemConfigResponse) def update_config( key: str, config_data: SystemConfigUpdate, db: Session = Depends(get_db), current_user = Depends(get_current_admin) ): """ 更新系统配置(仅管理员) """ try: config = SystemService.update_config(db, key, config_data) if not config: raise HTTPException(status_code=404, detail="配置不存在") return config except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) @router.delete("/configs/{key}") def delete_config( key: str, db: Session = Depends(get_db), current_user = Depends(get_current_admin) ): """ 删除系统配置(仅管理员) """ try: success = SystemService.delete_config(db, key) if not success: raise HTTPException(status_code=404, detail="配置不存在") return {"message": "配置删除成功"} except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) @router.get("/public/configs") def get_public_configs( db: Session = Depends(get_db) ): """ 获取公开配置(无需认证) """ return SystemService.get_public_configs(db) @router.post("/configs/initialize") def initialize_configs( db: Session = Depends(get_db), current_user = Depends(get_current_admin) ): """ 初始化默认配置(仅管理员) """ SystemService.initialize_default_configs(db) return {"message": "默认配置初始化成功"}