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

157 lines
6.8 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': 'freight_upload', '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': 'freight_upload',
'manual_upload_handoff': 'freight_upload',
}
# 已持久化的 Processing 技术阶段到五阶段业务工作面的唯一映射。它只用于
# 读模型展示,不推进订单、不创建任务,也不授予任何动作。正式 Gate 仍在
# 技术阶段缺失时提供兜底,避免历史数据被无条件覆盖。
LIFECYCLE_STAGE_BY_PROCESSING_STAGE: dict[str, str] = {
'upload_received': 'intake_parse',
'email_parsing': 'intake_parse',
'document_parsing': 'intake_parse',
'email_adjudicating': 'intake_parse',
'candidate_creating': 'intake_parse',
'waiting_review': 'order_review',
'customer_resolving': 'order_review',
'duplicate_checking': 'order_review',
'inventory_source_resolving': 'order_review',
'order_creating': 'fulfillment_prep',
'inventory_fetching': 'fulfillment_prep',
'inventory_preparation': 'fulfillment_prep',
'inventory_matching': 'fulfillment_prep',
'inventory_sufficiency': 'fulfillment_prep',
'excel_generating': 'fulfillment_prep',
'freight_uploading': 'freight_upload',
'manual_upload_handoff': 'freight_upload',
'completed': 'upload_complete',
}
def lifecycle_entries(
*,
lifecycle_stage: str | None,
has_candidate_context: bool,
has_intake_parse_context: bool = False,
) -> list[dict[str, str]]:
"""返回只读生命周期入口;空 Case 仅允许停留在接收解析阶段。"""
if lifecycle_stage not in LIFECYCLE_STAGE_CODES:
return []
if not has_candidate_context:
# 接收解析 Case 是已持久化的受控对象,允许在批次页展示其当前阶段;
# 但不能借此开放订单复核及后续阶段。
if not has_intake_parse_context or lifecycle_stage != 'intake_parse':
return []
return [
{
'code': code,
'state': 'current' if code == 'intake_parse' else 'unavailable',
'target_surface': 'order_lifecycle_workbench',
}
for code in LIFECYCLE_STAGE_CODES
]
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,
has_candidate_context: bool,
has_intake_parse_context: bool = False,
verified_upload_success: bool = False,
processing_stage: str | None = None,
) -> dict[str, Any]:
"""按成功事实 → Gate → 已持久化阶段 → 阻断归属的固定优先级裁决生命周期。
``processing_stage`` 是订单处理流程已写入的事实,仅映射展示工作面;它不能
授权动作或推进流程。``authority`` 仅用于诊断/迁移观测;前端不得据此再计算
阶段或放开按钮。
"""
gate = current_gate if isinstance(current_gate, dict) else {}
gate_code = str(gate.get('gate_code') or gate.get('code') or '').strip().lower()
# 成功事实高于当前 Gate/历史阶段:异步回调成功后旧 Gate 尚未清理时,用户仍应
# 看到已完成;反之任何技术完成、回执文本或旧 completed 都不能越过此条件。
if verified_upload_success:
lifecycle_stage = 'upload_complete'
authority = 'verified_upload_success'
elif gate_code in LIFECYCLE_STAGE_BY_GATE:
lifecycle_stage = LIFECYCLE_STAGE_BY_GATE[gate_code]
authority = 'process_gate'
elif str(processing_stage or '').strip().lower() in LIFECYCLE_STAGE_BY_PROCESSING_STAGE:
lifecycle_stage = LIFECYCLE_STAGE_BY_PROCESSING_STAGE[
str(processing_stage or '').strip().lower()
]
authority = 'processing_stage'
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:
lifecycle_stage = None
authority = 'unclassified'
return {
'lifecycle_stage': lifecycle_stage,
'lifecycle_entries': lifecycle_entries(
lifecycle_stage=lifecycle_stage,
has_candidate_context=has_candidate_context,
has_intake_parse_context=has_intake_parse_context,
),
'lifecycle_authority': authority,
}