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

@@ -3,7 +3,7 @@
* features/maintenance/components/LedgerConfigPanel.vue
* 用途:设备维保助手右侧固定面板中的“台账配置”页面,包含设备列表与设备配置两个 Tab。
* 依赖:maintenanceLedger.api、PaginationBar、useMessage。
* 逻辑:列表 Tab 负责查询/导入/选择设备;配置 Tab 负责新增或编辑设备台账,保存后刷新列表。
* 逻辑:列表 Tab 负责查询/导入/选择设备;配置 Tab 负责新增或编辑设备台账,保存后刷新列表;启用日期与维护周期仅保留后端字段,不在 UI 展示。
*/
import { computed, onMounted, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
@@ -83,8 +83,33 @@ const formatStatus = (value: string): string => {
return item?.label ? t(item.label) : value || '-';
};
/**
* 根据设备状态返回列表标签颜色,帮助管理员快速区分设备可用性。
* @param value - 设备状态枚举值。
* @returns Tailwind class 字符串。
*/
const statusBadgeClass = (value: string): string => {
if (value === 'in_use') return 'border-emerald-200 bg-emerald-50 text-emerald-700';
if (value === 'disabled') return 'border-slate-200 bg-slate-100 text-slate-600';
if (value === 'scrapped') return 'border-rose-200 bg-rose-50 text-rose-700';
if (value === 'sold') return 'border-sky-200 bg-sky-50 text-sky-700';
return 'border-slate-200 bg-slate-50 text-slate-600';
};
/**
* 将金额输入统一转为可提交字符串,兼容 type=number 绑定产生的 number 值。
* @param value - 表单中的原值/残值。
* @returns 裁剪后的字符串;空值返回 undefined。
*/
const normalizeAmountField = (value: string | number | null | undefined): string | undefined => {
if (value === null || value === undefined || value === '') return undefined;
const text = String(value).trim();
return text || undefined;
};
/**
* 从表单状态生成 API payload,集中裁剪字符串。
* 核心逻辑:startDate / maintenanceCycle 虽不在 UI 展示,编辑时仍随 payload 回传,避免保存其他字段时清空库内已有值。
* @returns 设备保存入参。
*/
const buildPayload = (): MaintenanceDevicePayload => ({
@@ -100,8 +125,8 @@ const buildPayload = (): MaintenanceDevicePayload => ({
startDate: form.startDate || undefined,
responsibleUser: form.responsibleUser?.trim(),
maintenanceCycle: form.maintenanceCycle?.trim(),
originalValue: form.originalValue?.trim(),
residualValue: form.residualValue?.trim(),
originalValue: normalizeAmountField(form.originalValue),
residualValue: normalizeAmountField(form.residualValue),
accountSet: form.accountSet?.trim(),
remark: form.remark?.trim(),
});
@@ -350,7 +375,7 @@ onMounted(loadDevices);
<th class="px-3 py-2">{{ t('maintenance.ledger.table.modelSpec') }}</th>
<th class="px-3 py-2">{{ t('maintenance.ledger.table.manufacturer') }}</th>
<th class="px-3 py-2">{{ t('maintenance.ledger.table.status') }}</th>
<th class="px-3 py-2">{{ t('maintenance.ledger.table.operation') }}</th>
<th class="px-3 py-2 text-right">{{ t('maintenance.ledger.table.operation') }}</th>
</tr>
</thead>
<tbody>
@@ -365,11 +390,24 @@ onMounted(loadDevices);
<td class="px-3 py-2">{{ device.deviceName }}</td>
<td class="px-3 py-2">{{ device.modelSpec || '-' }}</td>
<td class="px-3 py-2">{{ device.manufacturer || '-' }}</td>
<td class="px-3 py-2">{{ formatStatus(device.deviceStatus) }}</td>
<td class="px-3 py-2">
<button type="button" class="admin-report-page-btn" @click="startEdit(device)">
{{ t('maintenance.ledger.actions.edit') }}
</button>
<span
class="inline-flex min-w-14 items-center justify-center rounded-full border px-2.5 py-1 text-xs font-semibold"
:class="statusBadgeClass(device.deviceStatus)"
>
{{ formatStatus(device.deviceStatus) }}
</span>
</td>
<td class="px-3 py-2">
<div class="flex justify-end">
<button
type="button"
class="admin-report-page-btn admin-report-page-btn--edit"
@click="startEdit(device)"
>
{{ t('maintenance.ledger.actions.edit') }}
</button>
</div>
</td>
</tr>
</tbody>
@@ -437,14 +475,6 @@ onMounted(loadDevices);
{{ t('maintenance.ledger.fields.entryDate') }}
<input v-model="form.entryDate" type="date" class="admin-table-filter-input" />
</label>
<label class="grid gap-1 text-sm text-secondary">
{{ t('maintenance.ledger.fields.startDate') }}
<input v-model="form.startDate" type="date" class="admin-table-filter-input" />
</label>
<label class="grid gap-1 text-sm text-secondary">
{{ t('maintenance.ledger.fields.maintenanceCycle') }}
<input v-model="form.maintenanceCycle" class="admin-table-filter-input" :placeholder="t('maintenance.ledger.placeholder.maintenanceCycle')" />
</label>
<label class="grid gap-1 text-sm text-secondary">
{{ t('maintenance.ledger.fields.originalValue') }}
<input v-model="form.originalValue" type="number" min="0" step="0.01" class="admin-table-filter-input" />
@@ -0,0 +1,129 @@
<script setup lang="ts">
/**
* features/maintenance/components/MaintenanceDeviceSnapshotCard.vue
* 用途:维修记录表单中展示已匹配的设备台账快照卡片。
* 依赖:device-status-display.utils、maintenance ledger 类型、vue-i18n。
* 逻辑:MaintenanceRecordManagerPanel / MaintenanceRecordPanel 在设备编号下方引用;按加载、匹配、未找到、空态四种状态渲染。
*/
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { BadgeCheck, BriefcaseBusiness, Building2, MapPin, UserRound } from 'lucide-vue-next';
import type { MaintenanceDevice } from '@/features/maintenance/types/ledger.types';
import { deviceStatusBadgeClass, deviceStatusLabelKey } from '@/features/maintenance/utils/device-status-display.utils';
const props = defineProps<{
device: MaintenanceDevice | null;
loading?: boolean;
deviceCode?: string;
}>();
const { t } = useI18n();
const trimmedCode = computed(() => props.deviceCode?.trim() || '');
/**
* 根据加载态、匹配结果与是否已输入编号,计算快照卡片展示状态。
* @returns loading | matched | notFound | empty
*/
const displayState = computed(() => {
if (props.loading) return 'loading';
if (props.device) return 'matched';
if (trimmedCode.value) return 'notFound';
return 'empty';
});
/**
* 将设备状态枚举转为当前语言的展示文案。
* @param value - 设备状态枚举。
* @returns 本地化状态文案。
*/
const formatDeviceStatus = (value: string): string => {
const key = deviceStatusLabelKey(value);
return key ? t(key) : value || '-';
};
const infoCells = computed(() => {
if (!props.device) return [];
return [
{ label: t('maintenance.ledger.fields.modelSpec'), value: props.device.modelSpec, icon: BriefcaseBusiness },
{ label: t('maintenance.record.deviceSnapshot.position'), value: props.device.installLocation, icon: MapPin },
{ label: t('maintenance.record.deviceSnapshot.department'), value: props.device.department, icon: Building2 },
{ label: t('maintenance.record.deviceSnapshot.responsibleUser'), value: props.device.responsibleUser, icon: UserRound },
];
});
</script>
<template>
<div v-if="displayState === 'loading'" class="rounded-xl border border-sky-100 bg-gradient-to-r from-sky-50/90 to-white p-4 shadow-sm">
<div class="animate-pulse space-y-3">
<div class="flex items-center justify-between gap-3">
<div class="h-4 w-28 rounded bg-sky-100" />
<div class="h-6 w-14 rounded-full bg-sky-100" />
</div>
<div class="h-6 w-40 rounded bg-sky-100" />
<div class="h-4 w-28 rounded bg-sky-50" />
<div class="grid gap-2 sm:grid-cols-2 lg:grid-cols-4">
<div v-for="index in 4" :key="index" class="h-14 rounded-lg bg-white/80 ring-1 ring-sky-100" />
</div>
</div>
<p class="mt-3 text-xs text-secondary">{{ t('maintenance.record.deviceSnapshot.loading') }}</p>
</div>
<div
v-else-if="displayState === 'matched' && device"
class="rounded-xl border border-sky-100 border-l-4 border-l-brand-500 bg-gradient-to-r from-sky-50/90 to-white p-4 shadow-sm"
>
<div class="flex flex-wrap items-center gap-2">
<span class="text-xs font-semibold text-sky-800">{{ t('maintenance.record.deviceSnapshot.title') }}</span>
<span class="inline-flex items-center gap-1 rounded-full border border-emerald-200 bg-emerald-50 px-2 py-0.5 text-xs font-semibold text-emerald-700">
<BadgeCheck class="h-3.5 w-3.5" />
{{ t('maintenance.record.deviceSnapshot.matched') }}
</span>
</div>
<div class="mt-3 flex flex-wrap items-start justify-between gap-3">
<div class="min-w-0">
<div class="text-lg font-semibold text-primary">{{ device.deviceName || '-' }}</div>
<div class="mt-1 font-mono text-sm text-secondary">{{ device.deviceCode }}</div>
</div>
<span
class="inline-flex shrink-0 items-center rounded-full border px-2.5 py-1 text-xs font-semibold"
:class="deviceStatusBadgeClass(device.deviceStatus)"
>
{{ formatDeviceStatus(device.deviceStatus) }}
</span>
</div>
<div class="mt-3 grid gap-2 sm:grid-cols-2 lg:grid-cols-4">
<div
v-for="cell in infoCells"
:key="cell.label"
class="rounded-lg bg-white/85 px-3 py-2 ring-1 ring-sky-100"
>
<div class="flex items-start gap-2">
<component :is="cell.icon" class="mt-0.5 h-4 w-4 shrink-0 text-brand-600" />
<div class="min-w-0">
<div class="text-[11px] text-secondary">{{ cell.label }}</div>
<div class="mt-0.5 truncate text-xs font-medium text-primary" :title="cell.value || '-'">
{{ cell.value || '-' }}
</div>
</div>
</div>
</div>
</div>
</div>
<div
v-else-if="displayState === 'notFound'"
class="rounded-xl border border-dashed border-amber-300 bg-amber-50/60 px-4 py-3 text-sm text-amber-900"
>
{{ t('maintenance.record.deviceSnapshot.notFound') }}
</div>
<div
v-else
class="rounded-xl border border-dashed border-slate-300 bg-slate-50 px-4 py-3 text-sm text-secondary"
>
{{ t('maintenance.record.deviceSnapshot.empty') }}
</div>
</template>
@@ -1,15 +1,16 @@
<script setup lang="ts">
<script setup lang="ts">
/**
* features/maintenance/components/MaintenanceRecordManagerPanel.vue
* 用途:设备维保助手右侧固定面板中的维修记录管理页,覆盖查询、新增、编辑和删除。
* 依赖:maintenanceRecord.api、maintenanceLedger.api、PaginationBar、useMessage。
* 依赖:maintenanceRecord.api、maintenanceLedger.api、MaintenanceDeviceSnapshotCard、repair-status-display.utils、PaginationBar、useMessage。
* 逻辑:列表 Tab 按设备编号/名称和维修状态筛选;配置 Tab 复用同一表单新增或修改记录。
*/
import { computed, onMounted, reactive, ref } from 'vue';
import { computed, onMounted, reactive, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import PaginationBar from '@/shared/components/PaginationBar.vue';
import { useMessage } from '@/shared/composables/useMessage';
import MaintenanceDeviceSnapshotCard from '@/features/maintenance/components/MaintenanceDeviceSnapshotCard.vue';
import { listMaintenanceDevices } from '@/features/maintenance/api/maintenanceLedger.api';
import {
createMaintenanceRecord,
@@ -24,12 +25,29 @@ import type { MaintenanceDevice } from '@/features/maintenance/types/ledger.type
import type {
MaintenanceRecord,
MaintenanceRecordImportResult,
MaintenanceRecordListStatus,
MaintenanceRecordPayload,
MaintenanceRepairStatus,
} from '@/features/maintenance/types/record.types';
import { repairStatusBadgeClass } from '@/features/maintenance/utils/repair-status-display.utils';
import {
DEFAULT_MAINTENANCE_RECORD_TIME_RANGE,
resolveMaintenanceRecordReportDateRange,
type MaintenanceRecordTimeRange,
} from '@/features/maintenance/utils/record-list-filter.utils';
type TabName = 'list' | 'form';
interface MaintenanceRecordPanelCommand {
mode: 'list' | 'create';
device?: MaintenanceDevice | null;
nonce?: number;
}
const props = defineProps<{
editRecord?: MaintenanceRecord | null;
command?: MaintenanceRecordPanelCommand | null;
}>();
const msg = useMessage();
const { t } = useI18n();
const activeTab = ref<TabName>('list');
@@ -46,7 +64,8 @@ const total = ref(0);
const page = ref(1);
const pageSize = ref(10);
const keyword = ref('');
const status = ref('');
const status = ref<MaintenanceRecordListStatus>('active');
const timeRange = ref<MaintenanceRecordTimeRange>(DEFAULT_MAINTENANCE_RECORD_TIME_RANGE);
const editingId = ref<number | null>(null);
const importResult = ref<MaintenanceRecordImportResult | null>(null);
const deleteDialogOpen = ref(false);
@@ -71,14 +90,25 @@ const form = reactive<MaintenanceRecordPayload>({
sourceOrderNo: '',
});
const statusOptions: Array<{ value: MaintenanceRepairStatus | ''; label: string }> = [
const statusOptions: Array<{ value: MaintenanceRecordListStatus; label: string }> = [
{ value: '', label: 'maintenance.record.status.all' },
{ value: 'active', label: 'maintenance.record.status.active' },
{ value: 'pending', label: 'maintenance.record.status.pending' },
{ value: 'processing', label: 'maintenance.record.status.processing' },
{ value: 'done', label: 'maintenance.record.status.done' },
{ value: 'cancelled', label: 'maintenance.record.status.cancelled' },
];
const timeRangeOptions: Array<{ value: MaintenanceRecordTimeRange; label: string }> = [
{ value: '3m', label: 'maintenance.record.filters.timeRange3m' },
{ value: '1y', label: 'maintenance.record.filters.timeRange1y' },
{ value: 'all', label: 'maintenance.record.filters.timeRangeAll' },
];
const isDefaultScopedFilter = computed(
() => status.value === 'active' && timeRange.value === '1y' && !keyword.value.trim(),
);
const formTitle = computed(() => t(editingId.value ? 'maintenance.record.formTitle.edit' : 'maintenance.record.formTitle.create'));
const deleteConfirmMessage = computed(() =>
pendingDeleteRecord.value
@@ -118,17 +148,24 @@ const buildPayload = (): MaintenanceRecordPayload => ({
});
/**
* 查询维修记录列表,仅按设备编号/设备名称和状态筛选。
* 组装当前列表筛选参数,供查询与导出共用。
* @returns 维修记录列表 API 入参。
*/
const buildListQueryParams = () => ({
keyword: keyword.value.trim(),
status: status.value,
...resolveMaintenanceRecordReportDateRange(timeRange.value),
page: page.value,
pageSize: pageSize.value,
});
/**
* 查询维修记录列表,默认聚焦近一年内未完成记录。
*/
const loadRecords = async (): Promise<void> => {
loading.value = true;
try {
const result = await listMaintenanceRecords({
keyword: keyword.value.trim(),
status: status.value,
page: page.value,
pageSize: pageSize.value,
});
const result = await listMaintenanceRecords(buildListQueryParams());
records.value = result.items;
total.value = result.total;
} catch (error: any) {
@@ -147,6 +184,7 @@ const exportRecords = async (): Promise<void> => {
const blob = await exportMaintenanceRecords({
keyword: keyword.value.trim(),
status: status.value,
...resolveMaintenanceRecordReportDateRange(timeRange.value),
});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
@@ -237,7 +275,7 @@ const loadDeviceSnapshot = async (): Promise<void> => {
deviceLoading.value = true;
try {
const result = await listMaintenanceDevices({ keyword: code, page: 1, pageSize: 5 });
matchedDevice.value = result.items.find((item) => item.deviceCode === code) || result.items[0] || null;
matchedDevice.value = result.items.find((item) => item.deviceCode === code) || null;
} catch (error: any) {
msg.error(error?.message || t('maintenance.record.messages.deviceLoadFailed'));
} finally {
@@ -245,6 +283,31 @@ const loadDeviceSnapshot = async (): Promise<void> => {
}
};
/**
* 保存维修记录前精确校验设备编号,避免把记录挂到不存在的设备上。
* @returns 台账中是否存在当前设备编号。
*/
const ensureDeviceExists = async (): Promise<boolean> => {
const code = form.deviceCode.trim();
if (!code) return false;
if (matchedDevice.value?.deviceCode === code) return true;
deviceLoading.value = true;
try {
const result = await listMaintenanceDevices({ keyword: code, page: 1, pageSize: 5 });
matchedDevice.value = result.items.find((item) => item.deviceCode === code) || null;
if (!matchedDevice.value) {
msg.warning(t('maintenance.record.messages.deviceNotFound'));
return false;
}
return true;
} catch (error: any) {
msg.error(error?.message || t('maintenance.record.messages.deviceLoadFailed'));
return false;
} finally {
deviceLoading.value = false;
}
};
/**
* 重置表单并进入新增模式。
*/
@@ -270,6 +333,49 @@ const startCreate = (): void => {
activeTab.value = 'form';
};
/**
* 从会话结果里的设备卡片进入新增时,预填设备编号并复用设备快照,减少管理员重复录入。
* @param device - 当前会话结果命中的设备。
*/
const startCreateForDevice = (device: MaintenanceDevice): void => {
startCreate();
form.deviceCode = device.deviceCode || '';
matchedDevice.value = device;
};
/**
* 从会话结果里的设备卡片进入历史时,按设备编号筛选维修记录列表。
* @param device - 当前会话结果命中的设备。
*/
const showHistoryForDevice = async (device: MaintenanceDevice): Promise<void> => {
keyword.value = device.deviceCode || '';
status.value = '';
timeRange.value = 'all';
page.value = 1;
activeTab.value = 'list';
await loadRecords();
};
/**
* 切换报修日期快捷范围并刷新列表。
* @param range - 近 3 个月 / 近 1 年 / 全部时间。
*/
const applyTimeRange = async (range: MaintenanceRecordTimeRange): Promise<void> => {
timeRange.value = range;
page.value = 1;
await loadRecords();
};
/**
* 一键查看全部历史记录,取消默认的未完成与时间限制。
*/
const viewAllHistory = async (): Promise<void> => {
status.value = '';
timeRange.value = 'all';
page.value = 1;
await loadRecords();
};
/**
* 将选中的维修记录带入表单并进入编辑模式。
* @param record - 列表中选中的维修记录。
@@ -314,6 +420,9 @@ const saveRecord = async (): Promise<void> => {
msg.warning(t('maintenance.record.messages.required'));
return;
}
if (!await ensureDeviceExists()) {
return;
}
saving.value = true;
try {
if (editingId.value) {
@@ -366,6 +475,27 @@ const cancelDeleteDialog = (): void => {
};
onMounted(loadRecords);
watch(
() => props.editRecord,
(record) => {
if (record) startEdit(record);
},
{ immediate: true },
);
watch(
() => props.command,
async (command) => {
if (!command?.device) return;
if (command.mode === 'create') {
startCreateForDevice(command.device);
return;
}
await showHistoryForDevice(command.device);
},
{ immediate: true },
);
</script>
<template>
@@ -396,6 +526,10 @@ onMounted(loadRecords);
</div>
<div v-if="activeTab === 'list'" class="flex min-h-0 flex-1 flex-col gap-4">
<div class="rounded-xl border border-brand-100 bg-brand-50/70 px-3 py-2 text-sm text-brand-900">
{{ t('maintenance.record.filters.defaultHint') }}
</div>
<div class="flex flex-wrap gap-3">
<input
v-model="keyword"
@@ -410,6 +544,20 @@ onMounted(loadRecords);
</select>
</div>
<div class="flex flex-wrap items-center gap-2">
<span class="text-sm text-secondary">{{ t('maintenance.record.filters.timeRangeLabel') }}</span>
<button
v-for="item in timeRangeOptions"
:key="item.value"
type="button"
class="admin-report-page-btn"
:class="{ 'admin-report-page-btn--active': timeRange === item.value }"
@click="applyTimeRange(item.value)"
>
{{ t(item.label) }}
</button>
</div>
<div class="flex flex-wrap gap-2">
<button type="button" class="admin-report-page-btn admin-report-page-btn--active" @click="page = 1; loadRecords()">
{{ t('maintenance.record.actions.search') }}
@@ -439,34 +587,75 @@ onMounted(loadRecords);
</div>
<div class="min-h-0 overflow-auto rounded-xl border border-default bg-surface">
<table class="w-full table-fixed text-left text-sm">
<thead class="bg-muted/50 text-secondary">
<table class="w-full min-w-[1040px] table-fixed border-separate border-spacing-0 text-left text-sm">
<colgroup>
<col style="width: 140px" />
<col style="width: 150px" />
<col />
<col style="width: 120px" />
<col style="width: 120px" />
<col style="width: 110px" />
<col style="width: 110px" />
<col style="width: 150px" />
</colgroup>
<thead class="bg-surface-soft text-secondary">
<tr>
<th class="w-[24%] px-3 py-2">{{ t('maintenance.record.table.deviceCode') }}</th>
<th class="w-[22%] px-3 py-2">{{ t('maintenance.record.table.deviceName') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.table.deviceCode') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.table.deviceName') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.table.faultDescription') }}</th>
<th class="w-[14%] px-3 py-2">{{ t('maintenance.record.table.status') }}</th>
<th class="w-[18%] px-3 py-2 text-right">{{ t('maintenance.record.table.operation') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.fields.repairDate') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.fields.repairer') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.fields.repairCost') }}</th>
<th class="px-3 py-2">{{ t('maintenance.record.table.status') }}</th>
<th class="admin-table-sticky-right-head px-3 py-2 text-center">
{{ t('maintenance.record.table.operation') }}
</th>
</tr>
</thead>
<tbody>
<tr v-if="loading">
<td colspan="5" class="px-3 py-8 text-center text-secondary">{{ t('maintenance.record.table.loading') }}</td>
<td colspan="8" class="px-3 py-8 text-center text-secondary">{{ t('maintenance.record.table.loading') }}</td>
</tr>
<tr v-else-if="!records.length">
<td colspan="5" class="px-3 py-8 text-center text-secondary">{{ t('maintenance.record.table.empty') }}</td>
<td colspan="8" class="px-3 py-8 text-center text-secondary">
<div>{{ t(isDefaultScopedFilter ? 'maintenance.record.table.emptyActive' : 'maintenance.record.table.empty') }}</div>
<button
v-if="isDefaultScopedFilter"
type="button"
class="admin-report-page-btn admin-report-page-btn--active mt-3"
@click="viewAllHistory"
>
{{ t('maintenance.record.actions.viewAllHistory') }}
</button>
</td>
</tr>
<tr v-for="record in records" v-else :key="record.id" class="border-t border-default">
<td class="truncate px-3 py-2 font-semibold" :title="record.deviceCode">{{ record.deviceCode }}</td>
<td class="truncate px-3 py-2" :title="record.deviceName || '-'">{{ record.deviceName || '-' }}</td>
<td class="truncate px-3 py-2" :title="record.faultDescription">{{ record.faultDescription }}</td>
<td class="px-3 py-2">{{ formatStatus(record.repairStatus) }}</td>
<td class="px-3 py-2">
<div class="flex justify-end gap-2">
<button type="button" class="admin-report-page-btn" @click="startEdit(record)">{{ t('maintenance.record.actions.edit') }}</button>
<tr v-for="record in records" v-else :key="record.id">
<td class="border-t border-default px-3 py-2 font-semibold" :title="record.deviceCode">{{ record.deviceCode }}</td>
<td class="truncate border-t border-default px-3 py-2" :title="record.deviceName || '-'">{{ record.deviceName || '-' }}</td>
<td class="truncate border-t border-default px-3 py-2" :title="record.faultDescription">{{ record.faultDescription }}</td>
<td class="whitespace-nowrap border-t border-default px-3 py-2">{{ record.repairDate || '-' }}</td>
<td class="truncate border-t border-default px-3 py-2" :title="record.repairer || '-'">{{ record.repairer || '-' }}</td>
<td class="truncate border-t border-default px-3 py-2" :title="record.repairCost || '-'">{{ record.repairCost || '-' }}</td>
<td class="border-t border-default px-3 py-2">
<span
class="inline-flex min-w-14 items-center justify-center rounded-full border px-2.5 py-1 text-xs font-semibold"
:class="repairStatusBadgeClass(record.repairStatus)"
>
{{ formatStatus(record.repairStatus) }}
</span>
</td>
<td class="admin-table-sticky-right-cell border-t border-default px-3 py-2">
<div class="relative z-[1] flex justify-center gap-2">
<button
type="button"
class="admin-report-page-btn"
class="admin-report-page-btn admin-report-page-btn--edit"
@click="startEdit(record)"
>
{{ t('maintenance.record.actions.edit') }}
</button>
<button
type="button"
class="admin-report-page-btn admin-report-page-btn--danger"
:disabled="deletingId === record.id"
@click="openDeleteDialog(record)"
>
@@ -509,6 +698,17 @@ onMounted(loadRecords);
{{ t('maintenance.record.fields.reportDate') }} *
<input v-model="form.reportDate" type="date" class="admin-table-filter-input" />
</label>
</div>
<div class="mt-3">
<MaintenanceDeviceSnapshotCard
:device="matchedDevice"
:loading="deviceLoading"
:device-code="form.deviceCode"
/>
</div>
<div class="mt-4 grid gap-4 md:grid-cols-2">
<label class="grid gap-1 text-sm text-secondary">
{{ t('maintenance.record.fields.repairCategory') }}
<input v-model="form.repairCategory" class="admin-table-filter-input" :placeholder="t('maintenance.record.placeholder.repairCategory')" />
@@ -524,16 +724,6 @@ onMounted(loadRecords);
</label>
</div>
<div class="mt-3 rounded-xl border border-default bg-surface-soft px-3 py-2 text-sm">
<div v-if="deviceLoading" class="text-secondary">{{ t('maintenance.record.deviceSnapshot.loading') }}</div>
<div v-else-if="matchedDevice" class="grid gap-1 text-secondary">
<div class="font-semibold text-primary">{{ matchedDevice.deviceName }}({{ matchedDevice.deviceCode }})</div>
<div>{{ t('maintenance.record.deviceSnapshot.position') }}:{{ matchedDevice.installLocation || '-' }} | {{ t('maintenance.record.deviceSnapshot.department') }}:{{ matchedDevice.department || '-' }}</div>
<div>{{ t('maintenance.record.deviceSnapshot.responsibleUser') }}:{{ matchedDevice.responsibleUser || '-' }} | {{ t('maintenance.record.deviceSnapshot.status') }}:{{ matchedDevice.deviceStatus }}</div>
</div>
<div v-else class="text-secondary">{{ t('maintenance.record.deviceSnapshot.empty') }}</div>
</div>
<div class="mt-4 grid gap-4 md:grid-cols-2">
<label class="grid gap-1 text-sm text-secondary md:col-span-2">
{{ t('maintenance.record.fields.faultDescription') }} *
@@ -2,11 +2,12 @@
/**
* features/maintenance/components/MaintenanceRecordPanel.vue
* 用途:设备维保助手右侧固定面板中的“新增维修记录”页面。
* 依赖:maintenanceRecord.api、maintenanceLedger.api、useMessage。
* 依赖:maintenanceRecord.api、maintenanceLedger.api、MaintenanceDeviceSnapshotCard、useMessage。
* 逻辑:管理员通过按钮进入固定面板后填写维修记录;设备编号失焦时查询台账快照,保存后刷新最近记录用于确认。
*/
import { computed, onMounted, reactive, ref } from 'vue';
import { useMessage } from '@/shared/composables/useMessage';
import MaintenanceDeviceSnapshotCard from '@/features/maintenance/components/MaintenanceDeviceSnapshotCard.vue';
import { listMaintenanceDevices } from '@/features/maintenance/api/maintenanceLedger.api';
import {
createMaintenanceRecord,
@@ -100,7 +101,7 @@ const loadDeviceSnapshot = async (): Promise<void> => {
deviceLoading.value = true;
try {
const result = await listMaintenanceDevices({ keyword: code, page: 1, pageSize: 5 });
matchedDevice.value = result.items.find((item) => item.deviceCode === code) || result.items[0] || null;
matchedDevice.value = result.items.find((item) => item.deviceCode === code) || null;
} catch (error: any) {
msg.error(error?.message || '查询设备台账失败');
} finally {
@@ -108,6 +109,30 @@ const loadDeviceSnapshot = async (): Promise<void> => {
}
};
/**
* 保存前精确校验设备编号必须存在于台账,避免产生无设备归属的维修记录。
*/
const ensureDeviceExists = async (): Promise<boolean> => {
const code = form.deviceCode.trim();
if (!code) return false;
if (matchedDevice.value?.deviceCode === code) return true;
deviceLoading.value = true;
try {
const result = await listMaintenanceDevices({ keyword: code, page: 1, pageSize: 5 });
matchedDevice.value = result.items.find((item) => item.deviceCode === code) || null;
if (!matchedDevice.value) {
msg.warning('设备不存在,不能新增维修记录');
return false;
}
return true;
} catch (error: any) {
msg.error(error?.message || '鏌ヨ璁惧鍙拌处澶辫触');
return false;
} finally {
deviceLoading.value = false;
}
};
/**
* 查询最近维修记录,作为保存后的确认列表和轻量筛选入口。
*/
@@ -160,6 +185,9 @@ const saveRecord = async (): Promise<void> => {
msg.warning('设备编号、报修日期和故障描述不能为空');
return;
}
if (!await ensureDeviceExists()) {
return;
}
saving.value = true;
try {
await createMaintenanceRecord(payload);
@@ -199,6 +227,17 @@ onMounted(loadRecentRecords);
报修日期 *
<input v-model="form.reportDate" type="date" class="admin-table-filter-input" />
</label>
</div>
<div class="mt-3">
<MaintenanceDeviceSnapshotCard
:device="matchedDevice"
:loading="deviceLoading"
:device-code="form.deviceCode"
/>
</div>
<div class="mt-4 grid gap-4 md:grid-cols-2">
<label class="grid gap-1 text-sm text-secondary">
维修类别
<input v-model="form.repairCategory" class="admin-table-filter-input" placeholder="日常维修 / 保养 / 点检" />
@@ -214,16 +253,6 @@ onMounted(loadRecentRecords);
</label>
</div>
<div class="mt-3 rounded-xl border border-default bg-surface-soft px-3 py-2 text-sm">
<div v-if="deviceLoading" class="text-secondary">正在查询设备台账...</div>
<div v-else-if="matchedDevice" class="grid gap-1 text-secondary">
<div class="font-semibold text-primary">{{ matchedDevice.deviceName }}({{ matchedDevice.deviceCode }})</div>
<div>位置:{{ matchedDevice.installLocation || '-' }} | 部门:{{ matchedDevice.department || '-' }}</div>
<div>负责人:{{ matchedDevice.responsibleUser || '-' }} | 状态:{{ matchedDevice.deviceStatus }}</div>
</div>
<div v-else class="text-secondary">输入设备编号后可自动匹配设备台账快照。</div>
</div>
<div class="mt-4 grid gap-4 md:grid-cols-2">
<label class="grid gap-1 text-sm text-secondary md:col-span-2">
故障描述 *