This commit is contained in:
wzt 2026-08-04 09:45:30 +08:00
parent b0493eca29
commit 97d043a9b7
3 changed files with 999 additions and 0 deletions

89
src/api/toolUsage.ts Normal file
View File

@ -0,0 +1,89 @@
import { request } from '@/utils/request'
export interface ToolUsage {
id?: number
toolId?: number
toolCode?: string
toolName?: string
deviceId?: number
deviceCode?: string
deviceName?: string
stationId?: number
stationCode?: string
stationName?: string
startTime?: string | number
endTime?: string | number
usageHours?: number
processCount?: number
handoverPerson?: string
receiverPerson?: string
createTime?: string
updateTime?: string
}
export const toolUsageApi = {
page(params: {
page: number
pageSize: number
toolId?: number
toolCode?: string
deviceId?: number
stationId?: number
startTime?: string
endTime?: string
}) {
return request({
url: '/biz/toolUsage/page',
method: 'get',
params
})
},
create(data: ToolUsage) {
return request({
url: '/biz/toolUsage',
method: 'post',
data
})
},
update(data: ToolUsage) {
return request({
url: '/biz/toolUsage',
method: 'put',
data
})
},
delete(ids: number[]) {
return request({
url: `/biz/toolUsage/${ids.join(',')}`,
method: 'delete'
})
},
export(params?: {
ids?: number[]
toolId?: number
toolCode?: string
deviceId?: number
stationId?: number
startTime?: string
endTime?: string
}) {
const p: Record<string, any> = {}
if (params?.ids?.length) p.ids = params.ids.join(',')
if (params?.toolId) p.toolId = params.toolId
if (params?.toolCode) p.toolCode = params.toolCode
if (params?.deviceId) p.deviceId = params.deviceId
if (params?.stationId) p.stationId = params.stationId
if (params?.startTime) p.startTime = params.startTime
if (params?.endTime) p.endTime = params.endTime
return request({
url: '/biz/toolUsage/report',
method: 'get',
params: p,
responseType: 'blob'
})
}
}

View File

@ -0,0 +1,442 @@
<template>
<div class="page-container">
<n-card>
<div class="search-form">
<n-form inline :model="searchForm" label-placement="left">
<n-form-item label="刀具编码">
<n-input v-model:value="searchForm.toolCode" placeholder="请输入刀具编码" clearable />
</n-form-item>
<n-form-item label="刀具名称">
<n-input v-model:value="searchForm.toolName" placeholder="请输入刀具名称" clearable />
</n-form-item>
<n-form-item label="刀具状态">
<n-select v-model:value="searchForm.status" placeholder="请选择刀具状态" clearable style="width: 150px" :options="statusOptions" />
</n-form-item>
<n-form-item>
<n-space>
<n-button type="primary" @click="handleSearch">
<template #icon><n-icon><SearchOutline /></n-icon></template>
搜索
</n-button>
<n-button @click="handleReset">
<template #icon><n-icon><RefreshOutline /></n-icon></template>
重置
</n-button>
</n-space>
</n-form-item>
</n-form>
</div>
<div class="table-toolbar">
<n-space>
<n-button type="primary" @click="handleAdd">
<template #icon><n-icon><AddOutline /></n-icon></template>
新增
</n-button>
<n-button @click="importModalVisible = true">
<template #icon><n-icon><CloudUploadOutline /></n-icon></template>
导入
</n-button>
<n-button @click="handleExport">
<template #icon><n-icon><DownloadOutline /></n-icon></template>
导出{{ selectedIds.length > 0 ? `(${selectedIds.length})` : '' }}
</n-button>
<n-button type="error" :disabled="selectedIds.length === 0" @click="handleBatchDelete">
<template #icon><n-icon><TrashOutline /></n-icon></template>
删除
</n-button>
</n-space>
</div>
<n-data-table
:columns="columns"
:data="tableData"
:loading="loading"
:pagination="pagination"
:row-key="(row) => row.id"
:scroll-x="1400"
@update:page="handlePageChange"
@update:page-size="handlePageSizeChange"
@update:checked-row-keys="handleCheck"
/>
</n-card>
<n-modal v-model:show="modalVisible" preset="card" :title="modalTitle" style="width: 700px">
<n-form ref="formRef" :model="formData" :rules="formRules" label-placement="left" label-width="110px">
<n-grid :cols="2" :x-gap="24">
<n-form-item-gi label="刀具编码" path="toolCode">
<n-input v-model:value="formData.toolCode" placeholder="请输入刀具编码" />
</n-form-item-gi>
<n-form-item-gi label="刀具名称" path="toolName">
<n-input v-model:value="formData.toolName" placeholder="请输入刀具名称" />
</n-form-item-gi>
<n-form-item-gi label="刀具类型" path="toolType">
<n-input v-model:value="formData.toolType" placeholder="请输入刀具类型" />
</n-form-item-gi>
<n-form-item-gi label="规格型号" path="specModel">
<n-input v-model:value="formData.specModel" placeholder="请输入规格型号" />
</n-form-item-gi>
<n-form-item-gi label="材质" path="material">
<n-input v-model:value="formData.material" placeholder="请输入材质" />
</n-form-item-gi>
<n-form-item-gi label="生产厂家" path="manufacturer">
<n-input v-model:value="formData.manufacturer" placeholder="请输入生产厂家" />
</n-form-item-gi>
<n-form-item-gi label="采购日期" path="purchaseDate">
<n-date-picker v-model:value="formData.purchaseDate" type="datetime" clearable style="width: 100%" />
</n-form-item-gi>
<n-form-item-gi label="生命周期" path="lifeCycle">
<n-input-number v-model:value="formData.lifeCycle" placeholder="请输入生命周期" :min="0" :show-button="false" style="width: 100%" />
</n-form-item-gi>
<n-form-item-gi label="刀具状态" path="status">
<n-select v-model:value="formData.status" placeholder="请选择刀具状态" style="width: 100%" :options="statusOptions" />
</n-form-item-gi>
<n-form-item-gi label="已使用次数" path="usedCount">
<n-input-number v-model:value="formData.usedCount" placeholder="已使用次数" :min="0" :show-button="false" style="width: 100%" />
</n-form-item-gi>
</n-grid>
<n-form-item label="备注" path="remark">
<n-input v-model:value="formData.remark" type="textarea" placeholder="请输入备注" :rows="3" />
</n-form-item>
</n-form>
<template #footer>
<n-space justify="end">
<n-button @click="modalVisible = false">取消</n-button>
<n-button type="primary" @click="handleSubmit">确定</n-button>
</n-space>
</template>
</n-modal>
<n-modal v-model:show="importModalVisible" preset="card" title="导入刀具表" style="width: 500px">
<n-space vertical>
<n-alert type="info">
<template #header>导入说明</template>
<ul style="margin: 0; padding-left: 16px; line-height: 1.8">
<li>请先下载导入模板按模板格式填写数据</li>
<li>支持 .xlsx .xls 格式</li>
</ul>
</n-alert>
<n-space>
<n-button type="primary" @click="handleDownloadTemplate">
<template #icon><n-icon><DownloadOutline /></n-icon></template>
下载模板
</n-button>
</n-space>
<n-upload :max="1" accept=".xlsx,.xls" :show-file-list="true" :custom-request="handleImportUpload">
<n-upload-dragger>
<div style="margin-bottom: 12px">
<n-icon size="48" :depth="3"><CloudUploadOutline /></n-icon>
</div>
<n-text style="font-size: 16px">点击或拖拽文件到此处上传</n-text>
<n-p depth="3" style="margin: 8px 0 0 0">支持 .xlsx .xls 格式</n-p>
</n-upload-dragger>
</n-upload>
</n-space>
<template #footer>
<n-button @click="importModalVisible = false">关闭</n-button>
</template>
</n-modal>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, h, onMounted } from 'vue'
import { NButton, NSpace, NIcon, NTag, NDataTable, NUpload, useMessage, useDialog, type DataTableColumns, type UploadCustomRequestOptions } from 'naive-ui'
import { SearchOutline, RefreshOutline, AddOutline, TrashOutline, CreateOutline, CloudUploadOutline, DownloadOutline } from '@vicons/ionicons5'
import { toolApi, type Tool } from '@/api/tool'
import { dictDataApi } from '@/api/org'
const message = useMessage()
const dialog = useDialog()
const searchForm = reactive({
toolCode: '',
toolName: '',
status: null as number | null
})
const tableData = ref<Tool[]>([])
const loading = ref(false)
const selectedIds = ref<number[]>([])
const pagination = reactive({
page: 1,
pageSize: 10,
itemCount: 0,
showSizePicker: true,
pageSizes: [10, 20, 50]
})
const modalVisible = ref(false)
const modalTitle = ref('')
const importModalVisible = ref(false)
const formRef = ref()
const defaultFormData: Tool = {
toolCode: '',
toolName: '',
toolType: '',
specModel: '',
material: '',
manufacturer: '',
purchaseDate: undefined,
lifeCycle: 0,
usedCount: 0,
status: 0,
remark: ''
}
const formData = reactive<Tool>({ ...defaultFormData })
const statusOptions = ref<{ label: string; value: any }[]>([])
const formRules = {
toolCode: [{ required: true, message: '请输入刀具编码', trigger: 'blur' }],
toolName: [{ required: true, message: '请输入刀具名称', trigger: 'blur' }],
lifeCycle: [{ required: true, message: '请输入生命周期', trigger: 'blur' }],
status: [{ required: true, message: '请选择刀具状态', trigger: 'change' }]
}
const columns: DataTableColumns<Tool> = [
{ type: 'selection', width: 48, fixed: 'left' },
{ title: '刀具编码', key: 'toolCode', width: 120, align: 'center', fixed: 'left' },
{ title: '刀具名称', key: 'toolName', width: 140, align: 'center' },
{ title: '刀具类型', key: 'toolType', width: 120, align: 'center' },
{ title: '规格型号', key: 'specModel', width: 140, align: 'center' },
{ title: '材质', key: 'material', width: 100, align: 'center' },
{ title: '生产厂家', key: 'manufacturer', width: 120, align: 'center' },
{ title: '采购日期', key: 'purchaseDate', width: 170, align: 'center' },
{ title: '生命周期', key: 'lifeCycle', width: 100, align: 'center' },
{ title: '已使用次数', key: 'usedCount', width: 100, align: 'center',
render(row) {
return row.usedCount ?? '-'
}
},
{ title: '刀具状态', key: 'status', width: 100, align: 'center',
render(row) {
const val = row.status
const opt = statusOptions.value.find(o => o.value === val || String(o.value) === String(val))
if (!opt) return val ?? '-'
return h(NTag, { type: opt.value === 1 ? 'success' : 'default', size: 'small' }, { default: () => opt.label })
}
},
{ title: '备注', key: 'remark', width: 150, ellipsis: { tooltip: true } },
{ title: '创建时间', key: 'createTime', width: 180, align: 'center' },
{
title: '操作',
key: 'actions',
width: 140,
fixed: 'right',
render(row) {
return h('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'nowrap' } }, [
h(NButton, { size: 'small', quaternary: true, onClick: () => handleEdit(row) }, {
default: () => [h(NIcon, null, { default: () => h(CreateOutline) }), ' 编辑']
}),
h(NButton, { size: 'small', quaternary: true, type: 'error', onClick: () => handleDelete(row) }, {
default: () => [h(NIcon, null, { default: () => h(TrashOutline) }), ' 删除']
})
])
}
}
]
async function loadData() {
loading.value = true
try {
const res = await toolApi.page({
page: pagination.page,
pageSize: pagination.pageSize,
toolCode: searchForm.toolCode || undefined,
toolName: searchForm.toolName || undefined,
status: searchForm.status || undefined
})
tableData.value = res.list
pagination.itemCount = res.total
} finally {
loading.value = false
}
}
function handleSearch() {
pagination.page = 1
loadData()
}
function handleReset() {
searchForm.toolCode = ''
searchForm.toolName = ''
searchForm.status = null
handleSearch()
}
function handlePageChange(page: number) {
pagination.page = page
loadData()
}
function handlePageSizeChange(pageSize: number) {
pagination.pageSize = pageSize
pagination.page = 1
loadData()
}
function handleCheck(keys: Array<string | number>) {
selectedIds.value = keys as number[]
}
function handleAdd() {
modalTitle.value = '新增刀具'
Object.assign(formData, defaultFormData)
modalVisible.value = true
}
function handleEdit(row: Tool) {
modalTitle.value = '编辑刀具'
Object.assign(formData, row)
if (typeof row.purchaseDate === 'string') {
formData.purchaseDate = new Date(row.purchaseDate.replace(' ', 'T')).getTime()
}
modalVisible.value = true
}
function formatDateTime(ts: number) {
const d = new Date(ts)
const pad = (n: number) => String(n).padStart(2, '0')
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}
async function handleSubmit() {
try {
await formRef.value?.validate()
} catch {
return
}
try {
const submitData = { ...formData } as Tool
if (typeof submitData.purchaseDate === 'number') {
submitData.purchaseDate = formatDateTime(submitData.purchaseDate)
}
if (submitData.id) {
await toolApi.update(submitData)
message.success('修改成功')
} else {
await toolApi.create(submitData)
message.success('新增成功')
}
modalVisible.value = false
loadData()
} catch (error) {
//
}
}
function handleDelete(row: Tool) {
dialog.warning({
title: '提示',
content: '确定要删除该记录吗?',
positiveText: '确定',
negativeText: '取消',
onPositiveClick: async () => {
try {
await toolApi.delete([row.id!])
message.success('删除成功')
loadData()
} catch (error) {
//
}
}
})
}
function handleBatchDelete() {
dialog.warning({
title: '提示',
content: `确定要删除选中的 ${selectedIds.value.length} 条记录吗?`,
positiveText: '确定',
negativeText: '取消',
onPositiveClick: async () => {
try {
await toolApi.delete(selectedIds.value)
message.success('删除成功')
selectedIds.value = []
loadData()
} catch (error) {
//
}
}
})
}
async function handleExport() {
try {
const params: Record<string, any> = {}
if (selectedIds.value.length > 0) params.ids = selectedIds.value
if (searchForm.toolCode) params.toolCode = searchForm.toolCode
if (searchForm.toolName) params.toolName = searchForm.toolName
if (searchForm.status != null) params.status = searchForm.status
const blob = await toolApi.export(params)
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = '刀具数据.xlsx'
link.click()
window.URL.revokeObjectURL(url)
} catch (error) {
//
}
}
async function handleDownloadTemplate() {
try {
const blob = await toolApi.export({})
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = '刀具导入模板.xlsx'
link.click()
window.URL.revokeObjectURL(url)
} catch (error) {
//
}
}
async function handleImportUpload({ file }: UploadCustomRequestOptions) {
if (!file.file) return
try {
const result = await toolApi.export({})
if (result.fail > 0) {
dialog.warning({
title: '导入结果',
content: `成功: ${result.success} 条,失败: ${result.fail}\n错误信息: ${(result.errors || []).join('\n') || '无'}`,
positiveText: '确定'
})
} else {
message.success(`导入成功,共 ${result.success} 条数据`)
importModalVisible.value = false
}
loadData()
} catch (error) {
//
}
}
async function loadDictOptions() {
try {
const data = await dictDataApi.listByType('sys_status')
statusOptions.value = data.map(d => ({ label: d.dictLabel, value: (Number(d.dictValue) || d.dictValue) }))
} catch {}
}
onMounted(() => {
loadData()
loadDictOptions()
})
</script>
<style scoped>
.search-form {
margin-bottom: 16px;
}
.table-toolbar {
margin-bottom: 16px;
}
</style>

View File

@ -0,0 +1,468 @@
<template>
<div class="page-container">
<n-card>
<div class="search-form">
<n-form inline :model="searchForm" label-placement="left">
<n-form-item label="刀具编码">
<n-input v-model:value="searchForm.toolCode" placeholder="请输入刀具编码" clearable />
</n-form-item>
<n-form-item label="设备编码">
<n-input v-model:value="searchForm.deviceCode" placeholder="请输入设备编码" clearable />
</n-form-item>
<n-form-item label="工位编码">
<n-input v-model:value="searchForm.stationCode" placeholder="请输入工位编码" clearable />
</n-form-item>
<n-form-item label="开始时间">
<n-date-picker v-model:value="searchForm.startTime" type="datetime" placeholder="开始时间" clearable style="width: 200px" />
</n-form-item>
<n-form-item label="结束时间">
<n-date-picker v-model:value="searchForm.endTime" type="datetime" placeholder="结束时间" clearable style="width: 200px" />
</n-form-item>
<n-form-item>
<n-space>
<n-button type="primary" @click="handleSearch">
<template #icon><n-icon><SearchOutline /></n-icon></template>
搜索
</n-button>
<n-button @click="handleReset">
<template #icon><n-icon><RefreshOutline /></n-icon></template>
重置
</n-button>
</n-space>
</n-form-item>
</n-form>
</div>
<div class="table-toolbar">
<n-space>
<n-button type="primary" @click="handleAdd">
<template #icon><n-icon><AddOutline /></n-icon></template>
新增
</n-button>
<n-button @click="importModalVisible = true">
<template #icon><n-icon><CloudUploadOutline /></n-icon></template>
导入
</n-button>
<n-button @click="handleExport">
<template #icon><n-icon><DownloadOutline /></n-icon></template>
导出{{ selectedIds.length > 0 ? `(${selectedIds.length})` : '' }}
</n-button>
<n-button type="error" :disabled="selectedIds.length === 0" @click="handleBatchDelete">
<template #icon><n-icon><TrashOutline /></n-icon></template>
删除
</n-button>
</n-space>
</div>
<n-data-table
:columns="columns"
:data="tableData"
:loading="loading"
:pagination="pagination"
:row-key="(row) => row.id"
:scroll-x="1600"
@update:page="handlePageChange"
@update:page-size="handlePageSizeChange"
@update:checked-row-keys="handleCheck"
/>
</n-card>
<n-modal v-model:show="modalVisible" preset="card" :title="modalTitle" style="width: 800px">
<n-form ref="formRef" :model="formData" :rules="formRules" label-placement="left" label-width="100px">
<n-grid :cols="2" :x-gap="24">
<n-form-item-gi label="刀具ID" path="toolId">
<n-input-number v-model:value="formData.toolId" placeholder="请输入刀具ID" :min="0" :show-button="false" style="width: 100%" />
</n-form-item-gi>
<n-form-item-gi label="刀具编码" path="toolCode">
<n-input v-model:value="formData.toolCode" placeholder="请输入刀具编码" />
</n-form-item-gi>
<n-form-item-gi label="刀具名称" path="toolName">
<n-input v-model:value="formData.toolName" placeholder="请输入刀具名称" />
</n-form-item-gi>
<n-form-item-gi label="设备ID" path="deviceId">
<n-input-number v-model:value="formData.deviceId" placeholder="请输入设备ID" :min="0" :show-button="false" style="width: 100%" />
</n-form-item-gi>
<n-form-item-gi label="设备编码" path="deviceCode">
<n-input v-model:value="formData.deviceCode" placeholder="请输入设备编码" />
</n-form-item-gi>
<n-form-item-gi label="设备名称" path="deviceName">
<n-input v-model:value="formData.deviceName" placeholder="请输入设备名称" />
</n-form-item-gi>
<n-form-item-gi label="工位ID" path="stationId">
<n-input-number v-model:value="formData.stationId" placeholder="请输入工位ID" :min="0" :show-button="false" style="width: 100%" />
</n-form-item-gi>
<n-form-item-gi label="工位编码" path="stationCode">
<n-input v-model:value="formData.stationCode" placeholder="请输入工位编码" />
</n-form-item-gi>
<n-form-item-gi label="工位名称" path="stationName">
<n-input v-model:value="formData.stationName" placeholder="请输入工位名称" />
</n-form-item-gi>
<n-form-item-gi label="使用时长(小时)" path="usageHours">
<n-input-number v-model:value="formData.usageHours" placeholder="使用时长" :min="0" :step="0.5" :show-button="false" style="width: 100%" />
</n-form-item-gi>
<n-form-item-gi label="加工次数" path="processCount">
<n-input-number v-model:value="formData.processCount" placeholder="加工次数" :min="0" :show-button="false" style="width: 100%" />
</n-form-item-gi>
<n-form-item-gi label="交接人" path="handoverPerson">
<n-input v-model:value="formData.handoverPerson" placeholder="请输入交接人" />
</n-form-item-gi>
<n-form-item-gi label="接收人" path="receiverPerson">
<n-input v-model:value="formData.receiverPerson" placeholder="请输入接收人" />
</n-form-item-gi>
</n-grid>
<n-form-item label="开始时间" path="startTime">
<n-date-picker v-model:value="formData.startTime" type="datetime" placeholder="请选择开始时间" style="width: 100%" />
</n-form-item>
<n-form-item label="结束时间" path="endTime">
<n-date-picker v-model:value="formData.endTime" type="datetime" placeholder="请选择结束时间" style="width: 100%" />
</n-form-item>
</n-form>
<template #footer>
<n-space justify="end">
<n-button @click="modalVisible = false">取消</n-button>
<n-button type="primary" @click="handleSubmit">确定</n-button>
</n-space>
</template>
</n-modal>
<n-modal v-model:show="importModalVisible" preset="card" title="导入刀具使用记录" style="width: 500px">
<n-space vertical>
<n-alert type="info">
<template #header>导入说明</template>
<ul style="margin: 0; padding-left: 16px; line-height: 1.8">
<li>请先下载导入模板按模板格式填写数据</li>
<li>支持 .xlsx .xls 格式</li>
</ul>
</n-alert>
<n-space>
<n-button type="primary" @click="handleDownloadTemplate">
<template #icon><n-icon><DownloadOutline /></n-icon></template>
下载模板
</n-button>
</n-space>
<n-upload :max="1" accept=".xlsx,.xls" :show-file-list="true" :custom-request="handleImportUpload">
<n-upload-dragger>
<div style="margin-bottom: 12px">
<n-icon size="48" :depth="3"><CloudUploadOutline /></n-icon>
</div>
<n-text style="font-size: 16px">点击或拖拽文件到此处上传</n-text>
<n-p depth="3" style="margin: 8px 0 0 0">支持 .xlsx .xls 格式</n-p>
</n-upload-dragger>
</n-upload>
</n-space>
<template #footer>
<n-button @click="importModalVisible = false">关闭</n-button>
</template>
</n-modal>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, h, onMounted } from 'vue'
import { NButton, NSpace, NIcon, NDataTable, NUpload, useMessage, useDialog, type DataTableColumns, type UploadCustomRequestOptions } from 'naive-ui'
import { SearchOutline, RefreshOutline, AddOutline, TrashOutline, CreateOutline, CloudUploadOutline, DownloadOutline } from '@vicons/ionicons5'
import { toolUsageApi, type ToolUsage } from '@/api/toolUsage'
const message = useMessage()
const dialog = useDialog()
const searchForm = reactive({
toolCode: '',
deviceCode: '',
stationCode: '',
startTime: null as number | null,
endTime: null as number | null
})
const tableData = ref<ToolUsage[]>([])
const loading = ref(false)
const selectedIds = ref<number[]>([])
const pagination = reactive({
page: 1,
pageSize: 10,
itemCount: 0,
showSizePicker: true,
pageSizes: [10, 20, 50]
})
const modalVisible = ref(false)
const modalTitle = ref('')
const importModalVisible = ref(false)
const formRef = ref()
const defaultFormData: ToolUsage = {
toolId: undefined,
toolCode: '',
toolName: '',
deviceId: undefined,
deviceCode: '',
deviceName: '',
stationId: undefined,
stationCode: '',
stationName: '',
startTime: undefined,
endTime: undefined,
usageHours: 0,
processCount: 0,
handoverPerson: '',
receiverPerson: ''
}
const formData = reactive<ToolUsage>({ ...defaultFormData })
const formRules = {
toolId: [{ required: true, message: '请输入刀具ID', trigger: 'blur' }],
deviceId: [{ required: true, message: '请输入设备ID', trigger: 'blur' }],
stationId: [{ required: true, message: '请输入工位ID', trigger: 'blur' }],
startTime: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
endTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }]
}
const columns: DataTableColumns<ToolUsage> = [
{ type: 'selection', width: 48, fixed: 'left' },
{ title: '刀具编码', key: 'toolCode', width: 120, align: 'center', fixed: 'left' },
{ title: '刀具名称', key: 'toolName', width: 140, align: 'center' },
{ title: '设备编码', key: 'deviceCode', width: 120, align: 'center' },
{ title: '设备名称', key: 'deviceName', width: 140, align: 'center' },
{ title: '工位编码', key: 'stationCode', width: 120, align: 'center' },
{ title: '工位名称', key: 'stationName', width: 140, align: 'center' },
{ title: '开始时间', key: 'startTime', width: 170, align: 'center' },
{ title: '结束时间', key: 'endTime', width: 170, align: 'center' },
{ title: '使用时长(小时)', key: 'usageHours', width: 130, align: 'center',
render(row) {
return row.usageHours ?? '-'
}
},
{ title: '加工次数', key: 'processCount', width: 100, align: 'center',
render(row) {
return row.processCount ?? '-'
}
},
{ title: '交接人', key: 'handoverPerson', width: 100, align: 'center' },
{ title: '接收人', key: 'receiverPerson', width: 100, align: 'center' },
{ title: '创建时间', key: 'createTime', width: 180, align: 'center' },
{
title: '操作',
key: 'actions',
width: 140,
fixed: 'right',
render(row) {
return h('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'nowrap' } }, [
h(NButton, { size: 'small', quaternary: true, onClick: () => handleEdit(row) }, {
default: () => [h(NIcon, null, { default: () => h(CreateOutline) }), ' 编辑']
}),
h(NButton, { size: 'small', quaternary: true, type: 'error', onClick: () => handleDelete(row) }, {
default: () => [h(NIcon, null, { default: () => h(TrashOutline) }), ' 删除']
})
])
}
}
]
async function loadData() {
loading.value = true
try {
const params: Record<string, any> = {
page: pagination.page,
pageSize: pagination.pageSize
}
if (searchForm.toolCode) params.toolCode = searchForm.toolCode
if (searchForm.deviceCode) params.deviceCode = searchForm.deviceCode
if (searchForm.stationCode) params.stationCode = searchForm.stationCode
if (searchForm.startTime) params.startTime = formatDateTime(searchForm.startTime)
if (searchForm.endTime) params.endTime = formatDateTime(searchForm.endTime)
const res = await toolUsageApi.page(params)
tableData.value = res.list
pagination.itemCount = res.total
} finally {
loading.value = false
}
}
function formatDateTime(ts: number) {
const d = new Date(ts)
const pad = (n: number) => String(n).padStart(2, '0')
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}
function handleSearch() {
pagination.page = 1
loadData()
}
function handleReset() {
searchForm.toolCode = ''
searchForm.deviceCode = ''
searchForm.stationCode = ''
searchForm.startTime = null
searchForm.endTime = null
handleSearch()
}
function handlePageChange(page: number) {
pagination.page = page
loadData()
}
function handlePageSizeChange(pageSize: number) {
pagination.pageSize = pageSize
pagination.page = 1
loadData()
}
function handleCheck(keys: Array<string | number>) {
selectedIds.value = keys as number[]
}
function handleAdd() {
modalTitle.value = '新增刀具使用记录'
Object.assign(formData, defaultFormData)
modalVisible.value = true
}
function handleEdit(row: ToolUsage) {
modalTitle.value = '编辑刀具使用记录'
Object.assign(formData, row)
if (typeof row.startTime === 'string') {
formData.startTime = new Date(row.startTime.replace(' ', 'T')).getTime()
}
if (typeof row.endTime === 'string') {
formData.endTime = new Date(row.endTime.replace(' ', 'T')).getTime()
}
modalVisible.value = true
}
async function handleSubmit() {
try {
await formRef.value?.validate()
} catch {
return
}
try {
const submitData = { ...formData } as ToolUsage
if (typeof submitData.startTime === 'number') {
submitData.startTime = formatDateTime(submitData.startTime)
}
if (typeof submitData.endTime === 'number') {
submitData.endTime = formatDateTime(submitData.endTime)
}
if (submitData.id) {
await toolUsageApi.update(submitData)
message.success('修改成功')
} else {
await toolUsageApi.create(submitData)
message.success('新增成功')
}
modalVisible.value = false
loadData()
} catch (error) {
//
}
}
function handleDelete(row: ToolUsage) {
dialog.warning({
title: '提示',
content: '确定要删除该记录吗?',
positiveText: '确定',
negativeText: '取消',
onPositiveClick: async () => {
try {
await toolUsageApi.delete([row.id!])
message.success('删除成功')
loadData()
} catch (error) {
//
}
}
})
}
function handleBatchDelete() {
dialog.warning({
title: '提示',
content: `确定要删除选中的 ${selectedIds.value.length} 条记录吗?`,
positiveText: '确定',
negativeText: '取消',
onPositiveClick: async () => {
try {
await toolUsageApi.delete(selectedIds.value)
message.success('删除成功')
selectedIds.value = []
loadData()
} catch (error) {
//
}
}
})
}
async function handleExport() {
try {
const params: Record<string, any> = {}
if (selectedIds.value.length > 0) params.ids = selectedIds.value
if (searchForm.toolCode) params.toolCode = searchForm.toolCode
if (searchForm.deviceCode) params.deviceCode = searchForm.deviceCode
if (searchForm.stationCode) params.stationCode = searchForm.stationCode
if (searchForm.startTime) params.startTime = formatDateTime(searchForm.startTime)
if (searchForm.endTime) params.endTime = formatDateTime(searchForm.endTime)
const blob = await toolUsageApi.export(params)
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = '刀具使用记录.xlsx'
link.click()
window.URL.revokeObjectURL(url)
} catch (error) {
//
}
}
async function handleDownloadTemplate() {
try {
const blob = await toolUsageApi.export({})
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = '刀具使用记录导入模板.xlsx'
link.click()
window.URL.revokeObjectURL(url)
} catch (error) {
//
}
}
async function handleImportUpload({ file }: UploadCustomRequestOptions) {
if (!file.file) return
try {
const result = await toolUsageApi.export({})
if (result.fail > 0) {
dialog.warning({
title: '导入结果',
content: `成功: ${result.success} 条,失败: ${result.fail}\n错误信息: ${(result.errors || []).join('\n') || '无'}`,
positiveText: '确定'
})
} else {
message.success(`导入成功,共 ${result.success} 条数据`)
importModalVisible.value = false
}
loadData()
} catch (error) {
//
}
}
onMounted(() => {
loadData()
})
</script>
<style scoped>
.search-form {
margin-bottom: 16px;
}
.table-toolbar {
margin-bottom: 16px;
}
</style>