gaoluyang
2026-11-06 31a53af92e5f8c4c702eb4ad04df2bba94e80e3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<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 { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { createTransfer, getTransfer, updateTransfer } from '#/api/hrm/transfer';
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 userStore = useUserStore();
 
const isDetail = computed(() => formType.value === 'detail');
const getTitle = computed(() => {
  if (formType.value === 'detail') {
    return '查看调岗申请';
  }
  return formType.value === 'update' ? '修改调岗申请' : '新增调岗申请';
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-1',
    labelWidth: 100,
  },
  wrapperClass: 'grid-cols-2',
  layout: 'horizontal',
  schema: [],
  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) {
      await modalApi.close();
      return;
    }
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    const data = (await formApi.getValues()) as HrmTransferApi.Transfer;
    try {
      if (data.id) {
        await updateTransfer(data);
      } else {
        await createTransfer(data);
      }
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      return;
    }
    const data = modalApi.getData<{ formType: FormType; id?: number }>();
    formType.value = data.formType;
    formApi.setState({ schema: useFormSchema(data.formType, fillCurrentByUser) });
    formApi.setDisabled(data.formType === 'detail');
    modalApi.setState({ showConfirmButton: data.formType !== 'detail' });
    if (!data?.id) {
      // 新增时默认带出当前登录人,并回填其原部门 / 原岗位
      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);
      await formApi.setValues(transfer);
      if (transfer.attachmentList?.length) {
        await formApi.setFieldValue('blobIds', transfer.attachmentList.map(item => ({
          uid: String(item.id),
          name: item.name || '',
          url: item.url || '',
          status: 'done',
          id: item.id,
        })));
      }
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="getTitle" class="w-[600px]">
    <Form class="mx-4" />
  </Modal>
</template>