feat(shipping-notice): 新增发货通知重复检测与处理接口,支持重复决策保存与重新执行检测

This commit is contained in:
hugo committed 2026-07-14 21:12:36 +08:00
1 parent 729546a7b7
commit ceb49461e8
72 files changed
+14270 -460

No files matched your search

@@ -2,9 +2,9 @@
services/shipping_notice/notice_upload.py
用途:管理发货通知附件上传、文本抽取、截断与统一接入结果模型。
依赖:edkb_system 的 PDF/Docx/CSV/OCR 技能、export._storage_root、Flask current_app 配置。
依赖:edkb_system 的 PDF/Docx/CSV/OCR 技能、eml_parse、export._storage_root、Flask current_app 配置。
逻辑:workspace 单票建单、批量建单、重新上传三条入口均经 intake_notice_attachment / intake_temp_notice_uploads
收敛抽取与失败分流;下游 extract/task_workflow 只消费 NoticeIntakeResult。
收敛抽取与失败分流;.eml 走结构化解析(正文/内嵌资源/真实附件);下游只消费 NoticeIntakeResult。
"""
from __future__ import annotations
@@ -14,8 +14,6 @@ import hashlib
import re
import shutil
from dataclasses import dataclass, field
from email import policy
from email.parser import BytesParser
from pathlib import Path
from typing import Any, Literal, Sequence
@@ -24,6 +22,7 @@ from flask import current_app
from edkb_system.contracts import Block, Context
from edkb_system.skills import loader_csv, loader_docx, loader_pdf, ocr
from app.services.shipping_notice.export import _storage_root
from app.services.shipping_notice.eml_parse import parse_eml_bytes
def task_notice_upload_path(task_no: str, filename: str) -> Path:
@@ -112,32 +111,6 @@ def _html_to_text(html: str) -> str:
return s.strip()
def _eml_to_text(data: bytes) -> str:
msg = BytesParser(policy=policy.default).parsebytes(data)
parts: list[str] = []
subject = str(msg.get("subject") or "").strip()
if subject:
parts.append(f"Subject: {subject}")
if msg.is_multipart():
for part in msg.walk():
if part.is_multipart():
continue
ctype = str(part.get_content_type() or "")
payload = part.get_payload(decode=True) or b""
if ctype == "text/plain":
parts.append(_decode_text(payload).strip())
elif ctype == "text/html":
parts.append(_html_to_text(_decode_text(payload)).strip())
else:
payload = msg.get_payload(decode=True) or b""
ctype = str(msg.get_content_type() or "")
if ctype == "text/html":
parts.append(_html_to_text(_decode_text(payload)).strip())
else:
parts.append(_decode_text(payload).strip())
return "\n\n".join(x for x in parts if x)
def _image_bytes_to_blocks(raw: bytes) -> list[Block]:
if not raw:
return []
@@ -172,12 +145,23 @@ def _blocks_to_text(blocks: list[Block]) -> str:
return text
def extract_notice_text(file_path: Path) -> tuple[str, dict[str, Any]]:
def extract_notice_text(
file_path: Path,
*,
retain_payloads: bool = False,
) -> tuple[str, dict[str, Any]]:
"""
从发货通知附件抽取纯文本(PDF/DOCX/CSV/图片 OCR/EML/HTML 等)。
核心逻辑:普通文件走 edkb loaders;.eml 委托 eml_parse 做结构化解析(元数据 + 真实附件独立抽取),
内嵌 Logo/签名图不进入附件列表,也不参与订单文本拼接。
Args:
file_path: 已落盘的通知文件路径。
retain_payloads: 仅 EML;True 时保留 attachments[*]._payload_bytes 供 Intake 派生资产。
Returns:
tuple[str, dict]: (抽取文本, meta 含 warnings 与 file_type)。
tuple[str, dict]: (抽取文本, meta 含 warnings、file_type;eml 另含 email/attachments/attachment_count)。
"""
path = Path(file_path)
ext = path.suffix.lower().lstrip(".")
@@ -189,7 +173,12 @@ def extract_notice_text(file_path: Path) -> tuple[str, dict[str, Any]]:
if ext in {"html", "htm"}:
return _html_to_text(_decode_text(path.read_bytes())), {"warnings": warnings, "file_type": ext}
if ext in {"eml"}:
return _eml_to_text(path.read_bytes()), {"warnings": warnings, "file_type": ext}
# 叶附件递归抽取时复用本函数;eml_parse 通过嵌套深度上限防止 bom 炸弹式嵌套
return parse_eml_bytes(
path.read_bytes(),
extract_text=extract_notice_text,
retain_payloads=retain_payloads,
)
blocks: list[Block] = []
if ext in {"pdf"}: