From 0f201b88965e5e87e5a3d1cf5863f374ddbcfd7e Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Wed, 4 Nov 2026 01:33:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor(models):=20=E6=A8=A1=E5=9E=8B=E5=B1=82?= =?UTF-8?q?=E6=AD=BB=E5=AD=97=E6=AE=B5/=E6=AD=BB=E8=A1=A8=E4=B8=8B?= =?UTF-8?q?=E7=BA=BF=EF=BC=88=E5=AE=A1=E8=AE=A1=207b/8=20=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E9=83=A8=E5=88=86=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Period.start_date / end_date:全应用零读取(前端用 first_day/last_day 计算属性), 删模型列 + to_dict 键 + PeriodCreate 字段 + POST /periods 构造参数;老库残留列无害。 - Task.sort_order:只有 ai.py 建任务时写、没有任何排序/展示读它(子任务按 id 排), 删列 + to_dict 键 + ai.py 赋值。Project/Category/Staff 各自的 sort_order 在用,未动。 - CustomProject.currency:外币折算需求从未落地,删列。 - ImportLog.to_dict:无调用方(import_logs 只有写入留痕),删方法;表保留。 - LedgerItem 整表下线:客户项目台账快照只有 import_excel 写、全应用零读; 删模型、scripts/import_excel.py 的《项目明细表》导入段(原位置留一行注释交代)、 constants.LEDGER_CATEGORIES;legacy/ 归档脚本不动;老库 ledger_items 表原地保留。 - README 数据模型图去掉 LedgerItem 行;models.py 头注释同步。 - pytest 93 passed;app.main 导入通过 --- README.md | 1 - app/constants.py | 10 -------- app/models.py | 49 +----------------------------------- app/routers/ai.py | 2 +- app/routers/board.py | 4 --- scripts/import_excel.py | 56 ++++------------------------------------- 6 files changed, 7 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index d486c31..d35c58d 100644 --- a/README.md +++ b/README.md @@ -503,7 +503,6 @@ WeeklyFocus(工作内容, 说明, 录入周→Period, 预计完成时间, 执行 Staff(姓名, 归属小组, 人员归属, 说明, 排序, 归档) # 花名册:所有「负责人 / 团队成员」下拉的唯一来源 Category / Tag / Setting(AI 配置、年度计收目标 custom_annual_targets:{年份}) / ImportLog(AI 整理留痕) -LedgerItem(客户项目台账) # 只剩模型与 Excel 导入解析,界面无入口、接口已下线 # 权限(RBAC) Role(名称, 说明, is_builtin) ── RoleMenu(菜单, 级别 ro|rw) # 授权矩阵:角色 × 菜单 diff --git a/app/constants.py b/app/constants.py index a154a00..5410c2d 100644 --- a/app/constants.py +++ b/app/constants.py @@ -23,16 +23,6 @@ PRIORITIES = [ ("p3", "P3 低", "#6b7280"), ] -# 台账类别(来自《项目明细表》) -LEDGER_CATEGORIES = [ - "新增项目", - "重点项目", - "验收项目", - "交付项目", - "提前交付", - "异常项目", -] - # -------------------------------------------------------------------------- # 权限菜单目录(前后端共用:键是权限标识,顺序即权限管理页里的展示顺序) # rw = 可编辑(含新增/修改/删除/导出写入口) diff --git a/app/models.py b/app/models.py index 7222ef1..b06a969 100644 --- a/app/models.py +++ b/app/models.py @@ -1,7 +1,7 @@ """数据模型。 层级:Period(周期) -> Project(主要项目/工作项) -> Task(子任务) -> Update(执行情况) -辅助:Category(分类) / Tag(标记) / LedgerItem(客户项目台账) / Setting(配置) +辅助:Category(分类) / Tag(标记) / Setting(配置) 权限:Role(角色,一份菜单权限) <- User(用户) ; UserSession(登录会话) """ from __future__ import annotations @@ -202,8 +202,6 @@ class Period(Base): year: Mapped[int] = mapped_column(Integer, nullable=False) month: Mapped[int] = mapped_column(Integer, nullable=False) week: Mapped[int] = mapped_column(Integer, default=0, nullable=False) # 月视图为 0 - start_date: Mapped[str | None] = mapped_column(String(16), nullable=True) - end_date: Mapped[str | None] = mapped_column(String(16), nullable=True) # 排序键:year*10000 + month*100 + week sort_key: Mapped[int] = mapped_column(Integer, nullable=False, index=True) @@ -222,8 +220,6 @@ class Period(Base): "year": self.year, "month": self.month, "week": self.week, - "start_date": self.start_date, - "end_date": self.end_date, "sort_key": self.sort_key, "full_label": self.full_label, } @@ -239,7 +235,6 @@ class Task(Base, TimestampMixin): Integer, ForeignKey("projects.id", ondelete="CASCADE"), nullable=False, index=True ) name: Mapped[str] = mapped_column(String(255), nullable=False) - sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False) project: Mapped[Project] = relationship(back_populates="tasks") updates: Mapped[list["Update"]] = relationship(back_populates="task") @@ -249,7 +244,6 @@ class Task(Base, TimestampMixin): "id": self.id, "project_id": self.project_id, "name": self.name, - "sort_order": self.sort_order, } @@ -352,35 +346,6 @@ class WeeklyFocus(Base, TimestampMixin): } -class LedgerItem(Base, TimestampMixin): - """客户项目台账(来自《项目明细表》:区域 / 金额 / 类别 / 状态说明)。""" - - __tablename__ = "ledger_items" - - id: Mapped[int] = mapped_column(Integer, primary_key=True) - name: Mapped[str] = mapped_column(String(255), nullable=False) - region: Mapped[str | None] = mapped_column(String(64), nullable=True) - amount: Mapped[float | None] = mapped_column(Float, nullable=True) - category: Mapped[str | None] = mapped_column(String(32), nullable=True) - status_note: Mapped[str | None] = mapped_column(Text, nullable=True) - period_label: Mapped[str | None] = mapped_column(String(64), nullable=True) - period_id: Mapped[int | None] = mapped_column( - Integer, ForeignKey("periods.id", ondelete="SET NULL"), nullable=True - ) - - def to_dict(self): - return { - "id": self.id, - "name": self.name, - "region": self.region, - "amount": self.amount, - "category": self.category, - "status_note": self.status_note, - "period_label": self.period_label, - "period_id": self.period_id, - } - - class CustomProject(Base, TimestampMixin): """定制项目(与通用研发项目完全分离)。 @@ -394,7 +359,6 @@ class CustomProject(Base, TimestampMixin): customer: Mapped[str | None] = mapped_column(String(128), nullable=True) # 客户 / 办事处 region: Mapped[str | None] = mapped_column(String(64), nullable=True) # 区域(省/办) amount: Mapped[float | None] = mapped_column(Float, nullable=True) # 合同/订单金额 - currency: Mapped[str] = mapped_column(String(8), default="CNY", nullable=False) stage: Mapped[str] = mapped_column(String(32), default="signed", nullable=False, index=True) progress: Mapped[float] = mapped_column(Float, default=0.0, nullable=False) # 开发进度 % @@ -620,17 +584,6 @@ class ImportLog(Base): model: Mapped[str | None] = mapped_column(String(64), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=now, nullable=False) - def to_dict(self): - return { - "id": self.id, - "project_id": self.project_id, - "period_id": self.period_id, - "mode": self.mode, - "model": self.model, - "raw_input": (self.raw_input or "")[:400], - "created_at": self.created_at.isoformat(timespec="seconds") if self.created_at else None, - } - # ---------------------------------------------------------------------- 权限 # 三层模型:用户 ←(多对多)→ 角色 ←(一对多)→ 菜单授权 diff --git a/app/routers/ai.py b/app/routers/ai.py index dde97f0..9093791 100644 --- a/app/routers/ai.py +++ b/app/routers/ai.py @@ -98,7 +98,7 @@ def ai_commit(payload: AICommitIn, db: Session = Depends(get_db)): .one_or_none() ) if not task: - task = Task(project_id=project.id, name=name, sort_order=idx) + task = Task(project_id=project.id, name=name) db.add(task) db.flush() risk = (t.get("risk") or "").strip() or (risks[0] if idx == 0 and risks else None) diff --git a/app/routers/board.py b/app/routers/board.py index fdc984f..e9117c1 100644 --- a/app/routers/board.py +++ b/app/routers/board.py @@ -21,8 +21,6 @@ class PeriodCreate(BaseModel): year: int | None = None month: int | None = None week: int | None = None - start_date: str | None = None - end_date: str | None = None # 状态严重程度排序(越靠前越需要关注) _SEVERITY = ["blocked", "at_risk", "paused", "not_started", "in_progress", "done"] @@ -157,8 +155,6 @@ def create_period(payload: PeriodCreate, db: Session = Depends(get_db)): year=year, month=month, week=week, - start_date=payload.start_date, - end_date=payload.end_date, sort_key=sort_key, ) db.add(p) diff --git a/scripts/import_excel.py b/scripts/import_excel.py index 50e6108..41c8381 100644 --- a/scripts/import_excel.py +++ b/scripts/import_excel.py @@ -9,7 +9,6 @@ - 「周工作情况总结」:时间(周期) → 工作内容(项目) → 内容说明(子任务) → 完成情况(执行记录) * 三列均为稀疏填充,逐级向下继承(forward fill) * 仅填「完成情况」的行视为上一条记录的续行,追加到同一条执行记录 -- 「项目明细表」:客户项目台账(区域 / 金额 / 类别 / 状态说明) """ from __future__ import annotations @@ -25,12 +24,11 @@ import argparse # noqa: E402 import openpyxl # noqa: E402 from app.ai_parser import _guess_progress, _guess_status # noqa: E402 -from app.constants import LEDGER_CATEGORIES, parse_period_label # noqa: E402 +from app.constants import parse_period_label # noqa: E402 from app.db import SessionLocal, engine # noqa: E402 from app.models import ( # noqa: E402 Base, Category, - LedgerItem, Period, Project, Task, @@ -78,7 +76,7 @@ def main(): db = SessionLocal() if args.reset: - for model in (Update, Task, LedgerItem, Project, Period): + for model in (Update, Task, Project, Period): db.query(model).delete() db.commit() print("[i] 已清空历史业务数据") @@ -235,56 +233,12 @@ def main(): p.archived = True db.commit() - # ---------------------------------------------------------- 项目明细表 - ws2 = wb["项目明细表"] - n_ledger = 0 - all_periods = db.query(Period).all() - period_index = {(p.month, p.week): p for p in all_periods} - - for r in ws2.iter_rows(min_row=1, values_only=True): - cells = list(r) + [None] * (7 - min(7, len(r))) - _, p_label, name, region, amount, category, note = cells[:7] - if not name or not str(name).strip(): - continue - name = str(name).strip() - if name in ("项目", "项目名称"): - continue - try: - amt = float(amount) if isinstance(amount, (int, float)) else None - except (TypeError, ValueError): - amt = None - cat = str(category).strip() if category else None - if cat and cat not in LEDGER_CATEGORIES: - cat = None - label = str(p_label).strip() if p_label else None - parsed = parse_period_label(label, 2025) if label else None - pid = None - if parsed: - pid = period_index.get((parsed[1], parsed[2])) - pid = pid.id if pid else None - if db.query(LedgerItem).filter( - LedgerItem.name == name, - LedgerItem.period_label == label, - LedgerItem.category == cat, - ).one_or_none(): - continue - db.add( - LedgerItem( - name=name, - region=str(region).strip() if region else None, - amount=amt, - category=cat, - status_note=str(note).strip() if note else None, - period_label=label, - period_id=pid, - ) - ) - n_ledger += 1 - db.commit() + # 《项目明细表》sheet 过去逐行快照进 ledger_items,全应用没有任何读取方, + # 2026-11-04 随 LedgerItem 模型一并下线(历史表留在老库里不动)。 print( f"[ok] 导入完成:周期 {len(db.query(Period).all())} 个 / 项目 {len(db.query(Project).all())} 个 / " - f"子任务 {n_task} 个 / 执行记录 {len(db.query(Update).all())} 条 / 台账 {n_ledger} 条" + f"子任务 {n_task} 个 / 执行记录 {len(db.query(Update).all())} 条" ) db.close()