fix(shipping-notice): 修复订单解析签名与库存 RPA 导出流程
- 修正订单提取运行与模板签名处理 - 优化库存 RPA 归档及报表导出逻辑 - 补充库存参数、归档、运行与签名测试 - 更新 Shipping Notice 技术文档
This commit is contained in:
1 parent
dabf140e41
commit
2bb7822247
10 files changed
+300
-43
No files matched your search
@@ -13,7 +13,7 @@ from __future__ import annotations
|
||||
import json
|
||||
import re
|
||||
import zipfile
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -23,6 +23,7 @@ from app import db
|
||||
from app.core.errors import NotFoundError
|
||||
from app.models import ScheduleRun, ShippingInventoryFile
|
||||
from app.services.schedule.shipping_inventory_rpa_finalize import is_shipping_inventory_rpa_task
|
||||
from app.services.schedule.timezone_util import resolve_schedule_tzinfo
|
||||
|
||||
_RAW_ZIP_NOT_FOUND_MESSAGE = "原始库存包不存在,请重新执行或联系管理员"
|
||||
|
||||
@@ -52,26 +53,38 @@ def get_inventory_archive_dir() -> str:
|
||||
|
||||
def _attachment_when_from_run(run: ScheduleRun) -> datetime:
|
||||
"""
|
||||
推导企微/下载展示名中的时刻。
|
||||
推导企微/下载展示名中的当地时刻。
|
||||
|
||||
核心逻辑:优先 started_at → created_at → finished_at;再尝试解析 RUN{yyyyMMddHHmmss}*;
|
||||
最后回退 utcnow。完整 run_no 仍留在卡片与磁盘归档键,不塞进展示名。
|
||||
核心逻辑:started_at → created_at → finished_at → 解析 RUN{yyyyMMddHHmmss}* → utcnow;
|
||||
上述时刻在库内均为 UTC naive(含 run_no 时间戳),格式化前转到任务
|
||||
schedule_timezone(缺省 Asia/Shanghai)。完整 run_no 仍留在卡片与磁盘归档键。
|
||||
"""
|
||||
when_utc: datetime | None = None
|
||||
for value in (run.started_at, run.created_at, run.finished_at):
|
||||
if isinstance(value, datetime):
|
||||
return value
|
||||
match = re.match(r"^RUN(\d{14})", str(run.run_no or "").strip())
|
||||
if match:
|
||||
try:
|
||||
return datetime.strptime(match.group(1), "%Y%m%d%H%M%S")
|
||||
except ValueError:
|
||||
pass
|
||||
return datetime.utcnow()
|
||||
when_utc = value
|
||||
break
|
||||
if when_utc is None:
|
||||
match = re.match(r"^RUN(\d{14})", str(run.run_no or "").strip())
|
||||
if match:
|
||||
try:
|
||||
when_utc = datetime.strptime(match.group(1), "%Y%m%d%H%M%S")
|
||||
except ValueError:
|
||||
when_utc = None
|
||||
if when_utc is None:
|
||||
when_utc = datetime.now(timezone.utc).replace(tzinfo=None)
|
||||
|
||||
task = getattr(run, "task", None)
|
||||
tz_name = getattr(task, "schedule_timezone", None) if task is not None else None
|
||||
tz = resolve_schedule_tzinfo(tz_name)
|
||||
if when_utc.tzinfo is None:
|
||||
when_utc = when_utc.replace(tzinfo=timezone.utc)
|
||||
return when_utc.astimezone(tz)
|
||||
|
||||
|
||||
def inventory_rpa_attachment_stem(run: ScheduleRun) -> str:
|
||||
"""
|
||||
库存 RPA 企微附件共用前缀:库存RPA-{yyyyMMdd}-{HHmm}。
|
||||
库存 RPA 企微附件共用前缀:库存RPA-{yyyyMMdd}-{HHmm}(业务时区当地时间)。
|
||||
|
||||
Args:
|
||||
run: 排程执行记录。
|
||||
|
||||
@@ -6,6 +6,7 @@ services/shipping_notice/document_layout/order_profile_signature.py
|
||||
逻辑:由 validation_service 在发布检查前调用;本模块不负责数据库提交或 Profile 引用更新。
|
||||
另提供 clip_table_bbox_to_business_rows,供签名观测与运行时 matcher 共用。
|
||||
单逻辑表头行将格内折行副标签按 x 并入所属列,保留文案、不拆成新身份列。
|
||||
table_patterns 仅纳入 business_lines;line_field_supplement 不进匹配身份。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -198,8 +199,9 @@ def build_layout_signature(
|
||||
"""
|
||||
生成版面签名快照;相同输入必须得到相同 checksum。
|
||||
|
||||
核心逻辑:解析模板表区/表头/字段关系 → 从样本区域读取固定文字 → 多样本聚合
|
||||
中位数与容差 → 过滤动态值与单样本噪声 → 计算稳定 checksum。
|
||||
核心逻辑:解析模板表区/表头/字段关系 → 仅对 business_lines 建 table_patterns →
|
||||
从样本区域读取固定文字 → 多样本聚合中位数与容差 → 过滤动态值与单样本噪声 →
|
||||
计算稳定 checksum。line_field_supplement 只服务按 LN 补料号,不进匹配身份。
|
||||
|
||||
Args:
|
||||
definition: 当前草稿或冻结 Profile 定义 JSON。
|
||||
@@ -225,11 +227,15 @@ def build_layout_signature(
|
||||
order_field_layouts=definition.get('order_field_layouts') or [],
|
||||
)
|
||||
|
||||
# [STEP 2] 页面与表区几何模式
|
||||
# [STEP 2] 页面与主表明细几何模式(排除 line_field_supplement)
|
||||
page_patterns = _build_page_patterns(sample_layouts=sample_layouts)
|
||||
matching_tables = [
|
||||
table for table in profile.table_regions
|
||||
if str(getattr(table, 'region_role', None) or 'business_lines') == 'business_lines'
|
||||
]
|
||||
table_patterns = [
|
||||
_build_table_pattern(table=table, sample_layouts=sample_layouts)
|
||||
for table in profile.table_regions
|
||||
for table in matching_tables
|
||||
]
|
||||
field_relation_graph = _build_field_relation_graph(profile, sample_layouts=sample_layouts)
|
||||
evidence_quality = _build_evidence_quality(
|
||||
|
||||
@@ -1204,6 +1204,43 @@ def order_extraction_layouts_pending(run: ShippingOrderProcessingRun) -> bool:
|
||||
_EXTRACT_DONE_UNIT_STATUSES = frozenset({
|
||||
'candidate_ready', 'review_required', 'failed', 'cancelled',
|
||||
})
|
||||
# layout 与 extraction 共 shipping 队列且默认 priority=0;resume 抬高后才能插队,
|
||||
# 否则「单文件版式完成」的 extraction 仍排在剩余 layout FIFO 之后。
|
||||
_ORDER_EXTRACTION_READY_PRIORITY = 10
|
||||
|
||||
|
||||
def _enqueue_ready_order_extraction_job(
|
||||
run: ShippingOrderProcessingRun, *, message: str,
|
||||
) -> int:
|
||||
"""
|
||||
为已有 ready 活的 Run 入队(或复用)extraction Job,并抬高 queued 优先级以便插队。
|
||||
|
||||
核心逻辑:enqueue 去重可能返回仍在排队的早建 Job;必须把其 priority 提到
|
||||
_ORDER_EXTRACTION_READY_PRIORITY,否则 claim 仍按 created_at 先消化 layout。
|
||||
"""
|
||||
from app.jobs.queue import enqueue
|
||||
from app.jobs.types import JobType
|
||||
|
||||
job_id = enqueue(
|
||||
JobType.SHIPPING_ORDER_EXTRACTION,
|
||||
{'run_no': run.run_no},
|
||||
domain='shipping_notice',
|
||||
business_ref=run.run_no,
|
||||
created_by_id=run.user_id,
|
||||
priority=_ORDER_EXTRACTION_READY_PRIORITY,
|
||||
max_attempts=3,
|
||||
message=message,
|
||||
commit=False,
|
||||
)
|
||||
job = db.session.get(Job, int(job_id))
|
||||
if (
|
||||
job is not None
|
||||
and str(job.status or '') == 'queued'
|
||||
and int(job.priority or 0) < _ORDER_EXTRACTION_READY_PRIORITY
|
||||
):
|
||||
job.priority = _ORDER_EXTRACTION_READY_PRIORITY
|
||||
job.message = message
|
||||
return int(job_id)
|
||||
|
||||
|
||||
def order_extraction_ready_document_ids(run: ShippingOrderProcessingRun) -> set[int]:
|
||||
@@ -1360,12 +1397,9 @@ def resume_runs_waiting_for_layout(*, document_id: int) -> int:
|
||||
单份附件布局完成后唤醒仍等待版式的 Run。
|
||||
|
||||
核心逻辑:不再要求 Run 内全部 PDF 版式就绪;只要存在可抽取的 ready Document,
|
||||
即入队(或复用活跃)shipping_order_extraction。无 ready 活且无 pending、仅有失败版式、
|
||||
且尚未抽出任何 Unit 时,整 Run 失败收口。
|
||||
即以抬高 priority 入队(或复用活跃)shipping_order_extraction,避免被剩余 layout
|
||||
FIFO 堵住。无 ready 活且无 pending、仅有失败版式、且尚未抽出任何 Unit 时整 Run 失败。
|
||||
"""
|
||||
from app.jobs.queue import enqueue
|
||||
from app.jobs.types import JobType
|
||||
|
||||
resumed = 0
|
||||
# processing + waiting_layout:部分 Unit 已抽完、仍等兄弟文件版式。
|
||||
runs = (
|
||||
@@ -1380,11 +1414,8 @@ def resume_runs_waiting_for_layout(*, document_id: int) -> int:
|
||||
if int(document_id) not in _run_snapshot_document_ids(run):
|
||||
continue
|
||||
if order_extraction_has_extractable_ready_work(run):
|
||||
job_id = enqueue(
|
||||
JobType.SHIPPING_ORDER_EXTRACTION,
|
||||
{'run_no': run.run_no}, domain='shipping_notice', business_ref=run.run_no,
|
||||
created_by_id=run.user_id, max_attempts=3,
|
||||
message='附件解析完成,等待订单识别', commit=False,
|
||||
job_id = _enqueue_ready_order_extraction_job(
|
||||
run, message='附件解析完成,等待订单识别',
|
||||
)
|
||||
run.current_job_id = job_id
|
||||
# 仍有兄弟 layout pending 时保持 waiting_layout,便于再次 resume;
|
||||
@@ -1410,11 +1441,8 @@ def resume_runs_waiting_for_layout(*, document_id: int) -> int:
|
||||
)
|
||||
if progressed:
|
||||
# 已有 Unit 成功/复核:再入队一次让 Worker 收口,禁止整 Run 抹掉部分成果。
|
||||
job_id = enqueue(
|
||||
JobType.SHIPPING_ORDER_EXTRACTION,
|
||||
{'run_no': run.run_no}, domain='shipping_notice', business_ref=run.run_no,
|
||||
created_by_id=run.user_id, max_attempts=3,
|
||||
message='部分附件版式失败,等待订单识别收口', commit=False,
|
||||
job_id = _enqueue_ready_order_extraction_job(
|
||||
run, message='部分附件版式失败,等待订单识别收口',
|
||||
)
|
||||
run.current_job_id = job_id
|
||||
run.phase = 'queued'
|
||||
|
||||
Reference in new issue
Block a user