feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
@@ -2,9 +2,10 @@
|
||||
backend/app/services/shipping_notice/duplicate_detection.py
|
||||
|
||||
用途:发货通知业务重复检测(文件哈希 + 业务指纹),产出统一 duplicate 结果。
|
||||
依赖:models.ShippingTask/FileAsset/Document/Candidate、business_fingerprint、core.response.utc_iso。
|
||||
依赖:models.ShippingNotice/FileAsset/Document/Candidate、business_fingerprint、core.response.utc_iso。
|
||||
逻辑:Intake 级与 Candidate 级共用匹配规则;候选 A 重复只写回 A,不连带阻断同信其它候选;
|
||||
刷新入口按 Candidate 重检再聚合到 Intake;检测异常可 clear 为 unchecked;
|
||||
Candidate 级可 exclude_task_ids 豁免同复核 Case 既有履约波次;
|
||||
范围限定当前 user_id,不跨用户泄露。
|
||||
"""
|
||||
|
||||
@@ -17,11 +18,11 @@ from app import db
|
||||
from app.core.response import utc_iso
|
||||
from app.models import (
|
||||
FileAsset,
|
||||
ShippingNoticeCandidate,
|
||||
ShippingNoticeDocument,
|
||||
ShippingNoticeIntake,
|
||||
ShippingOrderCandidate,
|
||||
ShippingOrderDocument,
|
||||
ShippingOrderIntake,
|
||||
ShippingOrderSnapshot,
|
||||
ShippingTask,
|
||||
ShippingNotice,
|
||||
)
|
||||
from app.services.shipping_notice.business_fingerprint import (
|
||||
CONTENT_FINGERPRINT_VERSION,
|
||||
@@ -61,7 +62,7 @@ def classify_task_lifecycle(process_status: str | None) -> str:
|
||||
return 'unknown'
|
||||
|
||||
|
||||
def is_irreversible_task(task: ShippingTask) -> bool:
|
||||
def is_irreversible_task(task: ShippingNotice) -> bool:
|
||||
"""已 uploaded 或终态任务不可原地覆盖源文件。"""
|
||||
status = str(task.process_status or '').strip().lower()
|
||||
if status in _IRREVERSIBLE_PROCESS_STATUSES:
|
||||
@@ -70,7 +71,7 @@ def is_irreversible_task(task: ShippingTask) -> bool:
|
||||
return upload_status in {'uploaded', 'succeeded', 'success'}
|
||||
|
||||
|
||||
def _match_payload(task: ShippingTask, *, match_type: str, matched_fields: list[str]) -> dict[str, Any]:
|
||||
def _match_payload(task: ShippingNotice, *, match_type: str, matched_fields: list[str]) -> dict[str, Any]:
|
||||
return {
|
||||
'task_no': task.task_no,
|
||||
'process_status': task.process_status,
|
||||
@@ -84,7 +85,7 @@ def _match_payload(task: ShippingTask, *, match_type: str, matched_fields: list[
|
||||
}
|
||||
|
||||
|
||||
def _reservation_differences(task: ShippingTask, summary: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
def _reservation_differences(task: ShippingNotice, summary: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
"""比较候选与目标冻结订单的同 Reservation 行,供人工修订确认前展示。"""
|
||||
snapshot = db.session.get(ShippingOrderSnapshot, task.order_snapshot_id) if task.order_snapshot_id else None
|
||||
if snapshot is None:
|
||||
@@ -134,7 +135,7 @@ def _allowed_actions_for(
|
||||
|
||||
def detect_duplicates_for_intake(
|
||||
*,
|
||||
intake: ShippingNoticeIntake,
|
||||
intake: ShippingOrderIntake,
|
||||
asset: FileAsset | None = None,
|
||||
business_summary: dict[str, Any] | None = None,
|
||||
fingerprint_result: dict[str, Any] | None = None,
|
||||
@@ -176,13 +177,13 @@ def detect_duplicates_for_intake(
|
||||
content_hash = str(getattr(asset, 'content_hash', '') or '').strip().lower()
|
||||
if content_hash:
|
||||
hash_tasks = (
|
||||
db.session.query(ShippingTask)
|
||||
db.session.query(ShippingNotice)
|
||||
.filter(
|
||||
ShippingTask.user_id == int(intake.user_id),
|
||||
ShippingTask.source_content_hash == content_hash,
|
||||
ShippingTask.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
ShippingNotice.user_id == int(intake.user_id),
|
||||
ShippingNotice.source_content_hash == content_hash,
|
||||
ShippingNotice.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
)
|
||||
.order_by(ShippingTask.id.desc())
|
||||
.order_by(ShippingNotice.id.desc())
|
||||
.limit(10)
|
||||
.all()
|
||||
)
|
||||
@@ -200,13 +201,13 @@ def detect_duplicates_for_intake(
|
||||
# [STEP 2] 业务指纹
|
||||
if fingerprint and strength in {'strong', 'medium'}:
|
||||
fp_tasks = (
|
||||
db.session.query(ShippingTask)
|
||||
db.session.query(ShippingNotice)
|
||||
.filter(
|
||||
ShippingTask.user_id == int(intake.user_id),
|
||||
ShippingTask.business_fingerprint == fingerprint,
|
||||
ShippingTask.fingerprint_version == version,
|
||||
ShippingNotice.user_id == int(intake.user_id),
|
||||
ShippingNotice.business_fingerprint == fingerprint,
|
||||
ShippingNotice.fingerprint_version == version,
|
||||
)
|
||||
.order_by(ShippingTask.id.desc())
|
||||
.order_by(ShippingNotice.id.desc())
|
||||
.limit(20)
|
||||
.all()
|
||||
)
|
||||
@@ -247,14 +248,14 @@ def detect_duplicates_for_intake(
|
||||
elif fingerprint and strength == 'weak' and status == 'none':
|
||||
# 弱指纹:仅标记疑似,不按 active 强阻断
|
||||
fp_tasks = (
|
||||
db.session.query(ShippingTask)
|
||||
db.session.query(ShippingNotice)
|
||||
.filter(
|
||||
ShippingTask.user_id == int(intake.user_id),
|
||||
ShippingTask.business_fingerprint == fingerprint,
|
||||
ShippingTask.fingerprint_version == version,
|
||||
ShippingTask.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
ShippingNotice.user_id == int(intake.user_id),
|
||||
ShippingNotice.business_fingerprint == fingerprint,
|
||||
ShippingNotice.fingerprint_version == version,
|
||||
ShippingNotice.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
)
|
||||
.order_by(ShippingTask.id.desc())
|
||||
.order_by(ShippingNotice.id.desc())
|
||||
.limit(5)
|
||||
.all()
|
||||
)
|
||||
@@ -278,7 +279,7 @@ def detect_duplicates_for_intake(
|
||||
if strength == 'unavailable' and status == 'none':
|
||||
blocking = False
|
||||
for match in matches:
|
||||
task = db.session.query(ShippingTask).filter_by(task_no=match['task_no']).one_or_none()
|
||||
task = db.session.query(ShippingNotice).filter_by(task_no=match['task_no']).one_or_none()
|
||||
if task is not None:
|
||||
match['reservation_differences'] = _reservation_differences(task, summary)
|
||||
|
||||
@@ -300,7 +301,7 @@ def detect_duplicates_for_intake(
|
||||
|
||||
|
||||
def apply_duplicate_result_to_intake(
|
||||
intake: ShippingNoticeIntake,
|
||||
intake: ShippingOrderIntake,
|
||||
result: dict[str, Any],
|
||||
) -> None:
|
||||
"""将检测结果写回 intake ORM 字段(不 commit)。"""
|
||||
@@ -319,7 +320,7 @@ def apply_duplicate_result_to_intake(
|
||||
intake.updated_at = datetime.utcnow()
|
||||
|
||||
|
||||
def clear_duplicate_state_to_unchecked(*, intake: ShippingNoticeIntake) -> None:
|
||||
def clear_duplicate_state_to_unchecked(*, intake: ShippingOrderIntake) -> None:
|
||||
"""
|
||||
将 Intake 与其 Candidate 的重复结果/决策统一清空为 unchecked。
|
||||
|
||||
@@ -339,8 +340,8 @@ def clear_duplicate_state_to_unchecked(*, intake: ShippingNoticeIntake) -> None:
|
||||
intake.duplicate_resolved_at = None
|
||||
intake.updated_at = now
|
||||
candidates = (
|
||||
db.session.query(ShippingNoticeCandidate)
|
||||
.filter(ShippingNoticeCandidate.intake_id == int(intake.id))
|
||||
db.session.query(ShippingOrderCandidate)
|
||||
.filter(ShippingOrderCandidate.intake_id == int(intake.id))
|
||||
.all()
|
||||
)
|
||||
for candidate in candidates:
|
||||
@@ -354,7 +355,7 @@ def clear_duplicate_state_to_unchecked(*, intake: ShippingNoticeIntake) -> None:
|
||||
|
||||
|
||||
def apply_duplicate_result_to_candidate(
|
||||
candidate: ShippingNoticeCandidate,
|
||||
candidate: ShippingOrderCandidate,
|
||||
result: dict[str, Any],
|
||||
*,
|
||||
clear_resolution: bool = True,
|
||||
@@ -388,7 +389,7 @@ def apply_duplicate_result_to_candidate(
|
||||
|
||||
def refresh_candidates_and_aggregate_intake(
|
||||
*,
|
||||
intake: ShippingNoticeIntake,
|
||||
intake: ShippingOrderIntake,
|
||||
parent_asset: FileAsset | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
@@ -402,9 +403,9 @@ def refresh_candidates_and_aggregate_intake(
|
||||
聚合后的 duplicate 结果(已 apply 到 Intake;Candidate 已写回)。
|
||||
"""
|
||||
candidates = (
|
||||
db.session.query(ShippingNoticeCandidate)
|
||||
.filter(ShippingNoticeCandidate.intake_id == int(intake.id))
|
||||
.order_by(ShippingNoticeCandidate.id.asc())
|
||||
db.session.query(ShippingOrderCandidate)
|
||||
.filter(ShippingOrderCandidate.intake_id == int(intake.id))
|
||||
.order_by(ShippingOrderCandidate.id.asc())
|
||||
.all()
|
||||
)
|
||||
if not candidates:
|
||||
@@ -428,7 +429,7 @@ def refresh_candidates_and_aggregate_intake(
|
||||
|
||||
candidate_results: list[dict[str, Any]] = []
|
||||
for candidate in candidates:
|
||||
document = db.session.get(ShippingNoticeDocument, int(candidate.primary_document_id))
|
||||
document = db.session.get(ShippingOrderDocument, int(candidate.primary_document_id))
|
||||
structured = (
|
||||
candidate.structured_result_json
|
||||
if isinstance(candidate.structured_result_json, dict)
|
||||
@@ -469,18 +470,26 @@ def refresh_candidates_and_aggregate_intake(
|
||||
|
||||
def detect_duplicates_for_candidate(
|
||||
*,
|
||||
intake: ShippingNoticeIntake,
|
||||
intake: ShippingOrderIntake,
|
||||
asset: FileAsset | None = None,
|
||||
document: ShippingNoticeDocument | None = None,
|
||||
document: ShippingOrderDocument | None = None,
|
||||
business_summary: dict[str, Any] | None = None,
|
||||
fingerprint_result: dict[str, Any] | None = None,
|
||||
content_fingerprint_result: dict[str, Any] | None = None,
|
||||
exclude_task_ids: set[int] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
对单个订单候选执行重复检测(不写 Intake,不影响同信其它候选)。
|
||||
|
||||
核心逻辑:与 detect_duplicates_for_intake 同源规则,但 exact_file 优先用
|
||||
Document/派生资产 content_hash(单 PDF),避免整封 EML 哈希误伤其它订单。
|
||||
exclude_task_ids 用于同一复核 Case 合法后续履约波次豁免本 Case 既有 Task。
|
||||
|
||||
Args:
|
||||
intake: 来源 Intake。
|
||||
asset / document: 可选文件资产与 PDF 文档。
|
||||
business_summary / fingerprint_result / content_fingerprint_result: 指纹输入。
|
||||
exclude_task_ids: 可选;忽略这些 Task(同 Case 既有波次)。
|
||||
|
||||
Returns:
|
||||
与 intake 检测相同结构的结果字典。
|
||||
@@ -494,6 +503,7 @@ def detect_duplicates_for_candidate(
|
||||
content_fp = content_fingerprint_result or build_content_fingerprint(summary)
|
||||
content_fingerprint = content_fp.get('fingerprint')
|
||||
content_version = str(content_fp.get('version') or CONTENT_FINGERPRINT_VERSION)
|
||||
skipped_ids = {int(item) for item in (exclude_task_ids or []) if item is not None}
|
||||
|
||||
content_hash = ''
|
||||
if document is not None and str(document.content_hash or '').strip():
|
||||
@@ -506,17 +516,17 @@ def detect_duplicates_for_candidate(
|
||||
|
||||
# [STEP 1] 精确文件哈希(附件级)
|
||||
if content_hash:
|
||||
hash_tasks = (
|
||||
db.session.query(ShippingTask)
|
||||
hash_query = (
|
||||
db.session.query(ShippingNotice)
|
||||
.filter(
|
||||
ShippingTask.user_id == int(intake.user_id),
|
||||
ShippingTask.source_content_hash == content_hash,
|
||||
ShippingTask.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
ShippingNotice.user_id == int(intake.user_id),
|
||||
ShippingNotice.source_content_hash == content_hash,
|
||||
ShippingNotice.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
)
|
||||
.order_by(ShippingTask.id.desc())
|
||||
.limit(10)
|
||||
.all()
|
||||
)
|
||||
if skipped_ids:
|
||||
hash_query = hash_query.filter(ShippingNotice.id.notin_(list(skipped_ids)))
|
||||
hash_tasks = hash_query.order_by(ShippingNotice.id.desc()).limit(10).all()
|
||||
for task in hash_tasks:
|
||||
matches.append(
|
||||
_match_payload(
|
||||
@@ -530,17 +540,17 @@ def detect_duplicates_for_candidate(
|
||||
|
||||
# [STEP 2] 业务指纹
|
||||
if fingerprint and strength in {'strong', 'medium'}:
|
||||
fp_tasks = (
|
||||
db.session.query(ShippingTask)
|
||||
fp_query = (
|
||||
db.session.query(ShippingNotice)
|
||||
.filter(
|
||||
ShippingTask.user_id == int(intake.user_id),
|
||||
ShippingTask.business_fingerprint == fingerprint,
|
||||
ShippingTask.fingerprint_version == version,
|
||||
ShippingNotice.user_id == int(intake.user_id),
|
||||
ShippingNotice.business_fingerprint == fingerprint,
|
||||
ShippingNotice.fingerprint_version == version,
|
||||
)
|
||||
.order_by(ShippingTask.id.desc())
|
||||
.limit(20)
|
||||
.all()
|
||||
)
|
||||
if skipped_ids:
|
||||
fp_query = fp_query.filter(ShippingNotice.id.notin_(list(skipped_ids)))
|
||||
fp_tasks = fp_query.order_by(ShippingNotice.id.desc()).limit(20).all()
|
||||
matched_fields = list(components.keys())
|
||||
for task in fp_tasks:
|
||||
if any(m.get('task_no') == task.task_no for m in matches):
|
||||
@@ -572,18 +582,18 @@ def detect_duplicates_for_candidate(
|
||||
else:
|
||||
status = 'possible_duplicate'
|
||||
elif fingerprint and strength == 'weak' and status == 'none':
|
||||
fp_tasks = (
|
||||
db.session.query(ShippingTask)
|
||||
weak_query = (
|
||||
db.session.query(ShippingNotice)
|
||||
.filter(
|
||||
ShippingTask.user_id == int(intake.user_id),
|
||||
ShippingTask.business_fingerprint == fingerprint,
|
||||
ShippingTask.fingerprint_version == version,
|
||||
ShippingTask.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
ShippingNotice.user_id == int(intake.user_id),
|
||||
ShippingNotice.business_fingerprint == fingerprint,
|
||||
ShippingNotice.fingerprint_version == version,
|
||||
ShippingNotice.process_status.notin_(list(_CLOSED_PROCESS_STATUSES)),
|
||||
)
|
||||
.order_by(ShippingTask.id.desc())
|
||||
.limit(5)
|
||||
.all()
|
||||
)
|
||||
if skipped_ids:
|
||||
weak_query = weak_query.filter(ShippingNotice.id.notin_(list(skipped_ids)))
|
||||
fp_tasks = weak_query.order_by(ShippingNotice.id.desc()).limit(5).all()
|
||||
for task in fp_tasks:
|
||||
matches.append(
|
||||
_match_payload(
|
||||
@@ -606,7 +616,7 @@ def detect_duplicates_for_candidate(
|
||||
if strength == 'unavailable' and status == 'none':
|
||||
blocking = False
|
||||
for match in matches:
|
||||
task = db.session.query(ShippingTask).filter_by(task_no=match['task_no']).one_or_none()
|
||||
task = db.session.query(ShippingNotice).filter_by(task_no=match['task_no']).one_or_none()
|
||||
if task is not None:
|
||||
match['reservation_differences'] = _reservation_differences(task, summary)
|
||||
|
||||
@@ -630,7 +640,7 @@ def detect_duplicates_for_candidate(
|
||||
|
||||
|
||||
def aggregate_intake_duplicate_from_candidates(
|
||||
intake: ShippingNoticeIntake,
|
||||
intake: ShippingOrderIntake,
|
||||
candidate_results: list[dict[str, Any]],
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user