feat(shipping-notice): 新增发货通知重复检测与处理接口,支持重复决策保存与重新执行检测
This commit is contained in:
1 parent
729546a7b7
commit
ceb49461e8
72 files changed
+14270
-460
No files matched your search
@@ -0,0 +1,173 @@
|
||||
"""
|
||||
backend/app/services/shipping_notice/business_fingerprint.py
|
||||
|
||||
用途:基于业务摘要生成稳定业务指纹与强度判定(货代域,不含文件哈希)。
|
||||
依赖:po_number / reservation_id 标准化函数。
|
||||
逻辑:intake_parse_workflow / Candidate 级 duplicate_detection / 建单门闩共用;算法版本 shipping-v1。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from typing import Any
|
||||
|
||||
from app.services.shipping_notice.po_number import normalize_po_number
|
||||
from app.services.shipping_notice.reservation_id import normalize_reservation_id
|
||||
|
||||
FINGERPRINT_VERSION = 'shipping-v1'
|
||||
|
||||
|
||||
def _norm_text(value: Any) -> str:
|
||||
return str(value or '').strip().upper()
|
||||
|
||||
|
||||
def _norm_qty(value: Any) -> str:
|
||||
if value is None or value == '':
|
||||
return ''
|
||||
try:
|
||||
return format(Decimal(str(value)).quantize(Decimal('0.0001')), 'f')
|
||||
except (InvalidOperation, ValueError, TypeError):
|
||||
return _norm_text(value)
|
||||
|
||||
|
||||
def _line_digest(lines: list[dict[str, Any]]) -> str:
|
||||
parts: list[str] = []
|
||||
for line in lines:
|
||||
if not isinstance(line, dict):
|
||||
continue
|
||||
sku = _norm_text(line.get('sku'))
|
||||
rid = normalize_reservation_id(str(line.get('reservation_id') or '')) or ''
|
||||
qty = _norm_qty(line.get('qty'))
|
||||
if not sku and not rid and not qty:
|
||||
continue
|
||||
parts.append(f'{sku}|{rid}|{qty}')
|
||||
parts.sort()
|
||||
return '\n'.join(parts)
|
||||
|
||||
|
||||
def _customer_identity(summary: dict[str, Any]) -> tuple[str, str]:
|
||||
"""
|
||||
解析客户身份键。
|
||||
|
||||
核心逻辑:优先 customer_code;启发式预解析常只有 customer_name,须回退到名称,
|
||||
否则强业务单号(PO/Reservation)也无法生成 usable 指纹。
|
||||
Returns:
|
||||
(identity_value, identity_field) 如 ('ACME', 'customer_name');无则 ('', '')。
|
||||
"""
|
||||
code = _norm_text(summary.get('customer_code'))
|
||||
if code:
|
||||
return code, 'customer_code'
|
||||
name = _norm_text(summary.get('customer_name'))
|
||||
if name:
|
||||
return name, 'customer_name'
|
||||
return '', ''
|
||||
|
||||
|
||||
def build_business_fingerprint(business_summary: dict[str, Any] | None) -> dict[str, Any]:
|
||||
"""
|
||||
由业务摘要生成指纹、强度与组成依据。
|
||||
|
||||
核心逻辑(shipping-v1):
|
||||
- strong:客户身份(code 或 name)+ (PO 或 reservation 集合)
|
||||
或:PO + reservation 集合(稳定业务编号,无需客户码)
|
||||
- medium:客户身份 + 目的地 + required_date + 行 digest
|
||||
- weak:ship_to 或 仅行 digest(SKU+qty)
|
||||
- unavailable:不足以可靠去重
|
||||
|
||||
Args:
|
||||
business_summary: intake_business_parser 产出。
|
||||
|
||||
Returns:
|
||||
{fingerprint, version, strength, components}
|
||||
"""
|
||||
summary = business_summary if isinstance(business_summary, dict) else {}
|
||||
customer_id, customer_field = _customer_identity(summary)
|
||||
po_number = normalize_po_number(str(summary.get('po_number') or '')) or ''
|
||||
reservation_ids = sorted(
|
||||
{
|
||||
normalize_reservation_id(str(item)) or ''
|
||||
for item in (summary.get('reservation_ids') or [])
|
||||
if str(item or '').strip()
|
||||
}
|
||||
- {''}
|
||||
)
|
||||
dest = summary.get('destination') if isinstance(summary.get('destination'), dict) else {}
|
||||
destination_key = '|'.join(
|
||||
[
|
||||
_norm_text(dest.get('code')),
|
||||
_norm_text(dest.get('city')),
|
||||
_norm_text(dest.get('country')),
|
||||
]
|
||||
).strip('|')
|
||||
required_date = _norm_text(summary.get('required_date'))
|
||||
ship_to = _norm_text(summary.get('ship_to'))
|
||||
lines = summary.get('lines') if isinstance(summary.get('lines'), list) else []
|
||||
line_digest = _line_digest([item for item in lines if isinstance(item, dict)])
|
||||
|
||||
strength = 'unavailable'
|
||||
components: dict[str, Any] = {}
|
||||
|
||||
# [STEP 1] 强指纹:客户身份 + 稳定单号;或仅 PO+Reservation(编号本身足够强)
|
||||
if customer_id and (po_number or reservation_ids):
|
||||
strength = 'strong'
|
||||
components = {
|
||||
customer_field: customer_id,
|
||||
'po_number': po_number or None,
|
||||
'reservation_ids': reservation_ids,
|
||||
}
|
||||
elif po_number and reservation_ids:
|
||||
strength = 'strong'
|
||||
components = {
|
||||
'po_number': po_number,
|
||||
'reservation_ids': reservation_ids,
|
||||
}
|
||||
elif customer_id and destination_key and required_date and line_digest:
|
||||
strength = 'medium'
|
||||
components = {
|
||||
customer_field: customer_id,
|
||||
'destination': destination_key,
|
||||
'required_date': required_date,
|
||||
'line_digest': hashlib.sha256(line_digest.encode('utf-8')).hexdigest()[:16],
|
||||
}
|
||||
elif ship_to and line_digest:
|
||||
strength = 'weak'
|
||||
components = {
|
||||
'ship_to': ship_to,
|
||||
'line_digest': hashlib.sha256(line_digest.encode('utf-8')).hexdigest()[:16],
|
||||
}
|
||||
elif line_digest and (reservation_ids or po_number or customer_id):
|
||||
# 有行 digest 且至少一项单头标识:弱指纹,可标记 possible
|
||||
strength = 'weak'
|
||||
components = {
|
||||
'line_digest': hashlib.sha256(line_digest.encode('utf-8')).hexdigest()[:16],
|
||||
}
|
||||
if customer_id:
|
||||
components[customer_field] = customer_id
|
||||
if po_number:
|
||||
components['po_number'] = po_number
|
||||
if reservation_ids:
|
||||
components['reservation_ids'] = reservation_ids
|
||||
else:
|
||||
return {
|
||||
'fingerprint': None,
|
||||
'version': FINGERPRINT_VERSION,
|
||||
'strength': 'unavailable',
|
||||
'components': {},
|
||||
}
|
||||
|
||||
payload = {
|
||||
'version': FINGERPRINT_VERSION,
|
||||
'strength': strength,
|
||||
'components': components,
|
||||
}
|
||||
digest = hashlib.sha256(
|
||||
json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode('utf-8')
|
||||
).hexdigest()
|
||||
return {
|
||||
'fingerprint': digest,
|
||||
'version': FINGERPRINT_VERSION,
|
||||
'strength': strength,
|
||||
'components': components,
|
||||
}
|
||||
Reference in new issue
Block a user