feat(shipping-notice): 落地字段资产治理与唯一 Schema,自动建单随版本冻结
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b7307fc573
commit
3a1dbf7ce5
144 files changed
+15167
-4480
No files matched your search
@@ -1,9 +1,10 @@
|
||||
"""
|
||||
backend/app/services/shipping_notice/candidate_service.py
|
||||
|
||||
用途:提供订单候选的用户态查询、选择与重复决策能力。
|
||||
用途:提供订单候选的用户态查询、选择与重复决策能力;含 STP 严格建单门禁。
|
||||
依赖:ShippingNoticeCandidate/Document/Intake、duplicate_detection、business_fingerprint。
|
||||
逻辑:workspace API 调用本模块;所有写操作锁定 Candidate,并在字段变化后重算双指纹和重复状态。
|
||||
逻辑:workspace API 调用本模块;所有写操作锁定 Candidate,并在字段变化后重算双指纹和重复状态;
|
||||
仅严格门禁通过的 Candidate 可物化 Task(缺字段/低置信/缺证据/重复冲突 → 拒绝)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -20,10 +21,55 @@ from app.services.shipping_notice.duplicate_detection import apply_duplicate_res
|
||||
from app.services.shipping_notice.intake_service import can_force_create_shipping_duplicate
|
||||
|
||||
_ACTIONS = frozenset({'open_existing', 'create_revision', 'force_create'})
|
||||
_STP_DUPLICATE_OK = frozenset({'none', 'unchecked'})
|
||||
|
||||
|
||||
def evaluate_candidate_stp_gate(candidate: ShippingNoticeCandidate) -> tuple[bool, list[str]]:
|
||||
"""
|
||||
评估 Candidate 是否通过 STP 严格建单门禁(不抛异常)。
|
||||
|
||||
核心逻辑:仅对 automatic 接受态施加严格门禁(缺字段、阻断冲突、低置信、缺证据、
|
||||
未解决重复)。人工复核确认(acceptance_mode=manual)不走 STP 直通门禁。
|
||||
|
||||
Args:
|
||||
candidate: 待评估订单候选。
|
||||
|
||||
Returns:
|
||||
(是否通过, 失败原因码列表)。
|
||||
"""
|
||||
if str(candidate.acceptance_mode or '') != 'automatic':
|
||||
return True, []
|
||||
reasons: list[str] = []
|
||||
if str(candidate.validation_status or '') != 'ready':
|
||||
reasons.append('incomplete_fields')
|
||||
if candidate.conflicts_json:
|
||||
reasons.append('blocking_conflict')
|
||||
missing = list(candidate.missing_fields_json or [])
|
||||
if missing:
|
||||
reasons.append('incomplete_fields')
|
||||
evidence = candidate.evidence_json
|
||||
if evidence is None or (isinstance(evidence, list) and len(evidence) == 0):
|
||||
reasons.append('missing_evidence')
|
||||
try:
|
||||
confidence = float(candidate.confidence) if candidate.confidence is not None else None
|
||||
except (TypeError, ValueError):
|
||||
confidence = None
|
||||
if confidence is not None and confidence < 0.85:
|
||||
reasons.append('low_confidence')
|
||||
dup_status = str(candidate.duplicate_status or 'unchecked')
|
||||
if dup_status not in _STP_DUPLICATE_OK:
|
||||
if str(candidate.duplicate_resolution or '') != 'open_existing':
|
||||
reasons.append('duplicate_conflict')
|
||||
return not reasons, sorted(set(reasons))
|
||||
|
||||
|
||||
def ensure_candidate_fulfillment_ready(candidate: ShippingNoticeCandidate) -> None:
|
||||
"""统一履约门禁:只有已接受、当前且无阻断问题的 Candidate 才能建单。"""
|
||||
"""
|
||||
统一履约门禁:已接受、当前、无阻断;automatic 候选额外通过 STP 严格门禁。
|
||||
|
||||
Raises:
|
||||
ConflictError: 接受态、选中态、版本、消费态或 STP 严格门禁失败。
|
||||
"""
|
||||
if str(candidate.candidate_status or '') != 'accepted':
|
||||
raise ConflictError(
|
||||
code='SHIPPING_NOTICE.CANDIDATE_ACCEPTANCE_REQUIRED',
|
||||
@@ -40,6 +86,14 @@ def ensure_candidate_fulfillment_ready(candidate: ShippingNoticeCandidate) -> No
|
||||
code='SHIPPING_NOTICE.CANDIDATE_REVIEW_REQUIRED',
|
||||
message='订单候选仍有字段冲突或未完成审核',
|
||||
)
|
||||
# [STEP] STP 严格门禁仅约束自动直通;人工确认建单已由复核草稿校验背书。
|
||||
passed, reason_codes = evaluate_candidate_stp_gate(candidate)
|
||||
if not passed:
|
||||
raise ConflictError(
|
||||
code='SHIPPING_NOTICE.CANDIDATE_STP_GATE_FAILED',
|
||||
message='订单候选未通过直通建单严格门禁',
|
||||
details={'reason_codes': reason_codes},
|
||||
)
|
||||
|
||||
|
||||
def _candidate_for_user(candidate_no: str, user_id: int, *, lock: bool = False) -> tuple[ShippingNoticeCandidate, ShippingNoticeIntake, ShippingNoticeDocument]:
|
||||
|
||||
Reference in new issue
Block a user