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
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
<template>
  <el-table
    height="100%"
    ref="artificialInformationTable"
    :data="artificialInformationList"
    :row-style="{ height: '26px' }"
    :cell-style="{ padding: '0' }"
    :row-class-name="tableRowClassName"
    row-key="id"
    :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    :expand-row-keys="expandRowKeys"
    class="artificial-table"
  >
    <el-table-column
      label="人工编号"
      prop="handymanNo"
      align="center"
      min-width="140"
      show-overflow-tooltip
    ></el-table-column>
    <el-table-column
      label="序号"
      prop="index"
      align="center"
      width="45px"
      show-overflow-tooltip
    />
    <el-table-column
      label="人工名称"
      prop="handymanName"
      align="center"
      min-width="180"
      show-overflow-tooltip
    ></el-table-column>
    <el-table-column
      label="人员"
      prop="staffName"
      align="center"
      min-width="80"
    ></el-table-column>
    <el-table-column
      label="个人次数"
      prop="personNumber"
      align="center"
      min-width="80"
    >
      <template scope="scope">
        <!--<el-input size="small" v-model="scope.row.personNumber" placeholder="" v-if="scope.row.isUpdate"-->
        <!--@change="changePersonNumber($event, scope.row, scope.$index)"></el-input>-->
        <el-input
          size="small"
          v-model="scope.row.personNumber"
          placeholder=""
          v-if="scope.row.isUpdate"
          onkeyup="this.value=this.value.match(/\d+\.?\d{0,6}/);this.dispatchEvent(new Event('input'))"
        ></el-input>
        <template v-if="!scope.row.isUpdate">{{
          scope.row.personNumber
        }}</template>
      </template>
    </el-table-column>
    <el-table-column
      label="个人工时"
      prop="workingHours"
      align="center"
      min-width="80"
    >
      <template scope="scope">
        <el-input
          size="small"
          v-model="scope.row.workingHours"
          placeholder=""
          v-if="scope.row.isUpdate"
          onkeyup="this.value=this.value.match(/\d+\.?\d{0,6}/);this.dispatchEvent(new Event('input'))"
        ></el-input>
        <template v-if="!scope.row.isUpdate">{{
          scope.row.workingHours
        }}</template>
      </template>
    </el-table-column>
    <el-table-column
      label="个人产量"
      prop="personOutput"
      align="center"
      min-width="80"
    >
      <template scope="scope">
        <el-input
          size="small"
          v-model="scope.row.personOutput"
          placeholder=""
          v-if="scope.row.isUpdate"
          onkeyup="this.value=this.value.match(/\d+\.?\d{0,6}/);this.dispatchEvent(new Event('input'))"
        ></el-input>
        <template v-if="!scope.row.isUpdate">{{
          scope.row.personOutput
        }}</template>
      </template>
    </el-table-column>
    <el-table-column label="备注" prop="remark" align="center" min-width="100">
      <template scope="scope">
        <el-input
          size="small"
          v-model="scope.row.remark"
          placeholder=""
          v-if="scope.row.isUpdate"
        ></el-input>
        <template v-if="!scope.row.isUpdate">{{ scope.row.remark }}</template>
      </template>
    </el-table-column>
    <el-table-column align="center" width="190px" fixed="right">
      <template slot="header" slot-scope="scope">
        <el-button
          type="text"
          @click="saveArtificialInformationAll()"
          :disabled="commitLimit"
          class="commonButton"
          >全部保存</el-button
        >
        <el-button
          type="text"
          @click="openSelectHandymanType()"
          :disabled="commitLimit"
          class="commonButton"
          >登记人工</el-button
        >
      </template>
      <template slot-scope="scope">
        <el-button
          type="text"
          v-if="scope.row.isUpdate && scope.row.id < 0"
          class="commonButton"
          @click="saveArtificialInformation(scope.$index, scope.row)"
          :disabled="commitLimit"
          >批量保存</el-button
        >
        <el-button
          type="text"
          v-if="!scope.row.isUpdate && scope.row.id < 0"
          class="commonButton"
          @click="editArtificialInformation(scope.$index, scope.row)"
          :disabled="commitLimit"
          >批量编辑</el-button
        >
        <el-button
          type="text"
          v-if="scope.row.isUpdate && scope.row.id > 0"
          class="commonButton"
          @click="saveArtificialInformation(scope.$index, scope.row)"
          :disabled="commitLimit"
          >保存</el-button
        >
        <el-button
          type="text"
          v-if="!scope.row.isUpdate && scope.row.id > 0"
          class="commonButton"
          @click="editArtificialInformation(scope.$index, scope.row)"
          :disabled="commitLimit"
          >编辑</el-button
        >
        <el-button
          type="text"
          class="commonButton"
          @click="delArtificialInformation(scope.$index, scope.row)"
          :disabled="commitLimit"
          >删除</el-button
        >
      </template>
    </el-table-column>
    <handymanTypeDialog
      :currshowlist.sync="showHandymanType"
      :personList="personList"
      :workstationId="workstationId"
      @handleSelectionChange="selectHandymanType"
    />
  </el-table>
</template>
<style lang="scss" scoped>
.artificial-table {
  /deep/ .el-table__fixed-right {
    height: 100% !important; //设置高优先,以覆盖内联样式
  }
}
</style>
<script>
import {
  addArtificialInformationList,
  delArtificialInformation,
  delArtificialInformationById,
  putArtificialInformation,
  putArtificialInformationById,
  getArtificialInformationByDutyRecordId,
  checkPersonDutyTimeById,
  checkPersonDutyTime
} from '@/api/product/artificialinformation'
import handymanTypeDialog from './handymantype'
import { highPrecisionMul } from '@/util/highPrecision'
 
export default {
  props: {
    personList: {
      type: Array,
      default: []
    },
    currentDutyRecord: {
      type: Object,
      default: null
    },
    workstationId: {
      type: Number,
      default: null
    },
    commitLimit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showHandymanType: false,
      artificialInformationList: [],
      expandRowKeys: [],
      insertFlag: false,
      insertHandymanTypeIds: []
    }
  },
  components: {
    handymanTypeDialog
  },
  watch: {
    'currentDutyRecord.id': function(val, oldVal) {
      if (val) {
        this.getArtificialInformation()
      } else {
        this.artificialInformationList = []
      }
    },
    artificialInformationList: {
      handler: function(newVal, oldVal) {
        if (this.insertFlag) {
          this.expandRowKeys.length = 0
          for (let i = 0, len = newVal.length; i < len; i++) {
            for (
              let j = 0, length = this.insertHandymanTypeIds.length;
              j < length;
              j++
            ) {
              if (newVal[i].handymanTypeId == this.insertHandymanTypeIds[j]) {
                this.expandRowKeys.push(newVal[i].id + '')
                break
              }
            }
          }
          this.insertFlag = false
        }
      }
    }
  },
  methods: {
    // 获取人工列表
    getArtificialInformation() {
      if (this.currentDutyRecord != null && this.currentDutyRecord.id) {
        getArtificialInformationByDutyRecordId(this.currentDutyRecord.id).then(
          (response) => {
            this.artificialInformationList = response.data.data
          }
        )
      } else {
        this.artificialInformationList = []
      }
    },
    // 打开选择人工类型页面
    openSelectHandymanType() {
      if (this.currentDutyRecord && this.currentDutyRecord.id) {
        this.showHandymanType = true
      } else {
        this.$message.warning('请先选择上班记录')
      }
    },
    selectHandymanType(handymanType, personSelection) {
      if (handymanType == null || handymanType.length == 0) {
        this.$message.warning('请选择人工类型')
        return
      }
      if (personSelection == null || personSelection.length == 0) {
        this.$message.warning('请选择人员')
        return
      }
      this.insertHandymanTypeIds.length = 0
      const artificialInformationList = []
      for (let j = 0, length = handymanType.length; j < length; j++) {
        this.insertHandymanTypeIds.push(handymanType[j].id)
        for (let i = 0, len = personSelection.length; i < len; i++) {
          artificialInformationList.push(
            Object.assign({
              productionPersonId: personSelection[i].id,
              staffName: personSelection[i].staffName,
              handymanTypeId: handymanType[j].id,
              handymanNo: handymanType[j].handymanNo,
              handymanName: handymanType[j].handymanName,
              conversionCoefficient: handymanType[j].conversionCoefficient,
              remark: '',
              handle: '是',
              dutyRecordId: this.currentDutyRecord.id,
              isUpdate: true
            })
          )
        }
      }
      addArtificialInformationList(artificialInformationList).then(
        (response) => {
          this.insertFlag = true
          this.getArtificialInformation()
          this.$emit('refreshPersonList')
        }
      )
    },
    // 删除人工
    delArtificialInformation(index, row) {
      if (row.id > 0) {
        this.$confirm(
          '是否确认删除人工:' + (row.staffName ? row.staffName : ''),
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal: false,
            type: 'warning'
          }
        )
          .then(function() {
            return delArtificialInformationById(row.id)
          })
          .then((data) => {
            this.$message.success('人工删除成功')
            this.getArtificialInformation()
            this.$emit('refreshPersonList')
          })
          .catch(() => {})
      } else {
        this.$confirm('是否确认批量删除人工:' + row.handymanName, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          closeOnClickModal: false,
          type: 'warning'
        })
          .then(function() {
            return delArtificialInformation(
              Object.assign({
                handymanTypeId: row.handymanTypeId,
                dutyRecordId: row.dutyRecordId
              })
            )
          })
          .then((data) => {
            this.$message.success('人工批量删除成功')
            this.getArtificialInformation()
            this.$emit('refreshPersonList')
          })
          .catch(() => {})
      }
    },
    // 保存人工
    saveArtificialInformation(index, row) {
      if (row.id > 0) {
        checkPersonDutyTimeById(row).then((response) => {
          if (!response.data.msg) {
            putArtificialInformationById(row).then((response) => {
              this.$message.success('人工更新成功')
              this.$set(row, 'isUpdate', false)
              this.$emit('refreshPersonList')
            })
          } else {
            this.$confirm(
              response.data.msg + '请确认是否继续录入人工:',
              '提示',
              {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                closeOnClickModal: false,
                type: 'warning'
              }
            )
              .then(function() {
                return putArtificialInformationById(row)
              })
              .then((data) => {
                this.$message.success('人工更新成功')
                this.$set(row, 'isUpdate', false)
                this.$emit('refreshPersonList')
              })
              .catch(() => {})
          }
        })
      } else {
          let list = row.children
          list.forEach(e => {
              e.personNumber = row.personNumber
              e.personOutput = row.personOutput
              e.workingHours = row.workingHours
              e.remark = row.remark
          })
 
        checkPersonDutyTime(list).then((response) => {
          if (!response.data.msg) {
            putArtificialInformation(list).then((response) => {
              this.$message.success('人工更新成功')
              this.getArtificialInformation()
              this.$emit('refreshPersonList')
            })
          } else {
            this.$confirm(
              response.data.msg + '请确认是否继续录入人工:',
              '提示',
              {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                closeOnClickModal: false,
                type: 'warning'
              }
            )
              .then(function() {
                return putArtificialInformation(row)
              })
              .then((data) => {
                this.$message.success('人工更新成功')
                this.getArtificialInformation()
                this.$emit('refreshPersonList')
              })
              .catch(() => {})
          }
        })
      }
    },
    saveArtificialInformationAll() {
      const promiseList = []
      for (
        let i = 0, len = this.artificialInformationList.length;
        i < len;
        i++
      ) {
        const children = this.artificialInformationList[i].children
        for (let j = 0, length = children.length; j < length; j++) {
          if (children[j].isUpdate) {
            const promise = new Promise((resolve) => {
              if (children[j].id) {
                putArtificialInformationById(children[j]).then((response) => {
                  this.$set(children[j], 'isUpdate', false)
                  resolve('成功')
                })
              }
            })
            promiseList.push(promise)
          }
        }
      }
      if (promiseList.length > 0) {
        Promise.all(promiseList).then(() => {
          // 等所有异步请求完成后执行
          this.$message.success('人工保存成功')
          this.$emit('refreshPersonList')
        })
      } else {
        this.$message.warning('已全部保存')
      }
    },
    editArtificialInformation(index, row) {
      this.$set(row, 'isUpdate', true)
    },
    // 修改个人次数,计算个人工时
    changePersonNumber(number, row, index) {
      this.$set(
        row,
        'workingHours',
        highPrecisionMul(row.conversionCoefficient, number)
      )
    },
    // 人工分组上色
    tableRowClassName({ row, rowIndex }) {
      if (row.id < 0) {
        return 'danger-row'
      }
      return ''
    }
  }
}
</script>