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
<template>
  <el-dialog :visible.sync="isShow" :title="title" width="680px" :modal-append-to-body="false"
             :close-on-click-modal="false">
    <div class="singleCommodity-top-style">
      <el-row>
        <el-col :span="5" style="text-align:right;">
          <span>可分配数量:</span>
        </el-col>
        <el-col :span="19">
          <span class="count-s">{{form.dataSource[0].stock}}</span>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5" style="text-align:right;">
          <span class="required-style">可选渠道:</span>
        </el-col>
        <el-col :span="16">
          <el-form size="mini" :model="form" label-width="140px" ref="form" class="l-style">
            <channel-allot-module :dataSource="form.dataSource" placeholder="请输入">
            </channel-allot-module>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="confirmAllocation('form')" type="primary"
                 :disabled="isConfirmAllocation">
        确认分配</el-button>
      <el-button size="mini" @click="isShow = false">取消</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import dictApi from '@/api/dict'
import commodityStocksApi from '@/api/stockManagement/commodityStocks'
import channelAllotModule from '@/views/commodityStocks/module/channelAllotModule.vue'
//单商品分配
const reg = /^[0-9]*$/;
export default {
  name: "singleCommodityDialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  components: {
    channelAllotModule
  },
  data() {
    return {
      isShow: false,
      row: [],
      distributeStock: 0,//可分配数量
      form: {
        dataSource: [{ stockChannelDistributes: [], stock: 0, }]
      }
    }
  },
  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 = this.form.dataSource[0].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: {
    /**
     * 初始化渠道数据字典
     */
    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.form.dataSource[0].stockChannelDistributes = JSON.parse(JSON.stringify(_arr));
      }
    },
    /**
     * 获取当前操作项数据
     */
    add(row) {
      this.form.dataSource[0].stock = row.stock;//可分配数量
      this.row = row;
    },
    /**
     *确认分配
     */
    confirmAllocation(formName) {
      let _this = this;
      _this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证当前分配数量是否大于主商品可分配数量
          let bool = false;
          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 (bool) {
            this.$message({
              message: '渠道分配数量总和不得大于可分配数量',
              type: 'info'
            })
            return
          }
          _this.handleDistributeStocks();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    /**
     * 提交
     */
    async handleDistributeStocks() {
      let { spuNum } = this.row;
      let arr = this.form.dataSource[0].stockChannelDistributes;
      let params = [
        {
          spuNum: spuNum,
          stockChannelDistributes: []
        }
      ]
      arr.forEach((item) => {
        if (item.stock || item.stock === 0) {
          params[0].stockChannelDistributes.push({
            stock: item.stock,
            channel: item.channel
          })
        }
      })
      const res = await commodityStocksApi.distributeStocks(params)
      if (res.code === '200') {
        this.$message({
          message: '分配成功',
          type: 'success'
        })
        this.isShow = false;
        this.$emit("close")
      }
    },
    /**
     * 初始化表单数据
     */
    initData() {
      let stockChannelDistributes = this.form.dataSource[0].stockChannelDistributes;
      for (const i in stockChannelDistributes) {
        stockChannelDistributes[i].stock = null
      }
      this.distributeStock = 0;
    },
  }
}
</script>
 
<style lang="scss">
.singleCommodity-top-style {
  > div:not(:last-child) {
    margin-bottom: 15px;
  }
  .count-s {
    margin-left: 3px;
    top: 3px;
  }
  .l-style {
    bottom: 2px;
  }
  .count-s,
  .l-style {
    position: relative;
  }
}
.required-style:before {
  content: "*";
  color: #f08080;
  margin-right: 4px;
}
</style>