Files
syops/backend/app/services/shipping_notice/lifecycle_projection.py
T
hugo 1b6483a4ea feat(shipping-notice,receipt-report): 新增收料报表功能并优化发货通知流程
清理废弃的shipping xfa ocr automark相关代码和job配置
新增收料报表管理的前端页面、路由和侧边栏入口
新增后端收料报表配置接口、数据库模型和RPA脚本
优化发货通知流程的重试策略,将shipping相关job默认不自动重试
移除废弃的库存源确认相关逻辑,更新流程门禁规则
重构订单模板的值适配器模块,新增坐标OCR值适配器
清理前端shipping菜单和action的废弃选项
更新相关文档和测试用例,调整.gitignore忽略规则
2026-08-20 20:23:10 +08:00

140 lines
6.2 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,
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,
) -> 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,
has_intake_parse_context=has_intake_parse_context,
),
'lifecycle_authority': authority,
}