houzhongjian
2024-12-04 bb203eb72ee4604be8c9272cc583ecb9e393aeb8
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<template>
  <div class="p-16px" style="background-color: #ffffff">
    <el-header>
      {{title}}
    </el-header>
    <el-main>
      <el-form
        ref="formRef"
        v-loading="formLoading"
        :model="formData"
        :rules="formRules"
        label-width="120px"
      >
        <el-divider content-position="left">模型信息</el-divider>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="模型类型" prop="pyType">
              <el-radio-group v-model="formData.pyType" @change="pyTypeChange">
                <el-radio-button :disabled="actionType == 'edit'"
                  v-for="dict in getDictOptions(DICT_TYPE.MODEL_TYPE)"
                  :key="dict.label"
                  :label="dict.value"
                >
                  {{ dict.label }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="模型文件" prop="pyName">
              <el-input disabled v-model="formData.pyName" placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-upload
              ref="uploadRef"
              v-model:file-list="fileList"
              :show-file-list="false"
              :action="importUrl"
              :auto-upload="true"
              :disabled="uploadLoading"
              v-loading="uploadLoading"
              :before-upload="beforeUpload"
              :headers="uploadHeaders"
              :on-error="submitFormError"
              :on-success="submitFormSuccess"
              accept=".py"
            >
              <el-tooltip content="上传.py算法文件" placement="top" effect="light">
                <el-button type="primary">
                  <Icon icon="ep:upload"/>
                  模型上传
                </el-button>
              </el-tooltip>
            </el-upload>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="模型名称" prop="pyChineseName">
              <el-input v-model="formData.pyChineseName" placeholder=""/>
            </el-form-item>
          </el-col>
 
        </el-row>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="包名" prop="pkgName">
              <el-select v-model="formData.pkgName" clearable filterable placeholder="请选择包名">
                <el-option
                  v-for="item in pkgNameList"
                  :key="item.packName"
                  :label="item.packName"
                  :value="item.packName"
                >
                  <span style="float: left">{{ item.packName}}</span>
                  <span
                    style="
                      float: right;
                      color: var(--el-text-color-secondary);
                      font-size: 13px;">
                    {{ item.packDesc}}
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="所属目录" prop="menuAndGroup">
              <el-cascader
                style="width: 100%;"
                v-model="formData.menuAndGroup"
                :options="treeData"
                @change="handleChange"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="图标" prop="icon">
              <el-select v-model="formData.icon" clearable filterable placeholder="请选择图标">
                <el-option
                  v-for="item in iconList"
                  :key="item.iconName"
                  :label="item.iconName"
                  :value="item.iconName"
                >
                  <span style="float: left">{{ item.iconName}}</span>
                  <span
                      style="
                      float: right;
                      color: var(--el-text-color-secondary);
                      font-size: 13px;">
                    <img :src="staticDir + 'SimtreeUnitImage/' + item.iconName" style="height: 24px;" :alt=" item.iconDesc" />
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" placeholder="" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">模型方法</el-divider>
        <el-row :gutter="20">
          <el-col :span="4">
            <el-button type="primary" size="small" @click="addRow()" >新增</el-button>
          </el-col>
        </el-row>
        <el-table :data="formData.modelMethods" border
                  @expand-change="methodExpandChange" :expand-row-keys="methodExpandedRowKeys" :row-key="row => row.id">
          <el-table-column
            prop=""
            label="方法名"
            align="center"
            width="250">
            <template #default="scope">
              <el-input size="small" v-model="scope.row.methodName" placeholder=""/>
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="输入个数"
            align="center">
            <template #default="scope">
              <el-input-number size="small" step-strictly v-model="scope.row.dataLength" :min="1"
                               :max="50"/>
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="是否有model"
            align="center">
            <template #default="scope">
              <el-switch size="small" v-model="scope.row.model" :active-value="1"
                         :inactive-value="0"/>
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="结果key"
            align="center">
            <template #default="scope">
              <el-input size="small" v-model="scope.row.resultKey"/>
            </template>
          </el-table-column>
          <el-table-column label="方法参数" type="expand" width="100px">
            <template #default="props">
              <div class="m-16px">
                <el-button type="primary" size="small" @click="addSetting(props.row.methodSettings)">新增参数</el-button>
                <el-table :data="props.row.methodSettings" border size="small">
                  <el-table-column align="center" label="key" prop="settingKey"/>
                  <el-table-column align="center" label="参数名称" prop="name"/>
                  <el-table-column align="center" label="参数默认值" prop="value"/>
                  <el-table-column align="center" label="输入类型" prop="type">
                    <template #default="props">
                      <div class="flex file-row justify-center items-center">
                        {{props.row.type}}
                        <div class="ml-8px" v-if="props.row.type === 'select'">
                          <el-popover placement="left" :width="400">
                            <template #reference>
                              <el-button size="small" link type="primary">
                                <Icon icon="ep:more" />
                              </el-button>
                            </template>
                            <el-table width="50%" :data="props.row.settingSelects" border size="small">
                              <el-table-column align="center" label="key" prop="selectKey"/>
                              <el-table-column align="center" label="name" prop="name"/>
                            </el-table>
                          </el-popover>
                        </div>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="参数类型" prop="valueType"/>
                  <el-table-column align="center" label="最大值" prop="max"/>
                  <el-table-column align="center" label="最小值" prop="min"/>
                  <el-table-column label="操作" fixed="right" header-align="center" align="center" width="100">
                    <template #default="scope">
                      <el-button
                        @click="updateSetting(scope.row)"
                        key="danger"
                        type="primary"
                        :disabled="scope.row.settingKey === 'pyFile'"
                        link
                      >修改
                      </el-button>
                      <el-button
                        @click="deleteSetting(props.row.methodSettings,scope.$index)"
                        key="danger"
                        type="danger"
                        :disabled="scope.row.settingKey === 'pyFile'"
                        link
                      >删除
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" header-align="center" align="center" width="100">
            <template #default="scope">
              <el-button
                @click="deleteRow(scope.$index)"
                key="danger"
                type="danger"
                link
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-main>
    <el-footer>
      <div class="flex flex-row justify-end items-center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-footer>
  </div>
  <SettingForm ref="settingFormRef"/>
</template>
<script lang="ts" setup>
  import {DICT_TYPE, getDictOptions, getIntDictOptions} from '@/utils/dict'
  import * as MpkApi from '@/api/model/mpk/mpk'
  import * as MpkIconApi from '@/api/model/mpk/icon'
  import * as MpkPackApi from '@/api/model/mpk/pack'
  import * as MpkMenuApi from '@/api/model/mpk/menu'
  import {FormRules} from 'element-plus'
  import {getAccessToken, getTenantId} from "@/utils/auth";
  import SettingForm from './SettingForm.vue'
  import {generateUUID} from "@/utils";
 
  const {t} = useI18n() // 国际化
  const message = useMessage() // 消息弹窗
  const title = ref('') // 弹窗的标题
  const actionType = ref('') // 操作类型
  const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  const formType = ref('') // 表单的类型:create - 新增;update - 修改
  const route = useRoute() // 路由
  const router = useRouter();
 
  const staticDir = ref(import.meta.env.VITE_STATIC_DIR)
 
  const treeData = ref([])
  const iconList = ref([] as MpkIconApi.MpkIconVO)
  const pkgNameList = ref([] as MpkPackApi.MpkPackVO)
 
/** settingForm弹窗 */
  const settingFormRef = ref()
  // 添加setting
  const addSetting = (methodSettings) => {
    settingFormRef.value.open(undefined,methodSettings)
  }
 
  // 修改setting
  const updateSetting = (info) => {
    settingFormRef.value.open(info)
  }
  // 删除setting
  const deleteSetting = (methodSettings,index) => {
    methodSettings.splice(index, 1);
  }
 
  const methodExpandedRowKeys = ref([])
  const methodExpandChange = async (row: any, expandedRows: any[]) => {
    methodExpandedRowKeys.value = expandedRows.map(e => e.id)
  }
 
  const formData = ref({
    id: route.params.id,
    pyChineseName: undefined,
    pyName: undefined,
    pkgName: undefined,
    pyType: 'predict',
    className: undefined,
    pyModule: undefined,
    icon: undefined,
    menuName: undefined,
    groupName: undefined,
    remark: undefined,
    modelMethods: [],
    filePath: undefined,
    menuAndGroup: [],
  })
 
  const formRules = reactive<FormRules>({
    pyName: [
      {required: true, message: '模型名称不能为空,请上传模型文件', trigger: 'blur'}
    ],
    pyChineseName: [
      {required: true, message: '模型中文名称不能为空', trigger: 'blur'}
    ],
    pyType: [
      {required: true, message: '模型类型不能为空', trigger: 'blur'}
    ],
    pkgName: [
      {required: true, message: '包名不能为空', trigger: 'blur'}
    ],
    className: [
      {required: true, message: '类名不能为空', trigger: 'blur'}
    ],
    menuAndGroup: [
      {required: true, message: '所属目录不能为空', trigger: 'blur'}
    ],
    icon: [
      {required: true, message: 'icon不能为空', trigger: 'blur'}
    ],
  })
 
  const formRef = ref() // 表单 Ref
 
  /** 提交表单 */
  const submitForm = async () => {
    // 校验表单
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    // 模型方法校验
    if (formData.value.modelMethods?.length <= 0) {
      message.error('模型方法为空')
      return
    }
    // 模型方法名称校验
    if (formData.value.modelMethods.some(e => e.methodName === undefined || e.methodName === '' || e.dataLength === undefined || e.dataLength === null)) {
      message.error('存在不合法模型方法名')
      return
    }
    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value as unknown as MpkApi.MpkVO
      if (formType.value === 'create') {
        await MpkApi.createMpk(data)
        message.success(t('common.createSuccess'))
      } else {
        await MpkApi.updateMpk(data)
        message.success(t('common.updateSuccess'))
      }
    } finally {
      formLoading.value = false
    }
    // router.push({path:'/model/mpk'})
    router.back()
  }
 
  /** 重置表单 */
  const resetForm = () => {
    formData.value = {
      id: undefined,
      pyName: undefined,
      pyChineseName: undefined,
      pkgName: undefined,
      pyType: 'predict',
      className: undefined,
      pyModule: undefined,
      icon: undefined,
      menuName: undefined,
      groupName: undefined,
      remark: undefined,
      modelMethods: [],
      filePath: undefined
    }
    formRef.value?.resetFields()
  }
 
  const handleChange = function () {
 
  }
 
  const addRow = function () {
    formData.value.modelMethods.push({
      id: generateUUID(),
      methodName: undefined,
      dataLength: 1,
      model: 0,
      resultKey: undefined,
      methodSettings: []
    })
  }
  const deleteRow = function (index) {
    formData.value.modelMethods.splice(index, 1)
  }
 
  const fileList = ref([]) // 文件列表
  const importUrl = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/model/mpk/file/upload'
  const uploadLoading = ref(false) // 表单的加载中
  const uploadHeaders = ref() // 上传 Header 头
  const beforeUpload = function (file) {
    // 提交请求
    uploadHeaders.value = {
      Authorization: 'Bearer ' + getAccessToken(),
      'tenant-id': getTenantId()
    }
    uploadLoading.value = true
    return true;
  }
  const submitFormError = (): void => {
    message.error('上传失败!')
    uploadLoading.value = false
  }
  const submitFormSuccess = (response: any) => {
    if (response.code !== 0) {
      message.error(response.msg)
      uploadLoading.value = false
      return
    }
    const data = response.data;
    formData.value.filePath = data.filePath
    formData.value.pyName = data.fileName
    message.success('上传成功')
    uploadLoading.value = false
  }
 
  onMounted(async () => {
    const id = formData.value.id;
    const type = id ? 'edit' : 'create'
    actionType.value = type
    title.value = t('action.' + type)
    formType.value = type
    resetForm()
    pyTypeChange()
    // 修改时,设置数据
    if (id) {
      formLoading.value = true
      try {
        debugger
        formData.value = await MpkApi.getMpk(id)
        debugger
      } finally {
        formLoading.value = false
      }
    }
 
    // 加载图标列表
    iconList.value = await MpkIconApi.getList()
 
    pkgNameList.value = await MpkPackApi.getList()
 
    // 加载菜单,分组
    treeData.value = await MpkMenuApi.getTree()
  })
 
  const pyTypeChange = () => {
    if (formData.value.pyType === 'predict') {
      formData.value.modelMethods = [
        {
          id: generateUUID(),
          methodName: 'train',
          dataLength: 1,
          model: 0,
          resultKey: 'result',
          methodSettings: []
        },
        {
 
          id: generateUUID(),
          methodName: 'predict',
          dataLength: 1,
          model: 1,
          resultKey: undefined,
          methodSettings: []
        }
      ]
    }else {
      formData.value.modelMethods = [
        {
          id: generateUUID(),
          methodName: 'schedul',
          dataLength: 1,
          model: 0,
          resultKey: undefined,
          methodSettings: []
        }
      ]
    }
  }
</script>
 
<style scoped lang="scss">
</style>