| | |
| | | <script lang="ts" setup> |
| | | import type { FormType } from '../data'; |
| | | import type { HrmTransferApi } from '#/api/hrm/transfer'; |
| | | import type { SystemPostApi } from '#/api/system/post'; |
| | | import type { SystemUserApi } from '#/api/system/user'; |
| | | |
| | | import { computed, ref } from 'vue'; |
| | | |
| | | import { useUserStore } from '@vben/stores'; |
| | | |
| | | import { useVbenModal } from '#/packages/effects/common-ui/src'; |
| | | |
| | |
| | | |
| | | import { useVbenForm } from '#/adapter/form'; |
| | | import { createTransfer, getTransfer, updateTransfer } from '#/api/hrm/transfer'; |
| | | import { getEmployeeSimpleList } from '#/api/hrm/employee'; |
| | | import { getSimplePostList } from '#/api/system/post'; |
| | | import { getUser } from '#/api/system/user'; |
| | | import { $t } from '#/locales'; |
| | | |
| | | import { useFormSchema } from '../data'; |
| | | |
| | | const emit = defineEmits(['success']); |
| | | const formType = ref<FormType>('create'); |
| | | const currentEmployeeId = ref<number>(); |
| | | const userStore = useUserStore(); |
| | | |
| | | const isDetail = computed(() => formType.value === 'detail'); |
| | | const getTitle = computed(() => { |
| | |
| | | showDefaultActions: false, |
| | | }); |
| | | |
| | | /** 岗位列表缓存(用于把岗位 ID 回显成名称) */ |
| | | const postList = ref<SystemPostApi.Post[]>([]); |
| | | |
| | | /** 根据岗位编号取岗位名称 */ |
| | | async function getPostName(postId?: number) { |
| | | if (!postId) { |
| | | return undefined; |
| | | } |
| | | if (postList.value.length === 0) { |
| | | postList.value = await getSimplePostList(); |
| | | } |
| | | return postList.value.find((item) => item.id === postId)?.name; |
| | | } |
| | | |
| | | /** 选择调岗人员后,自动带出其原部门、原岗位 */ |
| | | async function fillCurrentByUser(user?: SystemUserApi.User) { |
| | | if (!user?.id) { |
| | | await formApi.setValues({ |
| | | currentDeptId: undefined, |
| | | currentDeptName: undefined, |
| | | currentPostId: undefined, |
| | | currentPostName: undefined, |
| | | }); |
| | | return; |
| | | } |
| | | // 选择组件带回的数据可能缺少部门名称 / 岗位,按编号再查一次用户详情 |
| | | const detail = !user.deptName || !user.postIds?.length |
| | | ? await getUser(user.id) |
| | | : user; |
| | | const postId = detail.postIds?.[0] ? Number(detail.postIds[0]) : undefined; |
| | | await formApi.setValues({ |
| | | currentDeptId: detail.deptId, |
| | | currentDeptName: detail.deptName, |
| | | currentPostId: postId, |
| | | currentPostName: await getPostName(postId), |
| | | }); |
| | | } |
| | | |
| | | const [Modal, modalApi] = useVbenModal({ |
| | | async onConfirm() { |
| | | if (isDetail.value) { |
| | |
| | | } |
| | | modalApi.lock(); |
| | | const data = (await formApi.getValues()) as HrmTransferApi.Transfer; |
| | | data.employeeId = currentEmployeeId.value; |
| | | try { |
| | | if (data.id) { |
| | | await updateTransfer(data); |
| | |
| | | } |
| | | const data = modalApi.getData<{ formType: FormType; id?: number }>(); |
| | | formType.value = data.formType; |
| | | formApi.setState({ schema: useFormSchema(data.formType) }); |
| | | formApi.setState({ schema: useFormSchema(data.formType, fillCurrentByUser) }); |
| | | formApi.setDisabled(data.formType === 'detail'); |
| | | modalApi.setState({ showConfirmButton: data.formType !== 'detail' }); |
| | | if (!data?.id) { |
| | | // 新增时,自动获取当前登录员工的部门和岗位信息 |
| | | const employees = await getEmployeeSimpleList(); |
| | | // TODO: 这里应该根据登录用户获取对应的员工信息,暂时取第一个员工作为示例 |
| | | const currentEmployee = employees[0]; |
| | | if (currentEmployee) { |
| | | currentEmployeeId.value = currentEmployee.id; |
| | | await formApi.setValues({ |
| | | currentDeptName: currentEmployee.deptName, |
| | | currentPostName: currentEmployee.postName, |
| | | }); |
| | | // 新增时默认带出当前登录人,并回填其原部门 / 原岗位 |
| | | const loginUserId = userStore.userInfo?.id; |
| | | if (loginUserId) { |
| | | formApi.setFieldValue('userId', loginUserId); |
| | | await fillCurrentByUser(await getUser(loginUserId)); |
| | | } |
| | | return; |
| | | } |
| | | modalApi.lock(); |
| | | try { |
| | | const transfer = await getTransfer(data.id); |
| | | currentEmployeeId.value = transfer.employeeId; |
| | | await formApi.setValues(transfer); |
| | | if (transfer.attachmentList?.length) { |
| | | await formApi.setFieldValue('blobIds', transfer.attachmentList.map(item => ({ |
| | |
| | | <Modal :title="getTitle" class="w-[600px]"> |
| | | <Form class="mx-4" /> |
| | | </Modal> |
| | | </template> |
| | | </template> |