""" 成本分析模型 存储人工成本分析结果,包括总额、部门拆分、费用科目拆分、环比变化等 """ from typing import Optional from sqlalchemy import ForeignKey, Integer, String, Float, JSON, DateTime from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import BaseModel class CostAnalysis(BaseModel): """ 成本分析模型 记录一次成本分析的计算结果 Attributes: task_id: 关联的对账任务ID company_id: 企业ID total_cost: 人工成本总额 salary_cost: 工资成本 social_security_cost: 社保成本(公司部分) fund_cost: 公积金成本(公司部分) department_breakdown: 部门成本拆分(JSON) expense_breakdown: 费用科目拆分(JSON) month_over_month: 环比变化数据(JSON) ai_summary: AI 生成的分析摘要 """ __tablename__ = "cost_analyses" task_id: Mapped[int] = mapped_column(Integer, ForeignKey("reconciliation_tasks.id"), nullable=False, index=True) company_id: Mapped[int] = mapped_column(Integer, ForeignKey("companies.id"), nullable=False, index=True) total_cost: Mapped[float] = mapped_column(Float, nullable=False, default=0.0, comment="人工成本总额") salary_cost: Mapped[float] = mapped_column(Float, nullable=False, default=0.0, comment="工资成本") social_security_cost: Mapped[float] = mapped_column(Float, nullable=False, default=0.0, comment="社保成本(公司部分)") fund_cost: Mapped[float] = mapped_column(Float, nullable=False, default=0.0, comment="公积金成本(公司部分)") department_breakdown: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True, comment="部门成本拆分") expense_breakdown: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True, comment="费用科目拆分") month_over_month: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True, comment="环比变化数据") ai_summary: Mapped[Optional[str]] = mapped_column(String(2000), nullable=True, comment="AI 生成的分析摘要") # 关系 task = relationship("ReconciliationTask", backref="cost_analyses") company = relationship("Company", backref="cost_analyses") def __repr__(self) -> str: return f""