zouyu
2023-11-17 d8ac6057eaad648687699e25a575f3b7b8c1b102
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
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="100px"
      class="l-mes"
    >
      <el-form-item label="标准编号" prop="standardNo">
        <el-input v-model="dataForm.standardNo" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="标准名称" prop="standardName">
        <el-input v-model="dataForm.standardName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="检测类型" prop="inspectionType">
        <el-select
          style="width:100%"
          v-model="dataForm.inspectionType"
          placeholder="检测类型"
        >
          <el-option
            v-for="ele in inspectionTypes"
            :key="ele.value"
            :label="ele.label"
            :value="ele.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工序" prop="operationName">
        <el-input v-model="dataForm.operationName" placeholder="请选择工序">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="openOperationDialog()"
          ></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="函数">
        <div style="display:flex;">
          <div
            v-for="(item, i) in funcs"
            :key="i"
            style="margin-right:18px;color:red;font-size:12px;cursor: pointer;"
            @click="addFun(item)"
          >
            <el-tooltip
              class="item"
              effect="dark"
              :content="item.desc"
              placement="top-start"
            >
              <div>{{ item.label }}</div>
            </el-tooltip>
          </div>
          <div
            v-for="(item, i) in constants"
            :key="item.name"
            style="margin-right:18px;color:red;font-size:12px;cursor: pointer;"
            @click="addFun(item)"
          >
            <el-tooltip
              class="item"
              effect="dark"
              :content="item.desc"
              placement="top-start"
            >
              <div>{{ item.labelDesc }}</div>
            </el-tooltip>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="判定条件">
        <div>
          <ZttMonacoEditor
            :items="params"
            :idSubfix="monacoId"
            ref="templateJudgmentCondition"
          ></ZttMonacoEditor>
        </div>
        <div style="color:red;">
          点击输入框后,鼠标右键可选择参数项
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        :disabled="isSubmit"
        v-thinclick="`dataFormSubmit`"
        v-if="editable"
        >确定</el-button
      >
    </span>
    <operationDialog
      :currshowlist.sync="showOperate"
      @listenToOperationEvent="selectOperate"
    />
  </el-dialog>
</template>
 
<script>
import {
  getTestStandard,
  addTestStandard,
  putTestStandard,
  getTestStandardParams
} from '@/api/quality/teststandard'
import ZttMonacoEditor from '@/views/common/ztt-monaco-editor.vue'
import { funcForStandard, constantForStandard } from '@/util/testFunctionRule'
import { remote } from '@/api/admin/dict'
import operationDialog from '@/views/common/operation.vue'
 
export default {
  props: {
    editable: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      monacoId: 'standard',
      visible: false,
      dataForm: {
        id: 0,
        standardName: '',
        standardNo: '',
        remark: '',
        judgeFormula: '',
        inspectionType: null,
        operationId: null,
        operationNo: null,
        operationName: null,
      },
      dataRule: {},
      params: [],
      funcs: [],
      constants: [],
      inspectionTypes: [],
      isSubmit: false,
      showOperate: false,
    }
  },
  components: {
    ZttMonacoEditor,
    operationDialog
  },
  created() {
    this.funcs = funcForStandard()
    this.constants = constantForStandard()
    this.getInspectionType('apply_report_type')
  },
  methods: {
    getInspectionType(type) {
      remote(type).then((response) => {
        const code = response.data.code
        if (code === 0) {
          const _data = response.data.data
          this.inspectionTypes = _data
        }
      })
    },
    addFun(item) {
      let index = this.$refs.templateJudgmentCondition.getPosition().column
      const lineNumber = this.$refs.templateJudgmentCondition.getPosition()
        .lineNumber
      let preIndex = 0
      if (lineNumber > 1) {
        for (let i = 1; i <= lineNumber - 1; i++) {
          preIndex += this.$refs.templateJudgmentCondition.getLineLength(i) + 2
        }
      }
      index = index + preIndex
      const pre = this.$refs.templateJudgmentCondition
        .getVal()
        .substring(0, index - 1)
      const fix = this.$refs.templateJudgmentCondition
        .getVal()
        .substring(index - 1)
      this.$refs.templateJudgmentCondition.setVal(pre + item.label + fix)
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.id) {
          getTestStandard(this.dataForm.id).then((response) => {
            this.dataForm = response.data.data
            this.$refs.templateJudgmentCondition.setVal(
              this.dataForm.judgeFormula == null
                ? ''
                : this.dataForm.judgeFormula
            )
          })
          this.params = []
          getTestStandardParams(
            Object.assign({
              testStandardId: this.dataForm.id
            })
          ).then((response) => {
            this.params = response.data.data
          })
        } else {
          this.$refs.templateJudgmentCondition.setVal('')
        }
      })
    },
    // 获取某段字符在字符串中出现的次数
    getNumCharInStr(wholeSection, fragment, i) {
      let num = i
      const existIndex = wholeSection.indexOf(fragment)
      if (existIndex >= 0) {
        num = num + 1
        wholeSection = wholeSection.substring(existIndex + fragment.length)
        return this.getNumCharInStr(wholeSection, fragment, num)
      } else {
        return num
      }
    },
    getRandom() {
      let num = Math.random()
      num = num * 100
      num = Math.floor(num)
      return num
    },
    // 表单提交
    dataFormSubmit() {
      this.isSubmit = true
      // 校验判定公式
      let judgeFormula = this.$refs.templateJudgmentCondition.getVal()
      // 替换公式中的pi常量为3.14
      if (judgeFormula.length > 0) {
        const piExistIndex = judgeFormula.indexOf('pi')
        if (piExistIndex >= 0) {
          judgeFormula = judgeFormula.replace(/pi/g, '3.14')
        }
      }
      // 替换公式中的passAll()函数为true
      if (judgeFormula.length > 0) {
        const piExistIndex = judgeFormula.indexOf('passAll()')
        if (piExistIndex >= 0) {
          judgeFormula = judgeFormula.replace(/passAll[(][)]/g, 'true')
        }
      }
      // 先判定公式中函数格式是否正确,再用默认值计算出函数值,替换进公式,最后将公式中剩余的参数进行默认值替换,利用js eval函数进行公式校验
      let flag = true
      let funcEl
      for (let j = 0; j < this.funcs.length; j++) {
        funcEl = this.funcs[j]
        // 计算函数关键在公式中出现的次数
        const num = this.getNumCharInStr(judgeFormula, funcEl.name, 0)
        // for循环次数校验替换
        for (let k = 0; k < num; k++) {
          const existIndex = judgeFormula.indexOf(funcEl.name)
          if (existIndex >= 0) {
            // 函数关键字在公式中存在
            const prefixStr = judgeFormula.substring(0, existIndex)
            const startToEndStr = judgeFormula.substring(
              existIndex + funcEl.name.length
            )
            if (startToEndStr.length > 0) {
              const paramStartIndex = startToEndStr.indexOf('(')
              const paramEndIndex = startToEndStr.indexOf(')')
              if (paramStartIndex === 0 && paramEndIndex > 0) {
                const suffixStr = startToEndStr.substring(paramEndIndex + 1)
                const paramStr = startToEndStr.substring(
                  paramStartIndex + 1,
                  paramEndIndex
                )
                if (paramStr.trim() !== '') {
                  const paramArr = paramStr.split(',')
                  if (paramArr.length > 0) {
                    if (
                      (funcEl.singleParam && paramArr.length === 1) ||
                      !funcEl.singleParam
                    ) {
                      let paramFlag = true
                      for (let i = 0; i < paramArr.length; i++) {
                        const param = this.params.find(
                          (item) =>
                            paramArr[i].trim() === 'V[' + item.code + ']'
                        )
                        if (param !== undefined) {
                        } else {
                          flag = false
                          paramFlag = false
                          this.$message.error('函数参数不存在')
                        }
                      }
                      if (paramFlag) {
                        if (funcEl.returnType === 'number') {
                          judgeFormula = prefixStr + '1' + suffixStr
                        } else {
                          judgeFormula = prefixStr + 'true' + suffixStr
                        }
                      } else {
                        break
                      }
                    } else {
                      flag = false
                      this.$message.error('函数参数个数不正确')
                      break
                    }
                  } else {
                    break
                  }
                } else {
                  flag = false
                  this.$message.error('函数必须要有参数')
                  break
                }
              } else {
                flag = false
                this.$message.error(
                  '函数需包含左右小括号,且左侧小括号需紧邻函数'
                )
                break
              }
            } else {
              flag = false
              this.$message.error('函数需包含左右小括号')
              break
            }
          }
        }
      }
      if (flag) {
        // 将公式中剩余的参数进行默认值替换
        let param
        for (let x = 0; x < this.params.length; x++) {
          param = this.params[x]
          // 计算参数在公式中出现的次数
          const num = this.getNumCharInStr(
            judgeFormula,
            'V[' + param.code + ']',
            0
          )
          // for循环次数校验替换
          for (let y = 0; y < num; y++) {
            const existIndex = judgeFormula.indexOf('V[' + param.code + ']')
            if (existIndex >= 0) {
              const prefixStr = judgeFormula.substring(0, existIndex)
              const suffixStr = judgeFormula.substring(
                existIndex + ('V[' + param.code + ']').length
              )
              judgeFormula = prefixStr + this.getRandom() + suffixStr
            }
          }
        }
        try {
          eval(judgeFormula)
        } catch (exception) {
          flag = false
        }
        if (flag) {
          this.dataForm.judgeFormula = this.$refs.templateJudgmentCondition.getVal()
          this.$refs.dataForm.validate((valid) => {
            if (valid) {
              if (this.dataForm.id) {
                putTestStandard(this.dataForm).then((data) => {
                  this.$message.success('修改成功')
                  this.visible = false
                  this.isSubmit = false
                  this.$emit('refreshDataList')
                })
              } else {
                addTestStandard(this.dataForm).then((data) => {
                  this.$message.success('添加成功')
                  this.visible = false
                  this.isSubmit = false
                  this.$emit('refreshDataList')
                })
              }
            } else {
              this.isSubmit = false
            }
          })
        } else {
          this.isSubmit = false
          this.$message.error('公式格式错误,请仔细检查')
        }
      } else {
        this.isSubmit = false
      }
    },
    openOperationDialog() {
      this.showOperate = true
    },
    selectOperate(operate) {
      if (operate) {
        this.dataForm.operationId = operate.id
        this.dataForm.operationNo = operate.operationNo
        this.dataForm.operationName = operate.name
      }
    },
  }
}
</script>