Files
syops/backend/app/models/training.py
T

172 lines
10 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。
逻辑:配置页创建或更新任务;员工提交观看与总结时关联本表;提醒服务只扫描已发布周任务。
"""
__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)
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])
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)