- 从服务器拉取完整代码 - 按框架规范整理项目结构 - 配置 Drone CI 测试环境部署 - 包含后端(FastAPI)、前端(Vue3)、管理端 技术栈: Vue3 + TypeScript + FastAPI + MySQL
107 lines
2.9 KiB
Python
107 lines
2.9 KiB
Python
"""
|
||
站内消息通知模型
|
||
用于记录用户的站内消息通知
|
||
"""
|
||
from datetime import datetime
|
||
from typing import Optional
|
||
from sqlalchemy import String, Text, Integer, Boolean, Index, ForeignKey
|
||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
||
from app.models.base import BaseModel
|
||
|
||
|
||
class Notification(BaseModel):
|
||
"""
|
||
站内消息通知模型
|
||
|
||
用于存储发送给用户的各类站内通知消息,如:
|
||
- 岗位分配通知
|
||
- 课程分配通知
|
||
- 考试提醒通知
|
||
- 系统公告通知
|
||
"""
|
||
__tablename__ = "notifications"
|
||
|
||
# 接收用户ID(外键关联到users表)
|
||
user_id: Mapped[int] = mapped_column(
|
||
Integer,
|
||
ForeignKey("users.id", ondelete="CASCADE"),
|
||
nullable=False,
|
||
index=True,
|
||
comment="接收用户ID"
|
||
)
|
||
|
||
# 通知标题
|
||
title: Mapped[str] = mapped_column(
|
||
String(200),
|
||
nullable=False,
|
||
comment="通知标题"
|
||
)
|
||
|
||
# 通知内容
|
||
content: Mapped[Optional[str]] = mapped_column(
|
||
Text,
|
||
nullable=True,
|
||
comment="通知内容"
|
||
)
|
||
|
||
# 通知类型
|
||
# position_assign: 岗位分配
|
||
# course_assign: 课程分配
|
||
# exam_remind: 考试提醒
|
||
# task_assign: 任务分配
|
||
# system: 系统通知
|
||
type: Mapped[str] = mapped_column(
|
||
String(50),
|
||
nullable=False,
|
||
default="system",
|
||
index=True,
|
||
comment="通知类型:position_assign/course_assign/exam_remind/task_assign/system"
|
||
)
|
||
|
||
# 是否已读
|
||
is_read: Mapped[bool] = mapped_column(
|
||
Boolean,
|
||
default=False,
|
||
nullable=False,
|
||
index=True,
|
||
comment="是否已读"
|
||
)
|
||
|
||
# 关联数据ID(可选,如岗位ID、课程ID等)
|
||
related_id: Mapped[Optional[int]] = mapped_column(
|
||
Integer,
|
||
nullable=True,
|
||
comment="关联数据ID(岗位ID/课程ID等)"
|
||
)
|
||
|
||
# 关联数据类型(可选,如position、course等)
|
||
related_type: Mapped[Optional[str]] = mapped_column(
|
||
String(50),
|
||
nullable=True,
|
||
comment="关联数据类型"
|
||
)
|
||
|
||
# 发送者ID(可选,系统通知时为空)
|
||
sender_id: Mapped[Optional[int]] = mapped_column(
|
||
Integer,
|
||
ForeignKey("users.id", ondelete="SET NULL"),
|
||
nullable=True,
|
||
comment="发送者用户ID"
|
||
)
|
||
|
||
# 关联关系
|
||
user = relationship("User", foreign_keys=[user_id], backref="notifications")
|
||
sender = relationship("User", foreign_keys=[sender_id])
|
||
|
||
# 创建索引以优化查询性能
|
||
__table_args__ = (
|
||
Index('idx_notifications_user_read', 'user_id', 'is_read'),
|
||
Index('idx_notifications_user_created', 'user_id', 'created_at'),
|
||
Index('idx_notifications_type', 'type'),
|
||
)
|
||
|
||
def __repr__(self):
|
||
return f"<Notification(id={self.id}, user_id={self.user_id}, title={self.title}, is_read={self.is_read})>"
|
||
|