feat(shipping-notice): 落地字段资产治理与唯一 Schema,自动建单随版本冻结
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b7307fc573
commit
3a1dbf7ce5
144 files changed
+15167
-4480
No files matched your search
@@ -1,10 +1,11 @@
|
||||
"""
|
||||
services/shipping_notice/ui_state.py
|
||||
|
||||
用途:发货通知任务/批次 UI 状态读模型,作为 Chat 按钮、步骤条与工作台的统一状态来源。
|
||||
用途:发货通知任务/批次 UI 状态读模型,作为通知摘要、人工复核与任务工作台的统一状态来源。
|
||||
依赖:models.ShippingTask/ShippingBatch、state_machine、query_service、constants。
|
||||
逻辑:从 ShippingTask DB 字段经状态机解析 checkpoint/action_buttons,再补充七步流程与用户可读摘要;
|
||||
文件解析阶段额外汇总 summary.issues(解析复核 / RSV / 库存源门禁 + parse warnings)。
|
||||
逻辑:从 ShippingTask DB 字段经状态机解析 checkpoint/checkpoint_kind/action_buttons,
|
||||
再补充七步流程与可解释 summary(当前待办、暂停原因、下一自动步骤、操作入口);
|
||||
批次 UI 仅作会话上传集合摘要,不作为履约写操作面。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -14,6 +15,8 @@ from typing import Any
|
||||
from app.models import ShippingBatch, ShippingTask
|
||||
from app.services.shipping_notice.constants import (
|
||||
CP_COMPLETED,
|
||||
CP_CUSTOMER_REVIEW,
|
||||
CP_EXPORT_GENERATED,
|
||||
CP_EXPORT_READY,
|
||||
CP_INVENTORY_PENDING,
|
||||
CP_INVENTORY_SOURCE_REVIEW,
|
||||
@@ -47,6 +50,7 @@ from app.services.shipping_notice.query_service import (
|
||||
serialize_task_summary,
|
||||
)
|
||||
from app.services.shipping_notice.state_machine import (
|
||||
CheckpointKind,
|
||||
inventory_source_action_buttons,
|
||||
resolve_task_ui_state,
|
||||
)
|
||||
@@ -90,6 +94,38 @@ _BLOCKED_CHECKPOINTS = {
|
||||
CP_UPLOAD_REQ,
|
||||
CP_UPLOAD_FAILED,
|
||||
CP_UPLOAD_RESULT_UNKNOWN,
|
||||
CP_CUSTOMER_REVIEW,
|
||||
CP_EXPORT_READY,
|
||||
CP_EXPORT_GENERATED,
|
||||
}
|
||||
|
||||
_REVIEW_ENTRY_CHECKPOINTS = {
|
||||
CP_PARSE_REVIEW,
|
||||
CP_RSV_REQ,
|
||||
CP_NOTICE_TEXT_EXTRACTION_FAILED,
|
||||
}
|
||||
|
||||
_NEXT_AUTO_STEP_BY_CHECKPOINT: dict[str, str] = {
|
||||
CP_INVENTORY_PENDING: "ensure_inventory",
|
||||
CP_ORDER_FREEZE: "freeze_order",
|
||||
CP_READY_MATCH: "match_inventory",
|
||||
CP_READY_EXPORT: "generate_excel",
|
||||
}
|
||||
|
||||
_PAUSE_REASON_BY_CHECKPOINT: dict[str, tuple[str, str]] = {
|
||||
CP_PARSE_REVIEW: ("parse_review_required", "解析结果需要人工确认"),
|
||||
CP_RSV_REQ: ("rsv_required", "缺少后续匹配必需字段"),
|
||||
CP_CUSTOMER_REVIEW: ("customer_ambiguous", "客户未唯一确认"),
|
||||
CP_UPLOAD_REQ: ("inventory_unavailable", "需要提供库存数据或重试库存获取"),
|
||||
CP_INVENTORY_SOURCE_REVIEW: ("inventory_source_unresolved", "库存源尚未唯一确定"),
|
||||
CP_ORDER_FREEZE: ("order_freeze_pending", "订单待冻结后进入库存履约"),
|
||||
RS_SHORTAGE_REVIEW: ("shortage", "库存存在缺口"),
|
||||
CP_NOTICE_TEXT_EXTRACTION_FAILED: ("notice_text_extraction_failed", "通知文本解析失败"),
|
||||
CP_UPLOAD_FAILED: ("upload_failed", "货代上传失败"),
|
||||
CP_UPLOAD_RESULT_UNKNOWN: ("upload_result_unknown", "货代上传结果未知"),
|
||||
CP_WAITING_UPLOAD: ("waiting_upload", "等待货代上传结果"),
|
||||
CP_EXPORT_READY: ("wait_manual_upload", "Excel 已生成,待人工上传并提交凭据"),
|
||||
CP_EXPORT_GENERATED: ("wait_manual_upload", "Excel 已生成,待人工上传并提交凭据"),
|
||||
}
|
||||
|
||||
|
||||
@@ -276,20 +312,67 @@ def _collect_parse_stage_issues(task: ShippingTask, checkpoint: str | None) -> l
|
||||
return issues
|
||||
|
||||
|
||||
def _build_task_summary(task: ShippingTask, checkpoint: str | None) -> dict[str, Any]:
|
||||
"""输出用户可读任务摘要,供 Chat 文案与工作台卡片复用。"""
|
||||
def _entry_surface_for(checkpoint: str | None, kind: CheckpointKind) -> str:
|
||||
"""
|
||||
解析当前操作应进入的工作台类型。
|
||||
|
||||
Returns:
|
||||
review_workbench | task_workbench | none
|
||||
"""
|
||||
if kind == CheckpointKind.COMPLETE:
|
||||
return "none"
|
||||
if str(checkpoint or "") in _REVIEW_ENTRY_CHECKPOINTS:
|
||||
return "review_workbench"
|
||||
if kind in {CheckpointKind.MUST_HUMAN, CheckpointKind.WAIT_MANUAL_UPLOAD}:
|
||||
return "task_workbench"
|
||||
if kind == CheckpointKind.AUTO_CONTINUE and checkpoint in {
|
||||
CP_ORDER_FREEZE,
|
||||
CP_READY_MATCH,
|
||||
CP_READY_EXPORT,
|
||||
CP_INVENTORY_PENDING,
|
||||
}:
|
||||
# 自动链可继续,但仍允许工作台手动兜底
|
||||
return "task_workbench"
|
||||
return "none"
|
||||
|
||||
|
||||
def _current_todo_label(checkpoint: str | None, action_buttons: list[dict[str, str]]) -> str | None:
|
||||
"""从当前动作按钮推导「当前待办」文案;无人工动作时返回 None。"""
|
||||
if not action_buttons:
|
||||
pause = _PAUSE_REASON_BY_CHECKPOINT.get(str(checkpoint or ""))
|
||||
return pause[1] if pause else None
|
||||
primary = next((b for b in action_buttons if b.get("variant") == "primary"), action_buttons[0])
|
||||
label = str(primary.get("label") or "").strip()
|
||||
return label or None
|
||||
|
||||
|
||||
def _build_task_summary(
|
||||
task: ShippingTask,
|
||||
checkpoint: str | None,
|
||||
*,
|
||||
checkpoint_kind: CheckpointKind,
|
||||
action_buttons: list[dict[str, str]],
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
输出可解释任务摘要,供通知卡、人工复核与任务工作台同源消费。
|
||||
|
||||
核心逻辑:保留 title/message/issues;补充 stage、checkpoint_kind、current_todo、
|
||||
pause_reason、next_auto_step、event_at、entry_surface、allowed_actions。
|
||||
"""
|
||||
title = user_facing_task_stage(task)
|
||||
message_by_checkpoint: dict[str, str] = {
|
||||
CP_PARSE_REVIEW: "解析结果需要人工确认后才能继续。",
|
||||
CP_RSV_REQ: "发货通知缺少后续匹配必需字段,请补充或重新上传。",
|
||||
CP_CUSTOMER_REVIEW: "客户尚未唯一确认,请在任务处理中选择标准客户。",
|
||||
CP_UPLOAD_REQ: "当前需要提供库存数据或重试库存获取。",
|
||||
CP_INVENTORY_PENDING: "发货通知已就绪,等待库存数据。",
|
||||
CP_INVENTORY_PENDING: "发货通知已就绪,系统将自动准备库存数据。",
|
||||
CP_INVENTORY_SOURCE_REVIEW: "库存源尚未确定,请先选择正式库存源。",
|
||||
CP_ORDER_FREEZE: "订单已复核,冻结订单后才能进入库存履约。",
|
||||
CP_ORDER_FREEZE: "订单已复核,系统将自动冻结后进入库存履约。",
|
||||
RS_SHORTAGE_REVIEW: "库存存在缺口,当前不能继续上传货代。",
|
||||
CP_READY_MATCH: "库存已就绪,可以继续执行匹配。",
|
||||
CP_READY_EXPORT: "库存匹配已满足发货条件,可以生成发货文件。",
|
||||
CP_EXPORT_READY: "发货文件已生成,可以下载或确认上传。",
|
||||
CP_READY_MATCH: "库存已就绪,系统将自动执行匹配。",
|
||||
CP_READY_EXPORT: "库存匹配已满足发货条件,系统将自动生成发货文件。",
|
||||
CP_EXPORT_READY: "发货文件已生成,请下载并在货代系统上传后提交截图/PDF 凭据。",
|
||||
CP_EXPORT_GENERATED: "发货文件已生成,请下载并在货代系统上传后提交截图/PDF 凭据。",
|
||||
CP_WAITING_UPLOAD: "系统正在等待货代上传结果。",
|
||||
CP_UPLOAD_FAILED: "货代上传失败,需要重试或手工确认。",
|
||||
CP_REJECTED: "任务已驳回,无需继续处理。",
|
||||
@@ -297,11 +380,34 @@ def _build_task_summary(task: ShippingTask, checkpoint: str | None) -> dict[str,
|
||||
CP_COMPLETED: "任务已关闭。",
|
||||
}
|
||||
issues = _collect_parse_stage_issues(task, checkpoint)
|
||||
pause = _PAUSE_REASON_BY_CHECKPOINT.get(str(checkpoint or ""))
|
||||
pause_reason_code = pause[0] if pause else (task.pending_human_reason or None)
|
||||
pause_reason = pause[1] if pause else (task.error_message or task.upload_error_message or None)
|
||||
if checkpoint_kind == CheckpointKind.AUTO_CONTINUE:
|
||||
pause_reason = None
|
||||
pause_reason_code = None
|
||||
allowed_actions = [str(btn.get("id")) for btn in action_buttons if btn.get("id")]
|
||||
event_at = None
|
||||
if task.updated_at is not None:
|
||||
event_at = task.updated_at.isoformat()
|
||||
return {
|
||||
"title": title,
|
||||
"message": message_by_checkpoint.get(str(checkpoint or ""), title),
|
||||
"error_message": task.error_message or task.upload_error_message,
|
||||
"issues": issues,
|
||||
"stage": title,
|
||||
"checkpoint_kind": str(checkpoint_kind),
|
||||
"current_todo": _current_todo_label(checkpoint, action_buttons),
|
||||
"pause_reason": pause_reason,
|
||||
"pause_reason_code": pause_reason_code,
|
||||
"next_auto_step": (
|
||||
_NEXT_AUTO_STEP_BY_CHECKPOINT.get(str(checkpoint or ""))
|
||||
if checkpoint_kind == CheckpointKind.AUTO_CONTINUE
|
||||
else None
|
||||
),
|
||||
"event_at": event_at,
|
||||
"entry_surface": _entry_surface_for(checkpoint, checkpoint_kind),
|
||||
"allowed_actions": allowed_actions,
|
||||
}
|
||||
|
||||
|
||||
@@ -309,8 +415,8 @@ def build_task_ui_state(task: ShippingTask) -> dict[str, Any]:
|
||||
"""
|
||||
构建单票任务 UI State。
|
||||
|
||||
核心逻辑:所有 UI 入口只消费这里输出的 checkpoint/action_buttons/step,避免 Chat、顶部步骤条、
|
||||
右侧工作台分别推导状态导致不一致。
|
||||
核心逻辑:所有 UI 入口只消费这里输出的 checkpoint/checkpoint_kind/action_buttons/summary,
|
||||
禁止 Chat、通知卡与任务中心分别推导状态。
|
||||
"""
|
||||
ui = resolve_task_ui_state(task)
|
||||
action_buttons = _action_buttons_for_task(task)
|
||||
@@ -321,13 +427,19 @@ def build_task_ui_state(task: ShippingTask) -> dict[str, Any]:
|
||||
"task_no": task.task_no,
|
||||
"batch_no": task.batch.batch_no if task.batch is not None else None,
|
||||
"checkpoint": ui.checkpoint,
|
||||
"checkpoint_kind": str(ui.checkpoint_kind),
|
||||
"process_status": task.process_status,
|
||||
"review_status": task.review_status,
|
||||
"pending_human_reason": task.pending_human_reason,
|
||||
"match_status": task.match_status,
|
||||
"current_step": current_step,
|
||||
"steps": steps,
|
||||
"summary": _build_task_summary(task, ui.checkpoint),
|
||||
"summary": _build_task_summary(
|
||||
task,
|
||||
ui.checkpoint,
|
||||
checkpoint_kind=ui.checkpoint_kind,
|
||||
action_buttons=action_buttons,
|
||||
),
|
||||
"action_buttons": action_buttons,
|
||||
"updated_at": task.updated_at.isoformat() if task.updated_at else None,
|
||||
}
|
||||
@@ -335,9 +447,10 @@ def build_task_ui_state(task: ShippingTask) -> dict[str, Any]:
|
||||
|
||||
def build_batch_ui_state(batch: ShippingBatch) -> dict[str, Any]:
|
||||
"""
|
||||
构建批次级 UI State。
|
||||
构建批次级会话上传集合摘要(非履约写操作面)。
|
||||
|
||||
核心逻辑:批次摘要、焦点任务、任务卡片均包含同源 task ui_state,供右侧工作台统一消费。
|
||||
核心逻辑:批次仅聚合各 PDF/Task 的同源 ui_state,供会话右侧「本次上传」摘要消费;
|
||||
履约写操作一律进入任务处理工作台。
|
||||
"""
|
||||
tasks = list_sorted_batch_tasks(batch)
|
||||
focus_task_no = str(batch.current_focus_task_no or "").strip()
|
||||
@@ -353,6 +466,7 @@ def build_batch_ui_state(batch: ShippingBatch) -> dict[str, Any]:
|
||||
]
|
||||
return {
|
||||
**serialize_batch_summary(batch),
|
||||
"role": "upload_session_summary",
|
||||
"process_summary": batch.process_summary_json or compute_batch_process_summary(tasks),
|
||||
"focus_task": serialize_task_summary(focus_task) if focus_task else None,
|
||||
"focus_task_ui_state": build_task_ui_state(focus_task) if focus_task else None,
|
||||
|
||||
Reference in new issue
Block a user