BatchModifyPackageInventoryDialog.vue
8.53 KB
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
<template>
<d2-dialog btn-ok-label="确认"
class="batch-modify-package-inventory-dialog"
title="批量修改库存模式"
width="1080px"
:is-visible.sync="dialogVisible"
@closeDialog="handleClose"
@confirmDialog="handleConfirm">
<div class="tip-text">
已选择{{ selectedPackageCount }}个组合套餐,{{ totalGoodsCount }}个商品,涉及到{{ relatedStoreCount }}家门店
</div>
<div class="tip-sub-text">
统一设置所有商品的门店管控模式
</div>
<el-table max-height="400"
style="width: 100%; margin-top: 20px;"
border
:data="tableData"
:span-method="objectSpanMethod">
<el-table-column align="center"
label="序号"
type="index"
width="60">
<template #default="scope">
{{ getIndex(scope.$index) }}
</template>
</el-table-column>
<el-table-column align="center"
label="套餐名称"
min-width="120"
prop="packageName"
show-overflow-tooltip>
</el-table-column>
<el-table-column align="center"
label="商品名称"
min-width="120"
prop="upgradeGoodsName"
show-overflow-tooltip>
</el-table-column>
<el-table-column align="center"
label="商品规格"
min-width="100"
prop="specificationData"
show-overflow-tooltip>
</el-table-column>
<el-table-column align="center"
label="商品UID"
min-width="180"
prop="commodityCode"
show-overflow-tooltip>
</el-table-column>
<el-table-column align="center"
label="库存模式"
width="120">
<template #default="scope">
<el-select v-model="scope.row.inventoryType" size="small">
<el-option label="实物" :value="2"></el-option>
<el-option label="虚拟" :value="1"></el-option>
</el-select>
</template>
</el-table-column>
<el-table-column align="center"
label="应用范围"
width="180">
<template #default="scope">
<el-radio-group v-model="scope.row.bindAllMerchant" @change="handleScopeChange(scope.row)">
<el-radio :label="false">
指定门店
</el-radio>
<el-radio :label="true">
所有门店
</el-radio>
</el-radio-group>
</template>
</el-table-column>
<el-table-column align="center"
label="具体门店"
width="160">
<template #default="scope">
<el-select v-if="!scope.row.bindAllMerchant"
v-model="scope.row.merchantIdList"
placeholder="请选择"
size="small"
collapse-tags
filterable
multiple>
<el-option v-for="merchant in merchantList"
:key="merchant.merchantId"
:label="merchant.merchantName"
:value="merchant.merchantId">
</el-option>
</el-select>
<span v-else>-</span>
</template>
</el-table-column>
</el-table>
</d2-dialog>
</template>
<script>
import { batchEditInventoryTypeByPackageId, getGoodsByPackageIdList, queryRelatedMerchantByPackageIdList } from "@/api/packageManage";
export default {
name: "BatchModifyPackageInventoryDialog",
props: {
visible: {
type: Boolean,
default: false,
},
selectedData: {
type: Array,
default: () => [],
},
},
data() {
return {
dialogVisible: false,
tableData: [],
merchantList: [],
spanArr: [], // 用于合并单元格
pos: 0,
};
},
computed: {
selectedPackageCount() {
return this.selectedData.length;
},
totalGoodsCount() {
return this.tableData.length;
},
relatedStoreCount() {
return this.merchantList.length;
},
},
watch: {
visible(val) {
this.dialogVisible = val;
if (val) {
this.tableData = [];
this.merchantList = [];
this.spanArr = [];
this.pos = 0;
this.fetchData();
}
},
dialogVisible(val) {
this.$emit("update:visible", val);
},
},
methods: {
getIndex(index) {
// 当有合并单元格时,计算当前行在 spanArr 中是第几个非0项,从而得出正确的顺序序号
let count = 0;
for (let i = 0; i <= index; i++) {
if (this.spanArr[i] > 0) {
count++;
}
}
return count;
},
async fetchData() {
const idList = this.selectedData.map(item => item.id);
if (idList.length === 0) {
return;
}
try {
const [merchantRes, goodsRes] = await Promise.all([
queryRelatedMerchantByPackageIdList({ idList }),
getGoodsByPackageIdList({ idList }),
]);
this.merchantList = merchantRes.merchantList || [];
const tableData = [];
(goodsRes || []).forEach((pkg) => {
if (pkg.goodsList && pkg.goodsList.length > 0) {
pkg.goodsList.forEach((goods) => {
tableData.push({
packageId: pkg.packageId,
packageName: pkg.packageName,
merchantGoodsId: goods.merchantGoodsId,
upgradeGoodsId: goods.upgradeGoodsId,
upgradeGoodsName: goods.upgradeGoodsName,
specificationData: goods.specificationData,
commodityCode: goods.commodityCode,
inventoryType: "", // 默认实物
bindAllMerchant: false, // 默认指定门店
merchantIdList: [],
});
});
}
});
this.tableData = tableData;
this.getSpanArr(this.tableData);
} catch (error) {
console.error(error);
}
},
getSpanArr(data) {
console.log(data, "9999");
this.spanArr = [];
this.pos = 0;
for (let i = 0; i < data.length; i++) {
if (i === 0) {
this.spanArr.push(1);
this.pos = 0;
} else {
// 判断当前元素与上一个元素是否属于同一个套餐
if (data[i].packageId === data[i - 1].packageId) {
this.spanArr[this.pos] += 1;
this.spanArr.push(0);
} else {
this.spanArr.push(1);
this.pos = i;
}
}
}
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
// 合并序号(0)和套餐名称(1)列
if (columnIndex === 0 || columnIndex === 1) {
const _row = this.spanArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
return {
rowspan: _row,
colspan: _col,
};
}
},
handleScopeChange(row) {
if (row.bindAllMerchant) {
row.merchantIdList = [];
}
},
handleClose() {
this.dialogVisible = false;
this.$emit("close");
},
handleConfirm() {
// 校验
for (let i = 0; i < this.tableData.length; i++) {
const row = this.tableData[i];
if (!row.inventoryType) {
this.$message.warning(`请选择 ${row.upgradeGoodsName} 的库存模式`);
return;
}
if (row.bindAllMerchant === false && (!row.merchantIdList || row.merchantIdList.length === 0)) {
this.$message.warning(`请选择 ${row.upgradeGoodsName} 的具体门店`);
return;
}
}
// 组装数据
const packageMap = {};
this.tableData.forEach((row) => {
if (!packageMap[row.packageId]) {
packageMap[row.packageId] = {
packageId: row.packageId,
goodsList: [],
};
}
packageMap[row.packageId].goodsList.push({
merchantGoodsId: row.merchantGoodsId,
upgradeGoodsId: row.upgradeGoodsId,
inventoryType: row.inventoryType,
bindAllMerchant: row.bindAllMerchant,
merchantIdList: row.bindAllMerchant ? [] : row.merchantIdList,
});
});
const packageList = Object.values(packageMap);
batchEditInventoryTypeByPackageId({ packageList }).then((res) => {
if (res) {
this.$message.success("批量修改库存模式成功");
this.dialogVisible = false;
this.$emit("confirm");
}
});
},
},
};
</script>
<style lang="scss" scoped>
.batch-modify-package-inventory-dialog {
.tip-text {
font-size: 14px;
color: #333;
margin-bottom: 8px;
padding-left: 10px;
border-left: 3px solid #1890ff;
}
.tip-sub-text {
font-size: 14px;
color: #666;
margin-bottom: 10px;
margin-left: 13px;
}
::v-deep .el-radio {
margin-right: 10px;
}
::v-deep .el-radio__label {
padding-left: 5px;
}
}
</style>