feat(shipping-notice): 新增发货通知重复检测与处理接口,支持重复决策保存与重新执行检测
This commit is contained in:
1 parent
729546a7b7
commit
ceb49461e8
72 files changed
+14270
-460
No files matched your search
@@ -2,8 +2,9 @@
|
||||
services/shipping_notice/ui_state.py
|
||||
|
||||
用途:发货通知任务/批次 UI 状态读模型,作为 Chat 按钮、步骤条与工作台的统一状态来源。
|
||||
依赖:models.ShippingTask/ShippingBatch、state_machine、query_service。
|
||||
逻辑:从 ShippingTask DB 字段经状态机解析 checkpoint/action_buttons,再补充七步流程与用户可读摘要。
|
||||
依赖:models.ShippingTask/ShippingBatch、state_machine、query_service、constants。
|
||||
逻辑:从 ShippingTask DB 字段经状态机解析 checkpoint/action_buttons,再补充七步流程与用户可读摘要;
|
||||
文件解析阶段额外汇总 summary.issues(解析复核 / RSV / 库存源门禁 + parse warnings)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -40,6 +41,7 @@ from app.services.shipping_notice.constants import (
|
||||
RS_PARSE_REVIEW,
|
||||
RS_SHORTAGE_REVIEW,
|
||||
)
|
||||
from app.services.shipping_notice.inventory_source_gate import is_inventory_source_stable
|
||||
from app.services.shipping_notice.query_service import (
|
||||
compute_batch_process_summary,
|
||||
list_sorted_batch_tasks,
|
||||
@@ -179,7 +181,88 @@ def _action_buttons_for_task(task: ShippingTask) -> list[dict[str, str]]:
|
||||
return ui.action_buttons
|
||||
|
||||
|
||||
def _build_task_summary(task: ShippingTask, checkpoint: str | None) -> dict[str, str | None]:
|
||||
_PARSE_STAGE_CHECKPOINTS = {
|
||||
CP_PARSE_REVIEW,
|
||||
CP_RSV_REQ,
|
||||
CP_INVENTORY_SOURCE_REVIEW,
|
||||
CP_NOTICE_TEXT_EXTRACTION_FAILED,
|
||||
}
|
||||
|
||||
_WARNING_DISPLAY_ALIASES: dict[str, str] = {
|
||||
"parse_fallback_heuristic": "解析引擎回退到启发式规则,请人工核对字段。",
|
||||
"heuristic_no_lines_detected": "启发式解析未识别到订单行,请核对通知内容。",
|
||||
}
|
||||
|
||||
|
||||
def _normalize_issue_text(raw: Any) -> str:
|
||||
"""将 warnings / error 原文规范化为可展示短文案,过滤空值。"""
|
||||
text = str(raw or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
return _WARNING_DISPLAY_ALIASES.get(text, text)
|
||||
|
||||
|
||||
def _append_unique_issue(issues: list[str], raw: Any) -> None:
|
||||
"""追加去重后的问题文案,保持首次出现顺序。"""
|
||||
text = _normalize_issue_text(raw)
|
||||
if text and text not in issues:
|
||||
issues.append(text)
|
||||
|
||||
|
||||
def _collect_parse_stage_issues(task: ShippingTask, checkpoint: str | None) -> list[str]:
|
||||
"""
|
||||
汇总文件解析阶段(步骤 2)全部待处理问题,供工作台一次列出。
|
||||
|
||||
核心逻辑:解析复核、RSV 缺失、库存源门禁、parse warnings 可能并发;
|
||||
以前仅透出 error_message(多为库存源),导致其它解析问题被吞掉。
|
||||
新顺序为先解析复核再库存源;存量 pending_parse_review_after_source 仍提示延后复核。
|
||||
"""
|
||||
checkpoint_value = str(checkpoint or "").strip()
|
||||
if checkpoint_value not in _PARSE_STAGE_CHECKPOINTS:
|
||||
return []
|
||||
|
||||
issues: list[str] = []
|
||||
if checkpoint_value == CP_INVENTORY_SOURCE_REVIEW:
|
||||
_append_unique_issue(
|
||||
issues,
|
||||
task.error_message or "无法根据发货通知唯一确定库存查询来源",
|
||||
)
|
||||
elif checkpoint_value == CP_RSV_REQ:
|
||||
_append_unique_issue(
|
||||
issues,
|
||||
task.error_message or "解析失败:未识别到 Reservation ID / COM ID(预定单号)",
|
||||
)
|
||||
elif checkpoint_value == CP_NOTICE_TEXT_EXTRACTION_FAILED:
|
||||
_append_unique_issue(
|
||||
issues,
|
||||
task.error_message or "未能从发货通知中解析到可审核内容",
|
||||
)
|
||||
elif checkpoint_value == CP_PARSE_REVIEW:
|
||||
_append_unique_issue(issues, "通知单解析置信度不足,需要复核单头或明细。")
|
||||
if not is_inventory_source_stable(task):
|
||||
_append_unique_issue(
|
||||
issues,
|
||||
"解析确认通过后,若库存源仍无法唯一命中,还需选择正式库存源。",
|
||||
)
|
||||
|
||||
payload = task.step_payload_json if isinstance(task.step_payload_json, dict) else {}
|
||||
progress = payload.get("current_step_progress") if isinstance(payload, dict) else None
|
||||
extra = progress.get("extra") if isinstance(progress, dict) else None
|
||||
warnings = extra.get("warnings") if isinstance(extra, dict) else None
|
||||
if isinstance(warnings, list):
|
||||
for item in warnings:
|
||||
_append_unique_issue(issues, item)
|
||||
|
||||
# 存量兼容:旧顺序「先库存源后解析」延后的低置信度提示。
|
||||
if payload.get("pending_parse_review_after_source") or (
|
||||
checkpoint_value == CP_INVENTORY_SOURCE_REVIEW and task.review_status == RS_PARSE_REVIEW
|
||||
):
|
||||
_append_unique_issue(issues, "解析置信度不足,确认库存源后仍需复核解析结果。")
|
||||
|
||||
return issues
|
||||
|
||||
|
||||
def _build_task_summary(task: ShippingTask, checkpoint: str | None) -> dict[str, Any]:
|
||||
"""输出用户可读任务摘要,供 Chat 文案与工作台卡片复用。"""
|
||||
title = user_facing_task_stage(task)
|
||||
message_by_checkpoint: dict[str, str] = {
|
||||
@@ -200,10 +283,12 @@ def _build_task_summary(task: ShippingTask, checkpoint: str | None) -> dict[str,
|
||||
CP_UPLOADED: "任务已完成上传。",
|
||||
CP_COMPLETED: "任务已关闭。",
|
||||
}
|
||||
issues = _collect_parse_stage_issues(task, checkpoint)
|
||||
return {
|
||||
"title": title,
|
||||
"message": message_by_checkpoint.get(str(checkpoint or ""), title),
|
||||
"error_message": task.error_message or task.upload_error_message,
|
||||
"issues": issues,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user