""" 对账任务模型 用于管理工资对账任务的生命周期 """ from datetime import datetime from enum import Enum from typing import List, Optional from sqlalchemy import ForeignKey, Integer, String, DateTime, JSON, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import BaseModel class TaskStatus(str, Enum): """任务状态""" CREATED = "CREATED" # 已创建 FILES_UPLOADED = "FILES_UPLOADED" # 文件已上传 PARSING = "PARSING" # 解析中 WAITING_MAPPING_CONFIRM = "WAITING_MAPPING_CONFIRM" # 等待字段映射确认 MAPPING_CONFIRMED = "MAPPING_CONFIRMED" # 字段映射已确认 RECONCILING = "RECONCILING" # 对账中 COMPLETED = "COMPLETED" # 完成 FAILED = "FAILED" # 失败 class ReconciliationTask(BaseModel): """ 对账任务模型 管理一次完整的工资对账流程 """ __tablename__ = "reconciliation_tasks" company_id: Mapped[int] = mapped_column(Integer, ForeignKey("companies.id"), nullable=False, index=True) name: Mapped[str] = mapped_column(String(200), nullable=False, comment="任务名称") period: Mapped[str] = mapped_column(String(20), nullable=False, index=True, comment="对账期间,如 2024-01") status: Mapped[str] = mapped_column(String(30), nullable=False, default=TaskStatus.CREATED.value, index=True) # 文件信息 (JSON 格式存储) file_ids: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True, comment="关联的文件ID") # 字段映射状态 mapping_completed: Mapped[bool] = mapped_column(Integer, nullable=False, default=False) mapping_confirmed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) mapping_confirmed_by: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("users.id"), nullable=True) # 对账结果摘要 total_employees: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) matched_count: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) exception_count: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) # 结果数据 (JSON) reconciliation_result: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) # 错误信息 error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True) # 确认信息 created_by: Mapped[Optional[int]] = mapped_column(Integer, 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) # 关系 company = relationship("Company", back_populates="tasks") creator = relationship("User", foreign_keys=[created_by]) confirm_user = relationship("User", foreign_keys=[mapping_confirmed_by]) def __repr__(self) -> str: return f""