""" 系统配置管理接口 """ from app.utils.timezone import beijing_now from typing import Optional, Any from fastapi import APIRouter, Depends, Query, Request from pydantic import BaseModel from sqlalchemy import or_ from sqlalchemy.orm import Session from app.api.deps import get_current_admin from app.database import get_db from app.models.system_config import SystemConfig router = APIRouter() class SystemConfigCreate(BaseModel): """创建系统配置请求""" config_key: str config_value: str config_type: str = 'string' description: Optional[str] = None is_system: int = 0 is_editable: int = 1 class SystemConfigUpdate(BaseModel): """更新系统配置请求""" config_value: Optional[str] = None config_type: Optional[str] = None description: Optional[str] = None @router.get("") async def get_system_configs( page: int = Query(1, ge=1), page_size: int = Query(10, ge=1, le=100), keyword: Optional[str] = Query(None, description="搜索关键词(配置键、描述)"), config_type: Optional[str] = Query(None, description="配置类型过滤"), db: Session = Depends(get_db), current_admin: Any = Depends(get_current_admin), ): """获取系统配置列表(支持检索与分页),按创建时间倒序""" offset = (page - 1) * page_size query = db.query(SystemConfig).filter(SystemConfig.is_deleted == 0) # 关键字搜索:配置键或描述模糊匹配 if keyword: like_pattern = f"%{keyword}%" query = query.filter( or_( SystemConfig.config_key.like(like_pattern), SystemConfig.description.like(like_pattern), ) ) # 配置类型过滤 if config_type: query = query.filter(SystemConfig.config_type == config_type) # 按创建时间倒序 query = query.order_by(SystemConfig.created_at.desc()) total = query.count() configs = query.offset(offset).limit(page_size).all() return { "code": 200, "msg": "成功", "data": [ { "id": config.id, "config_key": config.config_key, "config_value": config.config_value, "config_type": config.config_type, "description": config.description, "is_system": bool(config.is_system), "is_editable": bool(config.is_editable), "created_at": config.created_at.isoformat() if config.created_at else None, "updated_at": config.updated_at.isoformat() if config.updated_at else None, } for config in configs ], "total": total, "page": page, "page_size": page_size, } @router.get("/{config_id}") async def get_system_config_detail( config_id: int, db: Session = Depends(get_db), current_admin: Any = Depends(get_current_admin) ): """获取系统配置详情""" config = db.query(SystemConfig).filter(SystemConfig.id == config_id).first() if not config: return {"code": 404, "msg": "配置不存在"} return { "code": 200, "msg": "成功", "data": { "id": config.id, "config_key": config.config_key, "config_value": config.config_value, "config_type": config.config_type, "description": config.description, "is_system": bool(config.is_system), "is_editable": bool(config.is_editable), "created_at": config.created_at.isoformat() if config.created_at else None, "updated_at": config.updated_at.isoformat() if config.updated_at else None } } @router.post("") async def create_system_config( req: SystemConfigCreate, request: Request, db: Session = Depends(get_db), current_admin: Any = Depends(get_current_admin) ): """创建系统配置""" # 检查配置键是否已存在 existing_config = db.query(SystemConfig).filter( SystemConfig.config_key == req.config_key, SystemConfig.is_deleted == 0 ).first() if existing_config: return {"code": 400, "msg": "配置键已存在"} config = SystemConfig( config_key=req.config_key, config_value=req.config_value, config_type=req.config_type, description=req.description, is_system=req.is_system, is_editable=req.is_editable, created_at=beijing_now(), updated_at=beijing_now() ) db.add(config) # 记录操作日志 from app.utils.history import create_operation_log client_ip = request.client.host if request.client else None create_operation_log( db=db, admin_id=current_admin.id, module="系统配置管理", action="创建配置", description=f"创建系统配置:{req.config_key}", new_data={ "config_key": req.config_key, "config_value": req.config_value, "config_type": req.config_type, "description": req.description }, ip_address=client_ip ) db.commit() return {"code": 200, "msg": "创建成功", "data": {"id": config.id}} @router.put("/{config_id}") async def update_system_config( config_id: int, req: SystemConfigUpdate, request: Request, db: Session = Depends(get_db), current_admin: Any = Depends(get_current_admin) ): """更新系统配置""" config = db.query(SystemConfig).filter(SystemConfig.id == config_id).first() if not config: return {"code": 404, "msg": "配置不存在"} # 检查是否可编辑 if not config.is_editable: return {"code": 403, "msg": "该配置不可编辑"} # 记录更新前的状态 old_data = { "config_key": config.config_key, "config_value": config.config_value, "config_type": config.config_type, "description": config.description } # 更新字段 if req.config_value is not None: config.config_value = req.config_value if req.config_type is not None: config.config_type = req.config_type if req.description is not None: config.description = req.description config.updated_at = beijing_now() # 记录操作日志 from app.utils.history import create_operation_log client_ip = request.client.host if request.client else None create_operation_log( db=db, admin_id=current_admin.id, module="系统配置管理", action="更新配置", description=f"更新系统配置:{config.config_key}", old_data=old_data, new_data={ "config_key": config.config_key, "config_value": config.config_value, "config_type": config.config_type, "description": config.description }, ip_address=client_ip ) db.commit() return {"code": 200, "msg": "更新成功"} @router.put("/{config_id}/editable") async def toggle_system_config_editable( config_id: int, request: Request, db: Session = Depends(get_db), current_admin: Any = Depends(get_current_admin) ): """切换系统配置的可编辑状态""" config = db.query(SystemConfig).filter(SystemConfig.id == config_id).first() if not config: return {"code": 404, "msg": "配置不存在"} # 系统配置不能修改可编辑状态 if config.is_system: return {"code": 403, "msg": "系统配置的可编辑状态不能修改"} old_editable = config.is_editable config.is_editable = 0 if config.is_editable else 1 config.updated_at = beijing_now() # 记录操作日志 from app.utils.history import create_operation_log client_ip = request.client.host if request.client else None create_operation_log( db=db, admin_id=current_admin.id, module="系统配置管理", action="切换配置可编辑状态", description=f"{'启用' if config.is_editable else '禁用'}配置编辑:{config.config_key}", old_data={"is_editable": bool(old_editable)}, new_data={"is_editable": bool(config.is_editable)}, ip_address=client_ip ) db.commit() return {"code": 200, "msg": "可编辑状态更新成功", "data": {"is_editable": bool(config.is_editable)}} @router.delete("/{config_id}") async def delete_system_config( config_id: int, request: Request, db: Session = Depends(get_db), current_admin: Any = Depends(get_current_admin) ): """删除系统配置(软删除)""" config = db.query(SystemConfig).filter( SystemConfig.id == config_id, SystemConfig.is_deleted == 0 ).first() if not config: return {"code": 404, "msg": "配置不存在"} # 检查是否为系统配置(系统配置不能删除) if config.is_system: return {"code": 403, "msg": "系统配置不能删除"} config.is_deleted = 1 config.updated_at = beijing_now() # 记录操作日志 from app.utils.history import create_operation_log client_ip = request.client.host if request.client else None create_operation_log( db=db, admin_id=current_admin.id, module="系统配置管理", action="删除配置", description=f"删除系统配置:{config.config_key}", old_data={ "config_key": config.config_key, "config_value": config.config_value, "config_type": config.config_type, "description": config.description }, ip_address=client_ip ) db.commit() return {"code": 200, "msg": "删除成功"}