- 新增PO号提取、预约ID解析(支持`PR@xxx`格式)工具,完善库存匹配逻辑 - 新增库存映射缓存、匹配/解析结果格式化摘要,优化用户提示 - 修复数据库迁移脚本未检查表存在的问题,避免全新部署失败 - 新增路径工具类统一管理相对路径,优化配置文件处理逻辑 - 归档旧文档,将部署、Dify配置合并到`shipping_notice.md` - 新增完整单元测试与集成测试覆盖核心流程 - 更新前端国际化与会话管理逻辑,优化界面交互
90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
from __future__ import annotations
|
||
|
||
import re
|
||
from typing import Any
|
||
|
||
# 常见 Reservation ID:纯数字,如 1000140351(通常 8~12 位)
|
||
RESERVATION_ID_NUMERIC_RE = re.compile(r"^\d{8,12}$")
|
||
# 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(
|
||
r"PR\s*Number\s*[::]?\s*([^\n\r]+)",
|
||
re.IGNORECASE,
|
||
)
|
||
|
||
|
||
def normalize_reservation_id(value: Any) -> str:
|
||
"""Normalize reservation id; Excel often stores numeric ids as float."""
|
||
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:
|
||
"""Extract reservation id from plain numeric, Excel float, or PR Number (xxx@1000140351)."""
|
||
text = str(value or "").strip()
|
||
if not text:
|
||
return ""
|
||
|
||
direct = normalize_reservation_id(text)
|
||
if looks_like_reservation_id(direct):
|
||
return direct
|
||
|
||
match = RESERVATION_AT_SUFFIX_RE.search(text)
|
||
if match:
|
||
return match.group(1)
|
||
|
||
if "@" in text:
|
||
tail = normalize_reservation_id(text.rsplit("@", 1)[-1])
|
||
if looks_like_reservation_id(tail):
|
||
return tail
|
||
|
||
return direct
|
||
|
||
|
||
def find_reservation_id_in_document(text: str) -> str:
|
||
"""Scan document text for PR Number @ reservation id or standalone @1000140351."""
|
||
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:
|
||
text = normalize_reservation_id(value)
|
||
return bool(text and RESERVATION_ID_NUMERIC_RE.fullmatch(text))
|