✨ feat(mes): 更新保养记录状态值及表单操作逻辑

调整保养记录状态值,确保状态与操作逻辑一致。新增表单提交功能,允许在编辑模式下提交保养记录,优化用户体验。
pull/871/MERGE
YunaiV 2026-04-03 22:54:27 +08:00
parent efdb18c05b
commit a8e68c155a
2 changed files with 81 additions and 51 deletions

View File

@ -50,11 +50,18 @@
<MaintenRecordLineList :record-id="formData.id" :disabled="isDetail" />
</template>
<template #footer>
<el-button v-if="!isDetail" :disabled="formLoading" type="primary" @click="submitForm">
确 定
<el-button v-if="isEditable" :disabled="formLoading" type="primary" @click="submitForm">
保 存
</el-button>
<!-- TODO @AI:所有 isDetail ? '关 闭' : '取 消',这种,都改成 “取 消” -->
<el-button @click="dialogVisible = false">{{ isDetail ? '关 闭' : '取 消' }}</el-button>
<el-button
v-if="isEditable && formData.status === MesDvMaintenRecordStatusEnum.PREPARE"
:disabled="formLoading"
type="warning"
@click="handleSubmit"
>
提 交
</el-button>
<el-button @click="dialogVisible = false">关 闭</el-button>
</template>
</Dialog>
</template>
@ -66,24 +73,35 @@ import DvCheckPlanSelect from '@/views/mes/dv/checkplan/components/DvCheckPlanSe
import UserSelect from '@/views/system/user/components/UserSelect.vue'
import MaintenRecordLineList from './MaintenRecordLineList.vue'
import { useUserStore } from '@/store/modules/user'
import { MesDvMaintenRecordStatusEnum } from '@/views/mes/utils/constants'
defineOptions({ name: 'MaintenRecordForm' })
const emit = defineEmits(['success'])
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const userStore = useUserStore()
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
const formType = ref('') // 表单的类型:create - 新增;update - 修改;detail - 详情
const formLoading = ref(false) // 表单的加载中
const formType = ref<string>('create') // 表单的类型:create / update / detail
const isEditable = computed(() => ['create', 'update'].includes(formType.value)) // 是否为编辑模式
const isDetail = computed(() => formType.value === 'detail') // 是否为详情模式
const dialogTitle = computed(() => {
const titles: Record<string, string> = {
create: '新增保养记录',
update: '编辑保养记录',
detail: '保养记录详情'
}
return titles[formType.value] || formType.value
})
const formData = ref({
id: undefined,
id: undefined as number | undefined,
planId: undefined,
machineryId: undefined,
maintenTime: undefined,
userId: undefined,
status: undefined as number | undefined,
remark: ''
})
const formRules = reactive({
@ -91,11 +109,11 @@ const formRules = reactive({
maintenTime: [{ required: true, message: '保养时间不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const originalFormData = ref<string>('') // 原始表单数据快照,用于脏检查
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
dialogVisible.value = true
dialogTitle.value = type === 'detail' ? '保养记录详情' : t('action.' + type)
formType.value = type
resetForm()
// 修改/详情时,设置数据
@ -111,28 +129,31 @@ const open = async (type: string, id?: number) => {
if (type === 'create') {
formData.value.userId = userStore.getUser.id
}
// 保存原始数据快照
originalFormData.value = JSON.stringify(formData.value)
}
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
/** 提交表单(create/update 模式) */
const submitForm = async () => {
// 校验表单
if (!formRef) return
const valid = await formRef.value.validate()
if (!valid) return
await formRef.value.validate()
// 提交请求
formLoading.value = true
try {
const data = formData.value as any
if (formType.value === 'create') {
await DvMaintenRecordApi.createMaintenRecord(data)
const res = await DvMaintenRecordApi.createMaintenRecord(data)
message.success(t('common.createSuccess'))
// 创建成功后,更新表单数据和状态为编辑模式
formData.value.id = res
formData.value.status = MesDvMaintenRecordStatusEnum.PREPARE
formType.value = 'update'
} else {
await DvMaintenRecordApi.updateMaintenRecord(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
// 更新快照
originalFormData.value = JSON.stringify(formData.value)
// 发送操作成功的事件
emit('success')
} finally {
@ -140,6 +161,29 @@ const submitForm = async () => {
}
}
/** 提交操作:表单修改过则先保存,再提交 */
const handleSubmit = async () => {
// 校验表单
await formRef.value.validate()
try {
await message.confirm('确认提交该保养记录?【提交后将不能修改】')
formLoading.value = true
// 1. 表单有修改时,先保存
if (JSON.stringify(formData.value) !== originalFormData.value) {
const data = formData.value as any
await DvMaintenRecordApi.updateMaintenRecord(data)
}
// 2. 提交保养记录
await DvMaintenRecordApi.submitMaintenRecord(formData.value.id!)
message.success('提交成功')
dialogVisible.value = false
emit('success')
} catch {
} finally {
formLoading.value = false
}
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
@ -148,8 +192,11 @@ const resetForm = () => {
machineryId: undefined,
maintenTime: undefined,
userId: undefined,
status: undefined,
remark: ''
}
formRef.value?.resetFields()
}
defineExpose({ open })
</script>

View File

@ -55,8 +55,13 @@
<!-- 列表 -->
<ContentWrap>
<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
<!-- TODO @AI:增加一个【查看】操作,设备编码; -->
<el-table-column label="设备编码" align="center" prop="machineryCode" />
<el-table-column label="设备编码" align="center" prop="machineryCode" min-width="160">
<template #default="scope">
<el-button link type="primary" @click="openForm('detail', scope.row.id)">
{{ scope.row.machineryCode }}
</el-button>
</template>
</el-table-column>
<el-table-column label="设备名称" align="center" prop="machineryName" />
<el-table-column label="品牌" align="center" prop="machineryBrand" />
<el-table-column label="规格型号" align="center" prop="machinerySpec" />
@ -74,36 +79,24 @@
<dict-tag :type="DICT_TYPE.MES_MAINTEN_RECORD_STATUS" :value="scope.row.status" />
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<el-table-column label="操作" align="center" width="160" fixed="right">
<template #default="scope">
<el-button link type="primary" @click="openForm('detail', scope.row.id)">
查看
</el-button>
<!-- 草稿:编辑、删除 -->
<el-button
link
type="primary"
@click="openForm('update', scope.row.id)"
v-if="scope.row.status === MesDvMaintenRecordStatusEnum.PREPARE"
v-hasPermi="['mes:dv-mainten-record:update']"
v-if="scope.row.status === MesDvMaintenRecordStatusEnum.PREPARE"
>
编辑
</el-button>
<!-- TODO @AI:提交融合到【编辑】弹窗里; -->
<el-button
link
type="success"
@click="handleSubmit(scope.row)"
v-if="scope.row.status === MesDvMaintenRecordStatusEnum.PREPARE"
v-hasPermi="['mes:dv-mainten-record:update']"
>
提交
</el-button>
<el-button
link
type="danger"
@click="handleDelete(scope.row.id)"
v-if="scope.row.status === MesDvMaintenRecordStatusEnum.PREPARE"
v-hasPermi="['mes:dv-mainten-record:delete']"
v-if="scope.row.status === MesDvMaintenRecordStatusEnum.PREPARE"
>
删除
</el-button>
@ -125,13 +118,13 @@
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { DvMaintenRecordApi } from '@/api/mes/dv/maintenrecord'
import MaintenRecordForm from './MaintenRecordForm.vue'
import DvMachinerySelect from '@/views/mes/dv/machinery/components/DvMachinerySelect.vue'
import DvCheckPlanSelect from '@/views/mes/dv/checkplan/components/DvCheckPlanSelect.vue'
import UserSelect from '@/views/system/user/components/UserSelect.vue'
import { DICT_TYPE } from '@/utils/dict'
import MaintenRecordForm from './MaintenRecordForm.vue'
import { MesDvMaintenRecordStatusEnum } from '@/views/mes/utils/constants'
defineOptions({ name: 'MesDvMaintenRecord' })
@ -142,6 +135,7 @@ const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const exportLoading = ref(false) // 导出的加载中
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
@ -151,7 +145,7 @@ const queryParams = reactive({
maintenTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const formRef = ref() // 表单弹窗
/** 查询列表 */
const getList = async () => {
@ -178,21 +172,10 @@ const resetQuery = () => {
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
}
/** 提交按钮操作 */
const handleSubmit = async (row: any) => {
try {
await message.confirm('确认提交该保养记录吗?')
await DvMaintenRecordApi.submitMaintenRecord(row.id)
message.success('提交成功')
await getList()
} catch {}
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
try {
@ -216,7 +199,7 @@ const handleExport = async () => {
}
}
/** 初始化 **/
/** 初始化 */
onMounted(() => {
getList()
})