feat(shipping-notice): 我的待办改版为表格加抽屉并新增已完成明细报表

- 阶段 tab 收敛 4 个可动作阶段,计数改后端全量聚合
- 通知完成拆明细平铺报表(筛选/分页/12 列导出),批量改情境浮动条
- 同步 i18n 四语言与文档
This commit is contained in:
hugo committed 2026-09-17 15:54:53 +08:00
1 parent 18af972d20
commit d8299ebf20
21 files changed
+3315 -761

No files matched your search

@@ -88,6 +88,13 @@ from app.services.shipping_notice.constants import (
RS_SHORTAGE_REVIEW,
)
from app.services.shipping_notice.upload_attempt_service import latest_export_artifact
from app.services.shipping_notice.lifecycle_projection import (
LIFECYCLE_STAGE_BY_GATE,
LIFECYCLE_STAGE_BY_PROCESSING_STAGE,
)
from app.services.shipping_notice.process_gate_contract import (
PROCESS_GATE_REASON_OWNING_GATE,
)
from app.services.shipping_notice.state_machine import (
UserMode,
resolve_task_ui_state,
@@ -132,6 +139,17 @@ _TODO_BUCKET_CATEGORIES: dict[str, frozenset[str]] = {
"done": frozenset({"done"}),
}
# 阶段筛选 tab 仅覆盖四个可动作阶段;「通知完成」属 done 桶终态,不做筛选维度。
_ACTIONABLE_TODO_STAGES: tuple[str, ...] = (
"intake_parse", "order_review", "fulfillment_prep", "freight_upload",
)
# 无 processing_stage 且人工原因不可映射时的 todo_category 粗兜底(与 Gate 映射语义对齐)。
_TODO_STAGE_BY_CATEGORY_FALLBACK: dict[str, str] = {
"review": "order_review",
"exception": "fulfillment_prep",
"manual_upload": "freight_upload",
}
# 尚未挂 OT、仍需用户/系统关注的 Intake 解析状态(进入我的待办)。
_ORPHAN_INTAKE_TODO_PARSE_STATUSES = frozenset({
"created",
@@ -2762,12 +2780,14 @@ def _todo_tasks_base_query(
customer_needle: str,
customer_like: str,
task_no: str | None,
terminal_kind: str | None = None,
):
"""
构造带精确 category CASE 过滤的 Task 待办查询。
核心逻辑:可见性 / 客户过滤后,用 _task_todo_category_sql_expr().in_(allowed);
done 桶另加终态粗筛。pending/processing 不再用领域标量粗筛,避免与 COUNT 分叉。
done 桶另加终态粗筛;terminal_kind 进一步拆分 done 子视图(通知完成 / 驳回作废)。
pending/processing 不再用领域标量粗筛,避免与 COUNT 分叉。
"""
q = _visible_tasks_query_for_todos(user_id, is_admin=is_admin)
workflow_review_status = select(ShippingOrderTask.review_status).where(ShippingOrderTask.id == ShippingNotice.order_task_id).scalar_subquery()
@@ -2817,6 +2837,17 @@ def _todo_tasks_base_query(
),
)
)
# [STEP] 终态子视图过滤:前端「通知完成 / 复核驳回·已作废」子 tab 各取所需;
# 口径与分类 CASE 的 done 分支保持一致(上传成功优先,二者互斥)。
if terminal_kind == "upload_complete":
q = q.filter(upload_success)
elif terminal_kind == "reject_void":
q = q.filter(
db.or_(
workflow_review_status == RS_REJECTED,
ShippingNotice.lifecycle_status.in_(("voided", "archived", "deleted")),
)
)
return q.filter(_task_todo_category_sql_expr().in_(tuple(sorted(allowed))))
@@ -2825,6 +2856,108 @@ def _active_todo_tasks_scope(user_id: int, *, is_admin: bool = False):
return _visible_tasks_query_for_todos(user_id, is_admin=is_admin)
def _classify_todo_lifecycle_stage(
category: str,
*,
pending_human_reason: Any = None,
processing_stage: Any = None,
) -> str | None:
"""
按统一生命周期口径归类待办条目的阶段 tab(仅供计数,不替代 ui_state)。
核心逻辑:与 ``project_lifecycle`` 同优先级——processing_stage 权威 → 人工原因
归属 Gate 兜底 → todo_category 粗兜底;结果若不是四个可动作阶段(如 completed
映射出的 upload_complete)则返回 None,由调用方跳过,避免污染阶段 tab 计数。
Args:
category: 待办分类 CASE 结果(review/exception/manual_upload/in_progress)。
pending_human_reason: OT ``pending_human_reason`` 人工原因码(可空)。
processing_stage: OT ``stage`` 已持久化处理阶段(可空)。
Returns:
四个可动作阶段码之一;不可归入时 None。
"""
stage_key = str(processing_stage or '').strip().lower()
if stage_key in LIFECYCLE_STAGE_BY_PROCESSING_STAGE:
stage = LIFECYCLE_STAGE_BY_PROCESSING_STAGE[stage_key]
else:
gate = PROCESS_GATE_REASON_OWNING_GATE.get(str(pending_human_reason or '').strip())
if gate in LIFECYCLE_STAGE_BY_GATE:
stage = LIFECYCLE_STAGE_BY_GATE[gate]
else:
stage = _TODO_STAGE_BY_CATEGORY_FALLBACK.get(str(category or ''), 'fulfillment_prep')
return stage if stage in _ACTIONABLE_TODO_STAGES else None
def _my_todo_stage_counts(user_id: int, *, is_admin: bool) -> dict[str, int]:
"""
统计待处理桶在四个可动作阶段的分布,供前端阶段筛选 tab 展示全量计数。
核心逻辑:Review Case 走统一生命周期投影(候选有效性裁决 接收解析 vs 订单复核);
孤儿 Intake 恒为接收解析;Notice / OT 分支一次取回「分类 CASE + OT stage +
人工原因」后在 Python 侧归类,避免为计数复制一份 SQL 阶段状态机。
Returns:
{intake_parse, order_review, fulfillment_prep, freight_upload}(含 0 值键)。
"""
counts: dict[str, int] = {code: 0 for code in _ACTIONABLE_TODO_STAGES}
def _bump(stage: str | None) -> None:
if stage in counts:
counts[str(stage)] += 1
# [STEP 1] 开放 Review Case:复用统一投影,禁止另写候选有效性判断。
for case_row in _visible_open_review_cases_query(user_id, is_admin=is_admin).all():
projection = _review_case_lifecycle_projection(case_row)
_bump(str(projection.get('lifecycle_stage') or '').strip() or None)
# [STEP 2] 孤儿 Intake(未建 OT 的解析待办)恒为接收解析。
counts["intake_parse"] += int(
_visible_orphan_intakes_query(user_id, is_admin=is_admin).count()
)
# [STEP 3] Notice 分支:分类 CASE 直接作为列取回,连同 OT stage / 人工原因归类。
category_expr = _task_todo_category_sql_expr()
ot_stage_subquery = select(ShippingOrderTask.stage).where(
ShippingOrderTask.id == ShippingNotice.order_task_id,
).scalar_subquery()
ot_reason_subquery = select(ShippingOrderTask.pending_human_reason).where(
ShippingOrderTask.id == ShippingNotice.order_task_id,
).scalar_subquery()
notice_rows = (
_active_todo_tasks_scope(user_id, is_admin=is_admin)
.filter(category_expr.in_(tuple(sorted(_PENDING_TODO_CATEGORIES - {"intake_parse"}))))
.with_entities(
category_expr.label("category"),
ot_stage_subquery.label("ot_stage"),
ot_reason_subquery.label("ot_reason"),
)
.all()
)
for category, ot_stage, ot_reason in notice_rows:
_bump(_classify_todo_lifecycle_stage(
category, pending_human_reason=ot_reason, processing_stage=ot_stage,
))
# [STEP 4] OT 分支(库存准备尚未建正式 Notice)同理。
ot_category_expr = _order_task_todo_category_sql_expr()
ot_rows = (
_visible_order_tasks_query_for_todos(user_id, is_admin=is_admin)
.filter(ot_category_expr.in_(tuple(sorted(_PENDING_TODO_CATEGORIES - {"intake_parse"}))))
.with_entities(
ot_category_expr.label("category"),
ShippingOrderTask.stage.label("ot_stage"),
ShippingOrderTask.pending_human_reason.label("ot_reason"),
)
.all()
)
for category, ot_stage, ot_reason in ot_rows:
_bump(_classify_todo_lifecycle_stage(
category, pending_human_reason=ot_reason, processing_stage=ot_stage,
))
return counts
def list_my_todos(
user_id: int,
*,
@@ -2835,6 +2968,7 @@ def list_my_todos(
todo_category: str | None = None,
customer: str | None = None,
task_no: str | None = None,
terminal_kind: str | None = None,
) -> dict[str, Any]:
"""
分页列出「我的待办」:建单前 Review Case + 未建 OT Intake + 建单后 Task。
@@ -2851,6 +2985,7 @@ def list_my_todos(
todo_category: 可选细分类覆盖 bucket。
customer: 客户名/代码/来源文件名/Case 号/PO/Intake 号模糊过滤。
task_no: 精确任务号过滤(仅 Task 项;复核/Intake 项忽略)。
terminal_kind: done 桶终态子视图过滤(upload_complete | reject_void);其余 bucket 忽略。
Returns:
{items, page, page_size, total}
@@ -2869,6 +3004,7 @@ def list_my_todos(
customer_like = f"%{customer_needle}%" if customer_needle else ""
include_review_cases = "review" in allowed and not task_no
include_orphan_intakes = "intake_parse" in allowed and not task_no
normalized_terminal_kind = str(terminal_kind or "").strip().lower() or None
# [STEP 1] 多端投影为统一列:kind / entity_id / sort_at / sort_ref。
branches = []
@@ -2915,6 +3051,7 @@ def list_my_todos(
customer_needle=customer_needle,
customer_like=customer_like,
task_no=task_no,
terminal_kind=normalized_terminal_kind,
)
branches.append(
tasks_q.with_entities(
@@ -3053,14 +3190,17 @@ def list_my_todos(
}
def count_my_todos(user_id: int, *, is_admin: bool = False) -> dict[str, int]:
def count_my_todos(user_id: int, *, is_admin: bool = False) -> dict[str, Any]:
"""
统计「我的待办」各 bucket 数量,供导航徽标使用。
统计「我的待办」各 bucket 数量,供导航徽标与阶段筛选 tab 使用。
核心逻辑:开放 Review Case + 未建 OT Intake 计入 pending;Task 用 SQL CASE COUNT。
核心逻辑:开放 Review Case + 未建 OT Intake 计入 pending;Task 用 SQL CASE COUNT;
阶段 tab 计数与 done 终态拆分见 _my_todo_stage_counts / done_counts 组装。
Returns:
{pending, processing, done, total_pending_badge}
{pending, processing, done, total_pending_badge,
stage_counts: {intake_parse, order_review, fulfillment_prep, freight_upload},
done_counts: {upload_complete, reject_void}}
"""
workflow_review_status = select(ShippingOrderTask.review_status).where(ShippingOrderTask.id == ShippingNotice.order_task_id).scalar_subquery()
artifact_exists = exists(select(1).select_from(ShippingExportArtifact).where(ShippingExportArtifact.task_id == ShippingNotice.id, ShippingExportArtifact.status.in_(('ready', 'uploading', 'uploaded'))))
@@ -3077,7 +3217,7 @@ def count_my_todos(user_id: int, *, is_admin: bool = False) -> dict[str, int]:
pending += ot_active.filter(ot_category.in_(tuple(sorted(_PENDING_TODO_CATEGORIES - {"intake_parse"})))).count()
processing = active.filter(category_expr == "in_progress").count()
processing += ot_active.filter(ot_category == "in_progress").count()
done = (
done_base = (
_visible_tasks_query_for_todos(user_id, is_admin=is_admin)
.filter(
db.or_(
@@ -3091,11 +3231,19 @@ def count_my_todos(user_id: int, *, is_admin: bool = False) -> dict[str, int]:
)
)
.filter(category_expr == "done")
.count()
)
done = done_base.count()
# done 拆分:已核验上传成功 vs 复核驳回/作废归档;分类 CASE 中上传成功优先,
# 故二者互斥,reject_void 用差值即无重叠计数。
done_upload = done_base.filter(upload_success).count()
return {
"pending": pending,
"processing": processing,
"done": done,
"total_pending_badge": pending,
"stage_counts": _my_todo_stage_counts(user_id, is_admin=is_admin),
"done_counts": {
"upload_complete": done_upload,
"reject_void": max(0, done - done_upload),
},
}