feat(shipping-notice): 增加发货通知建单幂等性支持及内容哈希校验
This commit is contained in:
1 parent
0a9bd6415d
commit
143e054d86
39 files changed
+1750
-80
No files matched your search
@@ -10,6 +10,7 @@ services/shipping_notice/notice_upload.py
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import re
|
||||
import shutil
|
||||
from dataclasses import dataclass, field
|
||||
@@ -249,6 +250,8 @@ class NoticeIntakeResult:
|
||||
text: str
|
||||
warnings: list[str] = field(default_factory=list)
|
||||
meta: dict[str, Any] = field(default_factory=dict)
|
||||
content_hash: str = ""
|
||||
file_size: int = 0
|
||||
status: Literal["accepted", "empty_text", "extract_failed"] = "empty_text"
|
||||
saved_path: Path | None = None
|
||||
|
||||
@@ -263,6 +266,8 @@ class NoticeIntakeResult:
|
||||
"source_type": self.source_type,
|
||||
"source_file_name": self.filename,
|
||||
"source_file_type": self.source_file_type,
|
||||
"source_content_hash": self.content_hash,
|
||||
"file_size": self.file_size,
|
||||
"raw_text": self.text,
|
||||
"warnings": list(self.warnings),
|
||||
}
|
||||
@@ -274,6 +279,8 @@ class NoticeIntakeResult:
|
||||
"source_type": self.source_type,
|
||||
"source_file_type": self.source_file_type,
|
||||
"text_length": len(self.text),
|
||||
"content_hash": self.content_hash,
|
||||
"file_size": self.file_size,
|
||||
"warnings": list(self.warnings),
|
||||
}
|
||||
|
||||
@@ -349,6 +356,9 @@ def intake_notice_attachment(
|
||||
"""
|
||||
name = str(filename or "").strip() or "notice"
|
||||
source_type = guess_source_type(name)
|
||||
raw_bytes = raw or b""
|
||||
content_hash = hashlib.sha256(raw_bytes).hexdigest().lower()
|
||||
file_size = len(raw_bytes)
|
||||
saved_path: Path | None = None
|
||||
text = ""
|
||||
meta: dict[str, Any] = {}
|
||||
@@ -359,11 +369,11 @@ def intake_notice_attachment(
|
||||
if storage == "task":
|
||||
if not task_no:
|
||||
raise ValueError("task_no is required for task storage")
|
||||
saved_path = save_task_notice_upload(task_no, name, raw or b"")
|
||||
saved_path = save_task_notice_upload(task_no, name, raw_bytes)
|
||||
else:
|
||||
if not temp_request_id:
|
||||
raise ValueError("temp_request_id is required for temp storage")
|
||||
saved_path = save_temp_notice_upload(temp_request_id, temp_index, name, raw or b"")
|
||||
saved_path = save_temp_notice_upload(temp_request_id, temp_index, name, raw_bytes)
|
||||
text, meta = extract_notice_text(saved_path)
|
||||
warnings = list(meta.get("warnings") or []) if isinstance(meta, dict) else []
|
||||
except Exception as exc:
|
||||
@@ -376,6 +386,8 @@ def intake_notice_attachment(
|
||||
text="",
|
||||
warnings=warnings,
|
||||
meta=meta if isinstance(meta, dict) else {},
|
||||
content_hash=content_hash,
|
||||
file_size=file_size,
|
||||
status=status,
|
||||
saved_path=saved_path,
|
||||
)
|
||||
@@ -388,6 +400,8 @@ def intake_notice_attachment(
|
||||
text="",
|
||||
warnings=warnings,
|
||||
meta=meta if isinstance(meta, dict) else {},
|
||||
content_hash=content_hash,
|
||||
file_size=file_size,
|
||||
status="empty_text",
|
||||
saved_path=saved_path,
|
||||
)
|
||||
@@ -400,6 +414,8 @@ def intake_notice_attachment(
|
||||
text=text,
|
||||
warnings=warnings,
|
||||
meta=meta if isinstance(meta, dict) else {},
|
||||
content_hash=content_hash,
|
||||
file_size=file_size,
|
||||
status="accepted",
|
||||
saved_path=saved_path,
|
||||
)
|
||||
@@ -408,23 +424,37 @@ def intake_notice_attachment(
|
||||
def intake_temp_notice_uploads(
|
||||
temp_request_id: str,
|
||||
uploads: Sequence[Any],
|
||||
) -> tuple[list[NoticeIntakeResult], list[NoticeIntakeResult]]:
|
||||
) -> tuple[list[NoticeIntakeResult], list[NoticeIntakeResult], list[dict[str, Any]]]:
|
||||
"""
|
||||
批量建单入口:逐份附件接入并在 finally 外由调用方清理临时目录。
|
||||
批量建单入口:逐份附件接入、同请求内按内容哈希去重,并在 finally 外由调用方清理临时目录。
|
||||
|
||||
Args:
|
||||
temp_request_id: 请求级临时目录 ID。
|
||||
uploads: Flask request.files 中的上传对象序列。
|
||||
|
||||
Returns:
|
||||
tuple[list, list]: (accepted 结果列表, rejected 结果列表)。
|
||||
tuple: (accepted 结果列表, rejected 结果列表, duplicate_files 摘要列表)。
|
||||
"""
|
||||
accepted: list[NoticeIntakeResult] = []
|
||||
rejected: list[NoticeIntakeResult] = []
|
||||
duplicate_files: list[dict[str, Any]] = []
|
||||
seen_hashes: dict[str, str] = {}
|
||||
for index, upload in enumerate(uploads, start=1):
|
||||
raw = upload.read() or b""
|
||||
content_hash = hashlib.sha256(raw).hexdigest().lower()
|
||||
filename = str(upload.filename or "notice").strip() or "notice"
|
||||
if content_hash in seen_hashes:
|
||||
duplicate_files.append(
|
||||
{
|
||||
"filename": filename,
|
||||
"duplicate_of": seen_hashes[content_hash],
|
||||
"content_hash": content_hash,
|
||||
}
|
||||
)
|
||||
continue
|
||||
seen_hashes[content_hash] = filename
|
||||
result = intake_notice_attachment(
|
||||
filename=upload.filename or "notice",
|
||||
filename=filename,
|
||||
raw=raw,
|
||||
source_file_type=notice_source_file_type(upload),
|
||||
storage="temp",
|
||||
@@ -435,7 +465,7 @@ def intake_temp_notice_uploads(
|
||||
accepted.append(result)
|
||||
else:
|
||||
rejected.append(result)
|
||||
return accepted, rejected
|
||||
return accepted, rejected, duplicate_files
|
||||
|
||||
|
||||
def build_notice_file_summary(
|
||||
@@ -443,6 +473,7 @@ def build_notice_file_summary(
|
||||
total_files: int,
|
||||
accepted: Sequence[NoticeIntakeResult],
|
||||
rejected: Sequence[NoticeIntakeResult],
|
||||
duplicate_files: Sequence[dict[str, Any]] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
构建批量上传预检摘要,供前端回显与测试断言。
|
||||
@@ -451,16 +482,20 @@ def build_notice_file_summary(
|
||||
total_files: 请求内附件总数。
|
||||
accepted: 接入成功的结果列表。
|
||||
rejected: 接入失败的结果列表。
|
||||
duplicate_files: 同请求内内容重复、已跳过的文件摘要。
|
||||
|
||||
Returns:
|
||||
dict: total/accepted/rejected 计数与文件明细。
|
||||
dict: total/accepted/rejected/duplicate 计数与文件明细。
|
||||
"""
|
||||
accepted_files = [item.to_accepted_file_summary() for item in accepted]
|
||||
rejected_files = [item.to_rejected_file_summary() for item in rejected]
|
||||
duplicates = [dict(item) for item in (duplicate_files or [])]
|
||||
return {
|
||||
"total_files": total_files,
|
||||
"accepted_count": len(accepted_files),
|
||||
"rejected_count": len(rejected_files),
|
||||
"duplicate_count": len(duplicates),
|
||||
"accepted_files": accepted_files,
|
||||
"rejected_files": rejected_files,
|
||||
"duplicate_files": duplicates,
|
||||
}
|
||||
Reference in new issue
Block a user