Files
syops/frontend/src/features/chat/shipping/shippingStepper.ts
T
hugo 5ef8329bd8 refactor(shipping-notice): 重构货代发货通知状态管理,移除废弃的process_status字段
统一货代发货通知的业务阶段为四个标准阶段:接收解析、订单复核、发货准备、上传完成
设置 ui_state 为前端唯一状态数据源,不再依赖 process_status 驱动业务流程
删除 shipping_notices 表的废弃 process_status 列及对应复合索引,新增数据库迁移脚本
替换全栈代码中所有对 process_status 的引用为生命周期、上传状态等领域字段
更新相关文档、测试用例与前端展示逻辑适配新的状态管理规则
2026-08-15 10:19:10 +08:00

184 lines
6.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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.uploadComplete',
] as const;
const WORKFLOW_STEP_STATUSES = new Set<ShippingWorkflowStepItem['status']>([
'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<string, number> = {
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: 3,
};
/**
* 用统一投影 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<FulfillmentProgressNodeKey, string>,
): 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,
};
});
}