refactor(shipping-notice): 统一料号字段,移除过时的 SKU 相关字段,更新相关逻辑以支持新字段命名
This commit is contained in:
1 parent
dde25344ca
commit
46eb63f5bb
111 files changed
+3116
-1250
No files matched your search
@@ -45,11 +45,75 @@ _map_notice_header = map_notice_header
|
||||
_parse_number = parse_number
|
||||
|
||||
|
||||
def split_part_number_tokens(value: Any) -> list[str]:
|
||||
"""
|
||||
将复合料号文本拆成稳定的原子候选 token。
|
||||
|
||||
核心逻辑:仅把 `/` 视为业务确认的组合分隔符,去除空段和首尾空白;本函数不猜测 token 角色。
|
||||
Args:
|
||||
value: 原始表格单元格或 LLM 候选值。
|
||||
Returns:
|
||||
保持原顺序且去重的非空 token;无值时返回空数组。
|
||||
"""
|
||||
tokens: list[str] = []
|
||||
for part in str(value or '').split('/'):
|
||||
token = part.strip()
|
||||
if token and token not in tokens:
|
||||
tokens.append(token)
|
||||
return tokens
|
||||
|
||||
|
||||
def resolve_composite_part_numbers(value: Any) -> dict[str, str]:
|
||||
"""
|
||||
按已冻结 TEMP 业务规则解析二段复合料号。
|
||||
|
||||
核心逻辑:仅当恰有两个原子 token 且其中一个以 TEMP- 开头时,确定 TEMP 为客户料号、另一段为内部料号;
|
||||
其余组合保持未解析,交由字段冲突流程处理。
|
||||
Args:
|
||||
value: 可能包含 `/` 的原始料号文本。
|
||||
Returns:
|
||||
可确定时返回 part_number/customer_part_number;否则返回空对象。
|
||||
"""
|
||||
tokens = split_part_number_tokens(value)
|
||||
if len(tokens) != 2:
|
||||
return {}
|
||||
customer_tokens = [token for token in tokens if token.upper().startswith('TEMP-')]
|
||||
if len(customer_tokens) != 1:
|
||||
return {}
|
||||
customer_part_number = customer_tokens[0]
|
||||
part_number = next(token for token in tokens if token != customer_part_number)
|
||||
return {
|
||||
'part_number': part_number,
|
||||
'customer_part_number': customer_part_number,
|
||||
}
|
||||
|
||||
|
||||
def non_atomic_part_number_field(raw: dict[str, Any] | None) -> str | None:
|
||||
"""
|
||||
检查订单行三类料号是否违反原子值契约。
|
||||
|
||||
核心逻辑:内部料号仅允许命中已冻结 TEMP 二段拆分规则;客户料号与 AWS 料号出现 `/` 时直接返回字段名。
|
||||
Args:
|
||||
raw: 待检查的原始订单行。
|
||||
Returns:
|
||||
首个非法字段名;全部合法时返回 None。
|
||||
"""
|
||||
if not isinstance(raw, dict):
|
||||
return None
|
||||
raw_part_number = str(raw.get("part_number") or "").strip()
|
||||
if "/" in raw_part_number and not resolve_composite_part_numbers(raw_part_number):
|
||||
return "part_number"
|
||||
for field in ("customer_part_number", "aws_part_number"):
|
||||
if "/" in str(raw.get(field) or "").strip():
|
||||
return field
|
||||
return None
|
||||
|
||||
|
||||
def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
"""
|
||||
标准化单条发货通知订单行。
|
||||
|
||||
核心逻辑:校验 SKU 与数量必填项,统一字段别名、币种、交期和 Reservation ID,并在缺少 amount 时按单价回填。
|
||||
核心逻辑:校验原子 part_number 与数量必填项,统一币种、交期和 Reservation ID,并在缺少 amount 时按单价回填。
|
||||
Args:
|
||||
raw: 原始订单行字典,可包含多种字段别名。
|
||||
|
||||
@@ -58,9 +122,13 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
"""
|
||||
if not isinstance(raw, dict):
|
||||
return None
|
||||
if non_atomic_part_number_field(raw):
|
||||
return None
|
||||
|
||||
sku = str(raw.get("sku") or raw.get("part_number") or raw.get("material") or "").strip()
|
||||
if not sku:
|
||||
raw_part_number = str(raw.get("part_number") or "").strip()
|
||||
composite = resolve_composite_part_numbers(raw_part_number)
|
||||
part_number = composite.get('part_number') or raw_part_number
|
||||
if not part_number or "/" in part_number:
|
||||
return None
|
||||
|
||||
qty = parse_number(raw.get("qty") if raw.get("qty") is not None else raw.get("quantity"))
|
||||
@@ -68,7 +136,7 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
return None
|
||||
|
||||
line: dict[str, Any] = {
|
||||
"sku": sku,
|
||||
"part_number": part_number,
|
||||
"qty": qty,
|
||||
"uom": str(raw.get("uom") or "EA").strip() or "EA",
|
||||
}
|
||||
@@ -131,34 +199,21 @@ def normalize_notice_line(raw: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
if required_date:
|
||||
line["required_date"] = required_date
|
||||
|
||||
vendor_part_number = str(
|
||||
raw.get("vendor_part_number")
|
||||
or raw.get("vendorPartNumber")
|
||||
or raw.get("vendor_pn")
|
||||
or raw.get("supplier_part_number")
|
||||
or raw.get("supplierPartNumber")
|
||||
or ""
|
||||
).strip()
|
||||
if vendor_part_number:
|
||||
line["vendor_part_number"] = vendor_part_number
|
||||
|
||||
optional_aliases = {
|
||||
"po_number": ("po_number", "poNumber", "PO", "PO Number"),
|
||||
"allocation_id": ("allocation_id", "allocationId", "Allocation ID"),
|
||||
"source_type": (
|
||||
"source_type",
|
||||
"sourceType",
|
||||
"inventory_source_hint": (
|
||||
"inventory_source_hint",
|
||||
"Source Type",
|
||||
),
|
||||
"hub_code": (
|
||||
"hub_code",
|
||||
"hubCode",
|
||||
"Hub Code",
|
||||
"Hub",
|
||||
"HUB",
|
||||
),
|
||||
"customer_sku": ("customer_sku", "customerSku", "allocated_hhpn", "Allocated HHPN"),
|
||||
"customer_part_number": ("customer_part_number", "Customer Part Number", "Customer PN", "Allocated HHPN"),
|
||||
"aws_part_number": ("aws_part_number", "AWS Sku", "AWS SKU"),
|
||||
}
|
||||
if composite.get('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:
|
||||
@@ -175,7 +230,7 @@ def normalize_notice_lines(lines: list[Any] | None) -> list[dict[str, Any]]:
|
||||
"""
|
||||
批量标准化订单行列表。
|
||||
|
||||
核心逻辑:逐行调用 `normalize_notice_line`,自动过滤掉无效、缺 SKU 或缺数量的脏数据。
|
||||
核心逻辑:逐行调用 `normalize_notice_line`,自动过滤缺原子 part_number 或缺数量的脏数据。
|
||||
"""
|
||||
if not isinstance(lines, list):
|
||||
return []
|
||||
@@ -195,7 +250,7 @@ def adjudicate_notice_lines(
|
||||
"""
|
||||
对 LLM 候选订单行执行后端真值裁决,并输出可审计问题清单。
|
||||
|
||||
核心逻辑:先从保留列边界的原文解析结构证据;非法候选仅在同 SKU 唯一命中合法表格值时纠正,
|
||||
核心逻辑:先从保留列边界的原文解析结构证据;非法候选仅在同 part_number 唯一命中合法表格值时纠正,
|
||||
否则清空并阻断。合法候选与明确表格证据冲突时保留候选但标记阻断,禁止静默覆盖。
|
||||
Args:
|
||||
lines: 已完成基础标准化的 LLM/启发式候选行。
|
||||
@@ -205,13 +260,13 @@ def adjudicate_notice_lines(
|
||||
tuple: (裁决后的业务行、parse_issues、validation_status)。
|
||||
"""
|
||||
evidence_lines = parse_notice_table_text(raw_text) if "\t" in str(raw_text or "") else []
|
||||
evidence_by_sku: dict[str, list[dict[str, Any]]] = {}
|
||||
evidence_by_part_number: dict[str, list[dict[str, Any]]] = {}
|
||||
for evidence_line in evidence_lines:
|
||||
sku = str(evidence_line.get("sku") or "").strip()
|
||||
part_number = str(evidence_line.get("part_number") or "").strip()
|
||||
evidence = evidence_line.get("reservation_id_evidence")
|
||||
source_column = norm_header(evidence.get("source_column")) if isinstance(evidence, dict) else ""
|
||||
if sku and source_column and validated_reservation_id(evidence_line.get("reservation_id")):
|
||||
evidence_by_sku.setdefault(sku, []).append(evidence_line)
|
||||
if part_number and source_column and validated_reservation_id(evidence_line.get("reservation_id")):
|
||||
evidence_by_part_number.setdefault(part_number, []).append(evidence_line)
|
||||
|
||||
adjudicated: list[dict[str, Any]] = []
|
||||
issues: list[dict[str, Any]] = []
|
||||
@@ -219,22 +274,8 @@ def adjudicate_notice_lines(
|
||||
line = dict(source_line)
|
||||
raw_candidate = str(line.get("reservation_id") or "").strip()
|
||||
candidate = validated_reservation_id(raw_candidate)
|
||||
line_sku = str(line.get("sku") or "").strip()
|
||||
line_vendor_part = str(line.get("vendor_part_number") or "").strip()
|
||||
matches = list(evidence_by_sku.get(line_sku, []))
|
||||
if not matches:
|
||||
matches = [
|
||||
item
|
||||
for candidates in evidence_by_sku.values()
|
||||
for item in candidates
|
||||
if (
|
||||
str(item.get("vendor_part_number") or "").strip()
|
||||
and (
|
||||
str(item.get("vendor_part_number") or "").strip() == line_vendor_part
|
||||
or str(item.get("vendor_part_number") or "").strip() in line_sku
|
||||
)
|
||||
)
|
||||
]
|
||||
line_part_number = str(line.get("part_number") or "").strip()
|
||||
matches = list(evidence_by_part_number.get(line_part_number, []))
|
||||
evidence_values = {
|
||||
validated_reservation_id(item.get("reservation_id"))
|
||||
for item in matches
|
||||
@@ -246,7 +287,7 @@ def adjudicate_notice_lines(
|
||||
issues.append(
|
||||
{
|
||||
"line_index": index,
|
||||
"sku": line.get("sku"),
|
||||
"part_number": line.get("part_number"),
|
||||
"field": "reservation_id",
|
||||
"reason": "source_conflict",
|
||||
"severity": "blocking",
|
||||
@@ -262,7 +303,7 @@ def adjudicate_notice_lines(
|
||||
issues.append(
|
||||
{
|
||||
"line_index": index,
|
||||
"sku": line.get("sku"),
|
||||
"part_number": line.get("part_number"),
|
||||
"field": "reservation_id",
|
||||
"reason": "value_corrected",
|
||||
"severity": "info",
|
||||
@@ -276,7 +317,7 @@ def adjudicate_notice_lines(
|
||||
issues.append(
|
||||
{
|
||||
"line_index": index,
|
||||
"sku": line.get("sku"),
|
||||
"part_number": line.get("part_number"),
|
||||
"field": "reservation_id",
|
||||
"reason": "missing" if not raw_candidate else "invalid_format",
|
||||
"severity": "blocking",
|
||||
@@ -400,10 +441,10 @@ def validate_notice_lines(lines: list[dict[str, Any]]) -> list[str]:
|
||||
"""
|
||||
warnings: list[str] = []
|
||||
if not lines:
|
||||
warnings.append("未解析到有效订单行(需至少包含 SKU 与需求数量)。")
|
||||
warnings.append("未解析到有效订单行(需至少包含内部料号与需求数量)。")
|
||||
return warnings
|
||||
|
||||
missing_reservation = [line["sku"] for line in lines if not str(line.get("reservation_id") or "").strip()]
|
||||
missing_reservation = [line["part_number"] for line in lines if not str(line.get("reservation_id") or "").strip()]
|
||||
if missing_reservation and len(missing_reservation) == len(lines):
|
||||
warnings.append(
|
||||
"订单行未解析到 Reservation ID;当前匹配规则会阻断后续库存匹配,"
|
||||
@@ -416,7 +457,7 @@ def validate_notice_lines(lines: list[dict[str, Any]]) -> list[str]:
|
||||
)
|
||||
|
||||
invalid_reservation = [
|
||||
line["sku"]
|
||||
line["part_number"]
|
||||
for line in lines
|
||||
if str(line.get("reservation_id") or "").strip()
|
||||
and not is_valid_reservation_id(line.get("reservation_id"))
|
||||
@@ -437,14 +478,14 @@ def notice_parse_completeness(lines: list[dict[str, Any]]) -> float:
|
||||
"""
|
||||
评估订单行解析完整度。
|
||||
|
||||
核心逻辑:按 SKU、数量、Reservation ID、价格等字段覆盖率计算 0~1 分数。
|
||||
核心逻辑:按内部料号、数量、Reservation ID、价格等字段覆盖率计算 0~1 分数。
|
||||
"""
|
||||
if not lines:
|
||||
return 0.0
|
||||
scores: list[float] = []
|
||||
for line in lines:
|
||||
item = 0.0
|
||||
if line.get("sku"):
|
||||
if line.get("part_number"):
|
||||
item += 0.35
|
||||
if line.get("qty"):
|
||||
item += 0.35
|
||||
@@ -466,8 +507,11 @@ __all__ = [
|
||||
"adjudicate_notice_lines",
|
||||
"normalize_notice_line",
|
||||
"normalize_notice_lines",
|
||||
"non_atomic_part_number_field",
|
||||
"notice_parse_completeness",
|
||||
"parse_notice_table_rows",
|
||||
"parse_notice_table_text",
|
||||
"resolve_composite_part_numbers",
|
||||
"split_part_number_tokens",
|
||||
"validate_notice_lines",
|
||||
]
|
||||
Reference in new issue
Block a user