Files
syops/backend/app/models/training.py
runny 7927556ee2 feat(training): 新增精益培训周任务视频推送与定向提醒闭环
- 后端:新增周任务视频推送服务与定时任务,新增模型迁移 fb26bc033d27
- 配置:WECOM_NOTIFY_EVENTS 追加 training_lean_video_push 事件(dev/test/prod/shared 同步)
- 管理端:培训配置页新增定向目标弹窗 LeanTargetModal
- 工作台:完成看板提醒按钮按 sent/failed 实发统计如实提示(部分成功/失败/未发送),避免企微发送失败误报成功
- 文档:更新功能清单与技术架构,新增精益管理周任务规则手册;四语言 i18n 同步补齐
2026-09-10 21:57:31 +08:00

207 lines
13 KiB
Python

"""
backend/app/models/training.py
用途:定义员工培训记录模型,承载入职培训/安全培训的进度、考试结果与 HR 统计来源。
依赖:models.base.Base、SQLAlchemy 映射、users 主表外键。
逻辑:每个用户在每种培训类型下维护一条当前记录;P0 阶段先为安全培训补齐三级状态、考试模式和版本号持久化。
"""
from __future__ import annotations
from datetime import date, datetime
from typing import Optional
from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Integer, JSON, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base
class TrainingRecord(Base):
"""培训记录。"""
__tablename__ = "training_records"
__table_args__ = (UniqueConstraint("user_id", "training_type", name="uq_user_training_type"),)
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True, nullable=False)
training_type: Mapped[str] = mapped_column(String(50), nullable=False, default="onboarding", index=True)
training_version: Mapped[str] = mapped_column(String(64), nullable=False, default="onboarding-v1", index=True)
exam_mode: Mapped[str] = mapped_column(String(20), nullable=False, default="unified")
safety_stage: Mapped[Optional[str]] = mapped_column(String(20), nullable=True, index=True)
safety_progress: Mapped[dict | None] = mapped_column(JSON, nullable=True)
employee_id: Mapped[Optional[str]] = mapped_column(String(50), nullable=True, index=True)
name: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, index=True)
department: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, index=True)
join_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True, index=True)
stage: Mapped[str] = mapped_column(String(20), nullable=False, default="video", index=True)
current_question_index: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
score: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
passed: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, index=True)
attempt_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
answer_details: Mapped[list[dict] | None] = mapped_column(JSON, nullable=True)
started_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
completion_time: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True, index=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, index=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime,
nullable=False,
default=datetime.utcnow,
onupdate=datetime.utcnow,
index=True,
)
user = relationship("User", back_populates="training_records")
class TrainingConfig(Base):
__tablename__ = "training_configs"
id: Mapped[int] = mapped_column(primary_key=True)
training_type: Mapped[str] = mapped_column(String(50), nullable=False, index=True)
name: Mapped[str] = mapped_column(String(100), nullable=False)
version: Mapped[str] = mapped_column(String(64), nullable=False)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="draft", index=True)
pass_score: Mapped[int] = mapped_column(Integer, nullable=False, default=90)
settings: Mapped[dict | None] = mapped_column(JSON, nullable=True)
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True)
updated_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True)
published_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
resources: Mapped[list["TrainingConfigResource"]] = relationship(
"TrainingConfigResource", back_populates="config", cascade="all, delete-orphan", order_by="TrainingConfigResource.sort_order"
)
class TrainingConfigResource(Base):
__tablename__ = "training_config_resources"
id: Mapped[int] = mapped_column(primary_key=True)
config_id: Mapped[int] = mapped_column(ForeignKey("training_configs.id", ondelete="CASCADE"), nullable=False, index=True)
resource_type: Mapped[str] = mapped_column(String(32), nullable=False)
training_level: Mapped[Optional[str]] = mapped_column(String(20), nullable=True, index=True)
agent_guide_id: Mapped[Optional[int]] = mapped_column(ForeignKey("agent_guides.id"), nullable=True)
kb_document_version_id: Mapped[Optional[int]] = mapped_column(ForeignKey("kb_document_versions.id"), nullable=True)
inline_questions: Mapped[list | None] = mapped_column(JSON, nullable=True)
title: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
required_duration: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
config: Mapped[TrainingConfig] = relationship("TrainingConfig", back_populates="resources")
agent_guide = relationship("AgentGuide", foreign_keys=[agent_guide_id])
kb_document_version = relationship("KbDocumentVersion", foreign_keys=[kb_document_version_id])
class LeanTrainingWeeklyTask(Base):
"""
精益管理周度培训任务。
用途:冻结每个自然周由 HR 指定的培训视频集合和受训对象,作为员工完成状态、视频推送与提醒扫描的统一事实源。
依赖:TrainingConfigResource、TrainingConfig、User。
逻辑:配置页创建或更新任务并设定每周推送数量;员工提交观看与总结时关联本表;
视频推送服务按 pushed_video_count 游标每周推进 weekly_count 个,提醒服务只扫描已发布周任务。
"""
__tablename__ = "lean_training_weekly_tasks"
__table_args__ = (UniqueConstraint("week_key", name="uq_lean_training_week_key"),)
id: Mapped[int] = mapped_column(primary_key=True)
config_id: Mapped[int] = mapped_column(ForeignKey("training_configs.id"), nullable=False, index=True)
week_key: Mapped[str] = mapped_column(String(10), nullable=False, index=True)
due_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, index=True)
video_resource_ids: Mapped[list] = mapped_column(JSON, nullable=False)
weekly_count: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default="1")
pushed_video_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
target_roles: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
target_user_ids: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="published", index=True)
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
config: Mapped[TrainingConfig] = relationship("TrainingConfig", foreign_keys=[config_id])
push_logs: Mapped[list["LeanTrainingVideoPushLog"]] = relationship(
"LeanTrainingVideoPushLog", back_populates="task", cascade="all, delete-orphan", order_by="LeanTrainingVideoPushLog.id.desc()"
)
class LeanTrainingVideoPushLog(Base):
"""
精益管理视频推送日志。
用途:记录每一轮(每周一排程或 HR 手动)按每周任务数量推送视频的执行结果与失败原因,供管理端展示报错日志。
依赖:LeanTrainingWeeklyTask、User。
逻辑:推送服务每执行一轮写入一行;推送游标推进后天然幂等,重复触发不会重复推送同一批视频;
failed_count>0 时 error_message 保存按用户聚合的失败明细。
"""
__tablename__ = "lean_training_video_push_logs"
id: Mapped[int] = mapped_column(primary_key=True)
weekly_task_id: Mapped[int] = mapped_column(ForeignKey("lean_training_weekly_tasks.id"), nullable=False, index=True)
trigger: Mapped[str] = mapped_column(String(20), nullable=False, default="schedule", index=True)
pushed_resource_ids: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
pushed_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
recipient_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
sent_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
skipped_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
failed_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="sent", index=True)
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, index=True)
task: Mapped[LeanTrainingWeeklyTask] = relationship("LeanTrainingWeeklyTask", back_populates="push_logs")
class LeanTrainingCompletion(Base):
"""
精益管理周度学习完成事实。
用途:保存员工对周度已推送视频的观看时间及不少于 50 字的学习感悟。
依赖:LeanTrainingWeeklyTask、User。
逻辑:员工只能写入本人的记录;服务端依据视频集合和总结长度判定完成,管理看板与提醒任务读取该事实。
"""
__tablename__ = "lean_training_completions"
__table_args__ = (UniqueConstraint("weekly_task_id", "user_id", name="uq_lean_training_task_user"),)
id: Mapped[int] = mapped_column(primary_key=True)
weekly_task_id: Mapped[int] = mapped_column(ForeignKey("lean_training_weekly_tasks.id"), nullable=False, index=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False, index=True)
watched_at_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
summary_text: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
summary_char_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="in_progress", index=True)
completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True, index=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
class LeanTrainingReminder(Base):
"""
精益管理提醒发送审计。
用途:对指派、逾期轮次和班组长定向提醒去重并保留发送结果。
依赖:LeanTrainingWeeklyTask、User、企微通知审计 trace_id。
逻辑:扫描服务先写 pending 行,再调用企微服务更新最终状态,数据库唯一约束防止重复排程产生重复消息。
"""
__tablename__ = "lean_training_reminders"
__table_args__ = (UniqueConstraint("weekly_task_id", "recipient_user_id", "event_key", name="uq_lean_training_reminder_event"),)
id: Mapped[int] = mapped_column(primary_key=True)
weekly_task_id: Mapped[int] = mapped_column(ForeignKey("lean_training_weekly_tasks.id"), nullable=False, index=True)
recipient_user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False, index=True)
event_key: Mapped[str] = mapped_column(String(64), nullable=False)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending", index=True)
trace_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True)
error_message: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
sent_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)