Files
syops/frontend/src/features/chat/components/shipping-review/ReviewCustomerResolutionPanel.vue
T
hugo 6ac2effac3 feat(shipping-notice): 支持已确认订单改绑客户并重冻结快照
- 客户识别面板两态化:已识别折叠为摘要条 +「修改客户」入口,前端管控复用编辑窗头级谓词
- 新增 rebind_confirmed_customer 链路:守卫→更新 resolution 行→审计→手动重冻结,改后重新生成 Excel
- 守卫保证仅在无发货通知/Excel 产物时可改,匹配与门禁零触碰、无失效连锁
- 路由引擎/建单资格/编辑窗服务同步适配,errors 补充改绑错误码四语言
- 同步客户面板测试、确认对话框与四语言文案,落设计与实施计划文档
2026-10-13 21:13:58 +08:00

457 lines
20 KiB
Vue
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/chat/components/shipping-review/ReviewCustomerResolutionPanel.vue
用途:复核工作台固定「建单前客户确认」区域,两态渲染:未确认走识别候选确认流程;已确认折叠为一行摘要条并提供「修改客户」改绑入口。
依赖:vue-i18n、ShippingCustomerConfirmDialog、shipping.api 客户确认/重识别/改绑接口。
逻辑:resolved=false 时完整展开置顶(现状不动);resolved=true 时默认折叠摘要条(已确认徽标+标准客户名+chevron+修改客户按钮),
chevron 展开既有识别详情(不含 ODM 范围);改绑走「复用客户搜索选定 → 影响确认弹窗 → rebind API → 成功 toast → emit refreshed」。
resolution.rebind_allowed=false(Excel 已产生/活动 Attempt,后端投影下发)时按钮禁用并以 tooltip 说明;
后端 fail-closed 兜底,409 的 BusinessError localized message 直接经 toast 展示。
-->
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { BusinessError, resolveLocalizedErrorMessage } from '@/shared/api/client';
import { useMessage } from '@/shared/composables/useMessage';
import ShippingCustomerConfirmDialog from '@/features/chat/components/ShippingCustomerConfirmDialog.vue';
import {
confirmWorkspaceShippingOrderReviewCustomer,
listWorkspaceShippingCustomerOptions,
rebindWorkspaceShippingOrderReviewCustomer,
retryWorkspaceShippingOrderReviewCustomerResolution,
} from '@/features/chat/api/shipping.api';
import type {
ShippingCustomerOption,
ShippingOrderReviewCustomerResolution,
} from '@/features/chat/types/shipping.api.types';
const props = defineProps<{
caseNo: string;
resolution: ShippingOrderReviewCustomerResolution | null | undefined;
editable: boolean;
/** 已确认改绑入口可用性:edit_window.header 明确 false 时禁用;缺失/undefined 视为可用(后端权威守卫兜底) */
/** 改绑乐观锁版本:case 投影的 order_data_version(正式任务当前版本),缺省时后端按版本冲突拒绝 */
orderDataVersion?: number | null;
/** 已解决时缩为一行摘要(历史兼容属性;已确认态现在恒为折叠摘要条) */
compact?: boolean;
}>();
const emit = defineEmits<{
refreshed: [];
}>();
const { t, te } = useI18n();
const msg = useMessage();
const busy = ref(false);
const dialogOpen = ref(false);
const options = ref<ShippingCustomerOption[]>([]);
const loadingOptions = ref(false);
/** 已确认态识别详情是否展开(默认折叠为一行摘要条) */
const confirmedDetailsExpanded = ref(false);
/** 改绑影响确认弹窗显隐与待提交的新客户 */
const rebindConfirmOpen = ref(false);
const pendingRebindCustomerId = ref<number | null>(null);
const resolved = computed(() => Boolean(props.resolution?.resolved));
/** 改绑可用性:prop 缺省视为可用,与 formalHeaderEditable 的 fail-open 语义对齐(后端 409 兜底) */
const rebindAllowed = computed(() => props.resolution?.rebind_allowed === true);
const hasUniqueCandidate = computed(() => (
!resolved.value
&& String(props.resolution?.status || '').trim() === 'unique_candidate'
));
const statusLabel = computed(() => {
const status = String(props.resolution?.status || '').trim();
if (!status) {
return resolved.value
? t('chat.shippingOrderReview.customerResolution.status.confirmed')
: t('chat.shippingOrderReview.customerResolution.status.unmatched');
}
const key = `chat.shippingOrderReview.customerResolution.status.${status}`;
return te(key) ? t(key) : status;
});
const displayName = computed(() => {
const name = String(props.resolution?.standard_name || '').trim();
const code = String(props.resolution?.short_code || '').trim();
if (name && code) return `${name} (${code})`;
return name || code || String(props.resolution?.raw_name || '').trim() || '—';
});
const inputValues = computed(() => props.resolution?.input_values || []);
const candidateRows = computed(() => props.resolution?.candidates || []);
/** 改绑影响确认弹窗中展示的待提交客户名;选项已卸载时回落展示 ID。 */
const pendingRebindCustomerLabel = computed(() => {
const customerId = pendingRebindCustomerId.value;
if (customerId == null) return '—';
const match = options.value.find((item) => item.id === customerId);
if (match) return `${match.standard_name} (${match.short_code})`;
return `#${customerId}`;
});
function inputLabel(definitionCode: string | null | undefined): string {
const code = String(definitionCode || '').trim().toLowerCase();
if (code.includes('customer') && code.includes('code')) return t('chat.shippingOrderReview.customerResolution.inputLabels.customerCode');
if (code.includes('customer') && code.includes('name')) return t('chat.shippingOrderReview.customerResolution.inputLabels.customerName');
if (code.includes('address')) return t('chat.shippingOrderReview.customerResolution.inputLabels.address');
if (code.endsWith('customer_code')) return t('chat.shippingOrderReview.customerResolution.inputLabels.customerCode');
if (code.endsWith('customer_name')) return t('chat.shippingOrderReview.customerResolution.inputLabels.customerName');
if (code.endsWith('address')) return t('chat.shippingOrderReview.customerResolution.inputLabels.address');
if (code.includes('email')) return t('chat.shippingOrderReview.customerResolution.inputLabels.email');
if (code.includes('domain')) return t('chat.shippingOrderReview.customerResolution.inputLabels.domain');
return t('chat.shippingOrderReview.customerResolution.inputLabels.other');
}
function candidateValue(candidate: unknown, key: string): string {
const row = candidate && typeof candidate === 'object' ? candidate as Record<string, unknown> : {};
return String(row[key] ?? '').trim() || '—';
}
/** 切换已确认摘要条的识别详情展开/折叠。 */
function toggleConfirmedDetails() {
confirmedDetailsExpanded.value = !confirmedDetailsExpanded.value;
}
/**
* 打开标准客户选择弹窗并加载选项(未确认确认流程入口,also 供父级经 ref 外部触发)。
*/
async function openSelectDialog() {
if (!props.editable || busy.value) return;
dialogOpen.value = true;
loadingOptions.value = true;
try {
options.value = await listWorkspaceShippingCustomerOptions();
} catch (error: unknown) {
options.value = [];
msg.warning(error instanceof Error ? error.message : t('chat.shippingCustomer.loadFailed'));
} finally {
loadingOptions.value = false;
}
}
defineExpose({
openSelectDialog,
});
/**
* 打开改绑客户选择弹窗(已确认态「修改客户」入口;复用同一搜索选择弹窗)。
* AI 触发时机:用户在已确认摘要条点击「修改客户」时进入;rebindAllowed=false 时按钮已禁用,这里仅兜底防抖。
*/
async function openRebindDialog() {
if (!rebindAllowed.value || busy.value) return;
dialogOpen.value = true;
loadingOptions.value = true;
try {
options.value = await listWorkspaceShippingCustomerOptions();
} catch (error: unknown) {
options.value = [];
msg.warning(error instanceof Error ? error.message : t('chat.shippingCustomer.loadFailed'));
} finally {
loadingOptions.value = false;
}
}
/**
* 按关键词搜索标准客户(客户选择弹窗复用入口)。
*/
async function searchCustomers(query: string) {
loadingOptions.value = true;
try {
options.value = await listWorkspaceShippingCustomerOptions(query.trim());
} catch (error: unknown) {
msg.warning(error instanceof Error ? error.message : t('chat.shippingCustomer.loadFailed'));
} finally {
loadingOptions.value = false;
}
}
/**
* 客户选择弹窗确认分发:已确认态进入改绑影响确认(不重走识别确认);未确认态走既有确认建单链路。
*/
function onCustomerDialogConfirm(customerId: number) {
if (resolved.value) {
pendingRebindCustomerId.value = customerId;
dialogOpen.value = false;
rebindConfirmOpen.value = true;
return;
}
void onConfirm(customerId);
}
/**
* 确认标准客户并通知父级刷新 Case / 建单资格(仅未确认态)。
*/
async function onConfirm(customerId: number) {
if (!props.caseNo || busy.value) return;
busy.value = true;
try {
const result = await confirmWorkspaceShippingOrderReviewCustomer(props.caseNo, {
customer_id: customerId,
reason: t('chat.shippingOrderReview.customerResolution.confirmReason'),
});
dialogOpen.value = false;
const resume = result.resume_outcome;
if (resume?.outcome === 'auto_created' || resume?.outcome === 'idempotent_replay') {
msg.success(t('chat.shippingOrderReview.resume.autoCreated', {
taskNo: resume.task_no || '',
}));
} else if (resume?.outcome === 'waiting_manual_confirm') {
msg.success(t('chat.shippingOrderReview.resume.waitingManualConfirm'));
} else {
msg.success(t('chat.shippingOrderReview.customerResolution.confirmed'));
}
emit('refreshed');
} catch (error: unknown) {
const message = error instanceof BusinessError
? resolveLocalizedErrorMessage(error.code, error.message)
: (error instanceof Error ? error.message : t('chat.shippingCustomer.confirmFailed'));
msg.warning(message);
} finally {
busy.value = false;
}
}
/**
* 提交改绑:携带 case 投影的 order_data_version 乐观锁调用 rebind API;成功后 toast 并 emit refreshed 走既有刷新链。
* 失败:BusinessError(含 409 守卫/版本冲突)展示后端 localized message;其余错误回落 rebindFailedToast。
*/
async function onRebindConfirmSubmit() {
const customerId = pendingRebindCustomerId.value;
if (!props.caseNo || customerId == null || busy.value) return;
busy.value = true;
try {
await rebindWorkspaceShippingOrderReviewCustomer(props.caseNo, {
customer_id: customerId,
expected_order_data_version: Number(props.orderDataVersion ?? 0),
});
rebindConfirmOpen.value = false;
pendingRebindCustomerId.value = null;
confirmedDetailsExpanded.value = false;
msg.success(t('chat.shippingOrderReview.customerResolution.rebindSuccessToast'));
emit('refreshed');
} catch (error: unknown) {
const message = error instanceof BusinessError
? resolveLocalizedErrorMessage(error.code, error.message)
: t('chat.shippingOrderReview.customerResolution.rebindFailedToast');
msg.warning(message);
} finally {
busy.value = false;
}
}
/**
* 重新识别客户(不触发 PDF 重识别)。
*/
async function onRetry() {
if (!props.caseNo || !props.editable || busy.value) return;
busy.value = true;
try {
await retryWorkspaceShippingOrderReviewCustomerResolution(props.caseNo);
msg.success(t('chat.shippingOrderReview.customerResolution.retryDone'));
emit('refreshed');
} catch (error: unknown) {
const message = error instanceof BusinessError
? resolveLocalizedErrorMessage(error.code, error.message)
: (error instanceof Error ? error.message : t('chat.shippingOrderReview.customerResolution.retryFailed'));
msg.warning(message);
} finally {
busy.value = false;
}
}
</script>
<template>
<section
class="rounded-xl border border-default bg-surface p-3 shadow-sm"
data-testid="review-customer-resolution-panel"
:data-compact="compact && resolved ? 'true' : 'false'"
:data-state="resolved ? 'resolved' : 'pending'"
>
<!-- 已确认态:折叠摘要条(已确认徽标 + 标准客户名 + chevron + 修改客户入口),不整体占位 -->
<template v-if="resolved">
<div
class="flex flex-wrap items-center justify-between gap-2"
data-testid="review-customer-resolution-compact"
>
<button
type="button"
class="flex min-w-0 flex-1 items-center gap-2 text-left"
data-testid="review-customer-resolution-toggle"
:aria-expanded="confirmedDetailsExpanded"
@click="toggleConfirmedDetails"
>
<span
class="shrink-0 rounded-full bg-emerald-100 px-2 py-0.5 text-[11px] font-medium text-emerald-800 dark:bg-emerald-950 dark:text-emerald-100"
data-testid="review-customer-resolution-badge"
>
{{ statusLabel }}
</span>
<span class="truncate text-xs font-medium text-primary">{{ displayName }}</span>
<span class="shrink-0 text-[10px] text-muted" aria-hidden="true">{{ confirmedDetailsExpanded ? '▲' : '▼' }}</span>
</button>
<button
type="button"
class="shrink-0 rounded-xl border border-default bg-surface px-3 py-1.5 text-sm text-secondary hover:bg-surface-soft disabled:opacity-60"
data-testid="review-rebind-customer"
:disabled="!rebindAllowed || busy"
:title="rebindAllowed ? undefined : t('chat.shippingOrderReview.customerResolution.rebindDisabledExcelExists')"
@click="openRebindDialog"
>
{{ t('chat.shippingOrderReview.customerResolution.rebindButton') }}
</button>
</div>
<!-- 已确认展开区:既有识别详情(标准客户/编码/命中输入/候选),不含 ODM 范围 -->
<dl
v-if="confirmedDetailsExpanded"
class="mt-2 space-y-1 text-xs text-secondary"
data-testid="review-customer-resolution-details"
>
<div class="flex flex-wrap gap-x-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.standardCustomer') }}</dt>
<dd class="font-medium text-primary">{{ displayName }}</dd>
</div>
<div v-if="resolution?.short_code" class="flex flex-wrap gap-x-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.customerCode') }}</dt>
<dd class="font-mono font-medium text-primary">{{ resolution.short_code }}</dd>
</div>
<div v-if="inputValues.length" class="mt-2 border-t border-default pt-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.matchedInputs') }}</dt>
<dd v-for="item in inputValues" :key="`${item.definition_code}:${item.normalized_value}`" class="mt-1 flex flex-wrap gap-x-2">
<span class="text-muted">{{ inputLabel(item.definition_label || item.definition_code) }}</span>
<span class="font-mono text-primary">{{ item.raw_value || item.normalized_value || '—' }}</span>
</dd>
</div>
<div v-if="candidateRows.length > 1" class="mt-2 border-t border-default pt-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.candidates') }}</dt>
<dd v-for="candidate in candidateRows" :key="String(candidate.customer_id)" class="mt-1 flex flex-wrap gap-x-2">
<span class="text-primary">{{ candidateValue(candidate, 'standard_name') }}</span>
<span class="font-mono text-muted">{{ candidateValue(candidate, 'short_code') }}</span>
</dd>
</div>
</dl>
</template>
<!-- 未确认态:既有识别候选确认流程(现状不动) -->
<template v-else>
<header class="flex flex-wrap items-start justify-between gap-2">
<div>
<h2 class="text-sm font-semibold">{{ t('chat.shippingOrderReview.customerResolution.title') }}</h2>
<p class="mt-0.5 text-xs text-muted">
{{ hasUniqueCandidate
? t('chat.shippingOrderReview.customerResolution.uniqueCandidateHint')
: t('chat.shippingOrderReview.customerResolution.subtitle') }}
</p>
</div>
<span
class="rounded-full px-2.5 py-1 text-xs font-medium bg-amber-100 text-amber-900 dark:bg-amber-950 dark:text-amber-100"
data-testid="review-customer-resolution-badge"
>
{{ statusLabel }}
</span>
</header>
<dl class="mt-2 space-y-1 text-xs text-secondary">
<div class="flex flex-wrap gap-x-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.standardCustomer') }}</dt>
<dd class="font-medium text-primary">{{ displayName }}</dd>
</div>
<div v-if="resolution?.short_code" class="flex flex-wrap gap-x-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.customerCode') }}</dt>
<dd class="font-mono font-medium text-primary">{{ resolution.short_code }}</dd>
</div>
<div v-if="inputValues.length" class="mt-2 border-t border-default pt-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.matchedInputs') }}</dt>
<dd v-for="item in inputValues" :key="`${item.definition_code}:${item.normalized_value}`" class="mt-1 flex flex-wrap gap-x-2">
<span class="text-muted">{{ inputLabel(item.definition_label || item.definition_code) }}</span>
<span class="font-mono text-primary">{{ item.raw_value || item.normalized_value || '—' }}</span>
</dd>
</div>
<div v-if="candidateRows.length > 1" class="mt-2 border-t border-default pt-2">
<dt class="text-muted">{{ t('chat.shippingOrderReview.customerResolution.candidates') }}</dt>
<dd v-for="candidate in candidateRows" :key="String(candidate.customer_id)" class="mt-1 flex flex-wrap gap-x-2">
<span class="text-primary">{{ candidateValue(candidate, 'standard_name') }}</span>
<span class="font-mono text-muted">{{ candidateValue(candidate, 'short_code') }}</span>
</dd>
</div>
</dl>
<div v-if="editable" class="mt-3 flex flex-wrap gap-2">
<button
type="button"
class="rounded-xl bg-primary px-3 py-1.5 text-sm font-medium text-on-primary disabled:opacity-60"
data-testid="review-select-customer"
:disabled="busy"
@click="openSelectDialog"
>
{{ hasUniqueCandidate
? t('chat.shippingOrderReview.customerResolution.confirmUniqueCandidate')
: t('chat.shippingOrderReview.customerResolution.selectCustomer') }}
</button>
<button
type="button"
class="rounded-xl border border-default bg-surface px-3 py-1.5 text-sm text-secondary hover:bg-surface-soft disabled:opacity-60"
data-testid="review-retry-customer"
:disabled="busy"
@click="onRetry"
>
{{ busy ? t('chat.shippingOrderReview.customerResolution.retrying') : t('chat.shippingOrderReview.customerResolution.retryCustomer') }}
</button>
</div>
<p class="mt-2 text-[11px] text-muted">
{{ t('chat.shippingOrderReview.customerResolution.pdfReparseHint') }}
</p>
</template>
<!-- 客户搜索选择弹窗:未确认态确认 / 已确认态改绑共用 -->
<ShippingCustomerConfirmDialog
:open="dialogOpen"
:options="options"
:loading="loadingOptions || busy"
@close="dialogOpen = false"
@search="searchCustomers"
@confirm="onCustomerDialogConfirm"
/>
<!-- 改绑影响确认弹窗:明示快照重冻结与通知/Excel 重生成影响后再提交。Teleport 到 body 避免粘性列穿透。 -->
<Teleport to="body">
<div
v-if="rebindConfirmOpen"
class="admin-modal-overlay animate-fade-in items-center z-[180]"
role="dialog"
aria-modal="true"
data-testid="review-rebind-confirm-dialog"
>
<div class="admin-modal-panel max-w-md scale-100">
<div class="px-6 py-5 border-b border-subtle">
<h3 class="section-title">{{ t('chat.shippingOrderReview.customerResolution.rebindConfirmTitle') }}</h3>
</div>
<div class="px-6 py-5 space-y-3 text-sm">
<p class="text-secondary">{{ t('chat.shippingOrderReview.customerResolution.rebindConfirmImpact') }}</p>
<p class="font-medium text-primary" data-testid="review-rebind-confirm-customer">{{ pendingRebindCustomerLabel }}</p>
</div>
<div class="admin-modal-footer">
<button
type="button"
class="btn-ghost"
data-testid="review-rebind-confirm-cancel"
:disabled="busy"
@click="rebindConfirmOpen = false"
>
{{ t('common.cancel') }}
</button>
<button
type="button"
class="btn-primary"
data-testid="review-rebind-confirm-submit"
:disabled="busy"
@click="onRebindConfirmSubmit"
>
{{ t('chat.shippingCustomer.confirm') }}
</button>
</div>
</div>
</div>
</Teleport>
</section>
</template>