339 lines
16 KiB
Vue
339 lines
16 KiB
Vue
/**
|
||
* features/chat/components/messages/MaintenanceDeviceResultCard.vue
|
||
* 用途:设备维保助手会话中的设备信息与维修记录结果卡片。
|
||
* 依赖:auth.store、chat.types、maintenance ledger/record 类型、repair-status-display.utils、maintenance-panel-layout.utils、vue-i18n。
|
||
* 逻辑:后端返回 queryMode=device 时按台账配置表单同序展示设备信息;queryMode=records 时按维修记录配置字段展示记录列表。
|
||
*/
|
||
<script setup lang="ts">
|
||
import { computed, ref } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { useAuthStore } from '@/features/auth/stores/auth.store';
|
||
import type { ChatMessage } from '../../types/chat.types';
|
||
import type { MaintenanceDevice } from '@/features/maintenance/types/ledger.types';
|
||
import type { MaintenanceRecord } from '@/features/maintenance/types/record.types';
|
||
import { repairStatusBadgeClass } from '@/features/maintenance/utils/repair-status-display.utils';
|
||
import { buildMaintenancePanelWidthStyle } from '@/features/maintenance/utils/maintenance-panel-layout.utils';
|
||
|
||
interface MaintenanceDeviceResultPayload {
|
||
matched?: boolean;
|
||
queryMode?: 'device' | 'records';
|
||
keyword?: string;
|
||
device?: MaintenanceDevice;
|
||
records?: MaintenanceRecord[];
|
||
summary?: {
|
||
recentRecordCount?: number;
|
||
statusCounts?: Record<string, number>;
|
||
ledgerMatched?: boolean;
|
||
};
|
||
}
|
||
|
||
type DisplayCell = {
|
||
label: string;
|
||
value: string | number | null | undefined;
|
||
};
|
||
|
||
const props = defineProps<{
|
||
message: ChatMessage;
|
||
}>();
|
||
|
||
const emit = defineEmits<{
|
||
'edit-record': [record: MaintenanceRecord];
|
||
'view-record-history': [device: MaintenanceDevice];
|
||
'create-record': [device: MaintenanceDevice];
|
||
}>();
|
||
|
||
const { t } = useI18n();
|
||
const authStore = useAuthStore();
|
||
|
||
const recordDetailPanelStyle = buildMaintenancePanelWidthStyle();
|
||
|
||
const payload = computed<MaintenanceDeviceResultPayload | null>(() => {
|
||
const raw = props.message.payload;
|
||
if (!raw || typeof raw !== 'object') return null;
|
||
return raw as MaintenanceDeviceResultPayload;
|
||
});
|
||
|
||
const device = computed(() => payload.value?.device || null);
|
||
const records = computed(() => payload.value?.records || []);
|
||
const queryMode = computed(() => payload.value?.queryMode === 'records' ? 'records' : 'device');
|
||
const hasResult = computed(() => Boolean(device.value || records.value.length));
|
||
const ledgerMatched = computed(() => payload.value?.summary?.ledgerMatched !== false);
|
||
const canManageMaintenance = computed(() => ['admin', 'maintenance_manager'].includes(String(authStore.user?.role || '')));
|
||
const selectedRecord = ref<MaintenanceRecord | null>(null);
|
||
|
||
const deviceStatusLabel = (status?: string | null) => {
|
||
if (status === 'in_use') return t('maintenance.ledger.status.inUse');
|
||
if (status === 'disabled') return t('maintenance.ledger.status.disabled');
|
||
if (status === 'scrapped') return t('maintenance.ledger.status.scrapped');
|
||
if (status === 'sold') return t('maintenance.ledger.status.sold');
|
||
return status || '-';
|
||
};
|
||
|
||
const recordStatusLabel = (status?: string | null) => {
|
||
if (status === 'pending') return t('maintenance.record.status.pending');
|
||
if (status === 'processing') return t('maintenance.record.status.processing');
|
||
if (status === 'done') return t('maintenance.record.status.done');
|
||
if (status === 'cancelled') return t('maintenance.record.status.cancelled');
|
||
return status || '-';
|
||
};
|
||
|
||
const displayValue = (value: DisplayCell['value']) => {
|
||
if (value === null || value === undefined || value === '') return '-';
|
||
return String(value);
|
||
};
|
||
|
||
const firstCell = (row: DisplayCell[]): DisplayCell => row[0] || { label: '', value: '' };
|
||
|
||
const deviceRows = computed<DisplayCell[][]>(() => {
|
||
const item = device.value;
|
||
if (!item) return [];
|
||
// 字段顺序与 LedgerConfigPanel 设备配置表单保持一致(左列优先、逐行两列)。
|
||
return [
|
||
[
|
||
{ label: t('maintenance.ledger.fields.deviceCode'), value: item.deviceCode },
|
||
{ label: t('maintenance.ledger.fields.deviceName'), value: item.deviceName },
|
||
],
|
||
[
|
||
{ label: t('maintenance.ledger.fields.modelSpec'), value: item.modelSpec },
|
||
{ label: t('maintenance.ledger.fields.manufacturer'), value: item.manufacturer },
|
||
],
|
||
[
|
||
{ label: t('maintenance.ledger.fields.devicePosition'), value: item.installLocation },
|
||
{ label: t('maintenance.ledger.fields.deviceStatus'), value: deviceStatusLabel(item.deviceStatus) },
|
||
],
|
||
[
|
||
{ label: t('maintenance.ledger.fields.department'), value: item.department },
|
||
{ label: t('maintenance.ledger.fields.responsibleUser'), value: item.responsibleUser },
|
||
],
|
||
[
|
||
{ label: t('maintenance.ledger.fields.productionDate'), value: item.productionDate },
|
||
{ label: t('maintenance.ledger.fields.entryDate'), value: item.entryDate },
|
||
],
|
||
[
|
||
{ label: t('maintenance.ledger.fields.originalValue'), value: item.originalValue },
|
||
{ label: t('maintenance.ledger.fields.residualValue'), value: item.residualValue },
|
||
],
|
||
[
|
||
{ label: t('maintenance.ledger.fields.accountSet'), value: item.accountSet },
|
||
],
|
||
[
|
||
{ label: t('maintenance.ledger.fields.remark'), value: item.remark },
|
||
],
|
||
];
|
||
});
|
||
|
||
const latestRepairDate = computed(() => records.value[0]?.reportDate || '-');
|
||
const totalCost = computed(() => records.value.reduce((sum, item) => {
|
||
const value = Number(String(item.repairCost || '').replace(/[^\d.-]/g, ''));
|
||
return Number.isFinite(value) ? sum + value : sum;
|
||
}, 0));
|
||
const summaryCards = computed(() => [
|
||
{ label: t('maintenance.record.fields.deviceCode'), value: device.value?.deviceCode || payload.value?.keyword || '-' },
|
||
{ label: t('maintenance.chat.latestRepair'), value: latestRepairDate.value },
|
||
{ label: t('maintenance.chat.totalCount'), value: String(records.value.length) },
|
||
{ label: t('maintenance.chat.totalCost'), value: totalCost.value ? String(totalCost.value) : '-' },
|
||
]);
|
||
|
||
const recordDetailRows = (record: MaintenanceRecord): DisplayCell[] => [
|
||
{ label: t('maintenance.record.fields.deviceCode'), value: record.deviceCode },
|
||
{ label: t('maintenance.ledger.fields.deviceName'), value: record.deviceName },
|
||
{ label: t('maintenance.record.fields.reportDate'), value: record.reportDate },
|
||
{ label: t('maintenance.record.fields.repairCategory'), value: record.repairCategory },
|
||
{ label: t('maintenance.record.fields.repairStatus'), value: recordStatusLabel(record.repairStatus) },
|
||
{ label: t('maintenance.record.fields.faultDescription'), value: record.faultDescription },
|
||
{ label: t('maintenance.record.fields.repairDescription'), value: record.repairDescription },
|
||
{ label: t('maintenance.record.fields.reporter'), value: record.reporter },
|
||
{ label: t('maintenance.record.fields.repairDate'), value: record.repairDate },
|
||
{ label: t('maintenance.record.fields.repairUnit'), value: record.repairUnit },
|
||
{ label: t('maintenance.record.fields.repairer'), value: record.repairer },
|
||
{ label: t('maintenance.record.fields.approver'), value: record.approver },
|
||
{ label: t('maintenance.record.fields.repairCost'), value: record.repairCost },
|
||
{ label: t('maintenance.record.fields.repairHours'), value: record.repairHours },
|
||
{ label: t('maintenance.record.fields.sourceOrderNo'), value: record.sourceOrderNo },
|
||
{ label: t('maintenance.ledger.fields.devicePosition'), value: record.installLocation },
|
||
];
|
||
</script>
|
||
|
||
<template>
|
||
<section
|
||
v-if="hasResult"
|
||
class="mt-3 w-full max-w-[760px] overflow-hidden rounded-lg border border-slate-300 bg-white text-sm text-slate-800 shadow-sm"
|
||
>
|
||
<div class="flex items-center justify-between gap-3 border-b border-slate-200 bg-slate-50 px-3 py-2.5">
|
||
<h3 class="font-semibold text-slate-900">
|
||
{{ queryMode === 'records' ? t('maintenance.chat.recordQueryResult') : t('maintenance.chat.deviceLedgerInfo') }}
|
||
</h3>
|
||
<span
|
||
class="shrink-0 rounded-full px-2.5 py-0.5 text-xs font-semibold"
|
||
:class="ledgerMatched ? 'bg-emerald-50 text-emerald-700' : 'bg-amber-50 text-amber-700'"
|
||
>
|
||
{{ queryMode === 'records' ? t('maintenance.chat.returnedRecords', { count: records.length }) : t('maintenance.chat.matched') }}
|
||
</span>
|
||
</div>
|
||
|
||
<template v-if="queryMode === 'device'">
|
||
<div v-if="device" class="overflow-x-auto p-3">
|
||
<table class="w-full min-w-[680px] border-collapse text-sm">
|
||
<tbody>
|
||
<tr v-for="(row, rowIndex) in deviceRows" :key="rowIndex">
|
||
<template v-if="row.length === 1">
|
||
<th class="w-[16%] border border-slate-200 bg-slate-100 px-3 py-2 text-left font-semibold text-slate-700">
|
||
{{ firstCell(row).label }}
|
||
</th>
|
||
<td colspan="3" class="border border-slate-200 px-3 py-2 text-slate-900">
|
||
{{ displayValue(firstCell(row).value) }}
|
||
</td>
|
||
</template>
|
||
<template v-else>
|
||
<template v-for="cell in row" :key="cell.label">
|
||
<th class="w-[16%] border border-slate-200 bg-slate-100 px-3 py-2 text-left font-semibold text-slate-700">
|
||
{{ cell.label }}
|
||
</th>
|
||
<td class="w-[34%] border border-slate-200 px-3 py-2 text-slate-900">
|
||
{{ displayValue(cell.value) }}
|
||
</td>
|
||
</template>
|
||
</template>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<div v-if="canManageMaintenance" class="flex flex-wrap gap-2 px-3 pb-3">
|
||
<button
|
||
v-if="device"
|
||
type="button"
|
||
class="rounded-lg bg-brand-600 px-3 py-2 text-xs font-semibold text-white transition-colors hover:bg-brand-700"
|
||
@click="emit('view-record-history', device)"
|
||
>
|
||
{{ t('maintenance.chat.viewRepairHistory') }}
|
||
</button>
|
||
<button
|
||
v-if="device"
|
||
type="button"
|
||
class="rounded-lg border border-slate-300 bg-white px-3 py-2 text-xs font-semibold text-slate-700 transition-colors hover:bg-slate-50"
|
||
@click="emit('create-record', device)"
|
||
>
|
||
{{ t('maintenance.chat.addRepairRecord') }}
|
||
</button>
|
||
</div>
|
||
<p v-else class="px-3 pb-3 text-xs text-slate-500">
|
||
{{ t('maintenance.chat.adminActionsHidden') }}
|
||
</p>
|
||
</template>
|
||
|
||
<template v-else>
|
||
<div class="grid gap-2 p-3 sm:grid-cols-2 lg:grid-cols-4">
|
||
<div v-for="card in summaryCards" :key="card.label" class="rounded-lg border border-dashed border-slate-300 bg-white px-3 py-2">
|
||
<div class="text-xs text-slate-500">{{ card.label }}</div>
|
||
<div class="mt-1 text-lg font-bold text-slate-900">{{ card.value }}</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="px-3 pb-3">
|
||
<div class="overflow-hidden border border-slate-200">
|
||
<table class="w-full table-fixed border-collapse text-left text-xs">
|
||
<thead class="bg-slate-100 text-slate-700">
|
||
<tr>
|
||
<th class="w-[13%] border border-slate-200 px-2 py-2 font-semibold">{{ t('maintenance.record.fields.reportDate') }}</th>
|
||
<th class="w-[29%] border border-slate-200 px-2 py-2 font-semibold">{{ t('maintenance.record.fields.faultDescription') }}</th>
|
||
<th class="w-[29%] border border-slate-200 px-2 py-2 font-semibold">{{ t('maintenance.record.fields.repairDescription') }}</th>
|
||
<th class="w-[11%] border border-slate-200 px-2 py-2 font-semibold">{{ t('maintenance.record.fields.repairStatus') }}</th>
|
||
<th class="w-[18%] border border-slate-200 px-2 py-2 font-semibold">{{ t('maintenance.chat.operation') }}</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr v-for="record in records" :key="record.id" class="bg-white">
|
||
<td class="whitespace-nowrap border border-slate-200 px-2 py-2">{{ displayValue(record.reportDate) }}</td>
|
||
<td class="border border-slate-200 px-2 py-2 align-top" :title="displayValue(record.faultDescription)">
|
||
<span class="line-clamp-2 break-words leading-relaxed">{{ displayValue(record.faultDescription) }}</span>
|
||
</td>
|
||
<td class="border border-slate-200 px-2 py-2 align-top" :title="displayValue(record.repairDescription)">
|
||
<span class="line-clamp-2 break-words leading-relaxed">{{ displayValue(record.repairDescription) }}</span>
|
||
</td>
|
||
<td class="border border-slate-200 px-2 py-2">
|
||
<span
|
||
class="inline-flex whitespace-nowrap rounded-full border px-2 py-0.5 text-xs font-semibold"
|
||
:class="repairStatusBadgeClass(record.repairStatus)"
|
||
>
|
||
{{ recordStatusLabel(record.repairStatus) }}
|
||
</span>
|
||
</td>
|
||
<td class="border border-slate-200 px-2 py-2">
|
||
<div class="flex items-center gap-1.5">
|
||
<button
|
||
type="button"
|
||
class="inline-flex shrink-0 items-center justify-center whitespace-nowrap rounded-md border border-brand-200 bg-brand-50 px-2 py-1 text-xs font-semibold leading-none text-brand-700 transition-colors hover:bg-brand-100"
|
||
@click="selectedRecord = record"
|
||
>
|
||
{{ t('maintenance.chat.viewRecordDetail') }}
|
||
</button>
|
||
<button
|
||
v-if="canManageMaintenance"
|
||
type="button"
|
||
class="inline-flex shrink-0 items-center justify-center whitespace-nowrap rounded-md border border-brand-200 bg-brand-50 px-2 py-1 text-xs font-semibold leading-none text-brand-700 transition-colors hover:bg-brand-100"
|
||
@click="emit('edit-record', record)"
|
||
>
|
||
{{ t('maintenance.record.actions.edit') }}
|
||
</button>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<div
|
||
v-if="selectedRecord"
|
||
class="fixed inset-y-0 right-0 z-50 flex flex-col border-l border-slate-200 bg-white shadow-2xl"
|
||
:style="recordDetailPanelStyle"
|
||
>
|
||
<div class="flex items-start justify-between gap-3 border-b border-slate-200 px-5 py-4">
|
||
<div>
|
||
<h3 class="text-base font-semibold text-slate-900">{{ t('maintenance.chat.recordDetailTitle') }}</h3>
|
||
<p class="mt-1 text-xs text-slate-500">{{ t('maintenance.chat.recordDetailHint') }}</p>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
class="flex h-8 w-8 items-center justify-center rounded-lg border border-slate-200 text-slate-500 transition-colors hover:bg-slate-50 hover:text-slate-700"
|
||
@click="selectedRecord = null"
|
||
>
|
||
×
|
||
</button>
|
||
</div>
|
||
|
||
<div class="flex-1 overflow-y-auto px-4 py-4">
|
||
<div class="mb-4 rounded-lg border border-dashed border-brand-300 bg-brand-50 px-3 py-2 text-xs text-brand-700">
|
||
{{ t('maintenance.chat.recordDetailPermissionHint') }}
|
||
</div>
|
||
<dl class="overflow-hidden border border-slate-200 text-sm">
|
||
<div
|
||
v-for="detail in recordDetailRows(selectedRecord)"
|
||
:key="detail.label"
|
||
class="grid grid-cols-[118px_minmax(0,1fr)] border-b border-slate-200 last:border-b-0"
|
||
>
|
||
<dt class="bg-slate-100 px-3 py-2 font-semibold text-slate-700">{{ detail.label }}</dt>
|
||
<dd class="min-w-0 break-words px-3 py-2 text-slate-900">{{ displayValue(detail.value) }}</dd>
|
||
</div>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div v-if="canManageMaintenance" class="flex flex-wrap gap-2 px-3 pb-3">
|
||
<button
|
||
v-if="device"
|
||
type="button"
|
||
class="rounded-lg border border-slate-300 bg-white px-3 py-2 text-xs font-semibold text-slate-700 transition-colors hover:bg-slate-50"
|
||
@click="emit('create-record', device)"
|
||
>
|
||
{{ t('maintenance.chat.addRepairRecord') }}
|
||
</button>
|
||
</div>
|
||
<p v-else class="px-3 pb-3 text-xs text-slate-500">
|
||
{{ t('maintenance.chat.adminActionsHidden') }}
|
||
</p>
|
||
</template>
|
||
</section>
|
||
</template>
|