473 lines
20 KiB
Vue
473 lines
20 KiB
Vue
<!--
|
||
features/chat/components/messages/TransferBacklogSummaryCard.vue
|
||
用途:待处理异常卡(可恢复/已关闭/结果未知三组)——列与匹配窗口一致,
|
||
支持关键字/结果筛选、多选批量操作(重查库存/关闭)与行级快捷操作。
|
||
依赖:Element Plus ElTable/ElTableColumn/ElSelect/ElOption/ElPagination/
|
||
ElInput/ElButton/ElTag/ElCheckbox(2026-10-20:勾选列一并 EP;testid 挂
|
||
el-checkbox,规格经内层 input 读写)、
|
||
props.items(服务端返回的当前页切片)+ props.total(筛选后全量计数)、
|
||
i18n chat.transfer.*、transferBatch.deriveBatchNo、transferFormat.formatQuantity。
|
||
逻辑:2026-10-16 服务端分页 + 筛选下推(后端 GET /backlog page/page_size/
|
||
keyword/result_filter)——本组件只持有筛选 UI 态并向父级上抛 query-change,
|
||
由宿主经 useChatTransfer.loadBacklog 拉取当前页;行渲染/勾选/批量语义:
|
||
仅 blocked_recoverable 可勾选与操作;result_unknown / manually_closed 只读;
|
||
可恢复行按 result_code 细分徽标(blocked_file_removed=文件已移除);
|
||
blocked_file_removed 行行内仅留「关闭」(消失证据无法由重查翻回,
|
||
唯一出口=人工关闭或等文件重新同步);
|
||
批量与行内都只有「重查库存」和「关闭」。重查库存与恢复是同一次 ERP 核对,
|
||
2026-10-22 起不再并列两个按钮。
|
||
调拨类型列取行上 transfer_type,按启用类型配置显示当前语言名称。
|
||
勾选集合跨页保留(翻页/筛选不清空;批量计数=勾选总数,后端逐行校验兜底);
|
||
关键字 300ms 防抖后上抛,筛选/翻页变更即上抛,首挂载上抛初始切片查询
|
||
(首页计数卡进入时 initialFilter 直接生效)。
|
||
行定位:el-table 行不支持逐行 attr,行 testid 改经 row-class-name 注入
|
||
(transfer-backlog-row-<invoice_no>,spec/e2e 用类选择器等价定位)。
|
||
[2026-10-22 跨页全选] 服务端分页下表头全选只能覆盖当前页——Gmail 式补链:
|
||
有勾选且未全选时透出「选择全部 N 行(按当前筛选)」,点击经
|
||
GET /backlog/item-ids 按同一筛选口径拉全量 id 合并进勾选集合(批量端点
|
||
按 item_ids 列表逐行校验,无需新批量契约);拉取失败经 useMessage 行内报错。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import {
|
||
ElButton,
|
||
ElCheckbox,
|
||
ElInput,
|
||
ElOption,
|
||
ElPagination,
|
||
ElSelect,
|
||
ElTable,
|
||
ElTableColumn,
|
||
ElTag,
|
||
} from 'element-plus';
|
||
import type { CheckboxValueType } from 'element-plus';
|
||
import { useMessage } from '@/shared/composables/useMessage';
|
||
import { fetchBacklogSelectableItemIds } from '../../api/transfer.api';
|
||
import { TRANSFER_PAGE_SIZE } from '../../composables/useChatTransfer';
|
||
import { deriveBatchNo } from './transferBatch';
|
||
import { formatQuantity } from './transferFormat';
|
||
import { transferTypeDisplay } from './transferTypeLabel';
|
||
import type { TransferBacklogItem, TransferTypeOption } from '../../types/transfer.types';
|
||
|
||
const props = withDefaults(defineProps<{
|
||
/** 当前页行切片(服务端分页返回;筛选在服务端执行)。 */
|
||
items: TransferBacklogItem[];
|
||
/** 筛选后全量计数(分页器数据源;不传视为 0=隐藏分页条)。 */
|
||
total?: number;
|
||
busy?: boolean;
|
||
/** 初始结果筛选(首页计数卡切片进入:closed 卡落「已关闭」切片等)。 */
|
||
initialFilter?: 'all' | 'recoverable' | 'closed' | 'unknown';
|
||
/** 启用中的调拨类型。用来把行上的 code 显示成当前语言名称。 */
|
||
transferTypes?: TransferTypeOption[];
|
||
}>(), {
|
||
transferTypes: () => [],
|
||
});
|
||
|
||
const { locale } = useI18n();
|
||
|
||
/**
|
||
* 把待处理行的调拨类型 code 显示成名称。
|
||
* 核心逻辑:复用 transferTypeDisplay;没有类型时显示 —。
|
||
* @param code - 行上的 transfer_type
|
||
* @returns 当前语言名称、编码或占位
|
||
*/
|
||
function typeLabel(code: string | null | undefined): string {
|
||
return transferTypeDisplay(code, props.transferTypes, locale.value);
|
||
}
|
||
|
||
const emit = defineEmits<{
|
||
refresh: [];
|
||
/** 行级与批量共用:按明细 id 重查 ERP 库存。 */
|
||
requery: [itemIds: number[]];
|
||
close: [item: TransferBacklogItem];
|
||
batchClose: [itemIds: number[]];
|
||
/** 查询变更(翻页/筛选/首挂载):宿主据此调用 loadBacklog 拉取对应页。 */
|
||
'query-change': [query: { page: number; keyword: string; resultFilter: 'all' | 'recoverable' | 'closed' | 'unknown' }];
|
||
}>();
|
||
|
||
/** 每页行数(与组合式函数 loadBacklog 默认 pageSize 同源:TRANSFER_PAGE_SIZE)。 */
|
||
const PAGE_SIZE = TRANSFER_PAGE_SIZE;
|
||
|
||
/** 勾选集合(本地视图态;仅可恢复行可勾选;跨页保留——批量计数=勾选总数,
|
||
* 行状态变化由后端批量端点逐行校验兜底)。 */
|
||
const selectedIds = ref<Set<number>>(new Set());
|
||
const keyword = ref('');
|
||
const resultFilter = ref<'all' | 'recoverable' | 'closed' | 'unknown'>(props.initialFilter ?? 'all');
|
||
const currentPage = ref(1);
|
||
|
||
function isActionable(item: TransferBacklogItem): boolean {
|
||
return item.status === 'blocked_recoverable';
|
||
}
|
||
|
||
/**
|
||
* 失效行(文件已移除)判定:行内只留「标记无需继续处理」出口。
|
||
* @param item 待处理池行;result_code=blocked_file_removed 为失效行
|
||
*/
|
||
function isFileRemoved(item: TransferBacklogItem): boolean {
|
||
return item.result_code === 'blocked_file_removed';
|
||
}
|
||
|
||
const selectedActionableIds = computed(() => [...selectedIds.value]);
|
||
|
||
const allFilteredSelected = computed(() => {
|
||
const actionable = props.items.filter(isActionable);
|
||
return actionable.length > 0 && actionable.every((item) => selectedIds.value.has(item.item_id));
|
||
});
|
||
|
||
function toggleRow(item: TransferBacklogItem, checked: boolean) {
|
||
const next = new Set(selectedIds.value);
|
||
if (checked) next.add(item.item_id);
|
||
else next.delete(item.item_id);
|
||
selectedIds.value = next;
|
||
}
|
||
|
||
function toggleAll(checked: boolean) {
|
||
const next = new Set(selectedIds.value);
|
||
for (const item of props.items.filter(isActionable)) {
|
||
if (checked) next.add(item.item_id);
|
||
else next.delete(item.item_id);
|
||
}
|
||
selectedIds.value = next;
|
||
}
|
||
|
||
function clearSelection() {
|
||
selectedIds.value = new Set();
|
||
}
|
||
|
||
const { error: notifyError } = useMessage();
|
||
|
||
/** 跨页全选拉取进行中(防重入)。 */
|
||
const selectAllBusy = ref(false);
|
||
|
||
/** 跨页全选入口可见性:有筛选总量且勾选未覆盖全量时透出(选完后自动隐藏)。 */
|
||
const showSelectAll = computed(() => (props.total ?? 0) > selectedIds.value.size);
|
||
|
||
/**
|
||
* 跨页全选(Gmail 式,2026-10-22):按当前筛选拉取全部待处理明细 id 合并进勾选集合。
|
||
* 服务端分页下表头全选只覆盖当前页;批量端点按 item_ids 列表逐行校验,全量 id 直接复用
|
||
* (GET /backlog/item-ids,keyword/result_filter 与当前筛选同源)。
|
||
*/
|
||
async function selectAllMatching() {
|
||
if (selectAllBusy.value) return;
|
||
selectAllBusy.value = true;
|
||
try {
|
||
const data = await fetchBacklogSelectableItemIds({
|
||
keyword: keyword.value,
|
||
resultFilter: resultFilter.value,
|
||
});
|
||
selectedIds.value = new Set(data.ids.map((id) => Number(id)));
|
||
} catch (e) {
|
||
notifyError(e instanceof Error ? e.message : String(e));
|
||
} finally {
|
||
selectAllBusy.value = false;
|
||
}
|
||
}
|
||
|
||
// [STEP 1] 查询上抛:翻页/筛选立即上抛,关键字 300ms 防抖(服务端逐次查询防抖动);
|
||
// 首挂载即上抛初始切片查询(宿主不再兜底首拉,避免双请求)。
|
||
function emitQuery() {
|
||
emit('query-change', { page: currentPage.value, keyword: keyword.value.trim(), resultFilter: resultFilter.value });
|
||
}
|
||
|
||
watch(currentPage, () => emitQuery());
|
||
|
||
watch(resultFilter, () => {
|
||
if (currentPage.value !== 1) currentPage.value = 1; // 翻页 watch 负责上抛
|
||
else emitQuery();
|
||
});
|
||
|
||
let keywordTimer: ReturnType<typeof setTimeout> | undefined;
|
||
watch(keyword, () => {
|
||
clearTimeout(keywordTimer);
|
||
keywordTimer = setTimeout(() => {
|
||
if (currentPage.value !== 1) currentPage.value = 1;
|
||
else emitQuery();
|
||
}, 300);
|
||
});
|
||
|
||
onBeforeUnmount(() => clearTimeout(keywordTimer));
|
||
|
||
onMounted(() => emitQuery());
|
||
|
||
// [STEP 2] 行操作/刷新后总行数变小:收拢当前页防空白页(越界时回落末页并重查)。
|
||
watch(() => props.total, (total = 0) => {
|
||
const maxPage = Math.max(1, Math.ceil(total / PAGE_SIZE));
|
||
if (currentPage.value > maxPage) {
|
||
currentPage.value = maxPage; // 翻页 watch 负责上抛
|
||
}
|
||
});
|
||
|
||
function agingLabel(level: string): string {
|
||
const map: Record<string, string> = {
|
||
warn_3d: 'chat.transfer.agingWarn',
|
||
escalate_5d: 'chat.transfer.agingEscalate',
|
||
critical_7d: 'chat.transfer.agingCritical',
|
||
result_unknown: 'chat.transfer.resultUnknown',
|
||
closed: 'chat.transfer.statusClosed',
|
||
};
|
||
return map[level] ?? 'chat.transfer.agingNormal';
|
||
}
|
||
|
||
function rowKey(item: TransferBacklogItem): string {
|
||
return `${item.item_id}|${item.invoice_no}`;
|
||
}
|
||
|
||
/**
|
||
* el-table 行类:注入行定位类(替代旧行 testid)+ 不可操作行置灰(同旧行样式)。
|
||
*/
|
||
function rowClassName({ row }: { row: TransferBacklogItem }): string {
|
||
return [
|
||
`transfer-backlog-row-${row.invoice_no}`,
|
||
isActionable(row) ? '' : 'bg-surface-soft text-muted',
|
||
].filter(Boolean).join(' ');
|
||
}
|
||
|
||
/** 结果徽标文案键:可恢复行按 result_code 细分(blocked_file_removed=文件已移除),其余按状态。 */
|
||
function resultLabelKey(item: TransferBacklogItem): string {
|
||
if (item.status === 'result_unknown') return 'chat.transfer.resultUnknown';
|
||
if (item.status === 'manually_closed') return 'chat.transfer.statusClosed';
|
||
return item.result_code === 'blocked_file_removed'
|
||
? 'chat.transfer.resultFileRemoved'
|
||
: 'chat.transfer.resultBlocked';
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="my-2 rounded-2xl border border-brand-100 bg-surface p-4 shadow-sm" data-testid="transfer-backlog-card">
|
||
<div class="mb-2 flex items-center justify-between">
|
||
<h3 class="text-sm font-bold text-primary">{{ $t('chat.transfer.backlogTitle') }}</h3>
|
||
<el-button
|
||
size="small"
|
||
data-testid="transfer-backlog-refresh"
|
||
@click="emit('refresh')"
|
||
>
|
||
{{ $t('chat.transfer.refreshProgress') }}
|
||
</el-button>
|
||
</div>
|
||
|
||
<!-- 跨页全选入口(Gmail 式,2026-10-22):服务端分页下表头全选只覆盖当前页,
|
||
有未选中行时透出「选择全部 N 行」,按当前筛选拉全量 id 合并进勾选集合
|
||
(testid 独立于表头全选 checkbox 的 transfer-backlog-select-all) -->
|
||
<div v-if="showSelectAll" class="mb-2 text-xs">
|
||
<button
|
||
type="button"
|
||
class="text-secondary underline decoration-dotted underline-offset-2 disabled:opacity-50"
|
||
:disabled="selectAllBusy"
|
||
data-testid="transfer-backlog-select-all-matching"
|
||
@click="selectAllMatching"
|
||
>{{ $t('chat.transfer.selectAllRows', { count: total ?? 0 }) }}</button>
|
||
</div>
|
||
|
||
<!-- 批量操作条:勾选 ≥1 行时原位替换筛选区(跨页勾选同样计入) -->
|
||
<div v-if="selectedActionableIds.length" class="mb-2 flex flex-wrap items-center gap-2">
|
||
<span class="text-xs font-bold text-secondary" data-testid="transfer-backlog-selected-count">
|
||
{{ $t('chat.transfer.selectedRows', { count: selectedActionableIds.length }) }}
|
||
</span>
|
||
<el-button
|
||
size="small"
|
||
type="primary"
|
||
plain
|
||
data-testid="transfer-backlog-batch-requery"
|
||
:disabled="busy"
|
||
@click="emit('requery', selectedActionableIds)"
|
||
>
|
||
{{ $t('chat.transfer.requeryStock') }}({{ selectedActionableIds.length }})
|
||
</el-button>
|
||
<el-button
|
||
size="small"
|
||
type="danger"
|
||
plain
|
||
data-testid="transfer-backlog-batch-close"
|
||
:disabled="busy"
|
||
:title="$t('chat.transfer.manualClose')"
|
||
@click="emit('batchClose', selectedActionableIds)"
|
||
>
|
||
{{ $t('chat.transfer.manualCloseShort') }}({{ selectedActionableIds.length }})
|
||
</el-button>
|
||
<el-button
|
||
size="small"
|
||
data-testid="transfer-backlog-clear-selection"
|
||
@click="clearSelection"
|
||
>
|
||
{{ $t('chat.transfer.clearSelection') }}
|
||
</el-button>
|
||
</div>
|
||
<div v-else class="mb-2 flex flex-wrap items-center gap-2">
|
||
<div class="w-56 shrink-0">
|
||
<el-input
|
||
v-model="keyword"
|
||
size="small"
|
||
clearable
|
||
:placeholder="$t('chat.transfer.filterPlaceholder')"
|
||
data-testid="transfer-backlog-filter"
|
||
/>
|
||
</div>
|
||
<!-- el-select 默认 width:100%(EP 样式会覆盖工具类),外包定宽容器锁定紧凑尺寸,
|
||
与关键字输入并排一行,避免筛选区独占两行 -->
|
||
<div class="w-32 shrink-0">
|
||
<el-select
|
||
v-model="resultFilter"
|
||
size="small"
|
||
data-testid="transfer-backlog-result-filter"
|
||
:aria-label="$t('chat.transfer.colResult')"
|
||
>
|
||
<el-option value="all" :label="$t('chat.transfer.filterAll')" />
|
||
<el-option value="recoverable" :label="$t('chat.transfer.homeRecoverable')" />
|
||
<el-option value="closed" :label="$t('chat.transfer.homeClosed')" />
|
||
<el-option value="unknown" :label="$t('chat.transfer.homeUnknown')" />
|
||
</el-select>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- EP 表格:show-overflow-tooltip;左固定勾选,右固定结果+操作;单号/老化可滚 -->
|
||
<el-table
|
||
v-if="items.length"
|
||
:data="items"
|
||
:row-key="rowKey"
|
||
:row-class-name="rowClassName"
|
||
size="small"
|
||
class="text-xs"
|
||
data-testid="transfer-backlog-table"
|
||
>
|
||
<el-table-column fixed width="48">
|
||
<template #header>
|
||
<el-checkbox
|
||
size="small"
|
||
:model-value="allFilteredSelected"
|
||
data-testid="transfer-backlog-select-all"
|
||
@change="(val: CheckboxValueType) => toggleAll(Boolean(val))"
|
||
/>
|
||
</template>
|
||
<template #default="{ row }">
|
||
<el-checkbox
|
||
size="small"
|
||
:model-value="selectedIds.has(row.item_id)"
|
||
:disabled="!isActionable(row as TransferBacklogItem) || busy"
|
||
:data-testid="`transfer-backlog-check-${row.invoice_no}`"
|
||
@change="(val: CheckboxValueType) => toggleRow(row as TransferBacklogItem, Boolean(val))"
|
||
/>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colSource')" prop="source_code" min-width="120" show-overflow-tooltip />
|
||
<el-table-column :label="$t('chat.transfer.transferTypeLabel')" min-width="140" show-overflow-tooltip>
|
||
<template #default="{ row }">
|
||
<span :data-testid="`transfer-backlog-type-${row.invoice_no}`">{{ typeLabel(row.transfer_type) }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
:label="$t('chat.transfer.transferPendingMatchColDocNo')"
|
||
prop="invoice_no"
|
||
min-width="130"
|
||
show-overflow-tooltip
|
||
>
|
||
<template #default="{ row }">
|
||
<span class="font-semibold">{{ row.invoice_no || '—' }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
:label="$t('chat.transfer.transferPendingMatchColExternalDoc')"
|
||
min-width="130"
|
||
show-overflow-tooltip
|
||
>
|
||
<template #default="{ row }">
|
||
<span data-testid="transfer-backlog-external-doc">{{ row.external_doc_no || '—' }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colMovementDate')" min-width="110" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ row.movement_date || '—' }}</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colMaterial')" prop="material_no" min-width="140" show-overflow-tooltip />
|
||
<el-table-column :label="$t('chat.transfer.colName')" prop="material_name" min-width="160" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ row.material_name || '—' }}</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colFromWarehouse')" min-width="110" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ row.from_warehouse || '—' }}</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colFromBatch')" min-width="120" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ deriveBatchNo(row.lot_no ?? '') || '—' }}</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colFromQty')" min-width="90" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ formatQuantity(row.external_quantity) }}</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colToWarehouse')" min-width="110" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ row.to_warehouse || '—' }}</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colToQty')" min-width="90" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ formatQuantity(row.movement_quantity) }}</template>
|
||
</el-table-column>
|
||
<el-table-column :label="$t('chat.transfer.colAging')" min-width="90" show-overflow-tooltip>
|
||
<template #default="{ row }">{{ $t(agingLabel(row.aging_level)) }}</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
fixed="right"
|
||
:label="$t('chat.transfer.colResult')"
|
||
width="100"
|
||
>
|
||
<template #default="{ row }">
|
||
<el-tag
|
||
size="small"
|
||
effect="light"
|
||
:type="row.status === 'blocked_recoverable' ? 'danger' : row.status === 'result_unknown' ? 'warning' : 'info'"
|
||
:title="row.reason || undefined"
|
||
data-testid="transfer-backlog-result"
|
||
>
|
||
{{ $t(resultLabelKey(row as TransferBacklogItem)) }}
|
||
</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
fixed="right"
|
||
:label="$t('chat.transfer.colAction')"
|
||
width="150"
|
||
>
|
||
<template #default="{ row }">
|
||
<template v-if="isActionable(row as TransferBacklogItem)">
|
||
<!-- 失效行(文件已移除)只留关闭:重查会被后端拒绝(TRANSFER.RETRY_NOT_ALLOWED)。 -->
|
||
<el-button
|
||
v-if="!isFileRemoved(row as TransferBacklogItem)"
|
||
link
|
||
type="primary"
|
||
size="small"
|
||
:data-testid="`transfer-backlog-requery-${row.invoice_no}`"
|
||
:disabled="busy"
|
||
@click="emit('requery', [row.item_id])"
|
||
>
|
||
{{ $t('chat.transfer.requeryStock') }}
|
||
</el-button>
|
||
<el-button
|
||
link
|
||
type="danger"
|
||
size="small"
|
||
:title="$t('chat.transfer.manualClose')"
|
||
:data-testid="`transfer-backlog-close-${row.invoice_no}`"
|
||
:disabled="busy"
|
||
@click="emit('close', row as TransferBacklogItem)"
|
||
>
|
||
{{ $t('chat.transfer.manualCloseShort') }}
|
||
</el-button>
|
||
</template>
|
||
<span v-else-if="row.status === 'result_unknown'" class="text-[11px] text-muted">
|
||
{{ $t('chat.transfer.resultUnknownReadonly') }}
|
||
</span>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
<div v-else class="rounded-lg bg-surface-soft p-3 text-xs text-muted">
|
||
{{ $t('chat.transfer.noBacklog') }}
|
||
</div>
|
||
<!-- 服务端分页条(单页时隐藏):total=筛选后全量计数;文案走全局 ElConfigProvider 四语言 -->
|
||
<el-pagination
|
||
v-if="(total ?? 0) > PAGE_SIZE"
|
||
:current-page="currentPage"
|
||
:total="total ?? 0"
|
||
:page-size="PAGE_SIZE"
|
||
layout="total, prev, pager, next, jumper"
|
||
size="small"
|
||
class="mt-2 justify-end"
|
||
data-testid="transfer-backlog-pagination"
|
||
@current-change="(page: number) => { currentPage = page }"
|
||
/>
|
||
</div>
|
||
</template>
|