feat(shipping-freight-auto-upload): 新增货代发货Excel连接级自动上传全链路功能

为提升货代上传的自动化程度,减少人工操作,本提交实现完整的连接级自动上传功能,包含以下变更:
- 新增数据库迁移与ShippingRpaProfile模型字段,存储连接级上传配置
- 完善RPA自动上传触发、回调与状态管理逻辑,支持三态结果与人工兜底
- 优化聊天界面任务卡片加载与按钮渲染,修复无文案幽灵按钮问题
This commit is contained in:
hugo committed 2026-08-27 12:44:12 +08:00
1 parent 06c18b3c7c
commit 434c04ed2d
30 files changed
+1574 -185

No files matched your search

@@ -193,7 +193,8 @@ const {
const msg = useMessage();
/**
* 右侧主卡阶段:优先 Progress 投影(与顶部同源);Case current_stage 仅降级。
* 右侧主卡阶段:优先 Progress 投影(与顶部同源);正式 OT 投影未到时用
* Case.current_stage(与投影同源 project_stage_for_task),建单前 automation 仅作降级。
* 无论哪一路都带 label_key,禁止把 code 当用户文案。
*/
const currentProcessingStageDisplay = computed(() => {
@@ -206,6 +207,19 @@ const currentProcessingStageDisplay = computed(() => {
inconsistent: false,
};
}
const caseStage = activeCase.value?.current_stage;
const caseCode = String(caseStage?.code || '').trim();
// 正式 OT 的 Case.current_stage 与统一投影同源(project_stage_for_task):
// 投影读取中(正式任务已存在、投影在途)也必须先展示真实阶段,
// 禁止被下方 automation 分支降级为“创建正式订单”自动建单阶段。
if (hasFormalOt.value && caseCode) {
return {
code: caseCode,
labelKey: caseStage?.label_key
|| (caseCode === 'order_creating' ? 'waiting_system_start' : caseCode),
inconsistent: Boolean(caseStage?.inconsistent),
};
}
// STP 建单前尚无正式 OT/processing projection;自动建单 Job 仍属于“创建正式订单”阶段。
const automationStatus = String(activeCase.value?.automation?.status || '').trim().toLowerCase();
if (['queued', 'running', 'failed', 'manual_fallback'].includes(automationStatus)) {
@@ -215,12 +229,10 @@ const currentProcessingStageDisplay = computed(() => {
inconsistent: false,
};
}
const caseStage = activeCase.value?.current_stage;
const code = String(caseStage?.code || '').trim() || null;
return {
code,
code: caseCode || null,
labelKey: caseStage?.label_key
|| (code === 'order_creating' ? 'waiting_system_start' : code),
|| (caseCode === 'order_creating' ? 'waiting_system_start' : caseCode),
inconsistent: Boolean(caseStage?.inconsistent),
};
});
@@ -407,6 +419,34 @@ async function retryCurrentIntakeParse(): Promise<void> {
* 只切换前端工作面并同步可分享深链;禁止在导航点击中修改任何订单生命周期字段。
*/
const lifecycleStagePinnedByUserForCase = ref<string | null>(null);
/** 仅库存短缺放行可建立一次性自动推进许可,避免通用 Gate 恢复抢占用户导航。 */
const pendingReleaseAutoAdvanceCaseNo = ref<string | null>(null);
/**
* 消费库存短缺放行后的自动推进许可。
*
* 仅当同一 Case 仍停留在放行前的通知准备路由、用户未在放行后手动选阶段,且后端
* 权威投影已推进时替换深链。异步 Job 的首个投影可直接到目标阶段,不能依赖 watch
* 是否曾观察到旧阶段。
*/
async function syncPendingReleaseAutoAdvance(): Promise<boolean> {
const caseNo = String(activeCase.value?.case_no || '').trim();
if (!caseNo || pendingReleaseAutoAdvanceCaseNo.value !== caseNo) return false;
const requested = String(route.query.lifecycle_stage || '').trim();
if (
requested !== 'fulfillment_prep'
|| lifecycleStagePinnedByUserForCase.value === caseNo
) {
pendingReleaseAutoAdvanceCaseNo.value = null;
return false;
}
const current = lifecycleCurrentStageCode.value;
const currentIndex = current ? LIFECYCLE_STAGE_CODES.indexOf(current) : -1;
if (currentIndex <= LIFECYCLE_STAGE_CODES.indexOf('fulfillment_prep')) return false;
pendingReleaseAutoAdvanceCaseNo.value = null;
await selectLifecycleStage(current!, { replace: true });
return true;
}
async function selectLifecycleStage(code: ReviewLifecycleStageCode, options: { replace?: boolean } = {}) {
const target = lifecycleStageItems.value.find((item) => item.code === code);
@@ -414,7 +454,11 @@ async function selectLifecycleStage(code: ReviewLifecycleStageCode, options: { r
// 用户主动回看已完成阶段时,异步投影抵达不得将其强制带回当前阶段。
// 自动推进均使用 replace,不会写入该意图。
if (!options.replace) {
lifecycleStagePinnedByUserForCase.value = String(activeCase.value?.case_no || '').trim() || null;
const caseNo = String(activeCase.value?.case_no || '').trim() || null;
lifecycleStagePinnedByUserForCase.value = caseNo;
if (pendingReleaseAutoAdvanceCaseNo.value === caseNo) {
pendingReleaseAutoAdvanceCaseNo.value = null;
}
}
if (code === 'intake_parse' && intakeParseContextAuthorized.value && activeCase.value?.case_no) {
void ensureIntakeParseContextLoaded(activeCase.value.case_no);
@@ -472,7 +516,10 @@ watch(activeCase, (value) => {
watch(
() => String(activeCase.value?.case_no || '').trim(),
(caseNo, previousCaseNo) => {
if (caseNo !== previousCaseNo) lifecycleStagePinnedByUserForCase.value = null;
if (caseNo !== previousCaseNo) {
lifecycleStagePinnedByUserForCase.value = null;
pendingReleaseAutoAdvanceCaseNo.value = null;
}
},
);
@@ -628,7 +675,9 @@ const currentTaskPrimaryAction = computed(() => {
return null;
});
const currentTaskUserStatus = computed(() => {
if (formalTaskProjectionPending.value) return null;
// 已确认 OT 且权威投影在途时,先消费后端 automation 运行态(queued/running → running,
// failed/manual_fallback → failed),状态行即显真实值,不应退回「正在获取最新状态」骨架;
// authority 仍以右侧投影为准,投影到达后覆盖。
if (currentTaskUiState.value?.status) return currentTaskUiState.value.status;
// 建单前没有正式 OT 投影时,直接消费后端 Case automation 状态,避免页面看起来卡死。
const status = String(activeCase.value?.automation?.status || '').trim().toLowerCase();
@@ -1458,16 +1507,23 @@ async function retryFormalUploadRpa(taskNo: string) {
* 处理 Gate 恢复已被后端受理后的页面同步。
*
* 核心逻辑:恢复 Job 尚未完成时立即恢复用户交互,并重启五阶段轮询;三个只读投影
* 在后台刷新,避免把网络读取时间误表现为“恢复按钮卡住”。
* 在后台刷新,避免把网络读取时间误表现为“恢复按钮卡住”。库存短缺放行可额外建立
* 一次性自动推进许可,待后端生命周期投影确认后才更新深链。
*/
function handleGateRecoveryAccepted() {
function handleGateRecoveryAccepted(options: { releaseAutoAdvance?: boolean } = {}) {
const caseNo = String(activeCase.value?.case_no || '').trim();
if (options.releaseAutoAdvance && caseNo) {
// 放行前可能是用户手动进入通知准备;该历史意图不能阻止本次放行后的自动推进。
lifecycleStagePinnedByUserForCase.value = null;
pendingReleaseAutoAdvanceCaseNo.value = caseNo;
}
page.bumpWorkflowRefresh?.();
startWorkflowProgressPolling();
void Promise.allSettled([
loadFormalTaskDetail(),
reloadLatestDraft(),
refreshShippingProcessing(),
]);
]).then(() => { void syncPendingReleaseAutoAdvance(); });
}
/**
@@ -1998,6 +2054,48 @@ const lifecycleProgressReasonText = computed(() => {
if (!progress?.blocking_reason_i18n_key || !te(progress.blocking_reason_i18n_key)) return null;
return t(progress.blocking_reason_i18n_key);
});
/**
* Run 级进度横幅的统一展示消息。
*
* 核心逻辑:只有后端裁决 lifecycle_status == 'running' 才能声称「系统正在自动处理」;
* waiting(门禁待机)/ failed / cancelled 只展示等待/失败原因或步骤文本,禁止自动处理文案
* 与门禁状态自相矛盾;completed 时 Run 已无待办,横幅直接隐藏。
* 注意:后端仅在 running 下才有 current_step_i18n_key,因此旧逻辑在 waiting 时
* 会落入「系统正在自动处理」兜底文案,这正是本函数要消除的错误展示。
* 该横幅是「提取 Run(建单前流水线)」的运行提示,只在未有正式 OT 时展示
* (模板 v-if !hasFormalOt);正式 OT 后进度与操作指引由右栏当前任务卡承载。
*/
const lifecycleProgressMessage = computed<{
auto: boolean;
text: string;
reason: string | null;
} | null>(() => {
const progress = lifecycleProgressForView.value;
if (!progress) return null;
const status = String(progress.lifecycle_status || '').trim().toLowerCase();
const stepText = lifecycleProgressStepText.value;
const reasonText = lifecycleProgressReasonText.value;
if (status === 'running') {
return {
auto: true,
text: stepText || t('chat.shippingProcessing.progress.systemProcessingNeed'),
reason: reasonText,
};
}
if (status === 'completed') return null;
if (status === 'failed') {
return { auto: false, text: reasonText || t('chat.shippingProcessing.progress.lifecycleReason.failed'), reason: null };
}
if (status === 'cancelled') {
return { auto: false, text: reasonText || t('chat.shippingProcessing.progress.lifecycleReason.cancelled'), reason: null };
}
// waiting 或未知状态:绝不声称自动处理,优先展示后端原因,其次步骤文本。
return {
auto: false,
text: reasonText || stepText || t('chat.shippingProcessing.progress.lifecycleWaitingNeed'),
reason: null,
};
});
const lifecycleProgressActiveCode = computed<ReviewLifecycleStageCode | null>(() => {
const code = lifecycleProgressForView.value?.lifecycle_stage;
return LIFECYCLE_STAGE_CODES.includes(code as ReviewLifecycleStageCode)
@@ -2161,7 +2259,10 @@ const formalPreparationContext = computed(() => {
if (!fallbackLines.length) return preparation;
return {
...(preparation || {}),
status: preparation?.status || 'processing',
// [B1] 回退展示(任务详情未到、仅有 Case line_fulfillment)时不得伪造 status='processing':
// 那会让「processing → ready / blocked」看起来像二次刷新;真实状态由任务详情到达后显示,
// 徽章在状态为空时保持隐藏。
status: preparation?.status ?? null,
frozen_order: preparation?.frozen_order || {
task_no: confirmedNoticeTaskNo.value || null,
snapshot_id: null,
@@ -2274,7 +2375,7 @@ async function confirmReleaseInventoryShortage() {
// 放行写入成功即结束用户交互;Notice/Excel 由后端 Job 异步创建,不能让只读刷新
// 持续占用确认按钮。重启工作台轮询以立即接住新 Notice 的处理投影。
releaseInventoryBusy.value = false;
handleGateRecoveryAccepted();
handleGateRecoveryAccepted({ releaseAutoAdvance: true });
} catch (error) {
msg.warning(error instanceof Error ? error.message : t('chat.shippingOrderReview.workbench.resultNotReady'));
} finally {
@@ -2356,8 +2457,11 @@ async function loadFormalTaskDetail() {
formalTaskDetailLoadFailed.value = false;
return;
}
// 请求在途期间不保留上一文件详情,生命周期导航会自然回退至当前 Case/实时 Projection。
formalTaskDetail.value = null;
// 仅任务切换时清空旧详情。同一任务的刷新若先清空,会让 lifecycle_stage 在
// freight_upload 与空值之间来回跳,进而触发工作面 watcher 的重复拉取循环。
if (formalTaskDetail.value?.task_no !== taskNo) {
formalTaskDetail.value = null;
}
formalTaskDetailLoadFailed.value = false;
formalTaskDetailLoading.value = true;
try {
@@ -2668,6 +2772,71 @@ function freightLineNoDisplay(line: { line_no?: string | number | null; line_no_
return { text: String(value), muted: false, title: '' };
}
/**
* 逐发货通知单的货代上传状态展示。
* 核心逻辑:upload_status 为后端事实;把 uploaded/uploading/failed/pending 映射为文案与色阶,
* 并携带 upload_error_code/upload_error_message 作为逐单失败原因。前端禁止按 upload_status 之外的状态猜原因,
* 未知状态一律回落 pending,避免把内部执行字段当作用户原因。
* @param group - 当前发货通知单聚合行(含 upload_status 与逐单错误字段)。
*/
function freightUploadStatusDisplay(group: ShippingFreightNoticeGroup): {
state: 'uploaded' | 'uploading' | 'failed' | 'needs_human' | 'pending';
label: string;
chipClass: string;
reason: string | null;
} {
const status = String(group.upload_status || '').trim().toLowerCase();
const pendingHuman = String(group.pending_human_reason || '').trim().toLowerCase();
const reason = group.upload_error_message || group.upload_error_code || null;
if (['uploaded', 'succeeded', 'success'].includes(status)) {
return {
state: 'uploaded',
label: t('chat.shippingOrderReview.lifecycle.freightUpload.uploadStatus.uploaded'),
chipClass: 'bg-emerald-100 text-emerald-800 dark:bg-emerald-950/50 dark:text-emerald-200',
reason: null,
};
}
if (status === 'failed') {
return {
state: 'failed',
label: t('chat.shippingOrderReview.lifecycle.freightUpload.uploadStatus.failed'),
chipClass: 'bg-rose-100 text-rose-700 dark:bg-rose-950/50 dark:text-rose-300',
reason: reason ? t('chat.shippingOrderReview.lifecycle.freightUpload.uploadError', { reason }) : null,
};
}
// 存在错误码但状态未置 failed(如超时/结果未知):按“需人工”展示并始终带出逐单原因。
if (reason) {
return {
state: 'needs_human',
label: t('chat.shippingOrderReview.lifecycle.freightUpload.uploadStatus.needsHuman'),
chipClass: 'bg-amber-100 text-amber-800 dark:bg-amber-950/50 dark:text-amber-200',
reason: t('chat.shippingOrderReview.lifecycle.freightUpload.uploadError', { reason }),
};
}
if (['uploading', 'upload_rpa_pending'].includes(status) || pendingHuman === 'upload_rpa_pending') {
return {
state: 'uploading',
label: t('chat.shippingOrderReview.lifecycle.freightUpload.uploadStatus.uploading'),
chipClass: 'bg-blue-100 text-blue-800 dark:bg-blue-950/50 dark:text-blue-200',
reason: null,
};
}
if (pendingHuman) {
return {
state: 'needs_human',
label: t('chat.shippingOrderReview.lifecycle.freightUpload.uploadStatus.needsHuman'),
chipClass: 'bg-amber-100 text-amber-800 dark:bg-amber-950/50 dark:text-amber-200',
reason: reason ? t('chat.shippingOrderReview.lifecycle.freightUpload.uploadError', { reason }) : null,
};
}
return {
state: 'pending',
label: t('chat.shippingOrderReview.lifecycle.freightUpload.uploadStatus.pending'),
chipClass: 'bg-slate-100 text-slate-600 dark:bg-slate-800/60 dark:text-slate-300',
reason: null,
};
}
async function confirmPreparationWave() {
const caseNo = String(activeCase.value?.case_no || '').trim();
const draftVersion = activeCase.value?.draft?.draft_version;
@@ -3666,6 +3835,7 @@ watch(
watch(
lifecycleCurrentStageCode,
async (current, previous) => {
if (await syncPendingReleaseAutoAdvance()) return;
if (!current || !previous || current === previous) return;
// 只响应后端 current 阶段的真实推进:用户点击已完成阶段只会改路由,
// 绝不能触发自动跳转而被带回当前阶段。
@@ -3891,15 +4061,18 @@ watch(
@retry-parse="() => void retryCurrentIntakeParse()"
/>
<p
v-if="lifecycleProgressForView"
v-if="lifecycleProgressMessage && !hasFormalOt"
class="mt-2 rounded-md border-l-2 border-brand-500 bg-surface-soft px-2.5 py-1.5 text-xs text-secondary"
data-testid="review-run-lifecycle-current-step"
aria-live="polite"
>
<span class="mr-1 inline-block h-2 w-2 animate-pulse rounded-full bg-brand-500" aria-hidden="true" />
<span v-if="lifecycleProgressStepText">{{ lifecycleProgressStepText }}</span>
<span v-else>{{ t('chat.shippingProcessing.progress.systemProcessingNeed') }}</span>
<span v-if="lifecycleProgressReasonText" class="ml-1 text-warning">· {{ lifecycleProgressReasonText }}</span>
<span
v-if="lifecycleProgressMessage.auto"
class="mr-1 inline-block h-2 w-2 animate-pulse rounded-full bg-brand-500"
aria-hidden="true"
/>
<span>{{ lifecycleProgressMessage.text }}</span>
<span v-if="lifecycleProgressMessage.reason" class="ml-1 text-warning">· {{ lifecycleProgressMessage.reason }}</span>
</p>
</section>
</div>
@@ -4038,9 +4211,10 @@ watch(
<h3 class="text-sm font-semibold text-primary">{{ t('chat.shippingOrderReview.lifecycle.prepare.title') }}</h3>
<p class="mt-1 text-xs text-secondary">{{ t('chat.shippingOrderReview.lifecycle.prepare.subtitle') }}</p>
</div>
<span class="rounded-full bg-brand-50 px-2.5 py-1 text-xs font-medium text-brand-700 dark:bg-brand-950/30 dark:text-brand-300">
{{ formalPreparationContext.status || '-' }}
</span>
<span
v-if="formalPreparationContext.status"
class="rounded-full bg-brand-50 px-2.5 py-1 text-xs font-medium text-brand-700 dark:bg-brand-950/30 dark:text-brand-300"
>{{ formalPreparationContext.status }}</span>
</div>
<div v-if="formalPreparationContext.has_match_result && formalPreparationLines.length" class="review-fulfillment-prep-workspace mt-4 overflow-x-auto rounded-lg border border-default" data-testid="review-fulfillment-prep-workspace">
<div class="flex items-center justify-between gap-3 border-b border-default bg-surface-muted/30 px-3 py-2">
@@ -4100,15 +4274,7 @@ watch(
<h2 class="text-sm font-semibold text-primary">{{ t('chat.shippingOrderReview.lifecycle.freightUpload.title') }}</h2>
<p class="mt-1 text-xs text-secondary">{{ t('chat.shippingOrderReview.lifecycle.freightUpload.subtitle') }}</p>
</div>
<span class="rounded-full bg-surface-muted px-2 py-1 text-xs text-secondary">{{ formalTaskDetail.upload_status || '-' }}</span>
</div>
<div
v-if="formalTaskDetail.ui_state?.stage_context?.upload?.latest_attempt || formalTaskDetail.ui_state?.stage_context?.upload?.latest_attempt?.error_code"
class="mt-3 rounded-lg border border-default bg-surface-muted/50 p-3 text-xs text-secondary"
data-testid="review-upload-attempt-summary"
>
<p v-if="formalTaskDetail.ui_state?.stage_context?.upload?.latest_attempt" class="mt-1">{{ t('chat.shippingOrderReview.lifecycle.upload.attempt', { status: formalTaskDetail.ui_state.stage_context.upload.latest_attempt.status || '-', mode: formalTaskDetail.ui_state.stage_context.upload.latest_attempt.mode || '-' }) }}</p>
<p v-if="formalTaskDetail.ui_state?.stage_context?.upload?.latest_attempt?.error_code" class="mt-1 text-rose-700 dark:text-rose-300">{{ formalTaskDetail.ui_state.stage_context.upload.latest_attempt.error_code }}</p>
<span class="rounded-full bg-surface-muted px-2 py-1 text-xs text-secondary">{{ t('chat.shippingOrderReview.lifecycle.freightUpload.noticeCount', { count: freightNoticeGroups.length }) }}</span>
</div>
<div v-if="freightNoticeGroupsLoading" class="mt-3 text-xs text-secondary">{{ t('chat.shippingOrderReview.lifecycle.freightUpload.loading') }}</div>
<p v-else-if="freightNoticeGroupsLoadFailed" class="mt-3 rounded-lg border border-rose-200 bg-rose-50/70 p-3 text-xs text-rose-700 dark:border-rose-900 dark:bg-rose-950/20 dark:text-rose-200">{{ t('chat.shippingOrderReview.lifecycle.freightUpload.loadFailed') }}</p>
@@ -4120,8 +4286,9 @@ watch(
<div v-for="group in freightNoticeGroups" :key="group.business_no" class="rounded-lg border border-default" data-testid="review-freight-upload-notice">
<div class="flex flex-wrap items-center justify-between gap-3 border-b border-default bg-surface-muted/40 px-3 py-2 text-xs">
<label class="flex items-center gap-2 font-semibold text-primary"><input type="checkbox" :checked="selectedFreightBusinessNos.includes(group.business_no)" :aria-label="group.business_no" @change="toggleFreightNoticeSelection(group.business_no, ($event.target as HTMLInputElement).checked)"><span>{{ group.business_no }}</span></label>
<span class="text-secondary">PO: {{ group.po_number || '—' }} · {{ group.upload_status || '—' }} · {{ group.export_file_name || '—' }}</span>
<span class="text-secondary">PO: {{ group.po_number || '—' }} · {{ group.export_file_name || '—' }}</span>
<div class="flex items-center gap-2">
<span :class="freightUploadStatusDisplay(group).chipClass" class="rounded-full px-2 py-0.5 font-medium">{{ freightUploadStatusDisplay(group).label }}</span>
<button type="button" class="rounded border border-brand-200 px-2 py-1 font-medium text-brand-700 hover:bg-brand-50" data-testid="review-freight-upload-download" :disabled="saving" @click="void downloadFreightNotice(group.business_no)">{{ t('chat.shippingOrderReview.lifecycle.freightUpload.download') }}</button>
<details class="relative">
<summary class="list-none rounded border border-default p-1 text-secondary hover:bg-surface" :aria-label="t('chat.shippingProcessing.action.more')"><MoreHorizontal class="h-3.5 w-3.5" aria-hidden="true" /></summary>
@@ -4133,6 +4300,7 @@ watch(
</details>
</div>
</div>
<div v-if="freightUploadStatusDisplay(group).reason" class="border-b border-default bg-rose-50/50 px-3 py-2 text-xs text-rose-700 dark:bg-rose-950/10 dark:text-rose-300" data-testid="review-freight-upload-error">{{ freightUploadStatusDisplay(group).reason }}</div>
<table class="min-w-full text-left text-xs"><thead class="border-b border-default bg-surface-muted/30"><tr><th class="px-3 py-2">{{ t('chat.shippingOrderReview.lifecycle.freightUpload.lineNo') }}</th><th class="px-3 py-2">{{ t('chat.shippingOrderReview.workbench.resultPartNumber') }}</th><th class="px-3 py-2">{{ t('chat.shippingOrderReview.workbench.resultReservation') }}</th><th class="px-3 py-2">{{ t('chat.shippingOrderReview.lifecycle.prepare.requiredQty') }}</th></tr></thead><tbody><tr v-for="line in group.lines" :key="`${group.business_no}-${line.line_no}-${line.part_number}`" class="border-b border-default last:border-0" data-testid="review-freight-upload-line"><td class="px-3 py-2"><span :class="freightLineNoDisplay(line).muted ? 'text-muted' : ''" :title="freightLineNoDisplay(line).title">{{ freightLineNoDisplay(line).text }}</span></td><td class="px-3 py-2">{{ line.part_number || '—' }}</td><td class="px-3 py-2">{{ line.reservation_id || '—' }}</td><td class="px-3 py-2">{{ line.shipment_qty || '—' }}</td></tr></tbody></table>
</div>
</div>