484 lines
19 KiB
Vue
484 lines
19 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* features/maintenance/components/LedgerConfigPanel.vue
|
|
* 用途:设备维保助手右侧固定面板中的“台账配置”页面,包含设备列表与设备配置两个 Tab。
|
|
* 依赖:maintenanceLedger.api、PaginationBar、useMessage。
|
|
* 逻辑:列表 Tab 负责查询/导入/选择设备;配置 Tab 负责新增或编辑设备台账,保存后刷新列表。
|
|
*/
|
|
import { computed, onMounted, reactive, ref } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
import PaginationBar from '@/shared/components/PaginationBar.vue';
|
|
import { useMessage } from '@/shared/composables/useMessage';
|
|
import {
|
|
createMaintenanceDevice,
|
|
downloadMaintenanceLedgerImportTemplate,
|
|
importMaintenanceDevices,
|
|
listMaintenanceDevices,
|
|
updateMaintenanceDevice,
|
|
} from '@/features/maintenance/api/maintenanceLedger.api';
|
|
import type {
|
|
MaintenanceDevice,
|
|
MaintenanceDeviceImportResult,
|
|
MaintenanceDevicePayload,
|
|
MaintenanceDeviceStatus,
|
|
} from '@/features/maintenance/types/ledger.types';
|
|
|
|
type TabName = 'list' | 'form';
|
|
|
|
const msg = useMessage();
|
|
const { t } = useI18n();
|
|
const activeTab = ref<TabName>('list');
|
|
const loading = ref(false);
|
|
const saving = ref(false);
|
|
const importing = ref(false);
|
|
const downloadingTemplate = ref(false);
|
|
const devices = ref<MaintenanceDevice[]>([]);
|
|
const total = ref(0);
|
|
const page = ref(1);
|
|
const pageSize = ref(10);
|
|
const keyword = ref('');
|
|
const status = ref('');
|
|
const editingId = ref<number | null>(null);
|
|
const importResult = ref<MaintenanceDeviceImportResult | null>(null);
|
|
const MAX_IMPORT_FILE_SIZE = 10 * 1024 * 1024;
|
|
const DEFAULT_ACCOUNT_SET = '和信';
|
|
const accountSetOptions = ['和信', '科技', '电子', '和宏智'];
|
|
|
|
const form = reactive<MaintenanceDevicePayload>({
|
|
deviceCode: '',
|
|
deviceName: '',
|
|
modelSpec: '',
|
|
department: '',
|
|
installLocation: '',
|
|
deviceStatus: 'in_use',
|
|
manufacturer: '',
|
|
entryDate: '',
|
|
productionDate: '',
|
|
startDate: '',
|
|
responsibleUser: '',
|
|
maintenanceCycle: '',
|
|
originalValue: '',
|
|
residualValue: '',
|
|
accountSet: DEFAULT_ACCOUNT_SET,
|
|
remark: '',
|
|
});
|
|
|
|
const statusOptions: Array<{ value: MaintenanceDeviceStatus | ''; label: string }> = [
|
|
{ value: '', label: 'maintenance.ledger.status.all' },
|
|
{ value: 'in_use', label: 'maintenance.ledger.status.inUse' },
|
|
{ value: 'disabled', label: 'maintenance.ledger.status.disabled' },
|
|
{ value: 'scrapped', label: 'maintenance.ledger.status.scrapped' },
|
|
{ value: 'sold', label: 'maintenance.ledger.status.sold' },
|
|
];
|
|
|
|
const formTitle = computed(() => t(editingId.value ? 'maintenance.ledger.formTitle.edit' : 'maintenance.ledger.formTitle.create'));
|
|
|
|
/**
|
|
* 将后端状态值转换为中文展示。
|
|
* @param value - 设备状态值。
|
|
* @returns 中文状态。
|
|
*/
|
|
const formatStatus = (value: string): string => {
|
|
const item = statusOptions.find((option) => option.value === value);
|
|
return item?.label ? t(item.label) : value || '-';
|
|
};
|
|
|
|
/**
|
|
* 从表单状态生成 API payload,集中裁剪字符串。
|
|
* @returns 设备保存入参。
|
|
*/
|
|
const buildPayload = (): MaintenanceDevicePayload => ({
|
|
deviceCode: form.deviceCode.trim(),
|
|
deviceName: form.deviceName.trim(),
|
|
modelSpec: form.modelSpec?.trim(),
|
|
department: form.department?.trim(),
|
|
installLocation: form.installLocation?.trim(),
|
|
deviceStatus: form.deviceStatus,
|
|
manufacturer: form.manufacturer?.trim(),
|
|
entryDate: form.entryDate || undefined,
|
|
productionDate: form.productionDate || undefined,
|
|
startDate: form.startDate || undefined,
|
|
responsibleUser: form.responsibleUser?.trim(),
|
|
maintenanceCycle: form.maintenanceCycle?.trim(),
|
|
originalValue: form.originalValue?.trim(),
|
|
residualValue: form.residualValue?.trim(),
|
|
accountSet: form.accountSet?.trim(),
|
|
remark: form.remark?.trim(),
|
|
});
|
|
|
|
/**
|
|
* 加载设备台账分页列表。
|
|
* 核心逻辑:根据当前 keyword/status/page/pageSize 调用后端,刷新列表 Tab。
|
|
*/
|
|
const loadDevices = async (): Promise<void> => {
|
|
loading.value = true;
|
|
try {
|
|
const result = await listMaintenanceDevices({
|
|
keyword: keyword.value.trim(),
|
|
status: status.value,
|
|
page: page.value,
|
|
pageSize: pageSize.value,
|
|
});
|
|
devices.value = result.items;
|
|
total.value = result.total;
|
|
} catch (error: any) {
|
|
msg.error(error?.message || t('maintenance.ledger.messages.loadFailed'));
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 重置表单并切换到新增模式。
|
|
*/
|
|
const startCreate = (): void => {
|
|
editingId.value = null;
|
|
Object.assign(form, {
|
|
deviceCode: '',
|
|
deviceName: '',
|
|
modelSpec: '',
|
|
department: '',
|
|
installLocation: '',
|
|
deviceStatus: 'in_use',
|
|
manufacturer: '',
|
|
entryDate: '',
|
|
productionDate: '',
|
|
startDate: '',
|
|
responsibleUser: '',
|
|
maintenanceCycle: '',
|
|
originalValue: '',
|
|
residualValue: '',
|
|
accountSet: DEFAULT_ACCOUNT_SET,
|
|
remark: '',
|
|
});
|
|
activeTab.value = 'form';
|
|
};
|
|
|
|
/**
|
|
* 将选中设备带入表单并切换到配置 Tab。
|
|
* @param device - 列表中选中的设备。
|
|
*/
|
|
const startEdit = (device: MaintenanceDevice): void => {
|
|
editingId.value = device.id;
|
|
Object.assign(form, {
|
|
deviceCode: device.deviceCode || '',
|
|
deviceName: device.deviceName || '',
|
|
modelSpec: device.modelSpec || '',
|
|
department: device.department || '',
|
|
installLocation: device.installLocation || '',
|
|
deviceStatus: device.deviceStatus,
|
|
manufacturer: device.manufacturer || '',
|
|
entryDate: device.entryDate || '',
|
|
productionDate: device.productionDate || '',
|
|
startDate: device.startDate || '',
|
|
responsibleUser: device.responsibleUser || '',
|
|
maintenanceCycle: device.maintenanceCycle || '',
|
|
originalValue: device.originalValue != null ? String(device.originalValue) : '',
|
|
residualValue: device.residualValue != null ? String(device.residualValue) : '',
|
|
accountSet: device.accountSet || '',
|
|
remark: device.remark || '',
|
|
});
|
|
activeTab.value = 'form';
|
|
};
|
|
|
|
/**
|
|
* 保存设备台账配置。
|
|
* 核心逻辑:新增/编辑共用同一表单;保存成功后刷新列表并回到设备列表 Tab。
|
|
*/
|
|
const saveDevice = async (): Promise<void> => {
|
|
const payload = buildPayload();
|
|
if (!payload.deviceCode || !payload.deviceName) {
|
|
msg.warning(t('maintenance.ledger.messages.required'));
|
|
return;
|
|
}
|
|
saving.value = true;
|
|
try {
|
|
if (editingId.value) {
|
|
await updateMaintenanceDevice(editingId.value, payload);
|
|
} else {
|
|
await createMaintenanceDevice(payload);
|
|
}
|
|
msg.success(t('maintenance.ledger.messages.saved'));
|
|
activeTab.value = 'list';
|
|
await loadDevices();
|
|
} catch (error: any) {
|
|
msg.error(error?.message || t('maintenance.ledger.messages.saveFailed'));
|
|
} finally {
|
|
saving.value = false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 校验设备台账导入文件,避免明显不符合要求的文件进入后端解析。
|
|
* @param file - 用户选择的 Excel 文件。
|
|
* @returns 校验是否通过。
|
|
*/
|
|
const validateImportFile = (file: File): boolean => {
|
|
const fileName = file.name.toLowerCase();
|
|
if (!fileName.endsWith('.xlsx') && !fileName.endsWith('.xls')) {
|
|
msg.warning(t('maintenance.ledger.messages.invalidFormat'));
|
|
return false;
|
|
}
|
|
if (file.size > MAX_IMPORT_FILE_SIZE) {
|
|
msg.warning(t('maintenance.ledger.messages.fileTooLarge'));
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* 下载设备台账导入模板。
|
|
* 核心逻辑:通过 fetch 携带认证头获取 Blob,避免浏览器新开页面时因缺少鉴权跳转到错误 JSON。
|
|
*/
|
|
const downloadTemplate = async (): Promise<void> => {
|
|
downloadingTemplate.value = true;
|
|
try {
|
|
const blob = await downloadMaintenanceLedgerImportTemplate();
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = 'maintenance_devices_import_template.xlsx';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(url);
|
|
} catch (error: any) {
|
|
msg.error(error?.message || t('maintenance.ledger.messages.downloadTemplateFailed'));
|
|
} finally {
|
|
downloadingTemplate.value = false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 上传 Excel 模板并导入设备台账。
|
|
* @param event - 文件选择事件。
|
|
*/
|
|
const onImportFileChange = async (event: Event): Promise<void> => {
|
|
const input = event.target as HTMLInputElement;
|
|
const file = input.files?.[0];
|
|
input.value = '';
|
|
if (!file) return;
|
|
if (!validateImportFile(file)) return;
|
|
importing.value = true;
|
|
try {
|
|
importResult.value = await importMaintenanceDevices(file);
|
|
msg.success(t('maintenance.ledger.messages.importDone'));
|
|
await loadDevices();
|
|
} catch (error: any) {
|
|
msg.error(error?.message || t('maintenance.ledger.messages.importFailed'));
|
|
} finally {
|
|
importing.value = false;
|
|
}
|
|
};
|
|
|
|
onMounted(loadDevices);
|
|
</script>
|
|
|
|
<template>
|
|
<section class="flex h-full min-h-0 flex-col">
|
|
<div class="mb-4 rounded-xl border border-sky-200 bg-sky-50 px-3 py-2 text-sm text-sky-800">
|
|
{{ t('maintenance.ledger.notice') }}
|
|
</div>
|
|
|
|
<div class="mb-4 flex rounded-xl border border-default bg-muted/30 p-1">
|
|
<button
|
|
type="button"
|
|
class="relative flex-1 cursor-pointer rounded-lg border px-3 py-2 text-sm font-semibold transition-all hover:bg-surface hover:text-brand"
|
|
:class="activeTab === 'list' ? 'border-brand-300 bg-surface text-brand shadow-sm after:absolute after:inset-x-6 after:bottom-0 after:h-0.5 after:rounded-full after:bg-brand' : 'border-transparent text-secondary'"
|
|
:aria-selected="activeTab === 'list'"
|
|
@click="activeTab = 'list'"
|
|
>
|
|
{{ t('maintenance.ledger.tabs.list') }}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="relative flex-1 cursor-pointer rounded-lg border px-3 py-2 text-sm font-semibold transition-all hover:bg-surface hover:text-brand"
|
|
:class="activeTab === 'form' ? 'border-brand-300 bg-surface text-brand shadow-sm after:absolute after:inset-x-6 after:bottom-0 after:h-0.5 after:rounded-full after:bg-brand' : 'border-transparent text-secondary'"
|
|
:aria-selected="activeTab === 'form'"
|
|
@click="activeTab = 'form'"
|
|
>
|
|
{{ t('maintenance.ledger.tabs.form') }}
|
|
</button>
|
|
</div>
|
|
|
|
<div v-if="activeTab === 'list'" class="flex min-h-0 flex-1 flex-col gap-4">
|
|
<div class="flex flex-wrap gap-3">
|
|
<input
|
|
v-model="keyword"
|
|
class="admin-table-filter-input w-full md:w-[360px] lg:w-[420px]"
|
|
:placeholder="t('maintenance.ledger.filters.keywordPlaceholder')"
|
|
@keyup.enter="page = 1; loadDevices()"
|
|
/>
|
|
<select v-model="status" class="admin-table-filter-input w-full md:w-[150px]" @change="page = 1; loadDevices()">
|
|
<option v-for="item in statusOptions" :key="item.value || 'all'" :value="item.value">
|
|
{{ t(item.label) }}
|
|
</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="flex flex-wrap gap-2">
|
|
<button type="button" class="admin-report-page-btn admin-report-page-btn--active" @click="page = 1; loadDevices()">
|
|
{{ t('maintenance.ledger.actions.search') }}
|
|
</button>
|
|
<button type="button" class="admin-report-page-btn" @click="startCreate">
|
|
{{ t('maintenance.ledger.actions.create') }}
|
|
</button>
|
|
<button type="button" class="admin-report-page-btn" :disabled="downloadingTemplate" @click="downloadTemplate">
|
|
{{ downloadingTemplate ? t('maintenance.ledger.actions.downloadingTemplate') : t('maintenance.ledger.actions.downloadTemplate') }}
|
|
</button>
|
|
<label class="admin-report-page-btn cursor-pointer">
|
|
{{ importing ? t('maintenance.ledger.actions.importing') : t('maintenance.ledger.actions.importExcel') }}
|
|
<input type="file" class="hidden" accept=".xls,.xlsx" :disabled="importing" @change="onImportFileChange" />
|
|
</label>
|
|
</div>
|
|
|
|
<div v-if="importResult" class="rounded-xl border border-default bg-surface px-3 py-2 text-sm">
|
|
{{ t('maintenance.ledger.importResult', importResult) }}
|
|
<div v-if="importResult.errors.length" class="mt-2 text-danger">
|
|
<div v-for="err in importResult.errors.slice(0, 5)" :key="`${err.row}-${err.message}`">
|
|
{{ t('maintenance.ledger.importErrorRow', err) }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="min-h-0 overflow-auto rounded-xl border border-default bg-surface">
|
|
<table class="min-w-full text-left text-sm">
|
|
<thead class="bg-muted/50 text-secondary">
|
|
<tr>
|
|
<th class="px-3 py-2">{{ t('maintenance.ledger.table.deviceCode') }}</th>
|
|
<th class="px-3 py-2">{{ t('maintenance.ledger.table.deviceName') }}</th>
|
|
<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>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-if="loading">
|
|
<td colspan="6" class="px-3 py-8 text-center text-secondary">{{ t('maintenance.ledger.table.loading') }}</td>
|
|
</tr>
|
|
<tr v-else-if="!devices.length">
|
|
<td colspan="6" class="px-3 py-8 text-center text-secondary">{{ t('maintenance.ledger.table.empty') }}</td>
|
|
</tr>
|
|
<tr v-for="device in devices" v-else :key="device.id" class="border-t border-default">
|
|
<td class="px-3 py-2 font-semibold">{{ device.deviceCode }}</td>
|
|
<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>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<PaginationBar
|
|
v-model:page="page"
|
|
v-model:page-size="pageSize"
|
|
:total="total"
|
|
@update:page="loadDevices"
|
|
@update:page-size="loadDevices"
|
|
/>
|
|
</div>
|
|
|
|
<div v-else class="min-h-0 flex-1 overflow-auto">
|
|
<div class="mb-4 flex items-center justify-between">
|
|
<h3 class="text-base font-semibold text-primary">{{ formTitle }}</h3>
|
|
<button type="button" class="admin-report-page-btn" @click="activeTab = 'list'">{{ t('maintenance.ledger.actions.backToList') }}</button>
|
|
</div>
|
|
|
|
<div class="grid gap-4 md:grid-cols-2">
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.deviceCode') }} *
|
|
<input v-model="form.deviceCode" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.deviceName') }} *
|
|
<input v-model="form.deviceName" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.modelSpec') }}
|
|
<input v-model="form.modelSpec" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.manufacturer') }}
|
|
<input v-model="form.manufacturer" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.devicePosition') }}
|
|
<input v-model="form.installLocation" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.deviceStatus') }}
|
|
<select v-model="form.deviceStatus" class="admin-table-filter-input">
|
|
<option value="in_use">{{ t('maintenance.ledger.status.inUse') }}</option>
|
|
<option value="disabled">{{ t('maintenance.ledger.status.disabled') }}</option>
|
|
<option value="scrapped">{{ t('maintenance.ledger.status.scrapped') }}</option>
|
|
<option value="sold">{{ t('maintenance.ledger.status.sold') }}</option>
|
|
</select>
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.department') }}
|
|
<input v-model="form.department" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.responsibleUser') }}
|
|
<input v-model="form.responsibleUser" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.productionDate') }}
|
|
<input v-model="form.productionDate" type="date" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ 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" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.residualValue') }}
|
|
<input v-model="form.residualValue" type="number" min="0" step="0.01" class="admin-table-filter-input" />
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary">
|
|
{{ t('maintenance.ledger.fields.accountSet') }}
|
|
<select v-model="form.accountSet" class="admin-table-filter-input">
|
|
<option v-for="item in accountSetOptions" :key="item" :value="item">
|
|
{{ item }}
|
|
</option>
|
|
</select>
|
|
</label>
|
|
<label class="grid gap-1 text-sm text-secondary md:col-span-2">
|
|
{{ t('maintenance.ledger.fields.remark') }}
|
|
<textarea v-model="form.remark" class="admin-table-filter-input min-h-24" />
|
|
</label>
|
|
</div>
|
|
|
|
<div class="mt-5 flex justify-end gap-2">
|
|
<button type="button" class="admin-report-page-btn" @click="activeTab = 'list'">{{ t('maintenance.ledger.actions.cancel') }}</button>
|
|
<button
|
|
type="button"
|
|
class="admin-report-page-btn admin-report-page-btn--active"
|
|
:disabled="saving"
|
|
@click="saveDevice"
|
|
>
|
|
{{ saving ? t('maintenance.ledger.actions.saving') : t('maintenance.ledger.actions.save') }}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</template>
|