feat(安全培训): 新增员工安全培训相关功能

This commit is contained in:
runny committed 2026-08-12 11:56:35 +08:00
1 parent cbc3cb959f
commit 8e5c57790c
50 files changed
+3769 -87

No files matched your search

+54 -6
View File
@@ -1,9 +1,9 @@
"""
backend/app/models/training.py
用途:定义新人入职培训记录模型,承载学员当前培训进度、成绩结算与 HR 统计来源。
用途:定义员工培训记录模型,承载入职培训/安全培训的进度、考试结果与 HR 统计来源。
依赖:models.base.Base、SQLAlchemy 映射、users 主表外键。
逻辑:每个用户维护一条当前培训记录;前端学习流与管理端统计都基于该表读写。
逻辑:每个用户在每种培训类型下维护一条当前记录;P0 阶段先为安全培训补齐三级状态、考试模式和版本号持久化。
"""
from __future__ import annotations
@@ -11,19 +11,25 @@ from __future__ import annotations
from datetime import date, datetime
from typing import Optional
from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Integer, JSON, String
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"), unique=True, index=True, nullable=False)
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)
@@ -45,4 +51,46 @@ class TrainingRecord(Base):
index=True,
)
user = relationship("User", back_populates="training_record")
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])