bug
dengzedong
2024-12-05 e4de9b67964f9b728743179c5bbdfbd227d53906
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="60%">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-divider content-position="left">基本信息</el-divider>
      <el-row>
        <el-col :span="12">
          <el-form-item label="模型编号" prop="modelCode">
            <el-input v-model="formData.modelCode" placeholder="请输入模型编号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="模型类型" prop="modelType">
            <el-select v-model="formData.modelType" placeholder="请选择">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHE_MODEL_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调用方式" prop="invocation">
            <el-select v-model="formData.invocation" placeholder="请选择">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.SCHE_MODEL_INVOCATION)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">模型信息</el-divider>
      <div style="width: 120px;text-align: right;margin-bottom: 8px">
        <el-popover placement="right" :width="300" trigger="click" ref="modelPopover" @before-enter="model = undefined">
          <template #reference>
            <span style="color: #409eff;cursor: pointer">关联模型信息</span>
          </template>
          <template #default>
            <div style="display:flex;flex-direction: row;align-items: center;">
              <el-cascader style="width: 100%" v-model="model" placeholder="选择模型" :teleported="false" @change="changeModel" :options="scheduleModelList"/>
            </div>
          </template>
        </el-popover>
      </div>
      <el-row>
        <el-col :span="12">
          <el-form-item label="模型名称" prop="modelName">
            <el-input v-model="formData.modelName" placeholder="请输入模型名称" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类名" prop="className">
            <el-input v-model="formData.className" placeholder="请输入类名" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="方法名" prop="methodName">
            <el-input v-model="formData.methodName" placeholder="请输入方法名" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数数量" prop="portLength">
            <el-input-number v-model="formData.portLength" :min="0" controls-position="right" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="参数构造" prop="paramStructure">
            <el-input v-model="formData.paramStructure" placeholder="请输入参数构造" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="模型路径" prop="modelPath">
            <el-input v-model="formData.modelPath" placeholder="模型路径" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">输入参数</el-divider>
      <el-table
        :data="formData.paramList"
        border
        style="width: 100%; margin-top: 5px;">
        <el-table-column
          prop=""
          label="端口"
          width="100"
          align="center">
          <template #default="scope">
            <el-input v-model="scope.row.modelparamportorder" maxlength="5" clearable :disabled="true"
                      style="width:100%; hight:100%"/>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="序号"
          width="100"
          align="center">
          <template #default="scope">
            <el-input v-model="scope.row.modelparamorder" maxlength="5" clearable
                      style="width:100%;hight:100%"/>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="类型"
          width="150"
          align="center">
          <template #default="scope">
            <el-select v-model="scope.row.modelparamtype" placeholder="请选择" @change="changeModelparamtype(scope.row)">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.MODEL_PARAM_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="参数名称"
          align="center">
          <template #default="scope">
            <el-select v-if="scope.row.modelparamtype === 'NormalItem'"
              v-model="scope.row.modelparamid"
              placeholder="请选择"
              filterable
              style="width: 100%">
              <el-option-group
                v-for="group in modelparamListMap['NormalItem']"
                :key="group.value"
                :label="group.label"
              >
                <el-option
                  v-for="item in group.children"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-option-group>
            </el-select>
            <el-select v-else
              v-model="scope.row.modelparamid"
              filterable
              placeholder="请选择">
              <el-option
                v-for="(item, index) in modelparamListMap[scope.row.modelparamtype]"
                :key="index"
                :label="item.name"
                :value="item.id"/>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="参数长度"
          width="160"
          align="center">
          <template #default="scope">
            <el-input-number v-model="scope.row.datalength" :min="0" clearable controls-position="right"
                             style="width:100%;hight:100%"/>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="操作"
          width="100"
          align="center">
          <template #default="scope">
            <el-button
              link
              @click.prevent="addRow(scope.$index, formData.paramList)"
              type="primary"
              size="small">
              添加
            </el-button>
            <el-button
              link
              @click.prevent="deleteRow(scope.$index, scope.row, formData.paramList)"
              type="primary"
              size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
 
      <el-divider content-position="left">设置参数</el-divider>
      <el-table
        :data="formData.settingList"
        border
        style="width: 100%; margin-top: 5px;">
        <el-table-column
          prop=""
          label="键"
          align="center">
          <template #default="scope">
            <el-input v-model="scope.row.key" maxlength="20" clearable :disabled="true"
                      style="width:100%;hight:100%"/>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="名称"
          align="center">
          <template #default="scope">
            <el-input v-model="scope.row.name" maxlength="20" clearable :disabled="true"
                      style="width:100%;hight:100%"/>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="类型"
          align="center">
          <template #default="scope">
            <el-select v-model="scope.row.valuetype" placeholder="请选择" :disabled="true">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.MODEL_METHOD_SETTING_VALUE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column
          prop=""
          label="值"
          align="center">
          <template #default="scope">
            <el-input v-model="scope.row.value" maxlength="256" clearable  :disabled="scope.row.key === 'pyFile'"
                      style="width:100%;hight:100%"/>
          </template>
        </el-table-column>
<!--        <el-table-column-->
<!--          prop=""-->
<!--          label="操作"-->
<!--          width="100"-->
<!--          align="center">-->
<!--          <template #default="scope">-->
<!--            <el-button-->
<!--              @click.prevent="addRow(scope.$index, formData.settingList)"-->
<!--              link-->
<!--              type="primary"-->
<!--              size="small">-->
<!--              添加-->
<!--            </el-button>-->
<!--            <el-button-->
<!--              @click.prevent="deleteRow(scope.$index, formData.settingList)"-->
<!--              link-->
<!--              type="primary"-->
<!--              size="small">-->
<!--              删除-->
<!--            </el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
  import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
  import * as ScheduleModelApi from '@/api/model/sche/model'
  import { CommonStatusEnum } from '@/utils/constants'
  import * as MpkApi from "@/api/model/mpk/mpk";
 
  defineOptions({ name: 'ScheduleModelForm' })
 
  const { t } = useI18n() // 国际化
  const message = useMessage() // 消息弹窗
  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  const formType = ref('') // 表单的类型:create - 新增;update - 修改
  const formData = ref({
    id: undefined,
    modelCode: undefined,
    modelName: undefined,
    modelType: undefined,
    className: undefined,
    methodName: undefined,
    portLength: undefined,
    paramStructure: undefined,
    modelPath: undefined,
    resultStrId: undefined,
    invocation: undefined,
    status: CommonStatusEnum.ENABLE,
    paramList: [],
    settingList: []
  })
  const formRules = reactive({
    modelCode: [{ required: true, message: '模型编号不能为空', trigger: 'blur' }],
    modelName: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }],
    modelType: [{ required: true, message: '模型类型不能为空', trigger: 'blur' }]
  })
  const formRef = ref() // 表单 Ref
  const modelparamListMap = ref({})
  // 调度模型列表
  const scheduleModelList = ref([])
  const model = ref()
  const modelPopover = ref()
 
  const addRow = function (index, rows) {
    let row = JSON.parse(JSON.stringify(rows[index]))
    rows.splice(index, 0, row)
    this.orderRow(rows)
  }
 
  const deleteRow = function (index, row, rows) {
    if (!rows || rows.length === 1 || rows.filter(e => e.modelparamportorder === row.modelparamportorder).length === 1) {
      message.error('不可删除!')
      return
    }
    rows.splice(index, 1)
    this.orderRow(rows)
  }
 
  const orderRow = function (rows) {
    let modelparamorder = 0
    let modelparamportorder = 0
    rows.forEach(function (value) {
      if (value.modelparamportorder !== modelparamportorder) {
        modelparamportorder = value.modelparamportorder
        modelparamorder = 1
      }
      value.modelparamorder = modelparamorder
      modelparamorder++
    })
  }
 
  /** 打开弹窗 */
  const open = async (type: string, id?: number) => {
    dialogVisible.value = true
    dialogTitle.value = t('action.' + type)
    formType.value = type
    resetForm()
    // 修改时,设置数据
    if (id) {
      formLoading.value = true
      try {
        formData.value = await ScheduleModelApi.getScheduleModel(id)
      } finally {
        formLoading.value = false
      }
    }
    // 加载参数列表
    modelparamListMap.value = await ScheduleModelApi.getModelParamList()
    // 加载调度模型列表
    getScheduleModelList()
  }
  defineExpose({ open }) // 提供 open 方法,用于打开弹窗
 
  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  const submitForm = async () => {
    // 校验表单
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value as unknown as ScheduleModelApi.ScheduleModelVO
      if (formType.value === 'create') {
        await ScheduleModelApi.createScheduleModel(data)
        message.success(t('common.createSuccess'))
      } else {
        await ScheduleModelApi.updateScheduleModel(data)
        message.success(t('common.updateSuccess'))
      }
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')
    } finally {
      formLoading.value = false
    }
  }
 
  /** 重置表单 */
  const resetForm = () => {
    formData.value = {
      id: undefined,
      modelCode: undefined,
      modelName: undefined,
      modelType: undefined,
      className: undefined,
      methodName: undefined,
      portLength: undefined,
      paramStructure: undefined,
      modelPath: undefined,
      resultStrId: undefined,
      invocation: undefined,
      status: CommonStatusEnum.ENABLE,
      paramList: [],
      settingList: []
    }
    formRef.value?.resetFields()
  }
 
  const getScheduleModelList = async () => {
    let list = await MpkApi.list({pyType: 'schedul'})
    if (list && list.length > 0) {
      scheduleModelList.value = list.map(e => {
        return {
          label: e.pyChineseName,
          value: e,
          children: e.modelMethods.map(m => {
            return {
              label: m.methodName,
              value: m
            }
          })
        }
      })
    }
  }
 
  // 选择调度模型
  const changeModel = async () => {
    // 校验
    if (model.value && model.value.length > 0) {
      const modelInfo = model.value[0]
      const methodInfo = model.value[1]
      formData.value.modelName = modelInfo.pyChineseName
      formData.value.className = modelInfo.pkgName + '.impl.' + modelInfo.pyName + 'Impl';
      formData.value.methodName = methodInfo.methodName
      formData.value.portLength = methodInfo.dataLength
      // 参数构造
      let paramStructure = []
      for (let i = 0; i < methodInfo.dataLength; i++) {
        paramStructure.push('[[D')
      }
      if (methodInfo.model === 1) {
        paramStructure.push('java.util.HashMap')
      }
      paramStructure.push('java.util.HashMap')
      formData.value.paramStructure = paramStructure.join(',')
      formData.value.modelPath = modelInfo.pyModule
      // 输入参数
      let paramList = []
      for (let i = 0; i < methodInfo.dataLength; i++) {
        paramList.push({
          modelparamportorder: i+1 + '',
          modelparamorder: '1',
          modelparamtype: '',
          modelparamid: '',
          datalength: 0
        })
      }
 
      formData.value.paramList = paramList
      // 设置参数
      let settingList = []
      methodInfo.methodSettings.forEach(e => {
        settingList.push({
          key: e.settingKey,
          value: e.value,
          valuetype: e.valueType,
          name: e.name
        })
      })
      formData.value.settingList = settingList
      modelPopover.value.hide()
    }else {
      message.error("请先选择模型")
    }
  }
 
  function changeModelparamtype(row) {
    row.modelparamid = ''
  }
</script>