215 lines
7.7 KiB
TypeScript
215 lines
7.7 KiB
TypeScript
/**
|
|
* features/chat/shipping/useChatShippingParseReview.ts
|
|
* 用途:管理 parse_review 阶段的订单表卡片,包括打开、同步和增删改保存。
|
|
* 依赖:shipping.api、shippingParseReview、chat.types。
|
|
* 逻辑:由 useChatShipping 调用本文件;本文件在聊天区维护解析复核卡片,并把用户修订同步回后端 notice 编辑接口。
|
|
*/
|
|
import {
|
|
addWorkspaceShippingNoticeLine,
|
|
getShippingChatActions,
|
|
getWorkspaceShippingTask,
|
|
removeWorkspaceShippingNoticeLine,
|
|
updateWorkspaceShippingNoticeFields,
|
|
updateWorkspaceShippingNoticeLine,
|
|
type ShippingNoticeHeader,
|
|
type ShippingNoticeLine,
|
|
} from '@/features/chat/api/shipping.api';
|
|
import { getShippingParseReviewPayload } from './shippingParseReview';
|
|
import type { ChatMessage } from '../types/chat.types';
|
|
import type { ShippingParseReviewPayload } from '../types/shipping.types';
|
|
import type { Ref } from 'vue';
|
|
|
|
export interface ShippingParseReviewContext {
|
|
messages: Ref<ChatMessage[]>;
|
|
resolvedShippingTaskId: Ref<string>;
|
|
newLocalMessageId: (prefix: string) => string;
|
|
saveLocalMessageToBackend: (msg: ChatMessage, userQuery?: string) => Promise<void>;
|
|
refreshShippingActions: () => Promise<void>;
|
|
msg: { warning: (text: string) => void };
|
|
}
|
|
|
|
/** 由任务详情与 checkpoint 构建 parse_review 卡片 payload。 */
|
|
function buildParseReviewPayload(
|
|
taskId: string,
|
|
detail: Awaited<ReturnType<typeof getWorkspaceShippingTask>>,
|
|
checkpoint: string,
|
|
): ShippingParseReviewPayload {
|
|
return {
|
|
taskId,
|
|
checkpoint,
|
|
header: {
|
|
customer_name: detail.customer_name ?? null,
|
|
customer_code: detail.customer_code ?? null,
|
|
ship_to: detail.ship_to ?? null,
|
|
po_number: detail.po_number ?? null,
|
|
required_date: detail.required_date ?? null,
|
|
},
|
|
lines: Array.isArray(detail.lines) ? detail.lines : [],
|
|
saving: false,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建发货通知 parse review 卡片处理器。
|
|
*
|
|
* 核心逻辑:根据任务详情与 checkpoint 维护本地复核卡片,并把单头/订单行编辑回写到后端。
|
|
* @param ctx - parse review 卡片上下文
|
|
* @returns 复核卡片同步与编辑处理函数集合
|
|
*/
|
|
export function createShippingParseReviewHandlers(ctx: ShippingParseReviewContext) {
|
|
const removeParseReviewMessageForTask = (taskId: string) => {
|
|
const normalized = String(taskId || '').trim().toUpperCase();
|
|
if (!normalized) return;
|
|
for (let i = ctx.messages.value.length - 1; i >= 0; i -= 1) {
|
|
const msg = ctx.messages.value[i];
|
|
if (!msg) continue;
|
|
const payload = getShippingParseReviewPayload(msg);
|
|
if (payload?.taskId?.trim().toUpperCase() === normalized) {
|
|
ctx.messages.value.splice(i, 1);
|
|
}
|
|
}
|
|
};
|
|
|
|
const upsertParseReviewMessage = async (payload: ShippingParseReviewPayload) => {
|
|
const normalized = String(payload.taskId || '').trim().toUpperCase();
|
|
const existing = ctx.messages.value.find((msg) => {
|
|
const current = getShippingParseReviewPayload(msg);
|
|
return current?.taskId?.trim().toUpperCase() === normalized;
|
|
});
|
|
if (existing) {
|
|
existing.content = `请确认或修订以下解析结果。\n任务编号:${normalized}`;
|
|
existing.payload = payload;
|
|
existing.local_only = true;
|
|
return;
|
|
}
|
|
const reviewMsg: ChatMessage = {
|
|
role: 'assistant',
|
|
type: 'shipping-parse-review',
|
|
content: `请确认或修订以下解析结果。\n任务编号:${normalized}`,
|
|
payload,
|
|
local_id: ctx.newLocalMessageId('shipping-parse-review'),
|
|
local_only: true,
|
|
};
|
|
ctx.messages.value.push(reviewMsg);
|
|
await ctx.saveLocalMessageToBackend(reviewMsg);
|
|
};
|
|
|
|
const syncShippingParseReviewCard = async (taskId?: string): Promise<boolean> => {
|
|
const normalized = String(taskId || ctx.resolvedShippingTaskId.value || '').trim().toUpperCase();
|
|
if (!normalized) return false;
|
|
try {
|
|
const [detail, actions] = await Promise.all([
|
|
getWorkspaceShippingTask(normalized),
|
|
getShippingChatActions(normalized),
|
|
]);
|
|
const checkpoint = String(actions.checkpoint || '').trim();
|
|
if (checkpoint !== 'parse_review') {
|
|
removeParseReviewMessageForTask(normalized);
|
|
return false;
|
|
}
|
|
await upsertParseReviewMessage(buildParseReviewPayload(normalized, detail, checkpoint));
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const updateShippingParseReviewHeader = async (
|
|
msgObj: ChatMessage,
|
|
fields: Partial<ShippingNoticeHeader>,
|
|
) => {
|
|
const payload = getShippingParseReviewPayload(msgObj);
|
|
if (!payload || payload.saving) return false;
|
|
payload.saving = true;
|
|
const taskId = String(payload.taskId || '').trim().toUpperCase();
|
|
try {
|
|
await updateWorkspaceShippingNoticeFields(taskId, fields, '聊天区订单表修订');
|
|
await syncShippingParseReviewCard(taskId);
|
|
await ctx.refreshShippingActions();
|
|
return true;
|
|
} catch (e: unknown) {
|
|
ctx.msg.warning(e instanceof Error ? e.message : '更新解析结果失败');
|
|
return false;
|
|
} finally {
|
|
const current = getShippingParseReviewPayload(msgObj);
|
|
if (current) current.saving = false;
|
|
}
|
|
};
|
|
|
|
const updateShippingParseReviewLine = async (
|
|
msgObj: ChatMessage,
|
|
lineIndex: number,
|
|
patch: Partial<ShippingNoticeLine>,
|
|
) => {
|
|
const payload = getShippingParseReviewPayload(msgObj);
|
|
if (!payload || payload.saving) return false;
|
|
payload.saving = true;
|
|
const taskId = String(payload.taskId || '').trim().toUpperCase();
|
|
try {
|
|
await updateWorkspaceShippingNoticeLine(taskId, lineIndex, patch, '聊天区订单表修订');
|
|
await syncShippingParseReviewCard(taskId);
|
|
await ctx.refreshShippingActions();
|
|
return true;
|
|
} catch (e: unknown) {
|
|
ctx.msg.warning(e instanceof Error ? e.message : '更新解析结果失败');
|
|
return false;
|
|
} finally {
|
|
const current = getShippingParseReviewPayload(msgObj);
|
|
if (current) current.saving = false;
|
|
}
|
|
};
|
|
|
|
const addShippingParseReviewLine = async (
|
|
msgObj: ChatMessage,
|
|
line: Partial<ShippingNoticeLine>,
|
|
) => {
|
|
const payload = getShippingParseReviewPayload(msgObj);
|
|
if (!payload || payload.saving) return false;
|
|
payload.saving = true;
|
|
const taskId = String(payload.taskId || '').trim().toUpperCase();
|
|
try {
|
|
await addWorkspaceShippingNoticeLine(taskId, line, '聊天区订单表新增行');
|
|
await syncShippingParseReviewCard(taskId);
|
|
await ctx.refreshShippingActions();
|
|
return true;
|
|
} catch (e: unknown) {
|
|
ctx.msg.warning(e instanceof Error ? e.message : '新增订单行失败');
|
|
return false;
|
|
} finally {
|
|
const current = getShippingParseReviewPayload(msgObj);
|
|
if (current) current.saving = false;
|
|
}
|
|
};
|
|
|
|
const removeShippingParseReviewLine = async (
|
|
msgObj: ChatMessage,
|
|
lineIndex: number,
|
|
) => {
|
|
const payload = getShippingParseReviewPayload(msgObj);
|
|
if (!payload || payload.saving) return false;
|
|
payload.saving = true;
|
|
const taskId = String(payload.taskId || '').trim().toUpperCase();
|
|
try {
|
|
await removeWorkspaceShippingNoticeLine(taskId, lineIndex, '聊天区订单表删除行');
|
|
await syncShippingParseReviewCard(taskId);
|
|
await ctx.refreshShippingActions();
|
|
return true;
|
|
} catch (e: unknown) {
|
|
ctx.msg.warning(e instanceof Error ? e.message : '删除订单行失败');
|
|
return false;
|
|
} finally {
|
|
const current = getShippingParseReviewPayload(msgObj);
|
|
if (current) current.saving = false;
|
|
}
|
|
};
|
|
|
|
return {
|
|
getShippingParseReviewPayload,
|
|
syncShippingParseReviewCard,
|
|
updateShippingParseReviewHeader,
|
|
updateShippingParseReviewLine,
|
|
addShippingParseReviewLine,
|
|
removeShippingParseReviewLine,
|
|
};
|
|
}
|