Fixiaobai
2023-10-13 e8308ddac0ba4a1f406e8f63d7e6b6f2541cb770
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      label-width="80px"
      class="l-mes"
    >
      <el-row type="flex" justify="center">
        <el-col :span="10">
          <el-form-item label="模板编号" prop="templateNo">
            <el-input
              v-model="dataForm.templateNo"
              placeholder="模板编号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="报工类型" prop="testType">
            <el-select v-model="dataForm.testType" style="width: 100%">
              <el-option
                v-for="item in testTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="10">
          <el-form-item label="模板描述" prop="testDesc">
            <el-input
              v-model="dataForm.testDesc"
              placeholder="模板描述"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="编制人" prop="createUser">
            <el-input
              v-model="dataForm.createUser"
              placeholder="编制人"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="10">
          <el-form-item label="编制日期" prop="createTime">
            <el-date-picker
              v-model="dataForm.createTime"
              type="datetime"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="启用状态" prop="status">
            <el-switch v-model="dataForm.status" disabled></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-tabs type="border-card">
      <el-tab-pane label="报工项">
        <el-form
          :model="productOutItemsData"
          ref="itemForm"
          :rules="productOutItemsData.productOutItemsRules"
        >
          <el-table
            :data="productOutItemsData.productOutItemsList"
            max-height="330"
            class="tableItemsClass"
          >
            <el-table-column
              label="报工项目"
              prop="prodoutItems"
              align="center"
            >
              <template slot-scope="scope">
                <el-form-item
                  :prop="
                    'productOutItemsList.' + scope.$index + '.productOutItems'
                  "
                  :rules="
                    productOutItemsData.productOutItemsRules.productOutItems
                  "
                  class="colItemsClass"
                >
                  <el-input v-model="scope.row.productOutItems"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              label="默认值"
              prop="prodoutItemsValue"
              align="center"
            >
              <template slot-scope="scope">
                <el-form-item
                  :prop="
                    'productOutItemsList.' +
                      scope.$index +
                      '.productOutItemsValue'
                  "
                  :rules="
                    productOutItemsData.productOutItemsRules
                      .productOutItemsValue
                  "
                  class="colItemsClass"
                >
                  <el-input v-model="scope.row.productOutItemsValue"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="remark" align="center">
              <template slot-scope="scope">
                <el-form-item class="colItemsClass">
                  <el-input v-model="scope.row.remark"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column align="center" width="60px">
              <template slot="header" slot-scope="scope">
                <el-button
                  type="text"
                  icon="el-icon-circle-plus-outline"
                  size="medium"
                  style="color: #e6a23c"
                  @click="addItems()"
                ></el-button>
              </template>
              <template slot-scope="scope">
                <el-button
                  type="text"
                  icon="el-icon-delete"
                  size="medium"
                  style="color: red"
                  @click="deleteItem(scope.$index, scope.row)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="适配工作站">
        <el-form
          :model="productOutWorkstationData"
          ref="workstationForm"
          :rules="productOutWorkstationData.productOutWorkstationRules"
        >
          <el-table
            :data="productOutWorkstationData.productOutWorkstationList"
            max-height="330"
            class="tableItemsClass"
          >
            <el-table-column label="机台" prop="workstationId" align="center">
              <template slot-scope="scope">
                <el-form-item
                  :prop="
                    'productOutWorkstationList.' +
                      scope.$index +
                      '.workstationId'
                  "
                  :rules="
                    productOutWorkstationData.productOutWorkstationRules
                      .workstationId
                  "
                  class="colItemsClass"
                >
                  <el-select
                    v-model="scope.row.workstationId"
                    @change="changeWorkstation(scope.$index, scope.row)"
                    filterable
                  >
                    <el-option
                      v-for="item in workstations"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              label="编号"
              prop="workstationNumber"
              align="center"
            >
            </el-table-column>
            <el-table-column label="类型" prop="workstationType" align="center">
            </el-table-column>
            <el-table-column
              label="描述"
              prop="workstationRemark"
              align="center"
            >
            </el-table-column>
            <el-table-column align="center" width="60px">
              <template slot="header" slot-scope="scope">
                <el-button
                  type="text"
                  icon="el-icon-circle-plus-outline"
                  size="medium"
                  style="color: #e6a23c"
                  @click="addWorkstations()"
                ></el-button>
              </template>
              <template slot-scope="scope">
                <el-button
                  type="text"
                  icon="el-icon-delete"
                  size="medium"
                  style="color: red"
                  @click="deleteWorkstation(scope.$index, scope.row)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getObj, addObj, putObj } from '@/api/product/productouttemplate'
import { fetchList as fetchListForWorkstation } from '@/api/basic/workstation'
import { remote as remoteForm } from '@/api/admin/dict'
import ElCol from 'element-ui/packages/col/src/col'
import ElInput from '../../../../node_modules/element-ui/packages/input/src/input.vue'
 
export default {
  components: {
    ElInput,
    ElCol
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: null,
        templateNo: null,
        testType: null,
        testDesc: null,
        createUser: null,
        createTime: null,
        status: false
      },
      productOutItemsData: {
        productOutItemsList: [],
        productOutItemsRules: {
          productOutItems: [
            { required: true, message: '报工项目不能为空', trigger: 'blur' }
          ],
          productOutItemsValue: [
            { required: true, message: '默认值不能为空', trigger: 'blur' }
          ]
        }
      },
      productOutWorkstationData: {
        productOutWorkstationList: [],
        productOutWorkstationRules: {
          workstationId: [
            { required: true, message: '机台不能为空', trigger: 'blur' }
          ]
        }
      },
      dataRule: {
        templateNo: [
          { required: true, message: '模板编号不能为空', trigger: 'blur' }
        ],
        testDesc: [
          { required: true, message: '报工描述不能为空', trigger: 'blur' }
        ],
        testType: [
          { required: true, message: '报工类型不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '编制时间不能为空', trigger: 'blur' }
        ],
        createUser: [
          { required: true, message: '编制人不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '启用状态不能为空', trigger: 'blur' }
        ]
      },
      workstations: [],
      testTypes: [],
      clickDateArr: []
    }
  },
  methods: {
    remoteTestType() {
      remoteForm('work_type').then((response) => {
        var typeList = response.data.data
        this.testTypes = []
        if (typeList != null && typeList.length > 0) {
          var testType
          for (var i = 0; i < typeList.length; i++) {
            testType = {}
            testType.value = typeList[i].value
            testType.label = typeList[i].label
            this.testTypes.push(testType)
          }
        }
      })
    },
    getWorkstations() {
      this.workstations = []
      fetchListForWorkstation(
        Object.assign({
          current: 1,
          size: 9999
        })
      ).then((response) => {
        var records = response.data.data.records
        if (records != null && records.length > 0) {
          var workstation
          for (var i = 0; i < records.length; i++) {
            workstation = {}
            workstation.value = records[i].id
            workstation.label = records[i].name
            workstation.workstationNo = records[i].workstationNo
            workstation.type = records[i].type
            workstation.remark = records[i].remark
            this.workstations.push(workstation)
          }
        }
      })
    },
    async init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.clickDateArr = []
      await this.remoteTestType()
      await this.getWorkstations()
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        this.$refs.itemForm.resetFields()
        this.$refs.workstationForm.resetFields()
        this.productOutItemsData.productOutItemsList = []
        this.productOutWorkstationData.productOutWorkstationList = []
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then((response) => {
            var templateData = response.data.data
            this.dataForm.id = templateData.id
            this.dataForm.templateNo = templateData.templateNo
            this.dataForm.testType = templateData.testType
            this.dataForm.testDesc = templateData.testDesc
            this.dataForm.createUser = templateData.createUser
            this.dataForm.createTime = templateData.createTime
            this.dataForm.status = templateData.status
 
            var productOutItemsList = templateData.productOutItemsList
            var productOutWorkstationList =
              templateData.productOutWorkstationList
            // 渲染报工项
            if (productOutItemsList != null && productOutItemsList.length > 0) {
              var productOutItem
              for (var i = 0; i < productOutItemsList.length; i++) {
                productOutItem = {}
                productOutItem.productOutItems =
                  productOutItemsList[i].productOutItems
                productOutItem.productOutItemsValue =
                  productOutItemsList[i].productOutItemsValue
                productOutItem.remark = productOutItemsList[i].remark
                productOutItem.id = productOutItemsList[i].id
                this.productOutItemsData.productOutItemsList.push(
                  productOutItem
                )
              }
            }
            // 渲染适配机台
            if (
              productOutWorkstationList != null &&
              productOutWorkstationList.length > 0
            ) {
              var productOutWorkstation
              for (var i = 0; i < productOutWorkstationList.length; i++) {
                productOutWorkstation = {}
                productOutWorkstation.workstationId =
                  productOutWorkstationList[i].workstationId
                var workstation = this.workstations.find(
                  (item) => item.value === productOutWorkstation.workstationId
                )
                if (workstation != null) {
                  productOutWorkstation.workstationNumber =
                    workstation.workstationNo
                  productOutWorkstation.workstationType = workstation.type
                  productOutWorkstation.workstationRemark = workstation.remark
                }
                this.productOutWorkstationData.productOutWorkstationList.push(
                  productOutWorkstation
                )
              }
            }
          })
        } else {
          // 初始化默认表单
          this.initDefaultForm()
        }
      })
    },
    // 初始化默认表单
    initDefaultForm() {
      this.dataForm.id = 0
      this.dataForm.templateNo = null
      this.dataForm.testType = null
      this.dataForm.testDesc = null
      this.dataForm.createUser = null
      this.dataForm.createTime = null
      this.dataForm.status = false
      this.productOutItemsData.productOutItemsList = []
      this.productOutWorkstationData.productOutWorkstationList = []
    },
    // 添加报工项
    addItems() {
      var producItem = {}
      producItem.productOutItems = null
      producItem.productOutItemsValue = null
      producItem.remark = null
      this.productOutItemsData.productOutItemsList.push(producItem)
    },
    // 添加机台
    addWorkstations() {
      var productWorkstation = {}
      productWorkstation.workstationId = null
      productWorkstation.workstationNumber = null
      productWorkstation.workstationType = null
      productWorkstation.workstationRemark = null
      this.productOutWorkstationData.productOutWorkstationList.push(
        productWorkstation
      )
    },
    // 删除报工项
    deleteItem(index, row) {
      this.productOutItemsData.productOutItemsList.splice(index, 1)
    },
    // 删除机台
    deleteWorkstation(index, row) {
      this.productOutWorkstationData.productOutWorkstationList.splice(index, 1)
    },
    // 选择机台
    changeWorkstation(index, row) {
      var workstation = this.workstations.find(
        (item) => item.value === row.workstationId
      )
      if (workstation != null) {
        row.workstationNumber = workstation.workstationNo
        row.workstationType = workstation.type
        row.workstationRemark = workstation.remark
      }
    },
    // 表单提交
    dataFormSubmit() {
      var canClickFlag = true
      this.clickDateArr.push(new Date().getTime())
      if (this.clickDateArr.length > 1) {
        if (
          this.clickDateArr[this.clickDateArr.length - 1] -
            this.clickDateArr[this.clickDateArr.length - 2] <
          2000
        ) {
          // 小于2秒则认为重复提交
          canClickFlag = false
        }
      }
      if (canClickFlag) {
        var dataFlag = true
        var itemFlag = true
        var workstationFlag = true
        // 模板主表校验
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.dataForm.productOutItemsList = []
            this.dataForm.productOutWorkstationList = []
          } else {
            dataFlag = false
          }
        })
        // 报工项校验
        this.$refs.itemForm.validate((valid) => {
          if (valid) {
            this.dataForm.productOutItemsList = []
            // 将productOutItemsData中数据,同步至dataForm
            var productOutItemsList = this.productOutItemsData
              .productOutItemsList
            if (productOutItemsList != null && productOutItemsList.length > 0) {
              var productOutItem
              for (var i = 0; i < productOutItemsList.length; i++) {
                productOutItem = {}
                productOutItem.productOutItems =
                  productOutItemsList[i].productOutItems
                productOutItem.productOutItemsValue =
                  productOutItemsList[i].productOutItemsValue
                productOutItem.remark = productOutItemsList[i].remark
                this.dataForm.productOutItemsList.push(productOutItem)
              }
            }
          } else {
            itemFlag = false
          }
        })
        // 机台校验
        this.$refs.workstationForm.validate((valid) => {
          if (valid) {
            this.dataForm.productOutWorkstationList = []
            // 将productOutWorkstationData中数据,同步至dataForm
            var productOutWorkstationList = this.productOutWorkstationData
              .productOutWorkstationList
            if (
              productOutWorkstationList != null &&
              productOutWorkstationList.length > 0
            ) {
              var productOutWorkstation
              for (var i = 0; i < productOutWorkstationList.length; i++) {
                productOutWorkstation = {}
                productOutWorkstation.workstationId =
                  productOutWorkstationList[i].workstationId
                this.dataForm.productOutWorkstationList.push(
                  productOutWorkstation
                )
              }
            }
          } else {
            workstationFlag = false
          }
        })
        if (dataFlag && itemFlag && workstationFlag) {
          if (this.dataForm.id) {
            putObj(this.dataForm)
              .then((response) => {
                var data = response.data
                if (data.code == 0) {
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                } else {
                  this.$message.error('修改失败')
                }
              })
              .catch((error) => {})
          } else {
            addObj(this.dataForm)
              .then((response) => {
                var data = response.data
                if (data.code == 0) {
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                } else {
                  this.$message.error('添加失败')
                }
              })
              .catch((error) => {})
          }
        }
      }
    }
  }
}
</script>
<style>
.tableItemsClass td {
  padding: 0px 0px !important;
}
.colItemsClass {
  padding-bottom: 0px !important;
  padding-top: 17px !important;
}
.colItemsClass .el-form-item__content {
  margin-left: 0px !important;
}
</style>