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
<script lang="ts" setup>
import type { VbenFormSchema } from '#/adapter/form';
import type { MesWmProductIssueLineApi } from '#/api/mes/wm/productissue/line';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { getWorkOrderBomListByTaskId } from '#/api/mes/pro/workorder/bom';
import { getProductIssue } from '#/api/mes/wm/productissue';
import {
  createProductIssueLine,
  getProductIssueLine,
  updateProductIssueLine,
} from '#/api/mes/wm/productissue/line';
 
const emit = defineEmits(['success']);
const formData = ref<MesWmProductIssueLineApi.ProductIssueLine>();
const issueId = ref<number>();
 
const getTitle = computed(() => {
  return formData.value?.id ? '编辑物料' : '添加物料';
});
 
/** 物料行表单 schema */
function buildLineFormSchema(options: { label: string; value: number }[] = []): VbenFormSchema[] {
  return [
    {
      fieldName: 'itemId',
      label: '产品物料',
      component: 'Select',
      componentProps: {
        options,
        showSearch: true,
        optionFilterProp: 'label',
        placeholder: options.length ? '请选择当前工序物料' : '请先维护当前工序投入物料',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'quantity',
      label: '领料数量',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        min: 0,
        placeholder: '请输入领料数量',
        precision: 2,
      },
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: { placeholder: '请输入备注', rows: 2 },
    },
  ];
}
 
const [Form, formApi] = useVbenForm({
  commonConfig: { componentProps: { class: 'w-full' }, formItemClass: 'col-span-1', labelWidth: 90 },
  layout: 'horizontal',
  schema: buildLineFormSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-3',
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) return;
    modalApi.lock();
    const data = (await formApi.getValues()) as MesWmProductIssueLineApi.ProductIssueLine;
    data.issueId = issueId.value;
    try {
      if (formData.value?.id) {
        await updateProductIssueLine({ ...data, id: formData.value.id });
      } else {
        await createProductIssueLine(data);
      }
      await modalApi.close();
      emit('success');
      message.success('操作成功');
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    const data = modalApi.getData<{ id?: number; issueId: number }>();
    issueId.value = data.issueId;
    modalApi.lock();
    try {
      const issue = await getProductIssue(data.issueId);
      const boms = issue.taskId ? await getWorkOrderBomListByTaskId(issue.taskId) : [];
      const optionMap = new Map<number, { label: string; value: number }>();
      for (const item of boms) {
        if (item.itemId != null) {
          optionMap.set(item.itemId, {
            value: item.itemId,
            label: `${item.itemCode ?? ''} ${item.itemName ?? ''}`.trim(),
          });
        }
      }
      const options = [...optionMap.values()];
      await formApi.updateSchema(buildLineFormSchema(options));
      await formApi.resetForm();
      if (data.id) {
        formData.value = await getProductIssueLine(data.id);
        await formApi.setValues(formData.value);
      }
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="getTitle" class="w-3/5">
    <Form class="mx-4" />
  </Modal>
</template>