- 人工重试库存准备前置幂等重判:任务级整单重路由、行级定向重评目标行,复用裁决与冻结链路,零新动作码零契约变更 - 新版候选集 supersede 时内置失效旧库存事实,导出归档与查询服务同步适配 - 行级「库存源」下拉抽纯函数合并选项:歧义行候选置顶 + 全部已启用源去重兜底,任一已启用源可人工指定 - 库存源门禁归一 Notice→OT 身份解析;任务中心抽屉与表格接入行级重取入口 - 同步四语言文案、技术文档与实施计划
184 lines
7.5 KiB
TypeScript
184 lines
7.5 KiB
TypeScript
/**
|
||
* features/workspace/shipping-task-center/shippingTaskCenterShared.ts
|
||
* 用途:任务中心列表 / 表格 / 抽屉共用的纯函数与类型(阶段口径、编号、选择)。
|
||
* 依赖:chat shipping.api 类型、gateReasonLabel。
|
||
* 逻辑:阶段只消费后端 ui_state.lifecycle_entries / ui_state.stage(与页面同口径);
|
||
* 本模块不发起请求、不操作路由,供 TaskTable / Drawer / Page 复用。
|
||
*/
|
||
import type { ShippingTaskDetail, ShippingTaskSummary } from '@/features/chat/api/shipping.api';
|
||
import { resolveGateReasonLabel } from '@/features/shipping-progress/gateReasonLabel';
|
||
|
||
export type LifecycleStageCode = 'intake_parse' | 'order_review' | 'fulfillment_prep'
|
||
| 'freight_upload' | 'upload_complete';
|
||
|
||
/** 五阶段完整序列(抽屉阶段下拉消费后端 entries,仍需全集做过滤)。 */
|
||
export const LIFECYCLE_STAGES: LifecycleStageCode[] = [
|
||
'intake_parse',
|
||
'order_review',
|
||
'fulfillment_prep',
|
||
'freight_upload',
|
||
'upload_complete',
|
||
];
|
||
|
||
/** 阶段筛选 tab 仅覆盖四个可动作阶段;「通知完成」归 done 桶终态。 */
|
||
export const ACTIONABLE_TODO_STAGES: Exclude<LifecycleStageCode, 'upload_complete'>[] = [
|
||
'intake_parse',
|
||
'order_review',
|
||
'fulfillment_prep',
|
||
'freight_upload',
|
||
];
|
||
|
||
export type TodoLike = ShippingTaskSummary | ShippingTaskDetail;
|
||
|
||
/**
|
||
* 待办列表项稳定主键:复核用 case_no,Intake 用 intake_no,任务用 task_no。
|
||
*/
|
||
export function todoItemKey(item: ShippingTaskSummary): string {
|
||
if (item.item_kind === 'review_case' || item.review_case_no) {
|
||
return String(item.todo_ref || item.review_case_no || item.review_run_no || '');
|
||
}
|
||
if (item.item_kind === 'order_intake' || item.intake_no) {
|
||
return String(item.todo_ref || item.intake_no || '');
|
||
}
|
||
return String(item.todo_ref || item.task_no || '');
|
||
}
|
||
|
||
/**
|
||
* 列表展示编号:复核显示 case_no,Intake 显示 intake_no,任务显示业务编号/SN。
|
||
*/
|
||
export function todoDisplayNo(item: ShippingTaskSummary): string {
|
||
if (item.item_kind === 'review_case' || item.review_case_no) {
|
||
return String(item.review_case_no || item.review_run_no || item.todo_ref || '—');
|
||
}
|
||
if (item.item_kind === 'order_intake' || item.intake_no) {
|
||
return String(item.intake_no || item.todo_ref || '—');
|
||
}
|
||
return String(item.business_no || item.task_no || item.todo_ref || '—');
|
||
}
|
||
|
||
/**
|
||
* 读取后端已裁决的生命周期入口;缺失即无授权,绝不按 task/case/intake 身份补造入口。
|
||
*/
|
||
export function lifecycleEntriesOf(item: TodoLike) {
|
||
const entries = item.ui_state?.lifecycle_entries;
|
||
if (!Array.isArray(entries)) return [];
|
||
return entries.filter((entry) => LIFECYCLE_STAGES.includes(entry.code as LifecycleStageCode));
|
||
}
|
||
|
||
/** 当前阶段只取后端标为 current 的入口,必要时回退 ui_state.stage(仅统计口径)。 */
|
||
export function lifecycleStageCodeOf(item: TodoLike): LifecycleStageCode | null {
|
||
const current = lifecycleEntriesOf(item).find((entry) => entry.state === 'current');
|
||
if (current) return current.code as LifecycleStageCode;
|
||
// [AUTHORITY] 待人工处理的 Review Case 可能没有可进入工作台的 lifecycle entry,
|
||
// 但后端仍会下发 ui_state.stage 作为当前业务阶段;统计与筛选必须保留该阶段。
|
||
const stage = String(item.ui_state?.stage?.code || '').trim();
|
||
return LIFECYCLE_STAGES.includes(stage as LifecycleStageCode)
|
||
? stage as LifecycleStageCode
|
||
: null;
|
||
}
|
||
|
||
/** 可进入工作台的业务阶段仅由后端 entry.state 授权。 */
|
||
export function lifecycleEntryFor(
|
||
item: TodoLike,
|
||
stage: Exclude<LifecycleStageCode, 'intake_parse'>,
|
||
) {
|
||
return lifecycleEntriesOf(item).find((entry) => entry.code === stage) || null;
|
||
}
|
||
|
||
/** 后端仅以 current / available 表示可进入工作台;未知状态一律拒绝深链。 */
|
||
export function lifecycleEntryIsEnterable(
|
||
entry: ReturnType<typeof lifecycleEntryFor>,
|
||
): boolean {
|
||
return entry?.state === 'current' || entry?.state === 'available';
|
||
}
|
||
|
||
/** 阶段目标下拉默认值:优先 current,其次 available。 */
|
||
export function defaultLifecycleTarget(
|
||
item: TodoLike,
|
||
): Exclude<LifecycleStageCode, 'intake_parse'> | null {
|
||
const current = lifecycleEntriesOf(item).find(
|
||
(entry) => entry.code !== 'intake_parse' && entry.state === 'current',
|
||
);
|
||
const available = lifecycleEntriesOf(item).find(
|
||
(entry) => entry.code !== 'intake_parse' && entry.state === 'available',
|
||
);
|
||
return (current || available)?.code as Exclude<LifecycleStageCode, 'intake_parse'> | undefined || null;
|
||
}
|
||
|
||
/** 接收解析入口只消费后端独立授权 + run/agent 前提,不从阶段名称推导可写权限。 */
|
||
export function canOpenIntakeParse(item: TodoLike | null, chatAgentId: number | null): boolean {
|
||
if (!item) return false;
|
||
const runNo = String(item.review_run_no || '').trim();
|
||
const agentId = Number(item.owner_agent_id || chatAgentId || 0);
|
||
return Boolean(
|
||
runNo
|
||
&& agentId
|
||
&& item.ui_state?.intake_parse_authorized === true,
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 行状态摘要标题:后端待办标题优先,逐级回退到阶段标签。
|
||
*/
|
||
export function todoTitleText(item: ShippingTaskSummary): string {
|
||
return String(
|
||
item.ui_state?.todo?.title
|
||
|| item.ui_state?.summary?.current_todo
|
||
|| item.ui_state?.summary?.title
|
||
|| item.ui_state?.stage?.label
|
||
|| '—',
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 行阻断原因文本:优先后端 reason.message,其次 Gate 原因码的本地化标签。
|
||
* 空串表示当前无阻断(调用方据此决定红色高亮)。
|
||
*/
|
||
export function todoBlockerText(
|
||
item: TodoLike,
|
||
t: (key: string, named?: Record<string, unknown>) => string,
|
||
te: (key: string) => boolean,
|
||
): string {
|
||
const reason = item.ui_state?.reason;
|
||
const text = String(reason?.message || '').trim();
|
||
if (text) return text;
|
||
const code = String(reason?.code || '').trim();
|
||
if (!code) return '';
|
||
return resolveGateReasonLabel(code, { t, te });
|
||
}
|
||
|
||
/** 表格副行:客户 · PO · 源文件/导出文件,缺省段自动跳过。 */
|
||
export function todoSubTitleText(item: ShippingTaskSummary): string {
|
||
const segments = [
|
||
String(item.customer_name || '').trim(),
|
||
String(item.po_number || '').trim() ? `PO ${String(item.po_number).trim()}` : '',
|
||
String(item.source_file_name || '').trim()
|
||
|| String(item.artifact_summary?.export_file_name || '').trim(),
|
||
].filter(Boolean);
|
||
return segments.join(' · ');
|
||
}
|
||
|
||
/** 项次/缺口摘要:total 项 + 缺 n(缺口>0 的行数)。 */
|
||
export function todoLineBrief(item: ShippingTaskSummary): { total: number; shortage: number } {
|
||
const lines = Array.isArray(item.notice_line_summary) ? item.notice_line_summary : [];
|
||
const shortage = lines.filter((line) => Number(line.shortage_qty || 0) > 0).length;
|
||
return { total: lines.length, shortage };
|
||
}
|
||
|
||
/**
|
||
* 批量可选行:正式 Notice 且处于货代上传阶段(业务约束:批量操作仅该阶段可用)。
|
||
*/
|
||
export function isFreightUploadRow(item: ShippingTaskSummary): boolean {
|
||
return Boolean(String(item.business_no || '').trim())
|
||
&& lifecycleStageCodeOf(item) === 'freight_upload';
|
||
}
|
||
|
||
/**
|
||
* 批量可下载行:在上传阶段约束之上叠加下载授权谓词字段(创建者或管理员)。
|
||
* 核心逻辑:与后端 /shipping-notices/batch/export 同一口径,无权行不可勾选,
|
||
* 避免"勾了下单却整体 404"的体验;字段缺失(异常路径)按不可下载处理。
|
||
*/
|
||
export function isBatchDownloadableRow(item: ShippingTaskSummary): boolean {
|
||
return isFreightUploadRow(item) && item.can_download_export === true;
|
||
}
|