Files
syops/backend/app/services/shipping_notice/lifecycle_projection.py
T

120 lines
5.5 KiB
Python

"""
services/shipping_notice/lifecycle_projection.py
用途:为批次进度、任务详情与订单生命周期工作台裁决同一份四阶段业务生命周期。
依赖:process_gate_contract 的阻断原因归属;调用方提供正式 Gate、Case/Candidate 或历史兜底事实。
逻辑:正式 Process Gate 优先,其次是可解释阻断原因;仅无正式事实时才使用旧阶段兜底。
本模块只做只读业务投影,不授权任何写操作;阶段入口与实际动作权限必须分开。
"""
from __future__ import annotations
from typing import Any
from app.services.shipping_notice.process_gate_contract import PROCESS_GATE_REASON_OWNING_GATE
LIFECYCLE_STAGES: tuple[dict[str, str], ...] = (
{'code': 'intake_parse', 'label': '接收解析'},
{'code': 'order_review', 'label': '订单复核'},
{'code': 'fulfillment_prep', 'label': '通知准备'},
{'code': 'upload_complete', 'label': '通知完成'},
)
LIFECYCLE_STAGE_CODES: tuple[str, ...] = tuple(stage['code'] for stage in LIFECYCLE_STAGES)
# 正式 Gate 到用户生命周期的唯一映射。任何页面均不得维护第二份 Gate 映射。
LIFECYCLE_STAGE_BY_GATE: dict[str, str] = {
'document_materialization': 'intake_parse',
'layout_parsing': 'intake_parse',
'document_profile_routing': 'intake_parse',
'order_field_extraction': 'intake_parse',
'field_completeness': 'order_review',
'reservation_validation': 'order_review',
'customer_resolution': 'order_review',
'duplicate_detection': 'order_review',
'field_conflict_resolution': 'order_review',
'order_freeze_readiness': 'order_review',
'task_creation': 'order_review',
'inventory_source_routing': 'fulfillment_prep',
'inventory_preparation': 'fulfillment_prep',
'inventory_field_mapping': 'fulfillment_prep',
'inventory_matching': 'fulfillment_prep',
'inventory_sufficiency': 'fulfillment_prep',
'excel_readiness': 'fulfillment_prep',
'artifact_generation': 'fulfillment_prep',
'freight_upload_rpa': 'upload_complete',
'manual_upload_handoff': 'upload_complete',
}
# 没有正式 Gate 的历史项才能使用此映射。它必须保持在共享裁决器内,避免各入口漂移。
LIFECYCLE_STAGE_RANGES: tuple[tuple[str, set[str]], ...] = (
('intake_parse', {'upload_received', 'email_parsing', 'email_adjudicating', 'document_parsing', 'candidate_creating'}),
('order_review', {
'customer_resolving', 'duplicate_checking', 'order_creating', 'review_required',
'waiting_review', 'waiting_human_review', 'waiting_human', 'pre_ot_stp', 'order_review',
}),
('fulfillment_prep', {
'inventory_source_resolving', 'inventory_fetching', 'inventory_fetch', 'inventory_preparation',
'inventory_candidates_calculated', 'inventory_candidates_frozen', 'inventory_snapshots_ready',
'inventory_matching', 'inventory_match', 'inventory_matched', 'excel_generating',
'excel_generate', 'excel_generated',
}),
('upload_complete', {'freight_uploading', 'freight_upload', 'upload_receipt_recorded', 'completed'}),
)
def lifecycle_entries(*, lifecycle_stage: str | None, has_candidate_context: bool) -> list[dict[str, str]]:
"""返回只读生命周期入口;无有效 Candidate/正式任务时不开放任何订单生命周期面。"""
if not has_candidate_context or lifecycle_stage not in LIFECYCLE_STAGE_CODES:
return []
current_index = LIFECYCLE_STAGE_CODES.index(str(lifecycle_stage))
return [
{
'code': code,
'state': 'current' if index == current_index else ('available' if index < current_index else 'unavailable'),
'target_surface': 'order_lifecycle_workbench',
}
for index, code in enumerate(LIFECYCLE_STAGE_CODES)
]
def project_lifecycle(
*,
current_gate: dict[str, Any] | None,
blocking_reason: dict[str, Any] | None,
legacy_stage: str | None,
has_candidate_context: bool,
) -> dict[str, Any]:
"""按 Gate → 阻断归属 → 历史阶段的固定优先级裁决生命周期和入口。
``authority`` 仅用于诊断/迁移观测;前端不得据此再计算阶段或放开按钮。
"""
gate = current_gate if isinstance(current_gate, dict) else {}
gate_code = str(gate.get('gate_code') or gate.get('code') or '').strip().lower()
if gate_code in LIFECYCLE_STAGE_BY_GATE:
lifecycle_stage = LIFECYCLE_STAGE_BY_GATE[gate_code]
authority = 'process_gate'
else:
reason = blocking_reason if isinstance(blocking_reason, dict) else {}
reason_code = str(reason.get('code') or reason.get('blocking_reason_code') or '').strip()
owning_gate = PROCESS_GATE_REASON_OWNING_GATE.get(reason_code)
if owning_gate in LIFECYCLE_STAGE_BY_GATE:
lifecycle_stage = LIFECYCLE_STAGE_BY_GATE[owning_gate]
authority = 'blocking_reason'
else:
normalized_legacy = str(legacy_stage or '').strip().lower()
lifecycle_stage = next(
(code for code, aliases in LIFECYCLE_STAGE_RANGES if normalized_legacy in aliases),
normalized_legacy if normalized_legacy in LIFECYCLE_STAGE_CODES else None,
)
authority = 'legacy_fallback' if lifecycle_stage else 'unclassified'
return {
'lifecycle_stage': lifecycle_stage,
'lifecycle_entries': lifecycle_entries(
lifecycle_stage=lifecycle_stage,
has_candidate_context=has_candidate_context,
),
'lifecycle_authority': authority,
}