Files
syops/frontend/src/features/shipping-progress/progressWindow.ts
T

261 lines
9.8 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/shipping-progress/progressWindow.ts
* 用途:按密度从 15 阶中切出可见进度窗口,推导节点执行态,并提供订单台五域摘要。
* 依赖:SHIPPING_PROCESSING_STAGES / EML_ONLY / types ProgressDensity·ProgressDomain*、shippingProcessingLabels。
* 逻辑:锚点为当前 stage;stageNodes 优先;workbench=全 15 阶;五域供 ReviewProgressStrip 紧凑条。
*/
import {
EML_ONLY_PROCESSING_STAGES,
SHIPPING_PROCESSING_STAGES,
type OrderIntakeSourceType,
type ShippingProcessingProjection,
type ShippingProcessingStage,
type ShippingProcessingStageNodeStatus,
type ShippingProcessingStatus,
} from '@/features/chat/types/shipping.api.types';
import { shippingProcessingStageKey } from '@/features/chat/shipping/shippingProcessingLabels';
import {
PROGRESS_DENSITY_WINDOW_SIZE,
type ProgressDensity,
type ProgressDomainState,
type ProgressDomainSummaryItem,
type ProgressRangeHint,
type ProgressWindowNode,
} from './types';
/** 订单处理工作台五域(0-based 含端点,对齐 15 阶)。 */
export const REVIEW_PROGRESS_DOMAIN_RANGES = [
{ key: 'receive_parse', labelKey: 'chat.shippingProcessing.progress.domainReceiveParse', start: 0, end: 2 },
{ key: 'candidate_identity', labelKey: 'chat.shippingProcessing.progress.domainCandidateIdentity', start: 3, end: 7 },
{ key: 'order_confirm', labelKey: 'chat.shippingProcessing.progress.domainOrderConfirm', start: 8, end: 9 },
{ key: 'inventory', labelKey: 'chat.shippingProcessing.progress.domainInventoryHandle', start: 10, end: 11 },
{ key: 'fulfillment', labelKey: 'chat.shippingProcessing.progress.domainFulfillmentShip', start: 12, end: 14 },
] as const;
/**
* 返回 stage 在 15 阶中的下标;未知码回退 0。
*/
export function stageAbsoluteIndex(stage: string | null | undefined): number {
const code = String(stage || '').trim() as ShippingProcessingStage;
const idx = SHIPPING_PROCESSING_STAGES.indexOf(code);
return idx >= 0 ? idx : 0;
}
/**
* 从投影 stageNodes 查找指定阶的后端权威态。
*/
function resolveBackendNodeStatus(
projection: ShippingProcessingProjection,
stage: ShippingProcessingStage,
absoluteIndex: number,
): ShippingProcessingStageNodeStatus | null {
const nodes = projection.stageNodes;
if (!nodes?.length) return null;
const matched = nodes.find((node) => (
node.stage === stage || node.absoluteIndex === absoluteIndex
));
return matched?.status ?? null;
}
/**
* 推导单个节点执行态。
*
* 核心逻辑:stageNodes 优先;direct_pdf 的 EML 专属阶 → skipped;当前阶按投影 status 映射;
* 已过阶 → passed;未到 → pending;失败/阻断仅落在当前阶。
*/
export function resolveStageNodeStatus(options: {
absoluteIndex: number;
currentIndex: number;
projectionStatus: ShippingProcessingStatus | string | null | undefined;
sourceType?: OrderIntakeSourceType | string | null;
stage: ShippingProcessingStage;
backendStatus?: ShippingProcessingStageNodeStatus | null;
}): ShippingProcessingStageNodeStatus {
if (options.backendStatus) return options.backendStatus;
const source = String(options.sourceType || '').trim().toLowerCase();
if (source === 'direct_pdf' && EML_ONLY_PROCESSING_STAGES.has(options.stage)) {
return 'skipped';
}
if (options.absoluteIndex < options.currentIndex) return 'passed';
if (options.absoluteIndex > options.currentIndex) return 'pending';
const status = String(options.projectionStatus || '').trim().toLowerCase();
if (status === 'failed' || status === 'cancelled') return 'failed';
if (status === 'blocked' || status === 'needs_action' || status === 'waiting_review') {
return 'blocked';
}
if (status === 'completed' && options.stage === 'completed') return 'passed';
if (status === 'automating' || status === 'processing' || status === 'queued') return 'running';
return 'running';
}
/**
* 计算可见窗口的 [start, end) 下标(含当前)。
*/
export function computeProgressWindowRange(
currentIndex: number,
density: ProgressDensity,
total = SHIPPING_PROCESSING_STAGES.length,
): { start: number; end: number } {
const size = Math.min(PROGRESS_DENSITY_WINDOW_SIZE[density], total);
if (size >= total) return { start: 0, end: total };
let start = Math.max(0, currentIndex - Math.floor((size - 1) / 2));
let end = start + size;
if (end > total) {
end = total;
start = Math.max(0, end - size);
}
return { start, end };
}
/**
* 解析单阶节点态(供窗口与范围摘要共用)。
*/
function resolveProjectionStageStatus(
projection: ShippingProcessingProjection,
stage: ShippingProcessingStage,
absoluteIndex: number,
currentIndex: number,
): ShippingProcessingStageNodeStatus {
return resolveStageNodeStatus({
absoluteIndex,
currentIndex,
projectionStatus: projection.status,
sourceType: projection.sourceType,
stage,
backendStatus: resolveBackendNodeStatus(projection, stage, absoluteIndex),
});
}
/**
* 从统一投影构建密度窗口节点列表。
*
* @param projection - ShippingProcessingProjection;空则返回空列表
* @param density - 页面密度
*/
export function buildProgressWindow(
projection: ShippingProcessingProjection | null | undefined,
density: ProgressDensity,
): ProgressWindowNode[] {
if (!projection) return [];
const currentIndex = stageAbsoluteIndex(projection.stage);
const { start, end } = computeProgressWindowRange(currentIndex, density);
const nodes: ProgressWindowNode[] = [];
for (let i = start; i < end; i += 1) {
const stage = SHIPPING_PROCESSING_STAGES[i]!;
nodes.push({
stage,
absoluteIndex: i,
isCurrent: i === currentIndex,
status: resolveProjectionStageStatus(projection, stage, i, currentIndex),
});
}
// 紧凑模式:去掉窗口两端连续 skipped,避免空白占位。
if (density === 'chat' || density === 'right') {
while (nodes.length > 1 && nodes[0]?.status === 'skipped') nodes.shift();
while (nodes.length > 1 && nodes[nodes.length - 1]?.status === 'skipped') nodes.pop();
}
return nodes;
}
/**
* 构建进度范围摘要(已完成 n/total、当前阶段键、窗口外剩余阶数)。
*
* @param projection - 统一投影
* @param density - 页面密度(决定 hiddenAfter 计算窗口)
* @returns 摘要 DTO;无投影时 null
*/
export function buildProgressRangeHint(
projection: ShippingProcessingProjection | null | undefined,
density: ProgressDensity,
): ProgressRangeHint | null {
if (!projection) return null;
const currentIndex = stageAbsoluteIndex(projection.stage);
let done = 0;
let total = 0;
for (let i = 0; i < SHIPPING_PROCESSING_STAGES.length; i += 1) {
const stage = SHIPPING_PROCESSING_STAGES[i]!;
const status = resolveProjectionStageStatus(projection, stage, i, currentIndex);
if (status === 'skipped') continue;
total += 1;
if (status === 'passed') done += 1;
}
const { end } = computeProgressWindowRange(currentIndex, density);
const hiddenAfter = Math.max(0, SHIPPING_PROCESSING_STAGES.length - end);
return {
done,
total,
currentLabelKey: shippingProcessingStageKey(projection.stage),
hiddenAfter,
};
}
/**
* 构建订单处理工作台五域摘要(压缩业务域一行)。
*
* 核心逻辑:若传入 authoritativeNodes(来自 projection.stageNodes),仅据此聚合,禁止第二套启发式;
* 未传时回退当前 stage 索引(兼容详情抽屉等非工作台主条场景)。
*
* @param projection - 统一投影
* @param authoritativeNodes - 可选权威 15 阶节点(工作台主条必须传入)
*/
export function buildReviewProgressDomainSummary(
projection: ShippingProcessingProjection | null | undefined,
authoritativeNodes?: Array<{ absoluteIndex: number; status: ShippingProcessingStageNodeStatus }> | null,
): ProgressDomainSummaryItem[] {
if (!projection) return [];
const currentIndex = stageAbsoluteIndex(projection.stage);
const statusNorm = String(projection.status || '').trim().toLowerCase();
return REVIEW_PROGRESS_DOMAIN_RANGES.map((domain) => {
let state: ProgressDomainState = 'pending';
if (authoritativeNodes?.length) {
// [STEP] 仅由权威节点聚合:域内含 blocked/failed → 对应态;含 running/当前阶 → current;全 passed/skipped → passed。
const slice = authoritativeNodes.filter(
(node) => node.absoluteIndex >= domain.start && node.absoluteIndex <= domain.end,
);
if (slice.some((node) => node.status === 'failed')) {
state = 'failed';
} else if (slice.some((node) => node.status === 'blocked')) {
state = 'blocked';
} else if (
slice.some((node) => node.status === 'running')
|| (currentIndex >= domain.start && currentIndex <= domain.end)
) {
state = 'current';
} else if (
slice.length > 0
&& slice.every((node) => node.status === 'passed' || node.status === 'skipped')
) {
state = 'passed';
} else if (currentIndex > domain.end) {
state = 'passed';
}
} else if (currentIndex > domain.end) {
state = 'passed';
} else if (currentIndex >= domain.start && currentIndex <= domain.end) {
if (statusNorm === 'failed' || statusNorm === 'cancelled') state = 'failed';
else if (statusNorm === 'blocked' || statusNorm === 'waiting_review' || statusNorm === 'needs_action') {
state = 'blocked';
} else {
state = 'current';
}
}
return {
key: domain.key,
labelKey: domain.labelKey,
start: domain.start,
end: domain.end,
state,
};
});
}
/**
* 构建完整 15 阶窗口(流程详情抽屉专用)。
*/
export function buildFullProgressNodes(
projection: ShippingProcessingProjection | null | undefined,
): ProgressWindowNode[] {
return buildProgressWindow(projection, 'workbench');
}