Files
syops/backend/app/services/shipping_notice/lifecycle_projection.py
T
hugo 935bf26fa6 feat(shipping-notice): 升级货代发货流程为五阶段,完善业务校验
将货代发货流程从四阶段扩展为五阶段,拆分上传相关逻辑至独立的货代上传阶段;
统一多语言文案修正客户状态语义,将"客户尚未识别"调整为符合业务的"客户尚未确认";
新增严格的上传成功验证谓词,需同时满足任务状态、全部预约单上传成功、存在有效成功上传尝试三个条件方可标记为完成;
新增坐标取值模型配置,支持Legacy冻结绝对坐标和锚点框架受限平移两种模式;
更新全链路相关文档、前端组件、后端服务与测试用例,确保业务逻辑对齐新业务规则。
2026-08-21 09:20:47 +08:00

149 lines
6.6 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',
}
# 没有正式 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',
}),
('freight_upload', {
'freight_uploading', 'freight_upload', 'upload_receipt_recorded', 'upload_complete', 'completed',
}),
)
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,
legacy_stage: str | None,
has_candidate_context: bool,
has_intake_parse_context: bool = False,
verified_upload_success: bool = False,
) -> 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()
# 成功事实高于当前 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'
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,
has_intake_parse_context=has_intake_parse_context,
),
'lifecycle_authority': authority,
}