""" 后台管理系统ORM定义 定义管理员用户、操作日志和登录尝试记录的数据库表结构 Requirements: 13.2, 13.3 """ from sqlalchemy import Column, Integer, BigInteger, String, Text, DateTime, Boolean, ForeignKey, Index from sqlalchemy.dialects.postgresql import JSON from sqlalchemy.sql import func import json from app.database import Base class AdminUser(Base): """ 管理员用户ORM类 """ __tablename__ = "admin_users" id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID") username = Column(String(50), unique=True, nullable=False, index=True, comment="用户名") password_hash = Column(String(255), nullable=False, comment="密码哈希") nickname = Column(String(100), nullable=False, comment="昵称") status = Column(String(20), nullable=False, default="active", comment="状态:active/disabled") created_at = Column(DateTime, server_default=func.now(), comment="创建时间") updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间") __table_args__ = ( Index('idx_admin_users_status', 'status'), {'schema': 'aigcspace', 'comment': '管理员用户表'} ) def __repr__(self): return f"" class OperationLog(Base): """ 操作日志ORM类 """ __tablename__ = "operation_log" id = Column(BigInteger, primary_key=True, autoincrement=True, comment="主键ID") admin_id = Column(Integer, ForeignKey("aigcspace.admin_users.id"), nullable=False, comment="管理员ID") operation_type = Column(String(50), nullable=False, comment="操作类型:create/update/delete/login") module = Column(String(50), nullable=False, comment="操作模块:user/model/config") target_id = Column(String(100), nullable=True, comment="操作对象ID") detail = Column(JSON, nullable=True, comment="操作详情(JSON格式)") ip_address = Column(String(50), nullable=True, comment="IP地址") created_at = Column(DateTime, server_default=func.now(), comment="创建时间") __table_args__ = ( Index('idx_operation_log_admin_id', 'admin_id'), Index('idx_operation_log_operation_type', 'operation_type'), Index('idx_operation_log_module', 'module'), Index('idx_operation_log_created_at', 'created_at'), {'schema': 'aigcspace', 'comment': '管理员操作日志表'} ) def __repr__(self): return f"" class AdminLoginAttempt(Base): """ 管理员登录尝试记录ORM类 """ __tablename__ = "admin_login_attempt" id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID") username = Column(String(50), nullable=False, index=True, comment="用户名") success = Column(Boolean, nullable=False, comment="是否成功") ip_address = Column(String(50), nullable=True, comment="IP地址") created_at = Column(DateTime, server_default=func.now(), comment="创建时间") __table_args__ = ( Index('idx_admin_login_attempt_created_at', 'created_at'), {'schema': 'aigcspace', 'comment': '管理员登录尝试记录表'} ) def __repr__(self): return f""