""" 消息通知数据模型 """ from datetime import datetime from sqlalchemy import Column, BigInteger, String, Text, Integer, DateTime, Boolean, Index, ForeignKey from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.orm import relationship from app.db.base import Base class Notification(Base): """消息通知表""" __tablename__ = "notifications" id = Column(BigInteger, primary_key=True, index=True) recipient_id = Column(BigInteger, ForeignKey("users.id"), nullable=False, index=True, comment="接收人ID") recipient_name = Column(String(100), nullable=False, comment="接收人姓名(冗余)") title = Column(String(200), nullable=False, comment="通知标题") content = Column(Text, nullable=False, comment="通知内容") notification_type = Column(String(20), nullable=False, index=True, comment="通知类型: system/approval/maintenance/allocation等") priority = Column(String(20), default="normal", nullable=False, comment="优先级: low/normal/high/urgent") is_read = Column(Boolean, default=False, nullable=False, index=True, comment="是否已读") read_at = Column(DateTime, nullable=True, comment="已读时间") related_entity_type = Column(String(50), nullable=True, comment="关联实体类型") related_entity_id = Column(BigInteger, nullable=True, comment="关联实体ID") action_url = Column(String(500), nullable=True, comment="操作链接") extra_data = Column(JSONB, nullable=True, comment="额外数据") sent_via_email = Column(Boolean, default=False, nullable=False, comment="是否已发送邮件") sent_via_sms = Column(Boolean, default=False, nullable=False, comment="是否已发送短信") created_at = Column(DateTime, default=datetime.utcnow, nullable=False, index=True, comment="创建时间") updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间") expire_at = Column(DateTime, nullable=True, comment="过期时间") # 关系 recipient = relationship("User", foreign_keys=[recipient_id]) # 索引 __table_args__ = ( Index("idx_notification_recipient", "recipient_id"), Index("idx_notification_read", "is_read"), Index("idx_notification_type", "notification_type"), Index("idx_notification_time", "created_at"), ) def __repr__(self): return f"" class NotificationTemplate(Base): """消息通知模板表""" __tablename__ = "notification_templates" id = Column(BigInteger, primary_key=True, index=True) template_code = Column(String(50), unique=True, nullable=False, comment="模板编码") template_name = Column(String(200), nullable=False, comment="模板名称") notification_type = Column(String(20), nullable=False, comment="通知类型") title_template = Column(String(200), nullable=False, comment="标题模板") content_template = Column(Text, nullable=False, comment="内容模板") variables = Column(JSONB, nullable=True, comment="变量说明") priority = Column(String(20), default="normal", nullable=False, comment="默认优先级") send_email = Column(Boolean, default=False, nullable=False, comment="是否发送邮件") send_sms = Column(Boolean, default=False, nullable=False, comment="是否发送短信") is_active = Column(Boolean, default=True, nullable=False, comment="是否启用") description = Column(Text, nullable=True, comment="模板描述") created_at = Column(DateTime, default=datetime.utcnow, nullable=False) updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) def __repr__(self): return f""