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
<template>
  <el-dialog :visible.sync="isShow" :title="title" width="900px" :modal-append-to-body="false"
             :close-on-click-modal="false">
    <div class="batchAllocation-style">
      <el-form size="mini" :model="form" ref="form">
        <list-condition-template ref="table" :dataKey="'spuNum'" :tableData="form.dataSource"
                                 :total="total" :isClickRowExpand="true" :isShowPage="false"
                                 :isHiddenRow="true">
          <template slot="columns">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-row>
                  <el-col :span="3" class="cur-c">
                    <span class="required-style">可分配渠道:</span>
                  </el-col>
                  <el-col :span="21">
                    <channel-allot-module :dataSource="form.dataSource" colCount="2"
                                          :index="scope.$index">
                    </channel-allot-module>
                  </el-col>
                </el-row>
              </template>
            </el-table-column>
            <el-table-column label="商品主编码" prop="spuNum" width="180px" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="商品名称" prop="spuName" min-width="220px" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="规格型号" prop="spec" width="140px">
            </el-table-column>
            <el-table-column label="可分配数量" prop="stock" width="150px" show-overflow-tooltip>
            </el-table-column>
          </template>
        </list-condition-template>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="confirmAllocation('form')" type="primary"
                 :disabled="isConfirmAllocation" :loading="loading">确认分配
      </el-button>
      <el-button size="mini" @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import dictApi from '@/api/dict'
import commodityStocksApi from '@/api/stockManagement/commodityStocks'
import filterDialog from '@/components/filterDialog/filterDialog.vue'
import channelAllotModule from '@/views/commodityStocks/module/channelAllotModule.vue'
const reg = /^[0-9]*$/;//大于等于0的整数
export default {
  name: "batchAllocationDialog",
  components: { filterDialog, channelAllotModule },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isShow: false,
      total: 0,
      form: {
        dataSource: []
      },
      channelData: [],//渠道数据源
      loading: false,
    }
  },
  watch: {
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    dialogVisible: function (newShow, oldShow) {
      this.isShow = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    isShow: function (newDialogShow, oldDialogShow) {
      this.$emit('update:dialogVisible', newDialogShow)
      if (!newDialogShow) {
        this.initData();
      }
    }
  },
  computed: {
    /**
     * 若可选渠道均未分配任何数值,【确认分配】按钮不可点击
     */
    isConfirmAllocation: function () {
      let bool = true;
      let stockChannelDistributes = [];
      let dataSource = JSON.parse(JSON.stringify(this.form.dataSource));
      if (dataSource && dataSource.length > 0) {
        for (var i = 0; i < dataSource.length; i++) {
          stockChannelDistributes = stockChannelDistributes.concat(dataSource[i].stockChannelDistributes);
        }
        for (let key in stockChannelDistributes) {
          let val = stockChannelDistributes[key].stock;
          if (val && reg.test(val) && val != 0) {
            bool = false;
            break;
          }
        }
      }
      return bool;
    }
  },
  mounted() {
    this.initChannelData();
  },
  methods: {
    initData() {
      this.form.dataSource = [];
      this.total = 0;
    },
    /**
     * 初始化渠道数据字典
     */
    async initChannelData() {
      const res = await dictApi.getChanneldict()
      let _data = res.data;
      if (res.code === '0' && _data && _data.length > 0) {
        let _arr = [];
        _data.forEach((item) => {
          _arr.push({
            channel: item.dictCode,
            channelName: item.dictName,
            stock: null,
          })
        })
        this.stockChannelDistributes = _arr;
      }
    },
    /**
     * 获取批量分配数据源
     */
    add(data) {
      data.forEach((v) => {
        this.$set(v, 'stockChannelDistributes', JSON.parse(JSON.stringify(this.stockChannelDistributes)))
      })
      this.form.dataSource = data;
    },
    /**
     * 确认分配
     */
    confirmAllocation(formName) {
      let _this = this;
      _this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证当前分配数量是否大于主商品可分配数量
          let bool = false;
          let isNull = false;//单商品分配渠道是否为NULL
          let _data = this.form.dataSource;
          for (var i = 0; i < _data.length; i++) {
            let totalCount = 0;
            _data[i].stockChannelDistributes.forEach((item) => {
              if (item.stock) {
                totalCount += Number(item.stock);
              }
            })
            if (totalCount > _data[i].stock) {
              bool = true;
              break;
            }
            if (totalCount === 0) {
              isNull = true;
              break;
            }
          }
          if (isNull) {
            this.$message({
              message: '可分配渠道为必填项',
              type: 'info'
            })
            return
          }
          if (bool) {
            this.$message({
              message: '渠道分配数量总和不得大于可分配数量',
              type: 'info'
            })
            return
          }
          _this.handleDistributeStocks();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    /**
     * 提交
     */
    async handleDistributeStocks() {
      let params = [];
      let _data = JSON.parse(JSON.stringify(this.form.dataSource));
      _data.forEach((v1) => {
        let arr = [];
        v1.stockChannelDistributes.forEach((v2) => {
          if (v2.stock || v2.stock === 0) {
            arr.push({
              stock: v2.stock,
              channel: v2.channel
            })
          }
        })
        params.push({
          spuNum: v1.spuNum,
          stockChannelDistributes: arr
        })
      })
      this.loading = true;
      const res = await commodityStocksApi.distributeStocks(params)
      if (res.code === '200') {
        this.$message({
          message: '分配成功',
          type: 'success'
        })
        this.isShow = false;
        this.loading = false;
        this.$emit("close")
      } else {
        this.loading = false;
      }
    },
    cancel() {
      let _this = this;
      if (!_this.isConfirmAllocation) {
        _this.$confirm('取消后当前商品分配信息清空,确认要取消吗?')
          .then(_ => {
            _this.isShow = false;
          })
          .catch(_ => { });
      } else {
        _this.isShow = false;
      }
    },
  }
}
</script>
 
<style lang="scss">
.batchAllocation-style {
  .main-b-style {
    padding: 0;
  }
  .required-style:before {
    content: "*";
    color: #f08080;
    margin-right: 4px;
  }
  .l-style {
    bottom: 2px;
    position: relative;
  }
  .cur-c {
    position: relative;
    top: 3px;
    text-align: right;
  }
}
</style>