fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <div class="couponProduct">
    <el-button type="success" size="mini" @click="addProduct">选择赠品</el-button>
    <el-button size="mini" @click="clearTable">清空</el-button>
    <el-table :data="tableData" border>
      <slot></slot>
      <el-table-column label="赠品编码" width="150px" prop="prodMainCode"></el-table-column>
      <el-table-column label="赠品名称" prop="prodName" width="100px" show-overflow-tooltip>
      </el-table-column>
      <el-table-column v-if="isDisplayGiftType" width="100px" label="赠品类型" prop="giftType">
        <template slot-scope="scope">
          {{getGiftType(scope.row.giftType)}}
        </template>
      </el-table-column>
      <el-table-column label="赠品规格" width="160px">
        <template slot-scope="scope">
          <el-form-item v-if="JSON.parse(scope.row.extparams)[0].skuProps.length"
                        :prop="tableValidate.skuId(preferentialIndex,scope.$index)"
                        :rules="formRules.skuId">
            <el-select v-model="scope.row.skuId" @change="changeSkuId(scope.row)">
              <el-option v-for="item in JSON.parse(scope.row.extparams)" :key="item.skuId"
                         :value="item.skuId"
                         :label=" item.skuProps.length ? item.skuProps.map(item => item.propValueName).join('/') : '-'">
              </el-option>
            </el-select>
          </el-form-item>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <template v-if="warehouseServiceName === 'JD'">
        <el-table-column label="计量单位/计数" width="130px">
          <template slot-scope="scope">
            <el-form-item :prop="tableValidate.specificationCode(preferentialIndex,scope.$index)"
                          :rules="[
                        {
                          validator: specificationCodeVail,
                          row: scope.row,
                          warehouseServiceName:warehouseServiceName
                        }
                      ]">
              <el-select v-model="scope.row.specificationCode" multiple placeholder="请选择"
                         @change="changeSpecificationCodeVal($event,scope.row,scope.$index)">
                <el-option v-for="item in scope.row.specificationCodeList" :key="item.prodSkuNo"
                           :label="item.unitOfMeasurement + '*'+ item.skuUnit"
                           :value="item.prodSkuNo">
                </el-option>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="发货数量" width="180px">
          <template slot-scope="scope">
            <span v-if="scope.row.shipmentsCountList.length">
              <el-form-item v-for="(item,curIndex) in scope.row.shipmentsCountList" :key="curIndex"
                            :prop="tableValidate.shipmentsCount(preferentialIndex,scope.$index,curIndex)"
                            :rules="[
                              {
                                validator: shipmentsCountVail,
                                warehouseServiceName:warehouseServiceName
                              }
                            ]">
                <el-input-number :min="1" v-model="item.shipmentsCount"
                                 @change="changeShipmentsCountVal(scope.row.specificationCode,scope.row)">
                </el-input-number>
              </el-form-item>
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="赠品总数量(瓶)" min-width="160px">
        <template slot-scope="scope">
          <template v-if="warehouseServiceName === 'JD'">
            <span>{{scope.row.totalQuantity}}</span>
          </template>
          <template v-else>
            <el-form-item :prop="tableValidate.ensalequantity(preferentialIndex,scope.$index)"
                          :rules="formRules.ensalequantity">
              <el-input-number :min="0" @keydown.native="limiInputType"
                               v-model="scope.row.ensalequantity"
                               @change="changeEnsalequantity(scope.row, scope.$index)">
              </el-input-number>
            </el-form-item>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="剩余库存" prop="spuStorage" v-if="isShowSpuStorage"></el-table-column>
      <slot name="productTableRow"></slot>
      <el-table-column label="操作" width="100px">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteItem(scope.row, scope.$index)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <gift-prod-selected :show.sync="selectedDialog.show" :title="selectedDialog.title"
                        @hand-selected-row-data="handSelectedRowData"
                        :isShowSpuStorage="isShowSpuStorage"></gift-prod-selected>
  </div>
</template>
<script>
import orderMgtApi from '@/api/orderMgt'
import giftProdSelected from '@/views/giftMgt/components/giftProdSelected.vue'
import { limiInputType } from '@/utils/limiInputType'
export default {
  components: { giftProdSelected },
  props: ['productData', 'formRules', 'preferentialIndex', 'form', 'tableValidate', 'isDisplayGiftType', 'warehouseServiceName', 'isShowSpuStorage'],
  data() {
    return {
      selectedDialog: {
        show: false,
        title: '选择商品'
      },
      tableData: [],
      // 计量单位/计数下拉框数据
      measuringUnitData: [
        {
          code: '1',
          name: '盒',
          count: 3
        },
        {
          code: '2',
          name: '件',
          count: 6
        },
        {
          code: '3',
          name: '瓶',
          count: 1
        }
      ],
      // 临时存储商品计量单位/计数列的数据,区别select是选中还是移除
      newCheckUnitArr: []
    }
  },
  watch: {
    productData: {
      immediate: true, // immediate选项可以开启首次赋值监听
      handler: function(newValue, oldValue) {
        this.tableData = newValue
      }
    },
    tableData(newValue, oldValue) {
      this.$emit('update:productData', newValue)
    }
  },
  methods: {
    /**
     *验证规格编码
    */
    specificationCodeVail(rule, value, callback) {
      if (rule.warehouseServiceName === 'JD') {
        const specificationCodeList = rule.row.specificationCodeList
        if (value && value.length) {
          let flag = false
          for (let i = 0; i < value.length; i++) {
            const obj = specificationCodeList && specificationCodeList.length ? specificationCodeList.find((item) => item.prodSkuNo === value[i]) : null
            if (!obj) {
              flag = true
            }
          }
          // 未找到匹配的规格编码
          if (flag) {
            callback(new Error('请选择规格编码'))
          } else {
            callback()
          }
        } else {
          callback(new Error('请选择规格编码'))
        }
      } else {
        callback()
      }
    },
    /**
     *验证发货数量
    */
    shipmentsCountVail(rule, value, callback) {
      if (rule.warehouseServiceName === 'JD') {
        if (!value) {
          callback(new Error('请输入发货数量'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    },
    // 发货数量改变时计算赠品总数量
    changeShipmentsCountVal(specificationCode, row) {
      row.totalQuantity = this.getQuantityTotal(specificationCode, row)
    },
    /**
     * 获取赠品类型
     */
    getGiftType(val) {
      switch (val) {
        case 'GIFT':
          return '非卖品'
        case 'MATERIAL':
          return '物料'
        case 'DEPUTY_PROD':
          return '商品'
        case 'TASTING':
          return '品鉴'
        default:
          break
      }
    },
    /**
     * 当赠品数量改变时判断与限制数量的大小
     */
    changeEnsalequantity(row, index) {
      if (row.maxbuyquantity) {
        this.$parent.form.validateField(`preferentialInfo.${this.preferentialIndex}.products.${index}.maxbuyquantity`)
      }
    },
    /**
     * 选择规格时触发,获取规格名称
     */
    changeSkuId(row) {
      JSON.parse(row.extparams).forEach(item => {
        if (item.skuId === row.skuId) {
          row.prodSpecs = item.skuProps.map(item => item.propValueName).join('/')
          row.skuReduceStorage = item.skuReduceStorage
        }
      })
    },
    /**
     * 限制不能输入数字
     */
    limiInputType(e) {
      limiInputType(e)
    },
    /**
         * 清空表格数据
         */
    clearTable() {
      this.tableData = []
    },
    /**
         * 删除表格数据
         */
    deleteItem(row, index) {
      this.tableData.splice(index, 1)
    },
    /**
         * 打开添加商品弹窗
         */
    addProduct() {
      this.selectedDialog.show = true
    },
    /**
         * 获取弹出框返回的数据
         */
    handSelectedRowData(rows) {
      rows.forEach((rowItem) => {
        if (
          !this.tableData.find((item) => {
            return rowItem.spuId === item.prodId
          })
        ) {
          this.tableData.push({
            prodId: rowItem.spuId,
            prodMainCode: rowItem.spuNum,
            prodName: rowItem.spuName,
            spuStorage: rowItem.spuStorage, // 剩余库存
            prodSpecs: null,
            totalQuantity: null,
            ensalequantity: null,
            maxbuyquantity: null,
            skuId: rowItem.defaultSku ? rowItem.skuInfos[0].skuId : null,
            extparams: JSON.stringify(rowItem.skuInfos),
            spuNum: rowItem.spuNum,
            skuReduceStorage: null,
            spuUnit: rowItem.spuUnit,
            shopId: rowItem.shopId,
            imageUrl: rowItem.imageUrl,
            giftType: rowItem.giftType,
            specificationCode: [], // 计量单位/计数
            oldSpecificationCode: [], // 临时存储上一次选中的数据
            shipmentsCountList: [] // 发货数量
 
          })
          this.tableData.forEach((item) => {
            // 规格编码下拉数据集合
            this.getSpecificationCodeList(item)
          })
        }
      })
      this.$emit('hand-selected')
    },
    /**
  * 根据商品主编码获取所有的规格编码集合
  */
    getSpecificationCodeList(row) {
      orderMgtApi.getSpecificationCodeData({ prodNum: row.prodMainCode }).then((res) => {
        let arr = []
        if (res.code === '200' && res.data.length) {
          arr = [...res.data]
        }
        this.$set(row, 'specificationCodeList', arr)
      }).catch(() => {
        this.$set(row, 'specificationCodeList', [])
      })
    },
    /**
  * 规格编码选中值改变时触发
  */
    changeSpecificationCodeVal(event, row, index) {
      // 当前操作项
      const arr = this.getArrDifference(event, row.oldSpecificationCode)
      // 选中
      if (event.length > row.oldSpecificationCode.length) {
        this.tableData[index].shipmentsCountList.push({
          skuId: arr[0],
          shipmentsCount: 1
        })
      } else {
        // 移除
        const curIndnx = this.getArrayIndex(row.oldSpecificationCode, arr[0])
        if (curIndnx != -1) {
          this.tableData[index].shipmentsCountList.splice(curIndnx, 1)
        }
      }
      row.totalQuantity = this.getQuantityTotal(event, row)
      this.$set(row, 'oldSpecificationCode', event)
    },
    // 规格编码改变时计算赠品总数量
    getQuantityTotal(event, row) {
      let count = 0
      if (event && event.length) {
        for (let i = 0; i < event.length; i++) {
          const curObj = row.specificationCodeList.find((v2) => v2.prodSkuNo === event[i])
          if (curObj) {
            count += curObj.skuUnit * row.shipmentsCountList[i].shipmentsCount
          }
        }
      }
      return count || null
    },
    // 获取指定值在数组中的下标
    getArrayIndex(arr, val) {
      for (var i = 0; i < arr.length; i++) {
        if (arr[i] === val) {
          return i
        }
      }
      return -1
    },
    // 取出两个数组中不同的元素
    getArrDifference(arr1, arr2) {
      return arr1.concat(arr2).filter(function(v, i, arr) {
        return arr.indexOf(v) === arr.lastIndexOf(v)
      })
    }
  }
}
</script>
<style>
.couponProduct .el-table {
  margin-top: 20px;
}
</style>