feat(shipping-notice): 完善订单解析、模板管理与库存 RPA 流程
- 优化订单文档解析、补充合并与坐标重建 - 完善订单模板校验、发布及前端管理界面 - 改进库存 RPA 下载回退与工作流处理 - 补充后端、前端及 RPA 自动化测试 - 更新 Shipping Notice 技术文档与四语言文案
This commit is contained in:
1 parent
c4a27324c8
commit
8f2f568f92
46 files changed
+1745
-265
No files matched your search
@@ -1,9 +1,11 @@
|
||||
"""
|
||||
Reservation ID 专用证据区域解析器。
|
||||
|
||||
用途:从管理员圈选的 Vendor Comments 区域提取 Reservation ID 和可选行号。
|
||||
依赖:reservation_id 的统一格式校验。
|
||||
逻辑:只消费区域文字,不依赖客户名称、固定坐标或旧的三来源映射;无行号仅标记为未归属,最终由 Schema scope 决定表头写入或明细复核。
|
||||
用途:从已启用的 Vendor Comments→Terms 锚点窗提取 Reservation ID 与可选行号,
|
||||
并投影到验证证据 / 订单归属契约。
|
||||
依赖:reservation_id 格式校验、coordinate_region_text 视觉行序。
|
||||
逻辑:reservation_id_region 仅作启用标记;画布预览、盲测、正式解析共用本模块的
|
||||
锚点扫描与 apply/project,不再按保存矩形截取正式值。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -19,8 +21,11 @@ from app.services.shipping_notice.reservation_id import validated_reservation_id
|
||||
_ID_RE = re.compile(r"(?<!\d)(\d{5}|\d{10})(?!\d)")
|
||||
_LINE_RE = re.compile(r"\b(?:reservation\s+)?line\s*#?\s*(\d+)\b|\bfor\s+line\s*#?\s*(\d+)\b", re.I)
|
||||
_RESERVATION_LABEL_RE = re.compile(r"\breservation\b\s*(?:id)?\s*(?:#|:|-)?", re.I)
|
||||
_VENDOR_COMMENTS_HEADING_RE = re.compile(r'^\s*vendor\s+comments?\s*:?\s*$', re.I)
|
||||
_TERMS_AND_CONDITIONS_HEADING_RE = re.compile(r'^\s*terms?\s+and\s+conditions?\s*:?\s*$', re.I)
|
||||
_VENDOR_COMMENTS_HEADING_RE = re.compile(r'^\s*vendor\s+comments?\b', re.I)
|
||||
_TERMS_AND_CONDITIONS_HEADING_RE = re.compile(
|
||||
r'^\s*(?:\d+[\.\)]\s*)?terms?\s*(?:&|and)\s*conditions?\b',
|
||||
re.I,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -86,22 +91,147 @@ def extract_reservation_from_vendor_comments_pages(
|
||||
"""从跨页 Vendor Comments 区块提取 Reservation;仅首个 Terms 标题前的文字有效。
|
||||
|
||||
核心逻辑:Reservation 专区已启用时,页面坐标只是提供稳定阅读顺序,不能作为固定
|
||||
页码或矩形取值依据。缺少起止锚点时返回稳定原因,由调用方转人工复核。
|
||||
页码或矩形取值依据。起止标题按行首短语匹配(允许「Terms and Conditions of …」等
|
||||
加长标题);缺少起始锚点时不扫描;缺少结束锚点时仍返回已累计窗内候选,但附带稳
|
||||
定原因,由调用方标 Review,禁止当成可自动建单封闭证据。
|
||||
"""
|
||||
started = False
|
||||
window_lines: list[str] = []
|
||||
for _page_no, glyphs in sorted(pages, key=lambda item: item[0]):
|
||||
for line in visual_text_lines(glyphs):
|
||||
if not started:
|
||||
if _VENDOR_COMMENTS_HEADING_RE.fullmatch(line):
|
||||
if _VENDOR_COMMENTS_HEADING_RE.match(line):
|
||||
started = True
|
||||
continue
|
||||
if _TERMS_AND_CONDITIONS_HEADING_RE.fullmatch(line):
|
||||
if _TERMS_AND_CONDITIONS_HEADING_RE.match(line):
|
||||
return extract_reservation_from_region_text('\n'.join(window_lines)), None
|
||||
window_lines.append(line)
|
||||
if not started:
|
||||
return [], 'vendor_comments_anchor_missing'
|
||||
return [], 'terms_and_conditions_anchor_missing'
|
||||
# 结束锚点缺失:保留窗内候选供验证/盲测展示与人工复核,不能假装封闭成功。
|
||||
return extract_reservation_from_region_text('\n'.join(window_lines)), 'terms_and_conditions_anchor_missing'
|
||||
|
||||
|
||||
def reservation_region_candidates_from_layout(
|
||||
*,
|
||||
definition: dict[str, Any] | None,
|
||||
layout: dict[str, Any] | None,
|
||||
) -> dict[str, Any]:
|
||||
"""仅当模板标注了 reservation_id_region 时,按 Vendor Comments→Terms 提取候选。
|
||||
|
||||
核心逻辑:page_no/region 只表示专区已启用,正式取值必须与画布预览、盲测、
|
||||
正式解析共用同一锚点窗,禁止再按保存矩形截取。
|
||||
|
||||
Args:
|
||||
definition: Profile definition;无 reservation_id_region 时不扫描。
|
||||
layout: 含 coordinate_evidence.page_glyphs 的版面快照。
|
||||
|
||||
Returns:
|
||||
``{'candidates': [...], 'reason': str | None}``;reason 非空表示锚点异常。
|
||||
"""
|
||||
raw = definition if isinstance(definition, dict) else {}
|
||||
if not isinstance(raw.get('reservation_id_region'), dict):
|
||||
return {'candidates': [], 'reason': None}
|
||||
evidence = layout.get('coordinate_evidence') if isinstance(layout, dict) else None
|
||||
evidence = evidence if isinstance(evidence, dict) else {}
|
||||
page_glyphs = evidence.get('page_glyphs') if isinstance(evidence.get('page_glyphs'), dict) else {}
|
||||
pages = [
|
||||
(int(page_no), glyphs)
|
||||
for page_no, glyphs in page_glyphs.items()
|
||||
if str(page_no).isdigit() and isinstance(glyphs, list)
|
||||
]
|
||||
candidates, reason = extract_reservation_from_vendor_comments_pages(pages)
|
||||
return {'candidates': candidates, 'reason': reason}
|
||||
|
||||
|
||||
def project_reservation_onto_validation_evidence(
|
||||
evidence: dict[str, Any],
|
||||
*,
|
||||
candidates: list[dict[str, object]],
|
||||
reason: str | None,
|
||||
schema_fields: dict[str, dict[str, Any]],
|
||||
) -> dict[str, Any]:
|
||||
"""将专区候选投影到验证页共用的 header_fields / lines 证据 DTO。
|
||||
|
||||
核心逻辑:复用 apply_reservation_region_candidates 归属契约;锚点缺失或冲突时
|
||||
写入 reservation_region_*,不臆造字段值。
|
||||
|
||||
Args:
|
||||
evidence: 现有 validation_evidence。
|
||||
candidates: 锚点窗候选。
|
||||
reason: 锚点扫描失败原因;有值时强制 needs_review。
|
||||
schema_fields: field_id → Schema 字段(须含 enabled reservation_id)。
|
||||
|
||||
Returns:
|
||||
新的 evidence 字典;未启用专区字段时仅透传或附加 review 元数据。
|
||||
"""
|
||||
projected = {
|
||||
'header_fields': [
|
||||
dict(item) for item in (evidence.get('header_fields') or []) if isinstance(item, dict)
|
||||
],
|
||||
'lines': [
|
||||
{
|
||||
'row_index': int(line.get('row_index') or index),
|
||||
'fields': [
|
||||
dict(field) for field in (line.get('fields') or []) if isinstance(field, dict)
|
||||
],
|
||||
}
|
||||
for index, line in enumerate(evidence.get('lines') or [])
|
||||
if isinstance(line, dict)
|
||||
],
|
||||
'cross_checks': dict(evidence.get('cross_checks') or {}),
|
||||
}
|
||||
reservation_scope = {
|
||||
str(item.get('scope') or ''): item
|
||||
for item in schema_fields.values()
|
||||
if isinstance(item, dict) and str(item.get('field_code') or '') == 'reservation_id'
|
||||
}
|
||||
if reason:
|
||||
projected['reservation_region_status'] = 'needs_review'
|
||||
projected['reservation_region_review_reasons'] = [str(reason)]
|
||||
if not candidates and not reason:
|
||||
return projected
|
||||
|
||||
applied = apply_reservation_region_candidates(
|
||||
candidates=candidates,
|
||||
header_field_id=str((reservation_scope.get('header') or {}).get('field_id') or '') or None,
|
||||
line_field_id=str((reservation_scope.get('line') or {}).get('field_id') or '') or None,
|
||||
line_count=len(projected['lines']),
|
||||
)
|
||||
header_target = reservation_scope.get('header')
|
||||
if applied.header_value and header_target:
|
||||
projected['header_fields'] = [
|
||||
item for item in projected['header_fields']
|
||||
if str(item.get('field_code') or '') != 'reservation_id'
|
||||
] + [{
|
||||
'field_id': str(header_target.get('field_id') or ''),
|
||||
'field_code': 'reservation_id',
|
||||
'raw_value': applied.header_value,
|
||||
'normalized_value': applied.header_value,
|
||||
'status': 'passed',
|
||||
'issues': [],
|
||||
}]
|
||||
line_target = reservation_scope.get('line')
|
||||
if line_target:
|
||||
for line_no, value in applied.line_values.items():
|
||||
if line_no < 1 or line_no > len(projected['lines']):
|
||||
continue
|
||||
line = projected['lines'][line_no - 1]
|
||||
line['fields'] = [
|
||||
item for item in line['fields']
|
||||
if str(item.get('field_code') or '') != 'reservation_id'
|
||||
] + [{
|
||||
'field_id': str(line_target.get('field_id') or ''),
|
||||
'field_code': 'reservation_id',
|
||||
'raw_value': value,
|
||||
'normalized_value': value,
|
||||
'status': 'passed',
|
||||
'issues': [],
|
||||
}]
|
||||
if applied.needs_review:
|
||||
projected['reservation_region_status'] = 'needs_review'
|
||||
projected['reservation_region_review_reasons'] = list(applied.review_reasons)
|
||||
return projected
|
||||
|
||||
|
||||
def apply_reservation_region_candidates(
|
||||
|
||||
Reference in new issue
Block a user