convertBackendToData.js
9.7 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
import { groupBy, isArray } from "lodash";
import { assessmentFactorOptions, paramConfigListTemplate } from "../components/performanceRuleSelection/options";
import { setBackendAllGoods, setBackendConditionGoods, setBackendGoods } from "./help";
export function convertBackendToData(data) {
// merchantBrandModelPageBrands merchantBrandRefList 接口返回的字段不一样
const { merchantBrandRefList, paramConfigList, performanceConfigRule, ...lists } = data;
const response = {
...lists,
...handlePerformanceConfigRule(performanceConfigRule),
merchantBrandRefList: dealInterfaceBrand(merchantBrandRefList),
paramConfigList: coverParamConfigList(paramConfigList),
};
return response;
}
function handlePerformanceConfigRule(configRule) {
if (!configRule) {
console.warn("后端数据缺少 performanceConfigRule");
return {};
}
const goodsConfigTypes = configRule.goodsConfigTypes.split(",");
if (goodsConfigTypes && goodsConfigTypes.length) {
goodsConfigTypes.forEach((item) => {
item = Number(item);
});
}
return {
assessmentFactorValue: configRule.assessmentFactors.split(",").map(Number),
commissionBases: configRule.commissionBases.split(","),
commissionTypes: configRule.commissionTypes.split(","),
goodsConfigTypes,
};
}
const templateType = {
1: "ParamsType1",
2: "ParamsType2",
};
const tableEnum = {
title: "",
templateType: "ParamsType1",
goodsCategory: 0,
ParamsType1Products: [
// { productLabel: [], productValue: [], optionList: [] },
],
ParamsType1: [],
ParamsType2CommissionTypes: undefined,
ParamsType2: [],
priceRangeProps: [],
assessmentFactorOptions: undefined,
sort: -1,
};
function getAssessmentFactorOptionsTitle(assessmentFactorValue) {
const item = assessmentFactorOptions.find(item => item.value === assessmentFactorValue);
return item;
}
// 转换 表格数据
export function coverParamConfigList(paramConfigList) {
// 通过assessmentFactor 区分表格数量
// 通过 rowId = String(startValue) + Staring(endValue) 区分行
const tableList = groupBy(paramConfigList, "assessmentFactor");
const _paramConfigList = [];
if (tableList && Object.keys(tableList).length) {
for (const tableIndex in tableList) {
const tableItem = tableList[tableIndex][0];
const table = tableList[tableIndex];
// 因为每一个table的数据是一样的,所以直接取0位
const baseTable = structuredClone(tableEnum);
const assessmentFactorOptions = getAssessmentFactorOptionsTitle(tableItem.assessmentFactor);
baseTable.title = assessmentFactorOptions?.label; // 找enum
baseTable.templateType = templateType[tableList[tableIndex][0].templateType];
baseTable.goodsCategory = tableList[tableIndex][0].goodsConfigType;
baseTable.assessmentFactorOptions = structuredClone(assessmentFactorOptions);
baseTable.sort = assessmentFactorOptions.sort;
if (baseTable.templateType === "ParamsType2") {
baseTable.ParamsType2CommissionTypes = tableList[tableIndex][0].commissionType;
}
const colTable = groupBy(table, item => String(item.productDefineId) + String(item.goodsType) + String(item.secondaryGoodsType) + String(item.thirdLevelGoodsType));
// 整理ParamsType1Products
if (baseTable.templateType === "ParamsType1") {
console.log("colTable", colTable);
Object.keys(colTable).forEach((colIndex, colEachIndex) => {
const firstColItem = colTable[colIndex][0];
// 整理 所选商品
const paramsTypeProductsItem = {
productLabel: [],
productValue: [],
optionList: [],
};
console.log("firstColItem", firstColItem);
if ([1, 2].includes(firstColItem.goodsConfigType)) {
firstColItem.productDefineId && paramsTypeProductsItem.productLabel.push(firstColItem.productDefineId);
} else if (firstColItem.goodsConfigType === -1) {
paramsTypeProductsItem.productLabel = [-1];
} else {
setBackendGoods(firstColItem, paramsTypeProductsItem);
}
// 整理数值
colTable[colIndex].forEach((item, index) => {
paramsTypeProductsItem.productValue.push(item?.commissionTypeValue);
});
// push 列
baseTable.ParamsType1Products.push(paramsTypeProductsItem);
});
}
if (baseTable.templateType === "ParamsType1") {
const rowTable = groupBy(tableList[tableIndex], item => String(item.startValue) + String(item.endValue));
Object.keys(rowTable).forEach((rowIndex, rowEachIndex) => {
const rowItem = rowTable[rowIndex][0];
baseTable.ParamsType1.push({
priceRange: [rowItem.startValue, rowItem.endValue],
commissionTypes: rowItem.commissionType,
});
});
}
if (baseTable.templateType === "ParamsType2") {
const rowTable = groupBy(tableList[tableIndex], item => String(item.startValue) + String(item.endValue) + String(item.productDefineId) + String(item.goodsType) + String(item.secondaryGoodsType) + String(item.thirdLevelGoodsType));
Object.keys(rowTable).forEach((rowIndex, rowEachIndex) => {
const rowItem = rowTable[rowIndex][0];
console.log("??????");
const rowItemParamConfig = rowItem.performanceParamConfigConditions;
const conditionType = rowItemParamConfig.length ? rowItemParamConfig[0].conditionType : 0;
const conditionKey = `conditionType${conditionType}`;
const paramsType2Item = {
// 先还原默认数据
...structuredClone(paramConfigListTemplate.ParamsType2[0]),
condition: conditionType,
priceRange: [rowItem.startValue, rowItem.endValue],
productLabel: [],
};
if (rowItem.goodsConfigType === -1) {
paramsType2Item.productLabel = [-1];
} else {
setBackendAllGoods(rowItem, paramsType2Item);
}
if (conditionType === 0) {
paramsType2Item.conditionType0 = rowItem.commissionTypeValue;
} else {
paramsType2Item[conditionKey] = {
triggerCondition: rowItem.commissionTypeValue,
productValue: [],
};
if (rowItem.goodsConfigType === -1) {
paramsType2Item[conditionKey].productValue = [-1];
} else {
setBackendConditionGoods(rowItemParamConfig[0], paramsType2Item[conditionKey], "productValue");
}
// 到这里 conditionType4 已经解决
if (conditionType === 3) {
paramsType2Item[conditionKey].increaseReward = rowItemParamConfig[0].conditionCommissionTypeValue;
}
if (conditionType === 2) {
paramsType2Item[conditionKey].deductRate = rowItemParamConfig[0].conditionEndValue;
paramsType2Item[conditionKey].perOrderDeductAmount = rowItemParamConfig[0].conditionCommissionTypeValue;
}
if (conditionType === 1) {
// 要先排序. 小的在前面
rowItemParamConfig.sort((a, b) => {
if (a.id == null)
return 1;
if (b.id == null)
return -1;
return a.id - b.id;
});
console.log("排序", rowItemParamConfig);
paramsType2Item[conditionKey].permeabilityMin = rowItemParamConfig[0].conditionStartValue;
paramsType2Item[conditionKey].permeabilityMax = rowItemParamConfig[0].conditionEndValue;
paramsType2Item[conditionKey].productValue = [];
paramsType2Item[conditionKey].productValue1 = [];
if (rowItem.goodsConfigType === -1) {
paramsType2Item[conditionKey].productValue = [-1];
paramsType2Item[conditionKey].productValue1 = [-1];
} else {
setBackendConditionGoods(rowItemParamConfig[0], paramsType2Item[conditionKey], "productValue");
setBackendConditionGoods(rowItemParamConfig[1], paramsType2Item[conditionKey], "productValue1");
}
paramsType2Item[conditionKey].permeabilityMin1 = rowItemParamConfig[1].conditionStartValue === null ? undefined : rowItemParamConfig[1].conditionStartValue;
paramsType2Item[conditionKey].permeabilityMax1 = rowItemParamConfig[1].conditionEndValue === null ? undefined : rowItemParamConfig[1].conditionEndValue;
}
}
// paramsType2Item = sortTable(paramsType2Item);
baseTable.ParamsType2.push(paramsType2Item);
});
}
if (baseTable.goodsCategory === -1) {
baseTable.goodsCategory = 0;
}
if (baseTable.templateType === "ParamsType2") {
const productTableList = groupBy(baseTable.ParamsType2, item => JSON.stringify(item.productLabel));
let sortedTable = [];
Object.keys(productTableList).forEach((productIndex, productTableKey) => {
sortedTable = [
...sortedTable,
...sortTable(productTableList[productIndex]),
];
});
baseTable.ParamsType2 = sortedTable;
}
if (baseTable.templateType === "ParamsType1") {
baseTable.ParamsType1 = sortTable(baseTable.ParamsType1);
}
// baseTable[baseTable.templateType] = sortTable(baseTable, baseTable.templateType);
// console.log("sortTable(baseTable, baseTable.templateType)", baseTable);
_paramConfigList.push(baseTable);
}
}
console.log("即将返回出去的 _paramConfigList", _paramConfigList);
return _paramConfigList;
}
function sortTable(range) {
// // 先根据 rangePath 排序
// table.sort((a, b) => {
// const aPrice = a[rangePath];
// const bPrice = b[rangePath];
// return aPrice - bPrice;
return range.sort((a, b) => {
// 确保 priceRange 存在且有值,否则默认给个 0
const priceA = (a.priceRange && a.priceRange[0]) || 0;
const priceB = (b.priceRange && b.priceRange[0]) || 0;
return priceA - priceB;
});
}
// 转换 门店数据
function dealInterfaceBrand(brandList) {
const map = new Map();
(brandList || []).forEach((item) => {
const key = item.merchantId;
if (!map.has(key)) {
map.set(key, {
merchantId: item.merchantId,
merchantName: item.merchantName,
checkBrands: [],
});
}
const group = map.get(key);
group.checkBrands.push({
brandId: item.brandId,
brandName: item.brandName,
childId: item.childId,
childName: item.childName,
});
});
return Array.from(map.values());
}