feat(equipment-mainetance): 设备维保助手优化

This commit is contained in:
sophie committed 2026-07-08 09:12:11 +08:00
1 parent 9ea49d7060
commit fa965dd45b
25 files changed
+1609 -304

No files matched your search

@@ -19,6 +19,8 @@ import TrainingSummaryMessage from './TrainingSummaryMessage.vue';
import TrainingResultCard from './TrainingResultCard.vue';
import type { ChatMessage, InlineDownloadFile, SourceFileLink } from '../../types/chat.types';
import type { ShippingInventoryMappingFormPayload, ShippingMatchResultPayload } from '../../types/shipping.types';
import type { MaintenanceDevice } from '@/features/maintenance/types/ledger.types';
import type { MaintenanceRecord } from '@/features/maintenance/types/record.types';
export interface ChatMessageBubbleHandlers {
renderMarkdown: (text: string) => string;
@@ -76,6 +78,9 @@ const emit = defineEmits<{
resend: [content: string];
regenerate: [];
'open-feedback': [];
'maintenance-edit-record': [record: MaintenanceRecord];
'maintenance-view-record-history': [device: MaintenanceDevice];
'maintenance-create-record-for-device': [device: MaintenanceDevice];
}>();
const authStore = useAuthStore();
@@ -693,6 +698,9 @@ function handleInlineDownload(file: InlineDownloadFile): void {
<MaintenanceDeviceResultCard
v-else-if="message.type === 'maintenance-device-result'"
:message="message"
@edit-record="emit('maintenance-edit-record', $event)"
@view-record-history="emit('maintenance-view-record-history', $event)"
@create-record="emit('maintenance-create-record-for-device', $event)"
/>
<TrainingExamQuestionMessage
v-else-if="isTrainingExamMessage && trainingHandlers.getQuestion()"
@@ -1,31 +1,51 @@
/**
* features/chat/components/messages/MaintenanceDeviceResultCard.vue
* 用途:设备维保助手会话中的设备台账查询结果卡片。
* 依赖:chat.types、ledger/record 类型、vue-i18n。
* 逻辑:后端返回 maintenance-device-result payload 后,本组件按原型展示设备基础信息、匹配状态和快捷操作。
* 用途:设备维保助手会话中的设备信息与维修记录结果卡片。
* 依赖: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 } from 'vue';
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;
};
actions?: Array<{ id: string; label: string }>;
}
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;
@@ -35,21 +55,39 @@ const payload = computed<MaintenanceDeviceResultPayload | null>(() => {
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 statusLabel = computed(() => {
const status = device.value?.deviceStatus;
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 rows = computed(() => {
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 },
@@ -57,76 +95,244 @@ const rows = computed(() => {
],
[
{ label: t('maintenance.ledger.fields.modelSpec'), value: item.modelSpec },
{ label: t('maintenance.ledger.fields.department'), value: item.department },
{ 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: statusLabel.value },
{ label: t('maintenance.ledger.fields.deviceStatus'), value: deviceStatusLabel(item.deviceStatus) },
],
[
{ label: t('maintenance.ledger.fields.manufacturer'), value: item.manufacturer },
{ label: t('maintenance.ledger.fields.startDate'), value: item.startDate },
{ 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 rounded-xl border border-slate-300 bg-white p-3 text-sm text-slate-800 shadow-sm">
<div class="flex items-center justify-between gap-3 rounded-t-lg border border-slate-200 bg-slate-50 px-3 py-2">
<h3 class="font-semibold">
{{ device ? t('maintenance.chat.deviceLedgerInfo') : t('maintenance.chat.recordInfo') }}
<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="rounded-full px-2.5 py-0.5 text-xs font-semibold"
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'"
>
{{ ledgerMatched ? t('maintenance.chat.matched') : t('maintenance.chat.recordMatched') }}
{{ queryMode === 'records' ? t('maintenance.chat.returnedRecords', { count: records.length }) : t('maintenance.chat.matched') }}
</span>
</div>
<div v-if="device" class="overflow-hidden border-x border-b border-slate-200">
<table class="w-full table-fixed text-sm">
<tbody>
<tr v-for="(row, rowIndex) in rows" :key="rowIndex" class="border-t border-slate-200 first:border-t-0">
<template v-for="cell in row" :key="cell.label">
<th class="w-[16%] bg-slate-100 px-3 py-2 text-left font-semibold text-slate-700">{{ cell.label }}</th>
<td class="w-[34%] px-3 py-2 text-slate-800">{{ cell.value || '-' }}</td>
</template>
</tr>
</tbody>
</table>
</div>
<div v-if="records.length" class="mt-3 overflow-hidden rounded-lg border border-slate-200">
<div class="bg-slate-50 px-3 py-2 text-xs font-semibold text-slate-600">
{{ t('maintenance.chat.recentRecords', { count: records.length }) }}
<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>
<table class="w-full text-left text-xs">
<thead class="bg-slate-50 text-slate-500">
<tr>
<th class="px-3 py-2">{{ t('maintenance.record.fields.reportDate') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.fields.faultDescription') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.fields.repairer') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="record in records.slice(0, 3)" :key="record.id" class="border-t border-slate-200">
<td class="px-3 py-2">{{ record.reportDate || '-' }}</td>
<td class="max-w-[260px] truncate px-3 py-2">{{ record.faultDescription || '-' }}</td>
<td class="px-3 py-2">{{ record.repairer || '-' }}</td>
</tr>
</tbody>
</table>
</div>
<div class="mt-3 flex flex-wrap gap-2">
<button type="button" class="rounded-lg bg-brand-600 px-3 py-2 text-xs font-semibold text-white">
{{ t('maintenance.chat.viewRepairHistory') }}
</button>
<button type="button" class="rounded-lg border border-slate-300 bg-white px-3 py-2 text-xs font-semibold text-slate-700">
{{ t('maintenance.chat.addRepairRecord') }}
</button>
</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>