Files
smart-project-pricing/后端服务/app/models/operation_log.py
2026-01-31 21:33:06 +08:00

82 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""操作日志模型
记录用户操作审计日志
"""
from typing import Optional, Any
from datetime import datetime
from sqlalchemy import BigInteger, String, DateTime, JSON, ForeignKey, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class OperationLog(Base):
"""操作日志表"""
__tablename__ = "operation_logs"
id: Mapped[int] = mapped_column(
BigInteger,
primary_key=True,
autoincrement=True,
comment="主键ID"
)
user_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("users.id"),
nullable=True,
index=True,
comment="用户ID"
)
module: Mapped[str] = mapped_column(
String(50),
nullable=False,
index=True,
comment="模块cost/market/pricing/profit"
)
action: Mapped[str] = mapped_column(
String(50),
nullable=False,
comment="操作create/update/delete/export"
)
target_type: Mapped[str] = mapped_column(
String(50),
nullable=False,
comment="对象类型"
)
target_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
nullable=True,
comment="对象ID"
)
detail: Mapped[Optional[dict]] = mapped_column(
JSON,
nullable=True,
comment="详情"
)
ip_address: Mapped[Optional[str]] = mapped_column(
String(45),
nullable=True,
comment="IP地址"
)
created_at: Mapped[datetime] = mapped_column(
DateTime,
nullable=False,
default=func.now(),
server_default=func.now(),
index=True,
comment="操作时间"
)
# 关系
user: Mapped[Optional["User"]] = relationship(
"User",
back_populates="operation_logs"
)
# 避免循环导入
from app.models.user import User