feat(shipping-notice): 增强发货通知候选管理功能,支持候选选择、重复检测与业务摘要修订
This commit is contained in:
1 parent
70f684b94a
commit
b595e157e9
83 files changed
+5857
-200
No files matched your search
@@ -14,9 +14,10 @@ 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
|
||||
from app.services.shipping_notice.reservation_id import validated_reservation_id
|
||||
|
||||
FINGERPRINT_VERSION = 'shipping-v1'
|
||||
CONTENT_FINGERPRINT_VERSION = 'shipping-content-v2'
|
||||
|
||||
|
||||
def _norm_text(value: Any) -> str:
|
||||
@@ -38,7 +39,7 @@ def _line_digest(lines: list[dict[str, Any]]) -> str:
|
||||
if not isinstance(line, dict):
|
||||
continue
|
||||
sku = _norm_text(line.get('sku'))
|
||||
rid = normalize_reservation_id(str(line.get('reservation_id') or '')) or ''
|
||||
rid = validated_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
|
||||
@@ -87,7 +88,7 @@ def build_business_fingerprint(business_summary: dict[str, Any] | None) -> dict[
|
||||
po_number = normalize_po_number(str(summary.get('po_number') or '')) or ''
|
||||
reservation_ids = sorted(
|
||||
{
|
||||
normalize_reservation_id(str(item)) or ''
|
||||
validated_reservation_id(str(item)) or ''
|
||||
for item in (summary.get('reservation_ids') or [])
|
||||
if str(item or '').strip()
|
||||
}
|
||||
@@ -171,3 +172,71 @@ def build_business_fingerprint(business_summary: dict[str, Any] | None) -> dict[
|
||||
'strength': strength,
|
||||
'components': components,
|
||||
}
|
||||
|
||||
|
||||
def build_content_fingerprint(business_summary: dict[str, Any] | None) -> dict[str, Any]:
|
||||
"""
|
||||
生成订单内容版本指纹,用于区分完全重复与同一订单修订。
|
||||
|
||||
核心逻辑:标准化并排序订单行,纳入 Reservation、SKU、数量、单位、交期及邮件分配字段;
|
||||
单头目的地和 PO 仅用于增强内容比较,不替代身份指纹。
|
||||
Args:
|
||||
business_summary: Candidate 融合后的业务摘要。
|
||||
Returns:
|
||||
包含 fingerprint、version 和 components 的字典;无有效订单行时 fingerprint 为空。
|
||||
"""
|
||||
summary = business_summary if isinstance(business_summary, dict) else {}
|
||||
parts: list[str] = []
|
||||
for line in summary.get('lines') or []:
|
||||
if not isinstance(line, dict):
|
||||
continue
|
||||
values = [
|
||||
_norm_text(line.get('line_no')),
|
||||
validated_reservation_id(line.get('reservation_id')) or '',
|
||||
_norm_text(line.get('sku')),
|
||||
_norm_qty(line.get('qty')),
|
||||
_norm_text(line.get('uom')),
|
||||
_norm_qty(line.get('unit_price')),
|
||||
_norm_qty(line.get('amount')),
|
||||
_norm_text(line.get('currency')),
|
||||
_norm_text(line.get('required_date')),
|
||||
normalize_po_number(line.get('po_number')) or '',
|
||||
_norm_text(line.get('allocation_id')),
|
||||
_norm_text(line.get('source_type')),
|
||||
_norm_text(line.get('customer_sku')),
|
||||
_norm_text(line.get('vendor_part_number')),
|
||||
_norm_text(line.get('hub_code')),
|
||||
]
|
||||
if any(values):
|
||||
parts.append('|'.join(values))
|
||||
if not parts:
|
||||
return {
|
||||
'fingerprint': None,
|
||||
'version': CONTENT_FINGERPRINT_VERSION,
|
||||
'components': {},
|
||||
}
|
||||
parts.sort()
|
||||
destination = summary.get('destination') if isinstance(summary.get('destination'), dict) else {}
|
||||
components = {
|
||||
'po_number': normalize_po_number(summary.get('po_number')) or None,
|
||||
'destination': '|'.join(
|
||||
_norm_text(destination.get(key)) for key in ('code', 'city', 'country')
|
||||
).strip('|') or None,
|
||||
'ship_to': _norm_text(summary.get('ship_to')) or None,
|
||||
'required_date': _norm_text(summary.get('required_date')) or None,
|
||||
'warehouse_hint': _norm_text(summary.get('warehouse_hint')) or None,
|
||||
'lines': parts,
|
||||
}
|
||||
digest = hashlib.sha256(
|
||||
json.dumps(
|
||||
{'version': CONTENT_FINGERPRINT_VERSION, 'components': components},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
separators=(',', ':'),
|
||||
).encode('utf-8')
|
||||
).hexdigest()
|
||||
return {
|
||||
'fingerprint': digest,
|
||||
'version': CONTENT_FINGERPRINT_VERSION,
|
||||
'components': components,
|
||||
}
|
||||
Reference in new issue
Block a user