liu
3 天以前 f11bd0d8d9c1190340a445a67df9e43cdbde0a3e
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<script lang="ts" setup>
import type { MesPdParameterApi } from '#/api/mes/pro/processdesign/parameter';
import type { MesProProcessParamApi } from '#/api/mes/pro/processdesign/processparam';
 
import { computed, ref } from 'vue';
 
import { DICT_TYPE } from '@vben/constants';
 
import { useVbenModal } from '@vben/common-ui';
 
import { Button, Input, InputNumber, message, Switch } from 'ant-design-vue';
 
import {
  getProcessParamListByProcess,
  updateProcessParams,
} from '#/api/mes/pro/processdesign/processparam';
import { $t } from '#/locales';
import { DictTag } from '#/components/dict-tag';
 
import ParameterSelectDialog from '../../template/modules/parameter-select-dialog.vue';
 
const emit = defineEmits(['success']);
 
const processId = ref<number>();
const processName = ref<string>('');
const loading = ref(false);
const boundList = ref<MesProProcessParamApi.ProcessParam[]>([]);
const parameterSelectDialogRef =
  ref<InstanceType<typeof ParameterSelectDialog>>();
 
const getTitle = computed(() => `工艺参数绑定 - ${processName.value || ''}`);
 
/** 加载工序已绑定的参数 */
async function loadData() {
  if (!processId.value) {
    return;
  }
  loading.value = true;
  try {
    boundList.value = await getProcessParamListByProcess(processId.value);
  } finally {
    loading.value = false;
  }
}
 
/** 打开工艺参数选择弹窗 */
function handleSelectParameters() {
  parameterSelectDialogRef.value?.open(
    boundList.value
      .map((item) => item.processParamId)
      .filter((id): id is number => id !== undefined),
  );
}
 
/** 接收选中的参数,追加到绑定列表(必填/排序走默认值) */
function onParametersSelected(rows: MesPdParameterApi.Parameter[]) {
  const existingIds = new Set(
    boundList.value
      .map((item) => item.processParamId)
      .filter((id): id is number => id !== undefined),
  );
  const newRows = rows
    .filter((row) => row.id !== undefined && !existingIds.has(row.id!))
    .map((row, index) => ({
      processParamId: row.id,
      paramCode: row.paramCode,
      paramName: row.paramName,
      paramType: row.paramType,
      unitMeasureName: row.unitMeasureName,
      required: row.required,
      sort: boundList.value.length + index + 1,
      remark: row.remark,
    }));
  boundList.value = [...boundList.value, ...newRows];
}
 
/** 移除绑定 */
function removeItem(index: number) {
  boundList.value.splice(index, 1);
}
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    if (processId.value === undefined) {
      return;
    }
    modalApi.lock();
    try {
      await updateProcessParams(
        processId.value,
        boundList.value.map((item, index) => ({
          processParamId: item.processParamId!,
          required: item.required,
          sort: item.sort ?? index + 1,
          remark: item.remark,
        })),
      );
      message.success($t('ui.actionMessage.operationSuccess'));
      await modalApi.close();
      emit('success');
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      processId.value = undefined;
      boundList.value = [];
      return;
    }
    const data = modalApi.getData<{ processId: number; processName?: string }>();
    processId.value = data.processId;
    processName.value = data.processName || '';
    await loadData();
  },
});
</script>
 
<template>
  <Modal :title="getTitle" class="w-4/5">
    <div class="mx-4">
      <div class="mb-2 flex items-center justify-between">
        <span class="text-sm font-medium">已绑定工艺参数</span>
        <Button type="primary" size="small" @click="handleSelectParameters">
          添加参数
        </Button>
      </div>
      <div v-if="boundList.length > 0" class="rounded border border-gray-200">
        <div
          class="grid grid-cols-12 gap-2 border-b border-gray-200 bg-gray-50 p-2 text-xs font-medium"
        >
          <div class="col-span-3">参数名称</div>
          <div class="col-span-2">参数编码</div>
          <div class="col-span-2">参数类型</div>
          <div class="col-span-1">单位</div>
          <div class="col-span-1 text-center">是否必填</div>
          <div class="col-span-1">排序</div>
          <div class="col-span-1">备注</div>
          <div class="col-span-1 text-center">操作</div>
        </div>
        <div
          v-for="(item, index) in boundList"
          :key="item.processParamId || index"
          class="grid grid-cols-12 items-center gap-2 border-b border-gray-100 p-2"
        >
          <div class="col-span-3 text-sm">{{ item.paramName }}</div>
          <div class="col-span-2 text-sm text-gray-500">{{ item.paramCode }}</div>
          <div class="col-span-2">
            <DictTag
              v-if="item.paramType !== undefined"
              :type="DICT_TYPE.MES_PD_PARAM_TYPE"
              :value="item.paramType"
            />
          </div>
          <div class="col-span-1 text-sm">{{ item.unitMeasureName }}</div>
          <div class="col-span-1 flex items-center justify-center">
            <Switch v-model:checked="item.required" size="small" />
          </div>
          <div class="col-span-1">
            <InputNumber
              v-model:value="item.sort"
              :min="1"
              size="small"
              class="w-full"
            />
          </div>
          <div class="col-span-1">
            <Input
              v-model:value="item.remark"
              placeholder="备注"
              size="small"
            />
          </div>
          <div class="col-span-1 flex items-center justify-center">
            <Button type="text" danger size="small" @click="removeItem(index)">
              删除
            </Button>
          </div>
        </div>
      </div>
      <div
        v-else
        class="rounded border border-dashed border-gray-300 p-4 text-center text-gray-400"
      >
        {{ loading ? '加载中…' : '暂未绑定参数' }}
      </div>
    </div>
    <ParameterSelectDialog
      ref="parameterSelectDialogRef"
      @selected="onParametersSelected"
    />
  </Modal>
</template>