feat(shipping-notice): 增强发货通知候选管理功能,支持候选选择、重复检测与业务摘要修订
This commit is contained in:
1 parent
70f684b94a
commit
b595e157e9
83 files changed
+5857
-200
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
services/shipping_notice/reservation_id.py
|
||||
|
||||
用途:提取、标准化并识别发货通知与库存中的 Reservation ID。
|
||||
用途:提取、标准化并校验发货通知与库存中的 Reservation ID。
|
||||
依赖:正则表达式与标准 `typing` 工具。
|
||||
逻辑:由 notice_lines、inventory_snapshot、match、extract 等多个解析/匹配模块复用本文件完成 Reservation ID 归一化。
|
||||
"""
|
||||
@@ -13,6 +13,14 @@ from typing import Any
|
||||
|
||||
# 常见 Reservation ID:纯数字,如 1000140351(通常 8~12 位)
|
||||
RESERVATION_ID_NUMERIC_RE = re.compile(r"^\d{8,12}$")
|
||||
# 部分库存源使用带字母/连字符的业务单号,如 AWS-311、ORD-3002、R-1。
|
||||
RESERVATION_ID_BUSINESS_RE = re.compile(r"^[A-Z0-9][A-Z0-9._/-]{1,63}$", re.IGNORECASE)
|
||||
# 数据库主键、哈希或 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,
|
||||
)
|
||||
# Wistron 等 PO:PR Number = ADU_S-ENCWIW260528126265@1000140351
|
||||
RESERVATION_AT_SUFFIX_RE = re.compile(r"@(\d{8,12})\b")
|
||||
PR_NUMBER_LINE_RE = re.compile(
|
||||
@@ -51,7 +59,7 @@ def normalize_reservation_id(value: Any) -> str:
|
||||
|
||||
|
||||
def extract_reservation_id(value: Any) -> str:
|
||||
"""从纯数字、Excel 浮点或 PR Number(xxx@1000140351)中提取 Reservation ID。"""
|
||||
"""从原值提取 Reservation ID 候选;合法性必须另由 `is_valid_reservation_id` 判断。"""
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
@@ -96,3 +104,35 @@ def looks_like_reservation_id(value: Any) -> bool:
|
||||
"""判断标准化后的值是否符合 8~12 位纯数字 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。
|
||||
|
||||
核心逻辑:默认兼容数字型与现有字母型业务单号,但明确拒绝 32 位 hex、UUID、纯字母等内部或弱语义值。
|
||||
Args:
|
||||
value: 待校验的原始值。
|
||||
numeric_only: 为 True 时仅接受 8~12 位纯数字,用于明确的 External Reservation 模板。
|
||||
|
||||
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
|
||||
if RESERVATION_ID_NUMERIC_RE.fullmatch(text):
|
||||
return True
|
||||
if numeric_only:
|
||||
return False
|
||||
return bool(
|
||||
RESERVATION_ID_BUSINESS_RE.fullmatch(text)
|
||||
and re.search(r"[A-Z]", text, re.IGNORECASE)
|
||||
and re.search(r"\d", 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 ""
|
||||
Reference in new issue
Block a user