feat(shipping-notice): 增强发货通知候选管理功能,支持候选选择、重复检测与业务摘要修订

This commit is contained in:
hugo committed 2026-07-15 11:57:04 +08:00
1 parent 70f684b94a
commit b595e157e9
83 files changed
+5857 -200

No files matched your search

@@ -22,7 +22,12 @@ from app.models import (
ShippingNoticeIntake,
ShippingTask,
)
from app.services.shipping_notice.business_fingerprint import FINGERPRINT_VERSION, build_business_fingerprint
from app.services.shipping_notice.business_fingerprint import (
CONTENT_FINGERPRINT_VERSION,
FINGERPRINT_VERSION,
build_business_fingerprint,
build_content_fingerprint,
)
# 与 state_machine 主路径对齐的活跃/完成/关闭态(禁止 needs_review/matching/export_ready 等非主状态)
_ACTIVE_PROCESS_STATUSES = frozenset(
@@ -72,6 +77,8 @@ def _match_payload(task: ShippingTask, *, match_type: str, matched_fields: list[
'matched_fields': list(matched_fields),
'lifecycle': classify_task_lifecycle(task.process_status),
'created_at': utc_iso(task.created_at),
'content_fingerprint': task.content_fingerprint,
'content_fingerprint_version': task.content_fingerprint_version,
}
@@ -99,6 +106,7 @@ def detect_duplicates_for_intake(
asset: FileAsset | None = None,
business_summary: dict[str, Any] | None = None,
fingerprint_result: dict[str, Any] | None = None,
content_fingerprint_result: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""
对已解析 intake 执行重复检测。
@@ -122,6 +130,9 @@ def detect_duplicates_for_intake(
fingerprint = fp.get('fingerprint')
version = str(fp.get('version') or FINGERPRINT_VERSION)
components = fp.get('components') if isinstance(fp.get('components'), dict) else {}
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)
if asset is None:
asset = db.session.get(FileAsset, int(intake.asset_id))
@@ -180,13 +191,24 @@ def detect_duplicates_for_intake(
)
)
if matches and status == 'none':
primary_life = classify_task_lifecycle(matches[0].get('process_status'))
if primary_life == 'active':
status = 'same_active_task'
elif primary_life == 'completed':
status = 'same_completed_task'
comparable = [
task for task in fp_tasks
if task.content_fingerprint and task.content_fingerprint_version == content_version
]
if content_fingerprint and comparable:
status = (
'same_business_content'
if any(task.content_fingerprint == content_fingerprint for task in comparable)
else 'possible_revision'
)
else:
status = 'possible_duplicate'
primary_life = classify_task_lifecycle(matches[0].get('process_status'))
if primary_life == 'active':
status = 'same_active_task'
elif primary_life == 'completed':
status = 'same_completed_task'
else:
status = 'possible_duplicate'
elif matches and status == 'exact_file':
# 同文件命中优先;保留 status=exact_file
pass
@@ -315,10 +337,16 @@ def apply_duplicate_result_to_candidate(
candidate.fingerprint_version = result.get('fingerprint_version')
if result.get('fingerprint_strength') is not None:
candidate.fingerprint_strength = result.get('fingerprint_strength')
if result.get('content_fingerprint') is not None:
candidate.content_fingerprint = result.get('content_fingerprint')
if result.get('content_fingerprint_version') is not None:
candidate.content_fingerprint_version = result.get('content_fingerprint_version')
if clear_resolution:
candidate.duplicate_resolution = None
candidate.duplicate_resolution_reason = None
candidate.duplicate_target_task_no = None
candidate.duplicate_resolved_by_id = None
candidate.duplicate_resolved_at = None
candidate.updated_at = datetime.utcnow()
@@ -351,11 +379,13 @@ def refresh_candidates_and_aggregate_intake(
)
summary = summary if isinstance(summary, dict) else {}
fingerprint = build_business_fingerprint(summary)
content_fingerprint = build_content_fingerprint(summary)
result = detect_duplicates_for_intake(
intake=intake,
asset=parent_asset,
business_summary=summary,
fingerprint_result=fingerprint,
content_fingerprint_result=content_fingerprint,
)
apply_duplicate_result_to_intake(intake, result)
return result
@@ -408,6 +438,7 @@ def detect_duplicates_for_candidate(
document: ShippingNoticeDocument | None = None,
business_summary: dict[str, Any] | None = None,
fingerprint_result: dict[str, Any] | None = None,
content_fingerprint_result: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""
对单个订单候选执行重复检测(不写 Intake,不影响同信其它候选)。
@@ -424,6 +455,9 @@ def detect_duplicates_for_candidate(
fingerprint = fp.get('fingerprint')
version = str(fp.get('version') or FINGERPRINT_VERSION)
components = fp.get('components') if isinstance(fp.get('components'), dict) else {}
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)
content_hash = ''
if document is not None and str(document.content_hash or '').strip():
@@ -483,13 +517,24 @@ def detect_duplicates_for_candidate(
)
)
if matches and status == 'none':
primary_life = classify_task_lifecycle(matches[0].get('process_status'))
if primary_life == 'active':
status = 'same_active_task'
elif primary_life == 'completed':
status = 'same_completed_task'
comparable = [
task for task in fp_tasks
if task.content_fingerprint and task.content_fingerprint_version == content_version
]
if content_fingerprint and comparable:
status = (
'same_business_content'
if any(task.content_fingerprint == content_fingerprint for task in comparable)
else 'possible_revision'
)
else:
status = 'possible_duplicate'
primary_life = classify_task_lifecycle(matches[0].get('process_status'))
if primary_life == 'active':
status = 'same_active_task'
elif primary_life == 'completed':
status = 'same_completed_task'
else:
status = 'possible_duplicate'
elif fingerprint and strength == 'weak' and status == 'none':
fp_tasks = (
db.session.query(ShippingTask)
@@ -519,6 +564,8 @@ def detect_duplicates_for_candidate(
'same_active_task',
'same_completed_task',
'possible_duplicate',
'same_business_content',
'possible_revision',
}
if strength == 'unavailable' and status == 'none':
blocking = False
@@ -530,6 +577,8 @@ def detect_duplicates_for_candidate(
'fingerprint': fingerprint,
'fingerprint_version': version,
'components': components,
'content_fingerprint': content_fingerprint,
'content_fingerprint_version': content_version,
'matches': matches,
'allowed_actions': _allowed_actions_for(
status=status,