Files
syops/frontend/src/features/chat/shipping/shippingStepper.ts
T

184 lines
6.4 KiB
TypeScript

/**
* features/chat/shipping/shippingStepper.ts
* 用途:基于 shipping task 状态推导前端七步 Stepper 展示数据。
* 依赖:无外部模块;ShippingWorkflowStepper.vue 消费本模块输出。
* 逻辑:buildShippingWorkflowSteps 将 process_status / checkpoint 等映射为步骤 id 与 done/active 状态。
*/
export interface ShippingTaskStepperSource {
process_status?: string | null;
review_status?: string | null;
pending_human_reason?: string | null;
match_status?: string | null;
export_file_name?: string | null;
current_step?: string | 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.stepper.noticeSubmit',
'chat.shippingNoticeUi.stepper.parseConfirm',
'chat.shippingNoticeUi.stepper.inventoryPrepare',
'chat.shippingNoticeUi.stepper.alignConfirm',
'chat.shippingNoticeUi.stepper.matchInventory',
'chat.shippingNoticeUi.stepper.generateFile',
'chat.shippingNoticeUi.stepper.uploadFreight',
] as const;
const CURRENT_STEP_INDEX_MAP: Record<string, number> = {
step1_notice_submit: 0,
step2_parse_confirm: 1,
step3_inventory_prepare: 2,
step2_inventory_prepare: 2,
step4_align_confirm: 3,
step5_match_inventory: 4,
step3_inventory_match: 4,
step4_shortage_review: 4,
step6_generate_file: 5,
step5_export_excel: 5,
step7_upload_freight: 6,
step6_upload_freight: 6,
step7_finish: 6,
};
const TERMINAL_PROCESS_STATUSES = new Set(['uploaded', 'closed']);
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-step-${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;没有时按 task 状态本地推导。
*/
export function resolveShippingWorkflowSteps(
task: (ShippingTaskStepperSource & { ui_state?: { steps?: ShippingWorkflowStepSource[] | null } | null }) | null | undefined,
): ShippingWorkflowStepItem[] {
const uiStateSteps = normalizeShippingWorkflowSteps(task?.ui_state?.steps);
return uiStateSteps.length ? uiStateSteps : buildShippingWorkflowSteps(task);
}
function deriveCurrentIndex(task: ShippingTaskStepperSource | null | undefined): number {
const currentStep = String(task?.current_step || '').trim();
const processStatus = String(task?.process_status || '').trim();
const reviewStatus = String(task?.review_status || '').trim();
const pendingReason = String(task?.pending_human_reason || '').trim();
const matchStatus = String(task?.match_status || '').trim();
const hasExport = !!String(task?.export_file_name || '').trim();
if (currentStep) {
const currentStepIndex = CURRENT_STEP_INDEX_MAP[currentStep];
if (currentStepIndex !== undefined) {
return currentStepIndex;
}
}
if (TERMINAL_PROCESS_STATUSES.has(processStatus)) return 6;
if (processStatus === 'waiting_upload' || processStatus === 'upload_failed' || hasExport) return 5;
if (processStatus === 'matched') return 4;
if (reviewStatus === 'shortage_review' || pendingReason === 'shortage_review' || pendingReason === 'shortage') return 4;
if (reviewStatus === 'align_review' || pendingReason === 'align_review') return 3;
if (processStatus === 'inventory_ready' || matchStatus) return 4;
if (
processStatus === 'waiting_inventory'
|| pendingReason === 'map_infer'
|| pendingReason === 'rpa_pending'
|| pendingReason === 'inv_unavail'
|| pendingReason === 'rpa_exhausted'
) {
return 2;
}
if (
processStatus === 'parsed'
|| processStatus === 'pending_review'
|| reviewStatus === 'pending_confirm'
|| reviewStatus === 'parse_review'
|| reviewStatus === 'rsv_required'
|| pendingReason === 'low_confidence'
|| pendingReason === 'rsv_required'
|| pendingReason === 'inventory_source_unresolved'
) {
return 1;
}
return 0;
}
/**
* 将 task 状态映射为七步 Stepper 项(id / label / done / active)。
*/
export function buildShippingWorkflowSteps(
task: ShippingTaskStepperSource | null | undefined,
): ShippingWorkflowStepItem[] {
const currentIndex = deriveCurrentIndex(task);
const reviewStatus = String(task?.review_status || '').trim();
const pendingReason = String(task?.pending_human_reason || '').trim();
const processStatus = String(task?.process_status || '').trim();
return SHIPPING_WORKFLOW_STEP_LABELS.map((label, index) => {
let status: ShippingWorkflowStepItem['status'] = 'pending';
// [STEP 1] uploaded/closed 是整票已完成的终态,不能再被 current_step 渲染成“第 7 步进行中”。
if (TERMINAL_PROCESS_STATUSES.has(processStatus)) {
status = 'completed';
} else if (index < currentIndex) {
status = 'completed';
} else if (index === currentIndex) {
status = 'current';
}
if (
(index === 1 && (
reviewStatus === 'parse_review'
|| pendingReason === 'low_confidence'
|| pendingReason === 'rsv_required'
|| pendingReason === 'inventory_source_unresolved'
))
|| (index === 2 && (pendingReason === 'inv_unavail' || pendingReason === 'rpa_exhausted'))
|| (index === 3 && (reviewStatus === 'align_review' || pendingReason === 'align_review'))
|| (index === 4 && (reviewStatus === 'shortage_review' || pendingReason === 'shortage_review' || pendingReason === 'shortage'))
|| (index === 6 && processStatus === 'upload_failed')
) {
status = 'blocked';
}
return {
id: `shipping-step-${index + 1}`,
label,
status,
};
});
}