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
<template>
  <div class="mod-config">
    <basic-container>
      <el-table
        :data="tableData"
        border
        style="width: auto;margin-right: 20px;margin-left: 20px;"
        v-adaptive="{ bottomOffset: 215 }"
        height="100px"
        :header-cell-style="tableHeaderStyle"
        class="detailTableClass"
      >
        <el-table-column
          type="index"
          :index="indexMethod"
          label="序号"
        ></el-table-column>
        <el-table-column prop="partNo" label="零件编号"></el-table-column>
        <el-table-column prop="partBatchNo" label="零件批号"></el-table-column>
        <el-table-column prop="systemNo" label="系统编号"></el-table-column>
        <el-table-column label="库位号">
          <template slot-scope="scope">
            <span
              v-html="getWarehouseStock(scope.row, scope.row.locationId)"
            ></span>
          </template>
        </el-table-column>
        <el-table-column
          prop="availableStockQuantity"
          label="当前库存"
        ></el-table-column>
        <el-table-column label="预留数量">
          <template slot-scope="scope">
            <el-input
              size="small"
              v-model.number="scope.row.prepareQty"
              placeholder="请输入"
              clearable
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="outQty" label="出库数量"></el-table-column>
        <el-table-column prop="unit" label="零件单位"></el-table-column>
        <el-table-column label="操作" fixed="right" width="80" align="center">
          <template slot-scope="scope">
            <template v-if="showOperator">
              <el-button
                @click="deleteHandle(scope.row)"
                type="text"
                size="small"
                icon="el-icon-delete"
                >删除</el-button
              >
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div
        style="margin-right: 20px;margin-left: 20px;margin-top: 10px;text-align: right"
      >
        <el-button plain type="primary" @click="saveHandles">保存</el-button>
      </div>
    </basic-container>
  </div>
</template>
<style>
.detailTableClass td {
  padding: 2px 0px;
}
</style>
<script>
import adaptive from '@/util/adaptive'
import {
  fetchPrepareList,
  delObj,
  addObj,
  putObj,
  batchSave
} from '../../../api/warehouse/preparedetail'
import { newFetchList } from '../../../api/warehouse/stock'
import { mapGetters } from 'vuex'
import {
  loadLocation,
  loadPrepareLocation
} from '../../../api/warehouse/location'
export default {
  props: {
    reserveMainId: {
      type: Number,
      default: 0
    },
    visible: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: String,
      default: '01issue'
    }
  },
  data() {
    return {
      warehouseReserveDetailId: null,
      partId: null,
      tableData: [],
      prepareList: [],
      stockList: [],
      showOperator: false,
      locations: [],
      dataListLoading: false,
      oriTableData: []
    }
  },
  computed: {
    ...mapGetters(['permissions', 'userInfo'])
  },
  watch: {
    visible: {
      handler(newValue, oldValue) {
        if (newValue) {
          this.tableData = []
          /*
            this.paramObj={};
            this.paramObj.warehouseMainId=this.reserveMainId;
            this.$nextTick(() => {
              this.getData();
            }) */
        }
      },
      immediate: true
    },
    disabled: {
      handler(newValue, oldValue) {
        if (newValue === '02ingredient') {
          this.showOperator = false
        } else {
          this.showOperator = true
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.initLocationSelect()
  },
  directives: {
    adaptive
  },
  methods: {
    // 获取库位的数据
    initLocationSelect() {
      loadLocation().then((res) => {
        this.locations = JSON.parse(
          JSON.stringify(res.data.data)
            .replace(/id/g, 'value')
            .replace(/locNo/g, 'label')
        )
      })
    },
    // 将id进行匹配显示为对应的库位名称
    getWarehouseStock(row, cellValue) {
      this.locations.forEach((obj) => {
        if (obj.value == cellValue) {
          cellValue = obj.label
        }
      })
      return cellValue
    },
    // 更新备料信息和材料申请明细
    getAllDataList() {
      this.getData(this.partId, this.reserveMainId)
      this.$emit('refreshDataList')
    },
    // 点击左侧列表时,触发该方法
    getUpdateDataList(partId, warehouseReserveDetailId) {
      this.partId = partId
      this.warehouseReserveDetailId = warehouseReserveDetailId
      this.$nextTick(() => {
        this.getData(this.partId, this.reserveMainId)
      })
    },
    // 获取数据列表
    getData(partId, warehouseMainId) {
      // 获取已申请的材料明细、零件对应的库存,根据零件id、库位id、批次号进行合并
      Promise.all([
        this.getPrepareList(partId, warehouseMainId),
        this.getStockList(partId)
      ]).then((res) => {
        // 合并,以prepareList元素格式,取prepareList、stockList并集
        this.tableData = this.prepareList
        var stock
        var data
        for (var i = 0; i < this.stockList.length; i++) {
          stock = this.stockList[i]
          var prepare = this.prepareList.find(
            (item) =>
              item.partId === stock.partId &&
              item.partBatchNo === stock.partBatchNo &&
              item.locationId === stock.locationId &&
              item.systemNo === stock.systemNo
          )
          if (prepare == undefined) {
            // 以prepareList元素格式封装对象,放入tableData
            data = {}
            data.id = null
            data.partId = stock.partId
            data.partBatchNo = stock.partBatchNo
            data.locationId = stock.locationId
            data.prepareQty = 0
            data.outQty = 0
            data.warehouseReserveDetailId = this.warehouseReserveDetailId
            data.updateTime = null
            data.resNo = null
            data.partNo = stock.partNo
            data.locationName = stock.locationName
            data.warehouseMainId = warehouseMainId
            data.unit = stock.unit
            data.locNo = null
            data.recvQty = null
            data.partName = null
            data.systemNo = stock.systemNo
            data.unit = null
            data.factoryNo = null
            data.type = null
            data.resUser = null
            data.resDate = null
            data.availableStockQuantity = stock.availableStockQuantity
            this.tableData.push(data)
          } else {
            prepare.availableStockQuantity = stock.availableStockQuantity
          }
        }
        this.oriTableData = []
        if (this.tableData.length > 0) {
          for (var i = 0; i < this.tableData.length; i++) {
            var oriData = {}
            oriData.partId = this.tableData[i].partId
            oriData.partBatchNo = this.tableData[i].partBatchNo
            oriData.locationId = this.tableData[i].locationId
            oriData.prepareQty = this.tableData[i].prepareQty
            oriData.systemNo = this.tableData[i].systemNo
            oriData.unit = this.tableData[i].unit
            this.oriTableData.push(oriData)
          }
        }
      })
    },
    // 获取已申请的材料明细列表
    getPrepareList(partId, warehouseMainId) {
      return new Promise((resolve, reject) => {
        var paramObj = {}
        paramObj.partId = partId
        paramObj.warehouseMainId = warehouseMainId
        fetchPrepareList(
          Object.assign(
            {
              current: 1,
              size: 100
            },
            paramObj
          )
        )
          .then((response) => {
            var data = response.data
            if (data.code == 0) {
              this.prepareList = data.data.records
              resolve()
            } else {
              this.$message.error('获取材料列表失败')
              reject()
            }
          })
          .catch((error) => {
            reject()
          })
      })
    },
    // 获取零件对应的库存列表
    getStockList(partId) {
      return new Promise((resolve, reject) => {
        newFetchList(
          Object.assign({
            current: 1,
            size: 1000
          }),
          partId
        )
          .then((response) => {
            var data = response.data
            if (data.code == 0) {
              this.stockList = data.data.records
              resolve()
            } else {
              this.$message.error('获取库存失败')
              reject()
            }
          })
          .catch((error) => {
            reject()
          })
      })
    },
    // 新增 / 修改
    saveHandle(row) {
      if (
        row.prepareQty != null &&
        row.prepareQty != '' &&
        Number(row.prepareQty) != 0
      ) {
        var dataForm = {}
        dataForm.id = row.id
        dataForm.partNo = row.partNo
        dataForm.partBatchNo = row.partBatchNo
        dataForm.locationName = row.locationName
        dataForm.prepareQty = row.prepareQty
        dataForm.outQty = row.outQty
        dataForm.statuses = null
        dataForm.warehouseReserveDetailId = row.warehouseReserveDetailId
        dataForm.partId = row.partId
        dataForm.locationId = row.locationId
        dataForm.systemNo = row.systemNo
        if (dataForm.id) {
          putObj(dataForm).then((response) => {
            dataForm = response.data.data
            this.$message.success('保存成功')
            // 刷新申请明细两个table
            this.getAllDataList()
          })
        } else {
          addObj(dataForm).then((response) => {
            dataForm = response.data.data
            this.$message.success('保存成功')
            // 刷新申请明细两个table
            this.getAllDataList()
          })
        }
      } else {
        this.$message.error('请正确填写预留数量')
      }
    },
    // 删除
    deleteHandle(row) {
      if (row.id != null) {
        this.$confirm('是否确认删除零件批号为' + row.partBatchNo, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          closeOnClickModal: false,
          type: 'warning'
        })
          .then(function() {
            return delObj(row.id)
          })
          .then((data) => {
            this.$message.success('删除成功')
            this.getAllDataList()
          })
      }
    },
    // 序号
    indexMethod(index) {
      return index + 1
    },
    tableHeaderStyle(row, column, rowIndex, columnIndex) {
      return `background:#fff;color:#666`
    },
    // 批量保存
    saveHandles() {
      // 判断哪些记录进行了改动
      if (this.tableData != null && this.tableData.length > 0) {
        var data
        var dataForms = []
        for (var i = 0; i < this.tableData.length; i++) {
          data = this.tableData[i]
          var oriData = this.oriTableData.find(
            (item) =>
              item.partId === data.partId &&
              item.partBatchNo === data.partBatchNo &&
              item.locationId === data.locationId &&
              item.systemNo === data.systemNo
          )
          if (oriData !== undefined) {
            if (oriData.prepareQty != data.prepareQty) {
              var dataForm = {}
              dataForm.id = data.id
              dataForm.partNo = data.partNo
              dataForm.partBatchNo = data.partBatchNo
              dataForm.locationName = data.locationName
              dataForm.prepareQty = data.prepareQty
              dataForm.outQty = data.outQty
              dataForm.statuses = null
              dataForm.systemNo = data.systemNo
              dataForm.warehouseReserveDetailId = data.warehouseReserveDetailId
              dataForm.partId = data.partId
              dataForm.locationId = data.locationId
              dataForm.unit = data.unit
              dataForms.push(dataForm)
            }
          }
        }
        if (dataForms.length > 0) {
          batchSave(dataForms)
            .then((response) => {
              this.$message.success('保存成功')
              // 刷新申请明细两个table
              this.getAllDataList()
            })
            .catch((error) => {})
        }
      } else {
        this.$message.error('没有材料申请')
      }
    }
  }
}
</script>