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
<template>
  <el-dialog :visible.sync="isShow" :title="title" width="900px" :modal-append-to-body="false"
             :close-on-click-modal="false">
    <div class="allocationSet-style">
      <div class="row-style">
        <el-radio v-model="distributeType" label="AUTOMATIC">
          <span class="label-style">比例分配</span>
          <span class="memo-style">(选择该选项后,收到总库存数据将按照以下分配比例进行各渠道分发)</span>
        </el-radio>
        <el-form :model="tableForm" ref="tableForm">
          <div class="table1-style">
            <list-condition-template ref="table1" :tableData="tableForm.ratioTableData"
                                     :indexColumn="false" :isShowPage="false">
              <!-- 表格区域 -->
              <template slot="columns">
                <el-table-column label="渠道id" prop="id" align="center" v-if="disColumns">
                </el-table-column>
                <el-table-column label="渠道名称" prop="channelName" align="center">
                </el-table-column>
                <el-table-column label="分发比例(%)" align="center">
                  <template scope="scope">
                    <el-form-item :prop="'ratioTableData.'+ scope.$index + '.ratio'"
                                  :rules="formRules.ratio" style="width:100%;">
                      <el-input class="custom-input-style" v-model="scope.row.ratio"
                                placeholder="请输入渠道分发占比,0为不分发库存">
                      </el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
              </template>
            </list-condition-template>
          </div>
        </el-form>
      </div>
      <div class="row-style">
        <el-radio v-model="distributeType" label="MANUAL">
          <span class="label-style">手动分配</span>
          <span class="memo-style">(选择该选项后,收到总库存数据需手动分配各渠道库存)</span>
        </el-radio>
      </div>
      <el-collapse v-model="isExpand">
        <el-collapse-item title="操作日志" name="1">
          <div class="table2-style">
            <list-condition-template ref="table2" :tableData="logTableData" :total="logTotal"
                                     @page-info-change="handlePageInfoChange">
              <!-- 表格区域 -->
              <template slot="columns">
                <el-table-column label="操作时间" prop="createTime" width="160px">
                </el-table-column>
                <el-table-column label="分配类型" prop="allotType" align="center" width="120px"
                                 show-overflow-tooltip>
                  <template slot-scope="scope">
                    <span v-if="scope.row.distributeType">
                      <!-- AUTOMATIC比例分配, MANUAL手动分配 -->
                      {{scope.row.distributeType === 'AUTOMATIC' ? '比例分配' : '手动分配'}}
                    </span>
                    <span v-else>-</span>
                  </template>
                </el-table-column>
                <el-table-column label="操作内容" align="center" min-width="220px"
                                 show-overflow-tooltip>
                  <template slot-scope="scope">
                    <span v-if="scope.row.distributeType === 'AUTOMATIC'">
                      {{getOperContent(scope.row.distributeData,tableForm.ratioTableData)}}
                    </span>
                    <span v-else>-</span>
                  </template>
                </el-table-column>
                <el-table-column label="操作人" prop="createName" width="120px" show-overflow-tooltip>
                </el-table-column>
              </template>
            </list-condition-template>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="save" type="primary" :loading="loading">保存
      </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 { vailInputRuleFloat } from '@/utils/validator'
export default {
  name: "allocationSetDialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      disColumns: false,
      isShow: false,
      loading: false,
      distributeType: null,
      channelData: [],
      tableForm: {
        ratioTableData: [],
      },
      formRules: {
        'ratio': [{ validator: vailInputRuleFloat }]
      },
      isExpand: [],
      logTotal: 0,
      logTableData: []
    }
  },
  watch: {
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    dialogVisible: function (newShow, oldShow) {
      this.isShow = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    isShow: function (newDialogShow, oldDialogShow) {
      this.$emit('update:dialogVisible', newDialogShow)
      if (!newDialogShow) {
        this.$nextTick(() => {
          this.initData() // 初始化表单
        })
      }
    }
  },
  mounted() {
    this.initChannelData();
  },
  methods: {
    /**
     * 初始化表单
     */
    initData() {
      let _data = this.tableForm.ratioTableData;
      if (_data && _data.length > 0) {
        _data.forEach((item) => {
          item.ratio = null;
        })
      }
      this.logTableData = [];
      this.logTotal = 0;
      this.isExpand = [];
    },
    getInfo(_data) {
      let _this = this;
      _this.distributeType = _data.allotType;
      if (_data.data && _data.data.length > 0) {
        _this.tableForm.ratioTableData.forEach((v1) => {
          let _obj = _data.data.find((v2) => v1.id === v2.channel);
          if (_obj) {
            v1.ratio = _obj.ratio;
          }
        })
      }
      _this.$nextTick(() => {
        _this.getOperLogData(_this.$refs.table2.getPageInfo());
      })
    },
    /**
     *保存
    */
    save() {
      let _this = this;
      if (_this.distributeType === 'AUTOMATIC') {
        let total = 0;
        _this.$refs.tableForm.validate((valid) => {
          if (valid) {
            _this.tableForm.ratioTableData.forEach((item) => {
              if (item.ratio) {
                total += parseFloat(item.ratio)
              }
            })
            if (total > 100) {
              _this.$message({
                message: '渠道比例不得大于100%',
                type: 'warning'
              });
              return;
            } else if (total < 100) {
              _this.$message({
                message: `渠道比列剩余${(100 - total).toFixed(2)}%未分配`,
                type: 'warning'
              });
              return;
            } else {
              //比例分配 --- 分发比例
              let arr = [];
              _this.tableForm.ratioTableData.forEach((v) => {
                if (v.ratio) {
                  arr.push({
                    channel: v.id,
                    ratio: v.ratio
                  })
                }
              })
              _this.submitData(arr);
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      } else {
        _this.submitData();
      }
    },
    async submitData(data) {
      let params = {
        distributeType: this.distributeType,
      }
      if (data) {
        params.distributeData = data;
      }
      this.loading = true;
      const res = await commodityStocksApi.saveStockDistributeConf(params);
      if (res.code === '200') {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.isShow = false;
        this.loading = false;
        this.$emit("close")
      } else {
        this.loading = false;
      }
    },
    /**
     * 初始化渠道数据字典
     */
    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({
            id: item.dictCode,
            channelName: item.dictName,
            ratio: null,
          })
        })
        this.tableForm.ratioTableData = _arr;
      }
    },
    /**
     * 分页信息改变时查询列表
     */
    handlePageInfoChange(pageInfo) {
      this.getOperLogData(pageInfo)
    },
    /**
     * 获取操作日志
     */
    async getOperLogData(pageInfo = { pageNum: 1, pageSize: 10 }) {
      let pageParams = {
        current: pageInfo.pageNum,
        size: pageInfo.pageSize,
      }
      const res = await commodityStocksApi.getOperLogList(pageParams);
      if (res.code === '200') {
        let _data = res.data;
        this.logTableData = _data.records;
        this.logTotal = _data.total;
      }
    },
    /**
    * 操作内容字符串拼接
    */
    getOperContent(arr, channelData) {
      let str = "";
      arr.forEach((item) => {
        let _obj = channelData.find((v) => v.id === item.channel);
        if (_obj) {
          str += `${_obj.channelName}${item.ratio}%;`
        }
      })
      return str;
    }
  },
}
</script>
 
<style  lang="scss">
.allocationSet-style {
  .row-style {
    margin-bottom: 20px;
    .label-style {
      font-size: 16px;
    }
    .memo-style {
      font-size: 14px;
      color: rgb(144, 147, 153);
    }
  }
  .main-b-style {
    padding: 0 !important;
  }
  .el-collapse-item__header {
    font-size: 16px !important;
  }
  .el-form-item__error {
    margin-left: 50px;
  }
  .table1-style {
    .el-table td,
    .el-table th {
      padding: 12px 0 0 !important;
    }
  }
  .custom-input-style {
    .el-input__inner {
      height: 28px !important;
      line-height: 28px !important;
    }
  }
}
</style>