from enum import Enum from sqlalchemy import Integer, JSON, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.models.base import BaseModel class AuditAction(str, Enum): """审计操作类型""" CREATE = "CREATE" UPDATE = "UPDATE" DELETE = "DELETE" VIEW = "VIEW" EXPORT = "EXPORT" LOGIN = "LOGIN" LOGOUT = "LOGOUT" UPLOAD = "UPLOAD" DOWNLOAD = "DOWNLOAD" class AuditLog(BaseModel): """ 审计日志模型 记录所有关键业务操作 Attributes: company_id: 企业 ID user_id: 用户 ID action: 操作类型 resource_type: 资源类型 (如 company, user, task, file) resource_id: 资源 ID details: 操作详情 (JSON) ip_address: IP 地址 user_agent: User-Agent """ __tablename__ = "audit_logs" company_id: Mapped[int] = mapped_column(Integer, nullable=False, index=True) user_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True) action: Mapped[str] = mapped_column(String(50), nullable=False, index=True) resource_type: Mapped[str] = mapped_column(String(100), nullable=False, index=True) resource_id: Mapped[str | None] = mapped_column(String(200), nullable=True, index=True) details: Mapped[dict | None] = mapped_column(JSON, nullable=True) ip_address: Mapped[str | None] = mapped_column(String(50), nullable=True) user_agent: Mapped[str | None] = mapped_column(Text, nullable=True) def __repr__(self) -> str: return ( f"" )