feat(shipping-notice): 新增发货批次和任务管理功能,支持批次创建与任务处理

This commit is contained in:
hugo committed 2026-06-17 20:45:19 +08:00
1 parent 457f0f4e5e
commit 634dee4b81
81 files changed
+9088 -3625

No files matched your search

@@ -1,3 +1,11 @@
"""
services/shipping_notice/notice_lines.py
用途:标准化发货通知订单行,并从文本/表格中抽取可用于后续库存匹配的行级结构化数据。
依赖:reservation_id、inventory_snapshot 的表格转行工具、正则与字段映射常量。
逻辑:由 extract/task_workflow 等上游模块调用本文件完成订单行归一化,再把结果交给匹配、人工更正与摘要生成链路。
"""
from __future__ import annotations
import re
@@ -426,6 +434,16 @@ def _parse_notice_row(
def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
"""
标准化单条发货通知订单行。
核心逻辑:校验 SKU 与数量必填项,统一字段别名、币种、交期和 Reservation ID,并在缺少 amount 时按单价回填。
Args:
raw: 原始订单行字典,可包含多种字段别名。
Returns:
dict[str, Any] | None: 归一化后的订单行;若缺少关键字段则返回 None。
"""
if not isinstance(raw, dict):
return None
@@ -517,6 +535,16 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
def normalize_notice_lines(lines: list[Any] | None) -> list[dict[str, Any]]:
"""
批量标准化订单行列表。
核心逻辑:逐行调用 `normalize_notice_line`,自动过滤掉无效、缺 SKU 或缺数量的脏数据。
Args:
lines: 原始订单行列表。
Returns:
list[dict[str, Any]]: 标准化后的有效订单行集合。
"""
if not isinstance(lines, list):
return []
normalized: list[dict[str, Any]] = []
@@ -532,7 +560,17 @@ def enrich_notice_with_document_reservation(
*,
raw_text: str = "",
) -> dict[str, Any]:
"""Apply document-level PR Number / @1000140351 reservation id to lines missing it."""
"""
用文档级 Reservation ID 回填缺失的订单行。
核心逻辑:优先从 parsed 字段中找 document-level Reservation ID,找不到再从原始文本中兜底提取。
Args:
parsed: 已解析出的发货通知结构。
raw_text: 原始发货通知文本,用于补充文档级 Reservation ID。
Returns:
dict[str, Any]: 已完成文档级 Reservation ID 回填的解析结果。
"""
doc_rid = ""
for key in (
"reservation_id",
@@ -569,7 +607,16 @@ def enrich_notice_with_document_reservation(
def enrich_notice_with_document_required_date(parsed: dict[str, Any]) -> dict[str, Any]:
"""Apply document-level required_date to lines missing their own delivery date."""
"""
用文档级 required_date 回填缺失的订单行交期。
核心逻辑:统一格式化文档级日期,并在订单行未给出独立交期时逐行补齐。
Args:
parsed: 已解析出的发货通知结构。
Returns:
dict[str, Any]: 已完成文档级交期回填的解析结果。
"""
doc_date = _normalize_required_date(
parsed.get("required_date") or parsed.get("document_required_date")
)
@@ -597,7 +644,16 @@ def enrich_notice_with_document_required_date(parsed: dict[str, Any]) -> dict[st
def enrich_notice_with_document_currency(parsed: dict[str, Any]) -> dict[str, Any]:
"""Apply document-level currency to lines missing their own currency."""
"""
用文档级币种回填缺失的订单行币种。
核心逻辑:统一币种代码,并在订单行未给出币种时沿用文档级币种。
Args:
parsed: 已解析出的发货通知结构。
Returns:
dict[str, Any]: 已完成文档级币种回填的解析结果。
"""
doc_currency = _normalize_currency(
parsed.get("currency") or parsed.get("document_currency")
)
@@ -625,6 +681,16 @@ def enrich_notice_with_document_currency(parsed: dict[str, Any]) -> dict[str, An
def parse_notice_table_text(raw_text: str) -> list[dict[str, Any]]:
"""
从发货通知原始文本中的表格片段解析订单行。
核心逻辑:先借助 inventory_snapshot 的通用表格拆行工具得到二维表,再识别表头并解析数据行。
Args:
raw_text: 原始发货通知文本,通常来自 PDF/OCR/邮件正文。
Returns:
list[dict[str, Any]]: 解析出的订单行列表。
"""
from app.services.shipping_notice.inventory_snapshot import _rows_from_table_text
text = str(raw_text or "").strip()
@@ -678,6 +744,17 @@ def _find_notice_header_row_index(rows: list[list[Any]], *, max_scan: int = 40)
def parse_notice_table_rows(headers: list[str], data_rows: list[list[Any]]) -> list[dict[str, Any]]:
"""
按表头定义解析发货通知表格数据行。
核心逻辑:根据 headers 推断列映射,仅在识别到有效表头时逐行提取 SKU、数量、金额等字段。
Args:
headers: 表头行。
data_rows: 表头之后的数据行集合。
Returns:
list[dict[str, Any]]: 解析后的订单行列表。
"""
columns = _resolve_notice_columns(headers)
has_header = columns.get("sku") is not None or columns.get("qty") is not None
if not has_header and headers:
@@ -696,6 +773,16 @@ def parse_notice_table_rows(headers: list[str], data_rows: list[list[Any]]) -> l
def validate_notice_lines(lines: list[dict[str, Any]]) -> list[str]:
"""
生成订单行解析质量告警。
核心逻辑:检查 Reservation ID、价格字段等关键完整性,输出给前端/Agent 的提醒文本。
Args:
lines: 已标准化的订单行列表。
Returns:
list[str]: 告警文案列表,为空表示未发现明显问题。
"""
warnings: list[str] = []
if not lines:
warnings.append("未解析到有效订单行(需至少包含 SKU 与需求数量)。")
@@ -733,7 +820,16 @@ def validate_notice_lines(lines: list[dict[str, Any]]) -> list[str]:
def notice_parse_completeness(lines: list[dict[str, Any]]) -> float:
"""0~1 score for how complete order lines are (sku/qty/reservation/price)."""
"""
评估订单行解析完整度。
核心逻辑:按 SKU、数量、Reservation ID、价格等字段覆盖率计算 0~1 分数。
Args:
lines: 已标准化的订单行列表。
Returns:
float: 0~1 之间的解析完整度分数。
"""
if not lines:
return 0.0
scores: list[float] = []