notification.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """站内通知模型(追加型,类似审计日志,不用业务表 mixin)。"""
  2. from __future__ import annotations
  3. from datetime import datetime, timezone
  4. from sqlalchemy import BigInteger, CheckConstraint, Index, String, text
  5. from sqlalchemy.dialects.mysql import DATETIME
  6. from sqlalchemy.orm import Mapped, mapped_column
  7. from dms.extensions import db
  8. class Notification(db.Model):
  9. """用户站内通知:审批流等待处理等需用户关注的信息。
  10. 通知只追加与状态流转(未读→已读、待处理→已同意/已拒绝),
  11. 不做软删与乐观锁,故不使用 BusinessTableMixin。
  12. """
  13. __tablename__ = "sys_notification"
  14. __table_args__ = (
  15. CheckConstraint(
  16. "notify_type IN ('APPROVAL')",
  17. name="notification_notify_type",
  18. ),
  19. CheckConstraint(
  20. "action IN ('NONE', 'PENDING', 'AGREE', 'REJECT')",
  21. name="notification_action",
  22. ),
  23. CheckConstraint(
  24. "status IN ('UNREAD', 'READ')",
  25. name="notification_status",
  26. ),
  27. Index("ix_sys_notification_user_id", "user_id", "id"),
  28. )
  29. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  30. user_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
  31. title: Mapped[str] = mapped_column(String(200), nullable=False)
  32. content: Mapped[str | None] = mapped_column(String(1000), nullable=True)
  33. # 通知类别:APPROVAL 审批待办(后续审批流写入,预留扩展)
  34. notify_type: Mapped[str] = mapped_column(String(32), nullable=False)
  35. # 待办操作:NONE 无需操作 / PENDING 待处理 / AGREE 已同意 / REJECT 已拒绝
  36. action: Mapped[str] = mapped_column(String(16), nullable=False)
  37. status: Mapped[str] = mapped_column(String(16), nullable=False)
  38. created_at: Mapped[datetime] = mapped_column(
  39. DATETIME(fsp=3),
  40. nullable=False,
  41. # 应用侧 UTC 落库(与 sys_audit_log 同口径,序列化追加 Z)
  42. default=lambda: datetime.now(timezone.utc).replace(tzinfo=None),
  43. server_default=text("CURRENT_TIMESTAMP(3)"),
  44. )
  45. handled_at: Mapped[datetime | None] = mapped_column(DATETIME(fsp=3), nullable=True)