fourcal-pms/backend/app/models/project.py

548 lines
30 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""项目主档与项目成员。
相对老库 ``project`` 表做了三处结构性调整,其余字段尽量同名,方便迁移与对账:
1. **拆掉"编码列 + 描述列"成对冗余**。老表里 ``underwritten_mode`` /
``underwritten_mode_str``、``certainty`` / ``certainty_str`` 等有七八对,两边会漂移,
而且描述是中文写死的。这里只留编码,描述由 :mod:`app.enums` 统一给出。
2. **利润类冗余字段不再当事实来源**。``gross_profit`` 等仍然保留,但明确标注为
"投影",只能由 :mod:`app.service.calc` 重算写入,读取一律走计算快照
(``RISK-BUD-SSOT-001`` 的根治办法)。
3. **审批状态从四个平铺列改成按流程对象存**(``approval_instance``),项目上只留一个
当前阶段与当前待办人,避免"概算已通过但状态列还是待审"这类不同步。
"""
from __future__ import annotations
from datetime import date, datetime
from decimal import Decimal
from sqlalchemy import ForeignKey, Index, Integer, String, Text, UniqueConstraint, text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .base import Base, SoftDeleteMixin, date_col, money, percent, timestamp_col, u_bigint, u_tinyint
class ProjectType:
"""项目类型(老 ``project.type``)。
名字以前只散在两处:接口里拼一遍、前端列表页自己再抄一遍(抄成了
"系统集成/软件开发/运维服务")。现在统一挂 LABELS,由 :mod:`app.dicts`
同步进「系统 → 数据字典」,界面上改名不用再发版。
"""
ENGINEERING = 1 # 工程集成类
EQUIPMENT = 2 # 设备集成类
STRATEGIC = 3 # 战略合作类
LABELS = {ENGINEERING: "工程集成类", EQUIPMENT: "设备集成类", STRATEGIC: "战略合作类"}
class ProjectStage:
"""项目阶段。老系统 ``stage`` 的取值原样保留,历史数据能直接对上。"""
S0_INITIATION = 0 # S0-项目立项
S1_RESEARCH = 1
S2_DESIGN = 2
S3_BUDGET = 3 # S3-预算评估
S4_BID = 4
S5_CONTRACT = 5
S6_DELIVERY = 6
S7_SETTLEMENT = 7
S8_FINAL = 8
L_LOST = 9 # L-项目丢失
#: 老代码与迁移脚本按这个名字写了很久,留个别名,别再为了改名去动调用点
S0_ESTIMATE = 0
LABELS = {
S0_INITIATION: "S0-项目立项", S1_RESEARCH: "S1-项目调研", S2_DESIGN: "S2-方案设计",
S3_BUDGET: "S3-预算评估", S4_BID: "S4-招标期", S5_CONTRACT: "S5-合同期",
S6_DELIVERY: "S6-交付期", S7_SETTLEMENT: "S7-结算期", S8_FINAL: "S8-决算期",
L_LOST: "L-项目丢失",
}
class UnderwrittenMode:
"""垫资模式(老 ``underwritten_mode``)。"""
A = 1 # A类-不垫资(战略合作)
B = 2 # B类-不垫资(背靠背)
C = 3 # C类-垫资(账期覆盖)
D = 4 # D类-垫资(账期不覆盖)
LABELS = {A: "A类-不垫资(战略合作)", B: "B类-不垫资(背靠背)",
C: "C类-垫资(账期覆盖)", D: "D类-垫资(账期不覆盖)"}
class Certainty:
"""项目把握度(老 ``certainty``,立项表上的下拉框)。
取值与名字是从老库 ``project.certainty`` 的实际分布 + 立项表截图对出来的:
库里只有 1/2/3 三种,没见过的编码一概不加。
"""
A = 1
B = 2
C = 3
LABELS = {A: "A:项目成功率80%-100%", B: "B:项目成功率50%-80%", C: "C:项目成功率30%-50%"}
class YesNo:
"""是/否类字段(老库把"没填"存成 0 或 -1,这里原样留着,历史数据才显示得出名字)。"""
UNSET = 0
YES = 1
NO = 2
LEGACY_UNKNOWN = -1
LABELS = {YES: "是", NO: "否", UNSET: "未填", LEGACY_UNKNOWN: "未填"}
class IndustryScene:
"""行业场景应用(老 ``industry_scene``)。
名字逐条对过老库 ``project.industry_scene`` 与 ``industry_scenario`` 的描述列,
两边一致的才收进来;``0`` 是老数据里"只写了描述、没选场景"的那一批。
"""
LABELS = {
1: "信息安全", 2: "政府", 3: "公安", 4: "企业", 5: "教育", 6: "交通", 7: "医疗",
8: "电力能源", 9: "运营商", 10: "金融", 11: "专网", 12: "其他", 13: "互联网", 14: "银行",
}
class SaleStage:
"""公司销售阶段(老 ``sale_stage``)。
老库这一列没有注释,取值 -1..10 是从数据里数出来的;档位名字在老系统的
库里找不到出处,只从立项表截图确认了 S0-商机线索 与 S2-项目报备 两档。
所以这份取值当**可在「系统 → 数据字典」改名的初始值**看待:编码是稳定的
(送审分支与迁移都按编码走),名字由业务定。
"""
UNSET = -1
LEAD = 0 # S0-商机线索
INTENT = 1
REPORTED = 2 # S2-项目报备
PROPOSAL = 3
NEGOTIATION = 4
WON = 5
SIGNED = 6
DELIVERY = 7
RECEIPT = 8
CLOSED = 9
OTHER = 10
LABELS = {
UNSET: "未填写", LEAD: "S0-商机线索", INTENT: "S1-意向沟通", REPORTED: "S2-项目报备",
PROPOSAL: "S3-方案报价", NEGOTIATION: "S4-商务谈判", WON: "S5-中标",
SIGNED: "S6-合同签订", DELIVERY: "S7-项目交付", RECEIPT: "S8-回款完结",
CLOSED: "S9-项目关闭", OTHER: "S10-其他",
}
class ProjectSource:
"""项目从哪来。清理脚本靠这一列决定"演示/测试数据删、迁移数据留"。
没有这列之前,清库脚本只能整表清 ``project``,于是每清一次演示数据就得把
``migrate_legacy.py`` 导进来的八百多个真实项目一起冲掉——那是最贵的一种误删。
"""
LOCAL = "local" # 界面上建的(含 pytest 造的测试数据)
LEGACY = "legacy" # scripts/migrate_legacy.py 从老四算导入
LABELS = {LOCAL: "本系统创建", LEGACY: "老系统迁移"}
class Project(Base, SoftDeleteMixin):
"""项目主档。"""
__tablename__ = "project"
__table_args__ = (
Index("idx_project_creator", "creator_id"),
Index("idx_project_org", "org_unit_id"),
Index("idx_project_stage", "stage"),
Index("idx_project_no", "project_no"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
project_no: Mapped[str] = mapped_column(String(64), default="", nullable=False, comment="项目编号")
name: Mapped[str] = mapped_column(Text, nullable=False, comment="项目名称")
other_name: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="项目简称")
type: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="项目类型")
# --- 归属:数据权限的主要依据 ---------------------------------------------
creator_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("accounts.id", ondelete="RESTRICT"), nullable=False
)
creator_name: Mapped[str] = mapped_column(String(100), default="", nullable=False)
# 归属部门:老系统取"创建者的部门",但人员会调动,历史项目跟着人跑掉。
# 这里在创建时固化,之后只在显式"项目移交"时改动,数据权限按它算。
org_unit_id: Mapped[int | None] = mapped_column(
u_bigint(), ForeignKey("org_unit.id", ondelete="SET NULL"), nullable=True
)
org_unit_name: Mapped[str] = mapped_column(String(255), default="", nullable=False)
principal: Mapped[str] = mapped_column(String(100), default="", nullable=False, comment="项目负责人姓名")
principal_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True, comment="项目负责人账号")
# --- 生命周期 -------------------------------------------------------------
stage: Mapped[int] = mapped_column(Integer, default=ProjectStage.S0_ESTIMATE, nullable=False)
stage_remark: Mapped[str] = mapped_column(Text, default="", nullable=False, comment="当前进度描述")
next_plan: Mapped[str] = mapped_column(Text, default="", nullable=False, comment="下一步计划")
# --- 客户与合同要素 -------------------------------------------------------
customer: Mapped[str] = mapped_column(Text, default="", nullable=False)
terminal_customer: Mapped[str] = mapped_column(Text, default="", nullable=False)
plan_start: Mapped[date | None] = mapped_column(
date_col(), nullable=True, comment="计划开始(精确到月)")
plan_end: Mapped[date | None] = mapped_column(
date_col(), nullable=True, comment="计划结束(精确到月)")
contract_time: Mapped[datetime | None] = mapped_column(timestamp_col(), nullable=True)
bids_time: Mapped[datetime | None] = mapped_column(timestamp_col(), nullable=True)
underwritten_mode: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="垫资模式")
cooperate_type: Mapped[int | None] = mapped_column(Integer, nullable=True)
collaborator: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="合作对象")
collaborator_attachment: Mapped[str] = mapped_column(String(500), default="", nullable=False)
certainty: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="项目把握度")
is_second: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="是否二次合作")
is_huizhi_bid: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="是否汇智中标")
sign_type: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="签单方式")
resolve_plan: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="解决方案")
industry_scene: Mapped[int] = mapped_column(Integer, default=0, nullable=False, comment="行业场景")
industry_scene_desc: Mapped[str] = mapped_column(String(255), default="", nullable=False)
# 公司销售阶段:老库 project.sale_stage 同名列。从前迁移把它丢了,
# 立项表上这一栏是必填的,再丢一次就得让人重新录八百个项目。
sale_stage: Mapped[int] = mapped_column(Integer, default=SaleStage.UNSET, nullable=False,
comment="公司销售阶段")
source: Mapped[str] = mapped_column(String(16), default=ProjectSource.LOCAL, nullable=False,
comment="数据来源 local/legacy")
main_contract_collection_terms: Mapped[str] = mapped_column(Text, default="", nullable=False)
main_contract_resolve_plan: Mapped[str] = mapped_column(Text, default="", nullable=False)
calculation_collection: Mapped[str] = mapped_column(Text, default="", nullable=False, comment="计收计划")
value_risk: Mapped[str] = mapped_column(Text, default="", nullable=False, comment="价值及风险")
# --- 产品金额构成 ---------------------------------------------------------
huazhi_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
huizhi_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
h3c_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
other_product_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
# --- 计算投影:只由 calc 服务写,禁止业务代码直接改 -----------------------
contract_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
gross_profit_a: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
gross_profit_a_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False)
gross_profit: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
gross_profit_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False)
contribution_profit: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
contribution_profit_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False)
advance_peak_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
advance_interest_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False)
advance_interest_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False,
comment="垫资年利率")
# 口径见 RISK-PLAN-STAT-001:老系统字段名叫"峰值金额"但算的是各月垫资之和。
# 这里明确改名并保留旧列语义,新列 give_peak_month/give_peak_value 给真实峰值。
advance_plan_total: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False,
comment="各月垫资计划之和(老'峰值金额'的真实含义)")
advance_peak_month: Mapped[str] = mapped_column(String(16), default="", nullable=False,
comment="单月垫资最大值所在月份")
advance_peak_value: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False,
comment="单月垫资最大值")
calc_version: Mapped[str] = mapped_column(String(32), default="", nullable=False, comment="投影所属计算规则版本")
calc_snapshot_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True, comment="投影来源快照")
calc_at: Mapped[datetime | None] = mapped_column(timestamp_col(), nullable=True)
# --- 软删除留痕 -----------------------------------------------------------
# 光有一个 is_deleted 位,回收站就答不出"谁在什么时候、因为什么删的"。项目是财务
# 口径的载体,误删之后总得有人把它捞回来并交代来历,所以这几列跟着标记一起写;
# 恢复时清空,不拿上一次的删除信息冒充这一次的。
deleted_at: Mapped[datetime | None] = mapped_column(timestamp_col(), nullable=True, comment="删除时间")
deleted_by_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True, comment="删除人账号")
deleted_by_name: Mapped[str] = mapped_column(String(100), default="", nullable=False, comment="删除人姓名")
delete_reason: Mapped[str] = mapped_column(String(500), default="", nullable=False, comment="删除原因")
created_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False)
updated_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, onupdate=datetime.now,
server_default=text("CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"), nullable=False)
members: Mapped[list["ProjectMember"]] = relationship(
back_populates="project", cascade="all, delete-orphan", lazy="selectin"
)
@property
def stage_label(self) -> str:
return ProjectStage.LABELS.get(self.stage, "未知")
class ProjectMemberRole:
"""项目成员种类。数据权限的"我参与的"就是按这张表判定。"""
OWNER = "owner" # 项目负责人
PARTICIPANT = "participant"
COLLABORATOR = "collaborator" # 协作/支撑
VIEWER = "viewer" # 单独授权可见的人
ALL = (OWNER, PARTICIPANT, COLLABORATOR, VIEWER)
LABELS = {OWNER: "项目负责人", PARTICIPANT: "参与人",
COLLABORATOR: "协作支撑", VIEWER: "仅可见"}
class ProjectMember(Base):
"""项目成员(含老 ``project_visible`` 里 type=2 的"按人员授权可见")。"""
__tablename__ = "project_member"
__table_args__ = (
UniqueConstraint("project_id", "account_id", name="uk_project_member"),
Index("idx_project_member_account", "account_id", "member_role"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
project_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("project.id", ondelete="CASCADE"), nullable=False, index=True
)
account_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False
)
account_name: Mapped[str] = mapped_column(String(100), default="", nullable=False)
member_role: Mapped[str] = mapped_column(
String(24), default=ProjectMemberRole.PARTICIPANT, nullable=False)
created_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False)
project: Mapped["Project"] = relationship(back_populates="members")
class ProjectVisibleRole(Base):
"""按角色授权项目可见(老 ``project_visible`` type=1)。
保留它是因为业务上确有"整个财务部都能看所有项目"这种整批授权;日常更推荐直接在
数据权限里配策略,这里留着兼容老数据的逐项目授权。
"""
__tablename__ = "project_visible_role"
__table_args__ = (
UniqueConstraint("project_id", "role_id", name="uk_project_visible_role"),
Index("idx_project_visible_role", "role_id"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
project_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("project.id", ondelete="CASCADE"), nullable=False, index=True)
role_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("perm_role.id", ondelete="CASCADE"), nullable=False)
class ProjectStageLog(Base):
"""阶段变更流水,项目详情"基本信息"页的时间线用它。"""
__tablename__ = "project_stage_log"
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
project_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("project.id", ondelete="CASCADE"), nullable=False, index=True)
stage: Mapped[int] = mapped_column(Integer, nullable=False)
stage_label: Mapped[str] = mapped_column(String(64), default="", nullable=False)
account_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True)
account_name: Mapped[str] = mapped_column(String(100), default="", nullable=False)
remark: Mapped[str] = mapped_column(String(2000), default="", nullable=False)
created_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False)
class Attachment(Base, SoftDeleteMixin):
"""附件。老系统把附件路径平铺在业务表上(cost_detail_attr 等),一列一路径,
改一次留不住历史;这里统一成一张表,业务侧用 (object_type, object_id) 挂。"""
__tablename__ = "attachment"
__table_args__ = (
Index("idx_attachment_object", "object_type", "object_id"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
object_type: Mapped[str] = mapped_column(String(64), nullable=False,
comment="estimate/budget/contract/approval…")
object_id: Mapped[int] = mapped_column(u_bigint(), default=0, server_default="0", nullable=False)
field: Mapped[str] = mapped_column(String(64), default="", nullable=False, comment="挂在哪个字段上")
file_name: Mapped[str] = mapped_column(String(500), nullable=False)
stored_name: Mapped[str] = mapped_column(String(500), default="", nullable=False)
url: Mapped[str] = mapped_column(String(1000), default="", nullable=False)
size: Mapped[int] = mapped_column(u_bigint(), default=0, server_default="0", nullable=False)
ext: Mapped[str] = mapped_column(String(16), default="", nullable=False)
uploader_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True)
uploader_name: Mapped[str] = mapped_column(String(100), default="", nullable=False)
created_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False)
class SystemConfig(Base):
"""业务参数(利润率阈值、固定项目管理费用项等)。
老系统 ``sys_config`` 只有 code/name/value 三列且没有类型,读出来全是字符串。这里
加 value_type 与分组,界面上按组渲染表单,代码里按类型取值。
"""
__tablename__ = "system_config"
__table_args__ = (
UniqueConstraint("code", name="uk_system_config_code"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
code: Mapped[str] = mapped_column(String(128), nullable=False)
name: Mapped[str] = mapped_column(String(128), nullable=False)
value: Mapped[str] = mapped_column(Text, default="", nullable=False)
value_type: Mapped[str] = mapped_column(String(16), default="string", nullable=False,
comment="string/number/bool/json")
group: Mapped[str] = mapped_column(String(64), default="common", nullable=False, index=True)
editable: Mapped[bool] = mapped_column(u_tinyint(), default=True, server_default="1", nullable=False)
remark: Mapped[str] = mapped_column(String(500), default="", nullable=False)
updated_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, onupdate=datetime.now,
server_default=text("CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"), nullable=False)
class MilestoneStatus:
"""里程碑状态。``overdue`` 不入库,读的时候按"计划日期已过且未完成"现算,
否则每天凌晨没人跑任务,逾期就永远显示不出来。"""
PENDING = "pending"
DONE = "done"
OVERDUE = "overdue"
LABELS = {PENDING: "未完成", DONE: "已完成", OVERDUE: "已逾期"}
class ProjectMilestone(Base, SoftDeleteMixin):
"""项目里程碑:立项批准后要盯的那几个节点(招标、签约、到货、初验…)。
``receive_point`` 与预算收款计划的"收款节点"同名,是为了让"这个里程碑完成了该收钱"
这件事有个对得上的锚点;先不做自动关联,把字段留对,比事后按名字猜强。
"""
__tablename__ = "project_milestone"
__table_args__ = (
Index("idx_milestone_project", "project_id", "plan_date"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
project_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("project.id", ondelete="CASCADE"), nullable=False, index=True)
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="里程碑名称")
stage: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="挂在哪个项目阶段")
plan_date: Mapped[date | None] = mapped_column(date_col(), nullable=True, comment="计划完成日期")
actual_date: Mapped[date | None] = mapped_column(date_col(), nullable=True, comment="实际完成日期")
owner_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True)
owner_name: Mapped[str] = mapped_column(String(100), default="", nullable=False)
amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False,
comment="该节点对应的合同金额")
receive_point: Mapped[str] = mapped_column(String(255), default="", nullable=False,
comment="对应预算收款计划的收款节点")
status: Mapped[str] = mapped_column(String(16), default=MilestoneStatus.PENDING, nullable=False)
remark: Mapped[str] = mapped_column(String(1000), default="", nullable=False)
sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False)
created_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False)
updated_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, onupdate=datetime.now,
server_default=text("CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"), nullable=False)
class WbsTaskStatus:
"""WBS 任务状态。"""
NOT_STARTED = "not_started"
IN_PROGRESS = "in_progress"
DONE = "done"
BLOCKED = "blocked"
LABELS = {NOT_STARTED: "未开始", IN_PROGRESS: "进行中", DONE: "已完成", BLOCKED: "受阻"}
class WbsTask(Base, SoftDeleteMixin):
"""WBS 分解结构:一棵挂在项目上的任务树。
用 ``parent_id`` + ``path`` 两条腿:``parent_id`` 给外键与移动子树用,
``path``(``/1/8/19/``)让"取整棵子树"和"按层级排序"退化成一次 LIKE 与一次 ORDER BY,
不用在应用里递归。``rollup`` 读时算:进度 = 子任务按权重加权,
存一份就要负责把它算对,那是第三份会漂移的进度。
"""
__tablename__ = "wbs_task"
__table_args__ = (
Index("idx_wbs_task_project", "project_id", "path"),
Index("idx_wbs_task_parent", "parent_id"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
project_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("project.id", ondelete="CASCADE"), nullable=False, index=True)
parent_id: Mapped[int | None] = mapped_column(
u_bigint(), ForeignKey("wbs_task.id", ondelete="CASCADE"), nullable=True)
path: Mapped[str] = mapped_column(String(500), default="/", nullable=False, comment="物化路径")
level: Mapped[int] = mapped_column(Integer, default=1, server_default="1", nullable=False)
code: Mapped[str] = mapped_column(String(32), default="", nullable=False, comment="WBS 编码 1.2.3")
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="任务名称")
milestone_id: Mapped[int | None] = mapped_column(
u_bigint(), ForeignKey("project_milestone.id", ondelete="SET NULL"), nullable=True,
comment="归属的里程碑")
owner_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True)
owner_name: Mapped[str] = mapped_column(String(100), default="", nullable=False)
plan_start: Mapped[date | None] = mapped_column(date_col(), nullable=True)
plan_end: Mapped[date | None] = mapped_column(date_col(), nullable=True)
actual_start: Mapped[date | None] = mapped_column(date_col(), nullable=True)
actual_end: Mapped[date | None] = mapped_column(date_col(), nullable=True)
weight: Mapped[Decimal] = mapped_column(money(10, 2), default=Decimal("1"), nullable=False,
comment="父子进度加权的权重")
progress: Mapped[int] = mapped_column(Integer, default=0, server_default="0", nullable=False,
comment="自身进度 0-100")
status: Mapped[str] = mapped_column(String(16), default=WbsTaskStatus.NOT_STARTED, nullable=False)
budget_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False,
comment="任务预算(预算科目编制的落点)")
actual_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False,
comment="任务实际发生(实际成本归集的落点)")
remark: Mapped[str] = mapped_column(String(1000), default="", nullable=False)
sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False)
created_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False)
updated_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, onupdate=datetime.now,
server_default=text("CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"), nullable=False)
class ProjectStageEvent(Base):
"""阶段内的检查项/交付物清单(阶段推进时的"这几件事做完了吗")。
只做"记录",不做工作流:真要卡流程应该走审批引擎,别在阶段表里再长一套状态机。
"""
__tablename__ = "project_stage_item"
__table_args__ = (
Index("idx_stage_item_project", "project_id", "stage"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
project_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("project.id", ondelete="CASCADE"), nullable=False, index=True)
stage: Mapped[int] = mapped_column(Integer, nullable=False, comment="所属项目阶段")
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="检查项/交付物")
done: Mapped[bool] = mapped_column(u_tinyint(), default=False, server_default="0", nullable=False)
owner_name: Mapped[str] = mapped_column(String(100), default="", nullable=False)
due_date: Mapped[date | None] = mapped_column(date_col(), nullable=True)
remark: Mapped[str] = mapped_column(String(1000), default="", nullable=False)
sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False)
created_at: Mapped[datetime] = mapped_column(
timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False)