/** * features/chat/shipping/useChatShippingParseReview.ts * 用途:管理 parse_review 阶段的订单表卡片,包括打开、同步和增删改保存。 * 依赖:shipping.api、shippingParseReview、chat.types。 * 逻辑:由 useChatShipping 调用本文件;本文件在聊天区维护解析复核卡片,并把用户修订同步回后端 notice 编辑接口。 */ import { addWorkspaceShippingNoticeLine, 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 { Ref } from 'vue'; export interface ShippingParseReviewContext { messages: Ref; resolvedShippingTaskId: Ref; newLocalMessageId: (prefix: string) => string; saveLocalMessageToBackend: (msg: ChatMessage, userQuery?: string) => Promise; refreshShippingActions: () => Promise; msg: { warning: (text: string) => void }; t: (key: string, params?: Record) => string; } /** * 创建发货通知 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 syncShippingParseReviewCard = async (taskId?: string): Promise => { const normalized = String(taskId || ctx.resolvedShippingTaskId.value || '').trim().toUpperCase(); if (!normalized) return false; // 解析复核表单不再作为聊天历史消息展示;当前步骤操作统一放到轻量工作台/动作按钮中。 removeParseReviewMessageForTask(normalized); return false; }; const updateShippingParseReviewHeader = async ( msgObj: ChatMessage, fields: Partial, ) => { 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, ctx.t('chat.shippingNoticeToast.auditCommentHeaderEdit')); await syncShippingParseReviewCard(taskId); await ctx.refreshShippingActions(); return true; } catch (e: unknown) { ctx.msg.warning(e instanceof Error ? e.message : ctx.t('chat.shippingNoticeToast.updateParseFailed')); return false; } finally { const current = getShippingParseReviewPayload(msgObj); if (current) current.saving = false; } }; const updateShippingParseReviewLine = async ( msgObj: ChatMessage, lineIndex: number, patch: Partial, ) => { 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, ctx.t('chat.shippingNoticeToast.auditCommentHeaderEdit')); await syncShippingParseReviewCard(taskId); await ctx.refreshShippingActions(); return true; } catch (e: unknown) { ctx.msg.warning(e instanceof Error ? e.message : ctx.t('chat.shippingNoticeToast.updateParseFailed')); return false; } finally { const current = getShippingParseReviewPayload(msgObj); if (current) current.saving = false; } }; const addShippingParseReviewLine = async ( msgObj: ChatMessage, line: Partial, ) => { 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, ctx.t('chat.shippingNoticeToast.auditCommentLineAdd')); await syncShippingParseReviewCard(taskId); await ctx.refreshShippingActions(); return true; } catch (e: unknown) { ctx.msg.warning(e instanceof Error ? e.message : ctx.t('chat.shippingNoticeToast.addLineFailed')); 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, ctx.t('chat.shippingNoticeToast.auditCommentLineDelete')); await syncShippingParseReviewCard(taskId); await ctx.refreshShippingActions(); return true; } catch (e: unknown) { ctx.msg.warning(e instanceof Error ? e.message : ctx.t('chat.shippingNoticeToast.deleteLineFailed')); return false; } finally { const current = getShippingParseReviewPayload(msgObj); if (current) current.saving = false; } }; return { getShippingParseReviewPayload, syncShippingParseReviewCard, updateShippingParseReviewHeader, updateShippingParseReviewLine, addShippingParseReviewLine, removeShippingParseReviewLine, }; }