154 lines
5.6 KiB
Python
154 lines
5.6 KiB
Python
"""
|
||
services/shipping_notice/reservation_id.py
|
||
|
||
用途:提取、标准化并校验发货通知与库存中的 Reservation ID。
|
||
依赖:正则表达式与标准 `typing` 工具。
|
||
逻辑:由 notice_lines、inventory_snapshot、match、extract、EML 补充解析等多个模块复用;
|
||
归一结果恒为 5/10 位纯数字(可剥括号备注、@ 前缀、短数字行序后缀如 35354-1)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
from typing import Any
|
||
|
||
# Reservation ID 业务契约固定为 5 位或 10 位纯数字。
|
||
RESERVATION_ID_NUMERIC_RE = re.compile(r"^(?:\d{5}|\d{10})$")
|
||
# 数据库主键、哈希或 UUID 不属于业务 Reservation ID。
|
||
INTERNAL_HEX_ID_RE = re.compile(r"^[0-9a-f]{32}$", re.IGNORECASE)
|
||
INTERNAL_UUID_RE = re.compile(
|
||
r"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$",
|
||
re.IGNORECASE,
|
||
)
|
||
# Reservation 本体后可带一段半角或全角括号备注;普通尾随文本不属于合法值。
|
||
RESERVATION_VALUE_RE = re.compile(r"^(\d{10}|\d{5})(?!\d)\s*(?:\([^()\r\n]*\)|([^()\r\n]*))?\s*$")
|
||
# ``@`` 是预约号与前缀说明的显式分隔,后缀复用 Reservation 值的括号备注契约。
|
||
RESERVATION_AT_SUFFIX_RE = re.compile(r"@\s*(\d{10}|\d{5})(?!\d)(?=\s*(?:\([^()\r\n]*\)|([^()\r\n]*))?\s*$)")
|
||
# QUANTA IR# 等:合法 5/10 位本体 + 一段短数字行序后缀(- / - / _);多段后缀拒绝。
|
||
RESERVATION_SHORT_NUMERIC_SUFFIX_RE = re.compile(r"^(\d{10}|\d{5})[--_](\d+)$")
|
||
PR_NUMBER_LINE_RE = re.compile(
|
||
r"PR\s*Number\s*[::]?\s*([^\n\r]+)",
|
||
re.IGNORECASE,
|
||
)
|
||
|
||
|
||
def normalize_reservation_id(value: Any) -> str:
|
||
"""标准化 Reservation ID;处理 Excel 浮点、千分位与 x.0 尾缀。"""
|
||
if value is None:
|
||
return ""
|
||
|
||
if isinstance(value, bool):
|
||
return ""
|
||
|
||
if isinstance(value, float):
|
||
if value != value: # NaN
|
||
return ""
|
||
if value.is_integer():
|
||
return str(int(value))
|
||
text = str(value).strip()
|
||
elif isinstance(value, int):
|
||
text = str(value)
|
||
else:
|
||
text = str(value).strip()
|
||
|
||
if not text:
|
||
return ""
|
||
|
||
text = text.replace(",", "").replace(",", "").strip()
|
||
if re.fullmatch(r"\d+\.0+", text):
|
||
text = text.split(".", 1)[0]
|
||
|
||
return text
|
||
|
||
|
||
def extract_reservation_id(value: Any) -> str:
|
||
"""标准化 Reservation ID,并识别括号备注、``@`` 分隔与短数字行序后缀。
|
||
|
||
核心逻辑:先匹配纯 5/10 位(可带括号备注);再搜 ``@`` 后本体;最后剥离
|
||
一段 ``-``/``-``/``_`` + 数字后缀(如 ``35354-1`` → ``35354``)。多段后缀不采纳。
|
||
|
||
Args:
|
||
value: Reservation ID 字段原始值。
|
||
|
||
Returns:
|
||
str: 已确认的五位或十位纯数字 Reservation ID;无法安全抽取时返回空字符串。
|
||
"""
|
||
text = str(value or "").strip()
|
||
if not text:
|
||
return ""
|
||
|
||
# [STEP 1] 整体纯数字(可选括号备注)
|
||
direct = normalize_reservation_id(text)
|
||
direct_match = RESERVATION_VALUE_RE.fullmatch(direct)
|
||
if direct_match:
|
||
return direct_match.group(1)
|
||
|
||
# [STEP 2] 显式 @ 分隔(可带括号备注)
|
||
match = RESERVATION_AT_SUFFIX_RE.search(text)
|
||
if match:
|
||
return match.group(1)
|
||
|
||
# [STEP 3] 短数字行序后缀(IR# 35354-1);仅一段后缀
|
||
suffix_match = RESERVATION_SHORT_NUMERIC_SUFFIX_RE.fullmatch(direct)
|
||
if suffix_match:
|
||
return suffix_match.group(1)
|
||
|
||
return ""
|
||
|
||
|
||
def find_reservation_id_in_document(text: str) -> str:
|
||
"""扫描全文 PR Number 行或 ``@`` 分隔、可带括号备注的 Reservation ID。
|
||
|
||
Args:
|
||
text: 文档原始文本;仅接受 ``@`` 后可带括号备注的完整五位或十位纯数字。
|
||
|
||
Returns:
|
||
str: 已验证的五位或十位纯数字 Reservation ID;无明确分隔证据时返回空字符串。
|
||
"""
|
||
body = str(text or "")
|
||
if not body.strip():
|
||
return ""
|
||
|
||
pr_match = PR_NUMBER_LINE_RE.search(body)
|
||
if pr_match:
|
||
extracted = extract_reservation_id(pr_match.group(1))
|
||
if extracted and looks_like_reservation_id(extracted):
|
||
return extracted
|
||
|
||
for match in RESERVATION_AT_SUFFIX_RE.finditer(body):
|
||
candidate = match.group(1)
|
||
if looks_like_reservation_id(candidate):
|
||
return candidate
|
||
|
||
return ""
|
||
|
||
|
||
def looks_like_reservation_id(value: Any) -> bool:
|
||
"""判断标准化后的值是否符合 5 位或 10 位纯数字 Reservation ID 格式。"""
|
||
text = normalize_reservation_id(value)
|
||
return bool(text and RESERVATION_ID_NUMERIC_RE.fullmatch(text))
|
||
|
||
|
||
def is_valid_reservation_id(value: Any, *, numeric_only: bool = False) -> bool:
|
||
"""
|
||
判断候选值是否可作为业务 Reservation ID。
|
||
|
||
核心逻辑:只接受 5 位或 10 位纯数字;字母型编号、其他长度数字、hex 和 UUID 均拒绝。
|
||
Args:
|
||
value: 待校验的原始值。
|
||
numeric_only: 兼容既有调用签名;当前统一契约始终只接受 5 位或 10 位纯数字。
|
||
|
||
Returns:
|
||
bool: 值是否满足当前 Reservation ID 契约。
|
||
"""
|
||
text = extract_reservation_id(value)
|
||
if not text or INTERNAL_HEX_ID_RE.fullmatch(text) or INTERNAL_UUID_RE.fullmatch(text):
|
||
return False
|
||
return bool(RESERVATION_ID_NUMERIC_RE.fullmatch(text))
|
||
|
||
|
||
def validated_reservation_id(value: Any, *, numeric_only: bool = False) -> str:
|
||
"""提取并返回合法 Reservation ID;候选不满足契约时返回空字符串。"""
|
||
candidate = extract_reservation_id(value)
|
||
return candidate if is_valid_reservation_id(candidate, numeric_only=numeric_only) else ""
|