fix(shipping-notice): 预约单号兜底分作用域并镜像补写明细缺失行

订单级早退导致表头已有预约单号时明细行补写永不执行(回归
SNER-20261014-0AB6D98A doc 915 明细预约号空)。

- 兜底早退改分作用域:scope=header 表头有值才跳过;scope=line
  行级全有值才跳过,存在缺失行仍执行补写;
- 镜像语义:排除集移除表头已采纳的预约号值——它正是要写入缺失
  行级的值;其余订单观测值(尤其 PO)保持排除防误判;
- PO 行定位收敛不变:本单 PO 所在行的预约号唯一命中后写入。

真数据验证:9/9 PO 各自唯一写入本行预约号
(4510042199→1000161448 … 4510042208→1000131033),与表头一致。
This commit is contained in:
hugo committed 2026-10-15 08:43:32 +08:00
1 parent b8fe089b3d
commit d75f2e7d86
1 file changed
+48 -5
@@ -281,10 +281,15 @@ def _supplement_multi_value_conflict(
}
def _iter_order_observation_values(order: dict[str, Any]) -> list[Any]:
"""收集订单头 + 全部行的原始字段 Observation 值,供兜底排除集使用。"""
def _iter_order_observation_values(
order: dict[str, Any], *, include_header_values: bool = True,
) -> list[Any]:
"""收集订单头/行的原始字段 Observation 值;include_header_values=False 时
跳过表头观测(行级补写语义:表头已采纳值允许镜像写入缺失行级)。"""
values: list[Any] = []
field_groups: list[Any] = [order.get('header_fields')]
field_groups: list[Any] = []
if include_header_values:
field_groups.append(order.get('header_fields'))
for line in order.get('lines') or []:
if isinstance(line, dict):
field_groups.append(line.get('fields'))
@@ -303,15 +308,23 @@ def _iter_order_observation_values(order: dict[str, Any]) -> list[Any]:
def _heuristic_excluded_values(
order: dict[str, Any],
supplement: dict[str, Any],
*,
include_order_header_values: bool = True,
) -> set[str]:
"""构造裸数字候选的排除集:订单已提取值 + 邮件补充行值 + 邮件主题标签值。
核心逻辑:客户 PO 号本身就是 10 位纯数字,是启发式最大的误判源;因此除了
订单 Observation 原值外,还纳入 Reservation/PO 两种归一变体(千分位、x.0、
前导零),并排除主题里「PO xxx」等带标签数字(Q10 双保险)。
Args:
include_order_header_values: 行级补写语义下传 False——表头已采纳的预约号
正是要镜像写入缺失行级的值,不能出现在排除集里(否则行级永远补不上)。
"""
excluded: set[str] = set()
raw_values = _iter_order_observation_values(order)
raw_values = _iter_order_observation_values(
order, include_header_values=include_order_header_values,
)
rows = supplement.get('rows') if isinstance(supplement.get('rows'), list) else []
for row in rows:
if isinstance(row, dict):
@@ -421,9 +434,39 @@ def _apply_reservation_heuristic(
candidates = supplement.get('reservation_candidates')
if config is None or not field_id or not isinstance(candidates, list) or not candidates:
return
if _order_has_reservation_value(order, ids):
# [分作用域兜底] 「已有值永不覆盖」按字段作用域生效:表头已有预约号→不再重复
# 注入表头;行级缺失→仍执行行级补写(分配邮件一行一个 PO+预约号,明细缺预约
# 号属数据不完整,回归 SNER-20261014-0AB6D98A doc 915 明细预约号空)。
reverse_ids = {v: k for k, v in ids.items()}
header_has_reservation = bool(
_values(order.get('header_fields'), 'reservation_id', reverse_ids),
)
header_reservation_values = set(
_values(order.get('header_fields'), 'reservation_id', reverse_ids),
)
lines_missing_reservation = [
line for line in order.get('lines') or []
if isinstance(line, dict) and isinstance(line.get('fields'), list)
and not _values(line['fields'], 'reservation_id', reverse_ids)
]
scope = str(config.get('scope') or '').strip().lower()
if scope == 'header' and header_has_reservation:
return
if scope != 'header' and not lines_missing_reservation:
return
excluded = _heuristic_excluded_values(order, supplement) | claimed
if lines_missing_reservation and header_has_reservation:
# [镜像语义] 表头已采纳的预约号值从排除集中移除——它正是要写入缺失
# 行级的值;其余订单观测值(尤其 PO)保持排除,防止 PO 误当预约号。
mirror_allow: set[str] = set()
for value in header_reservation_values:
text = str(value).strip().upper()
if text:
mirror_allow.update({text, text.lstrip('0')})
normalized = validated_reservation_id(normalize_reservation_id(text))
if normalized:
mirror_allow.update({normalized, normalized.lstrip('0')})
excluded -= mirror_allow
surviving, status = filter_reservation_heuristic_candidates(candidates, excluded_values=excluded)
if status == 'none':
return