/** * features/chat/shipping/shippingStepper.ts * 用途:基于后端 ui_state 投影渲染五阶段业务 Stepper。 * 依赖:ShippingProcessingProjection(可选叠加)、shippingProcessingFulfillment;ShippingWorkflowStepper.vue 消费输出。 * 逻辑:只消费后端 ui_state.steps 五阶段投影;缺失投影时 fail-closed。 */ import { FULFILLMENT_PROGRESS_NODES, fulfillmentNodeIndexFromStage, type FulfillmentProgressNodeKey, } from './shippingProcessingFulfillment'; export interface ShippingTaskStepperSource { ui_state?: { steps?: ShippingWorkflowStepSource[] | null; } | null; } export interface ShippingWorkflowStepItem { id: string; label: string; status: 'completed' | 'current' | 'pending' | 'blocked'; } type ShippingWorkflowStepSource = { id?: string | null; code?: string | null; label?: string | null; status?: string | null; }; export const SHIPPING_WORKFLOW_STEP_LABELS = [ 'chat.shippingNoticeUi.stage.intakeParse', 'chat.shippingNoticeUi.stage.orderReview', 'chat.shippingNoticeUi.stage.fulfillmentPrep', 'chat.shippingNoticeUi.stage.freightUpload', 'chat.shippingNoticeUi.stage.uploadComplete', ] as const; const WORKFLOW_STEP_STATUSES = new Set([ 'completed', 'current', 'pending', 'blocked', ]); /** * 将后端 UI State steps 收窄为 Stepper 可渲染的前端结构。 */ export function normalizeShippingWorkflowSteps( steps: ShippingWorkflowStepSource[] | null | undefined, ): ShippingWorkflowStepItem[] { if (!Array.isArray(steps)) return []; return steps .map((step, index) => { const status = String(step?.status || '').trim(); if (!WORKFLOW_STEP_STATUSES.has(status as ShippingWorkflowStepItem['status'])) { return null; } return { id: String(step?.id || step?.code || `shipping-stage-${index + 1}`), label: String(step?.label || SHIPPING_WORKFLOW_STEP_LABELS[index] || ''), status: status as ShippingWorkflowStepItem['status'], }; }) .filter((step): step is ShippingWorkflowStepItem => !!step); } /** * 只消费后端状态机 UI State steps;没有投影时返回空数组,禁止本地猜测阶段。 */ export function resolveShippingWorkflowSteps( task: ShippingTaskStepperSource | null | undefined, ): ShippingWorkflowStepItem[] { const uiStateSteps = normalizeShippingWorkflowSteps(task?.ui_state?.steps); return uiStateSteps; } /** 统一 ShippingProcessingStage → 粗粒度 Stepper 下标(过渡期;阶段 2 换 UnifiedProgressTrack)。 */ const PROCESSING_STAGE_INDEX_MAP: Record = { upload_received: 0, email_parsing: 0, document_parsing: 0, email_adjudicating: 1, candidate_creating: 1, customer_resolving: 1, duplicate_checking: 1, waiting_review: 1, order_creating: 1, inventory_source_resolving: 2, inventory_fetching: 2, inventory_matching: 2, excel_generating: 2, freight_uploading: 3, completed: 4, }; /** * 用统一投影 stage/status 覆盖 Stepper 当前步(切流主源)。 * * 核心逻辑:有投影时按 stage 重算 completed/current/pending;blocked/failed 标当前步 blocked; * 无投影或无法映射时原样返回 baseSteps。 * * @param baseSteps - ui_state 推导的步骤 * @param projection - 统一 order_task 投影 */ export function applyShippingProcessingStageToSteps( baseSteps: ShippingWorkflowStepItem[], projection: { status?: string | null; stage?: string | null; currentGate?: { status?: string | null } | null } | null | undefined, ): ShippingWorkflowStepItem[] { if (!projection || !baseSteps.length) return baseSteps; const stage = String(projection.stage || '').trim(); const status = String(projection.status || '').trim().toLowerCase(); const stageIndex = PROCESSING_STAGE_INDEX_MAP[stage]; if (stageIndex === undefined && status !== 'completed' && status !== 'failed' && status !== 'blocked') { return baseSteps; } const currentIndex = status === 'completed' ? baseSteps.length - 1 : (stageIndex ?? 0); const gateBlocked = String(projection.currentGate?.status || '').trim().toLowerCase() === 'blocked' || status === 'blocked' || status === 'failed' || status === 'waiting_review'; return baseSteps.map((step, index) => { let nextStatus: ShippingWorkflowStepItem['status'] = 'pending'; if (status === 'completed' || status === 'cancelled') { nextStatus = 'completed'; } else if (index < currentIndex) { nextStatus = 'completed'; } else if (index === currentIndex) { nextStatus = gateBlocked ? 'blocked' : 'current'; } return { ...step, status: nextStatus }; }); } /** * 缺少后端投影时 fail-closed,不根据 review/match/upload 等领域事实猜测阶段。 */ export function buildShippingWorkflowSteps( _task: ShippingTaskStepperSource | null | undefined, ): ShippingWorkflowStepItem[] { return []; } /** * 从统一 ShippingProcessing 投影构建履约四节点 Stepper(任务台主源)。 * * 核心逻辑:仅在有投影时返回步骤;节点标签由调用方传入 i18n 文案。 * * @param projection - 统一 order_task 投影 * @param nodeLabels - 四节点展示文案(building_order … freight_uploading) * @returns 四步 Stepper 项;无投影时返回 null */ export function buildFulfillmentWorkflowSteps( projection: { stage?: string | null; status?: string | null; currentGate?: { status?: string | null } | null; } | null | undefined, nodeLabels: Record, ): ShippingWorkflowStepItem[] | null { if (!projection) return null; const activeIndex = fulfillmentNodeIndexFromStage(projection.stage, projection.status); const status = String(projection.status || '').trim().toLowerCase(); const gateBlocked = String(projection.currentGate?.status || '').trim().toLowerCase() === 'blocked' || status === 'blocked' || status === 'failed' || status === 'waiting_review'; return FULFILLMENT_PROGRESS_NODES.map((key, index) => { let stepStatus: ShippingWorkflowStepItem['status'] = 'pending'; if (status === 'completed' || status === 'cancelled') { stepStatus = 'completed'; } else if (activeIndex >= 0 && index < activeIndex) { stepStatus = 'completed'; } else if (activeIndex >= 0 && index === activeIndex) { stepStatus = gateBlocked ? 'blocked' : 'current'; } return { id: `fulfillment-${key}`, label: nodeLabels[key as FulfillmentProgressNodeKey], status: stepStatus, }; }); }