feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制

This commit is contained in:
hugo committed 2026-07-21 23:17:29 +08:00
1 parent ce26d6d0c4
commit c4095b08da
175 files changed
+13064 -1814

No files matched your search

@@ -10,12 +10,7 @@ from __future__ import annotations
from typing import Any
from app.services.shipping_notice.notice_line_constants import (
HEADER_SKIP_TOKENS,
NOTICE_COLUMN_MAP,
NOTICE_FIELD_LABELS,
NOTICE_LINE_FIELDS,
)
from app.services.shipping_notice.notice_line_constants import NOTICE_FIELD_LABELS, NOTICE_LINE_FIELDS
from app.services.shipping_notice.notice_line_table import (
parse_notice_table_rows,
parse_notice_table_text,
@@ -37,8 +32,7 @@ from app.services.shipping_notice.reservation_id import (
validated_reservation_id,
)
# 向后兼容:旧代码/tests 仍可从 notice_lines 引用私有别名
_HEADER_SKIP_TOKENS = HEADER_SKIP_TOKENS
# 仅保留无业务配置的值工具私有别名;表头业务别名不再从本模块导出。
_extract_currency = extract_currency
_norm_header = norm_header
_map_notice_header = map_notice_header
@@ -109,13 +103,18 @@ def non_atomic_part_number_field(raw: dict[str, Any] | None) -> str | None:
return None
def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
def normalize_notice_line(
raw: dict[str, Any] | None,
*,
schema_definition: dict[str, Any] | None = None,
) -> dict[str, Any] | None:
"""
标准化单条发货通知订单行。
核心逻辑:校验原子 part_number 与数量必填项,统一币种、交期和 Reservation ID,并在缺少 amount 时按单价回填。
Args:
raw: 原始订单行字典,可包含多种字段别名。
schema_definition: 冻结 Schema;提供时仅保留其中已启用的字段,且不注入默认 UOM。
Returns:
dict[str, Any] | None: 归一化后的订单行;若缺少关键字段则返回 None。
@@ -135,11 +134,16 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
if qty is None or qty <= 0:
return None
line: dict[str, Any] = {
"part_number": part_number,
"qty": qty,
"uom": str(raw.get("uom") or "EA").strip() or "EA",
enabled_fields = {
str(item.get("field_code") or "").strip()
for item in (schema_definition or {}).get("fields", [])
if isinstance(item, dict) and item.get("enabled", True)
}
field_enabled = lambda code: not schema_definition or code in enabled_fields
line: dict[str, Any] = {"part_number": part_number, "qty": qty}
raw_uom = str(raw.get("uom") or "").strip()
if raw_uom and field_enabled("uom"):
line["uom"] = raw_uom
line_no = normalize_line_no(
raw.get("line_no")
@@ -150,7 +154,7 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
or raw.get("Item")
or raw.get("Item No")
)
if line_no:
if line_no and field_enabled("line_no"):
line["line_no"] = line_no
reservation_id = extract_reservation_id(
@@ -162,20 +166,20 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
or raw.get("prNumber")
or raw.get("PR Number")
)
if reservation_id:
if reservation_id and field_enabled("reservation_id"):
line["reservation_id"] = reservation_id
evidence = raw.get("reservation_id_evidence")
if isinstance(evidence, dict):
if isinstance(evidence, dict) and field_enabled("reservation_id"):
line["reservation_id_evidence"] = dict(evidence)
unit_price = parse_number(raw.get("unit_price") if raw.get("unit_price") is not None else raw.get("price"))
if unit_price is not None:
if unit_price is not None and field_enabled("unit_price"):
line["unit_price"] = unit_price
amount = parse_number(raw.get("amount") if raw.get("amount") is not None else raw.get("total"))
if amount is not None:
if amount is not None and field_enabled("amount"):
line["amount"] = amount
elif unit_price is not None:
elif unit_price is not None and field_enabled("amount"):
line["amount"] = round(unit_price * qty, 4)
currency = normalize_currency(raw.get("currency") or raw.get("Currency"))
@@ -186,7 +190,7 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
raw.get("amount"),
raw.get("total"),
)
if currency:
if currency and field_enabled("currency"):
line["currency"] = currency
required_date = normalize_required_date(
@@ -196,7 +200,7 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
or raw.get("eta")
or raw.get("ETA")
)
if required_date:
if required_date and field_enabled("required_date"):
line["required_date"] = required_date
optional_aliases = {
@@ -213,20 +217,25 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
"aws_part_number": ("aws_part_number", "AWS Sku", "AWS SKU"),
}
if composite.get('customer_part_number'):
line['customer_part_number'] = composite['customer_part_number']
if field_enabled('customer_part_number'):
line['customer_part_number'] = composite['customer_part_number']
for field, aliases in optional_aliases.items():
value = next((str(raw.get(alias) or "").strip() for alias in aliases if raw.get(alias)), "")
if value:
if value and field_enabled(field):
line[field] = value
remark = str(raw.get("remark") or raw.get("note") or "").strip()
if remark:
if remark and field_enabled("remark"):
line["remark"] = remark
return line
def normalize_notice_lines(lines: list[Any] | None) -> list[dict[str, Any]]:
def normalize_notice_lines(
lines: list[Any] | None,
*,
schema_definition: dict[str, Any] | None = None,
) -> list[dict[str, Any]]:
"""
批量标准化订单行列表。
@@ -236,7 +245,10 @@ def normalize_notice_lines(lines: list[Any] | None) -> list[dict[str, Any]]:
return []
normalized: list[dict[str, Any]] = []
for item in lines:
parsed = normalize_notice_line(item if isinstance(item, dict) else None)
parsed = normalize_notice_line(
item if isinstance(item, dict) else None,
schema_definition=schema_definition,
)
if parsed:
normalized.append(parsed)
return normalized
@@ -246,6 +258,7 @@ def adjudicate_notice_lines(
lines: list[dict[str, Any]],
*,
raw_text: str = "",
schema_definition: dict[str, Any] | None = None,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]], str]:
"""
对 LLM 候选订单行执行后端真值裁决,并输出可审计问题清单。
@@ -255,11 +268,19 @@ def adjudicate_notice_lines(
Args:
lines: 已完成基础标准化的 LLM/启发式候选行。
raw_text: 原始通知正文;仅 TSV/HTML 保留下来的明确列关系可作为自动纠错证据。
schema_definition: 本次处理明确选择的完整订单 Schema;缺失时不生成表格纠错证据。
Returns:
tuple: (裁决后的业务行、parse_issues、validation_status)。
"""
evidence_lines = parse_notice_table_text(raw_text) if "\t" in str(raw_text or "") else []
if schema_definition and "\t" in str(raw_text or ""):
from app.services.shipping_notice.order_schema.table_contract import compile_table_header_contract
evidence_lines = parse_notice_table_text(
raw_text, contract=compile_table_header_contract(schema_definition),
)
else:
evidence_lines = []
evidence_by_part_number: dict[str, list[dict[str, Any]]] = {}
for evidence_line in evidence_lines:
part_number = str(evidence_line.get("part_number") or "").strip()
@@ -498,7 +519,6 @@ def notice_parse_completeness(lines: list[dict[str, Any]]) -> float:
__all__ = [
"NOTICE_COLUMN_MAP",
"NOTICE_FIELD_LABELS",
"NOTICE_LINE_FIELDS",
"enrich_notice_with_document_currency",