97 lines
5.6 KiB
Python
97 lines
5.6 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, 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])
|