gaoluyang
昨天 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
<script lang="ts" setup>
import type { MesMdAutoCodePartApi } from '#/api/mes/md/autocode/part';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { MesAutoCodePartTypeEnum } from '@vben/constants';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createAutoCodePart,
  getAutoCodePart,
  updateAutoCodePart,
} from '#/api/mes/md/autocode/part';
import { $t } from '#/locales';
 
import { usePartFormSchema } from '../data';
 
const emit = defineEmits(['success']);
const formData = ref<MesMdAutoCodePartApi.AutoCodePart>();
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['规则分段'])
    : $t('ui.actionTitle.create', ['规则分段']);
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 120,
  },
  layout: 'horizontal',
  schema: usePartFormSchema(),
  showDefaultActions: false,
});
 
/** 清理当前分段类型不需要的字段 */
function normalizePartData(data: MesMdAutoCodePartApi.AutoCodePart) {
  if (data.type !== MesAutoCodePartTypeEnum.DATE) {
    data.dateFormat = undefined;
  }
  if (data.type !== MesAutoCodePartTypeEnum.FIX) {
    data.fixCharacter = undefined;
  }
  if (data.type !== MesAutoCodePartTypeEnum.SERIAL) {
    data.serialStartNo = undefined;
    data.serialStep = undefined;
    data.cycleFlag = false;
    data.cycleMethod = undefined;
  } else if (!data.cycleFlag) {
    data.cycleMethod = undefined;
  }
  return data;
}
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data = normalizePartData(
      (await formApi.getValues()) as MesMdAutoCodePartApi.AutoCodePart,
    );
    try {
      await (formData.value?.id
        ? updateAutoCodePart(data)
        : createAutoCodePart(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    const data = modalApi.getData<{
      id?: number;
      maxSort?: number;
      ruleId: number;
    }>();
    if (!data?.id) {
      await formApi.setValues({
        ruleId: data.ruleId,
        sort: (data.maxSort || 0) + 1,
      });
      return;
    }
    modalApi.lock();
    try {
      formData.value = await getAutoCodePart(data.id);
      // 设置到 values
      await formApi.setValues(formData.value);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="getTitle" class="w-1/3">
    <Form class="mx-4" />
  </Modal>
</template>