reconciliationShared.js
11.1 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { deepClone } from "@/common/util";
import { uniqueBy } from "./arrayUniqueBy";
import { getUpgradeGoodsRowKey } from "./groundingUpgradeList";
/**
* 根据类目树反查完整路径(三级类目信息填充)
* @param {Array} tree - 类目树形数据(productType)
* @param {Number} targetValue - 目标类目值(goodsType)
* @returns {Object|null} 包含完整路径信息的对象 { goodsType, goodsTypeName, secondaryGoodsType, secondaryGoodsTypeName, thirdLevelGoodsType, thirdLevelGoodsTypeName }
*/
export function findCategoryPathInfo(tree, targetValue) {
if (!tree || !Array.isArray(tree) || targetValue == null) {
return null;
}
// 递归查找目标节点,并记录路径信息
const result = _findNodePathWithInfo(tree, targetValue, []);
return result;
}
/**
* 递归查找目标节点,同时记录路径和节点信息
* @param {Array} nodes - 当前层级的节点列表
* @param {Number} targetValue - 目标类目值
* @param {Array} currentPath - 当前路径信息数组 [{ categoryType, categoryName }]
* @returns {Object|null} 包含完整路径信息的对象
*/
function _findNodePathWithInfo(nodes, targetValue, currentPath) {
if (!nodes || !nodes.length) {
return null;
}
for (const node of nodes) {
const newPath = [...currentPath, { categoryType: node.categoryType, categoryName: node.categoryName }];
// 优先递归查找子节点(确保找到最深层的匹配)
if (node.children && node.children.length) {
const childResult = _findNodePathWithInfo(node.children, targetValue, newPath);
if (childResult) {
return childResult; // 子节点找到匹配,返回子节点路径信息
}
}
// 子节点没有匹配,再检查当前节点
if (node.categoryType === targetValue) {
// 根据路径长度构建返回对象
const result = {};
if (newPath.length === 1) {
// 一级类目
result.goodsType = newPath[0].categoryType;
result.goodsTypeName = newPath[0].categoryName;
} else if (newPath.length === 2) {
// 二级类目
result.goodsType = newPath[0].categoryType;
result.goodsTypeName = newPath[0].categoryName;
result.secondaryGoodsType = newPath[1].categoryType;
result.secondaryGoodsTypeName = newPath[1].categoryName;
} else if (newPath.length === 3) {
// 三级类目
result.goodsType = newPath[0].categoryType;
result.goodsTypeName = newPath[0].categoryName;
result.secondaryGoodsType = newPath[1].categoryType;
result.secondaryGoodsTypeName = newPath[1].categoryName;
result.thirdLevelGoodsType = newPath[2].categoryType;
result.thirdLevelGoodsTypeName = newPath[2].categoryName;
}
return result;
}
}
return null;
}
/**
* 填充 goodsTypeGroupRules 中的 goodsTypes 三级类目信息
* @param {Array} goodsTypeGroupRules - 分账品类规则数组
* @param {Array} productType - 类目树形数据
*/
export function fillGoodsTypesCategoryInfo(goodsTypeGroupRules, productType, key) {
if (!Array.isArray(goodsTypeGroupRules) || !Array.isArray(productType)) {
return goodsTypeGroupRules;
}
goodsTypeGroupRules.forEach((rule) => {
if (!rule || !Array.isArray(rule[key || "goodsTypes"])) {
return;
}
rule[key || "goodsTypes"]?.forEach((item) => {
if (!item || item.goodsType == null) {
return;
}
// 根据最细一级类目值查找完整路径信息
const targetValue = item.thirdLevelGoodsType || item.secondaryGoodsType || item.goodsType;
const pathInfo = findCategoryPathInfo(productType, targetValue);
if (pathInfo) {
// 填充完整信息
item.goodsType = pathInfo.goodsType;
item.goodsTypeName = pathInfo.goodsTypeName;
if (pathInfo.secondaryGoodsType) {
item.secondaryGoodsType = pathInfo.secondaryGoodsType;
item.secondaryGoodsTypeName = pathInfo.secondaryGoodsTypeName;
}
if (pathInfo.thirdLevelGoodsType) {
item.thirdLevelGoodsType = pathInfo.thirdLevelGoodsType;
item.thirdLevelGoodsTypeName = pathInfo.thirdLevelGoodsTypeName;
}
}
});
});
return goodsTypeGroupRules;
}
/**
* 商户对账 / 财务分账共用:嘀加商品来源 + 特殊品类时补齐 goodsTypeGroupRelations。
*/
export function fillDjGoodsTypeGroupRelations(rule) {
if (!rule || typeof rule !== "object") {
return rule;
}
if (rule.goodsSource != 1 || rule.groupType != 2) {
return rule;
}
const existing = rule.goodsTypeGroupRelations;
if (Array.isArray(existing) && existing.length > 0) {
return rule;
}
let ct = rule.categoryType;
let list = [];
if (ct == 1 && rule.goodsTypes?.length) {
list = deepClone(rule.goodsTypes);
} else if (ct == 2 && rule.upgradeGoods?.length) {
list = deepClone(rule.upgradeGoods);
} else if (ct == 3 && rule.packageGoods?.length) {
list = deepClone(rule.packageGoods);
} else {
const g = rule.goodsTypes?.length;
const u = rule.upgradeGoods?.length;
const p = rule.packageGoods?.length;
if (g && !u && !p) {
ct = 1;
list = deepClone(rule.goodsTypes);
} else if (!g && u && !p) {
ct = 2;
list = deepClone(rule.upgradeGoods);
} else if (!g && !u && p) {
ct = 3;
list = deepClone(rule.packageGoods);
}
}
if (!list.length) {
return rule;
}
rule.categoryType = ct;
rule.goodsTypeGroupRelations = list.map(row => ({
...row,
categoryType:
row && row.categoryType != null && row.categoryType !== ""
? row.categoryType
: ct,
}));
return rule;
}
/** el-tabs 中排除「+」占位 pane */
export function filterDivisionTabPanes(list) {
return Array.isArray(list)
? list.filter(item => item && String(item.name) !== "add")
: [];
}
function accumulateFromReconciliationRule(rule, sinks) {
const { pushG, pushU, pushP } = sinks;
if (!rule || typeof rule !== "object") {
return;
}
const ct = rule.categoryType;
const rel = Array.isArray(rule.goodsTypeGroupRelations)
? rule.goodsTypeGroupRelations
: [];
const ug = Array.isArray(rule.upgradeGoods) ? rule.upgradeGoods : [];
const pRaw = rule.packageGoods;
const pg = Array.isArray(pRaw)
? pRaw
: pRaw && typeof pRaw === "object"
? [pRaw]
: [];
const gt = Array.isArray(rule.goodsTypes) ? rule.goodsTypes : [];
if (ct === 1) {
;(rel.length ? rel : gt).forEach(pushG);
} else if (ct === 2) {
;(ug.length ? ug : rel).forEach(pushU);
} else if (ct === 3) {
;(pg.length ? pg : rel).forEach(pushP);
} else {
if (ug.length) {
ug.forEach(pushU);
} else if (pg.length) {
pg.forEach(pushP);
} else if (rel.length) {
const inf = rel[0] && rel[0].categoryType;
if (inf === 2) {
rel.forEach(pushU);
} else if (inf === 3) {
rel.forEach(pushP);
} else {
rel.forEach(pushG);
}
} else if (gt.length) {
gt.forEach(pushG);
}
}
}
/**
* 从 goodsTypeGroupReconciliationRules + 可选占位 tab 快照聚合跨 Tab 已选品类/商品/套餐(对账编辑页)。
* 与 {@link rebuildFinancialCrossTabGoodsSelections} 字段与去重策略相近;合并为单实现前需对财务/对账两页做回归(禁用列表、phantom 等差异)。
*/
export function rebuildCrossTabReconciliationSelections(
rules,
{ includePhantom = false, phantomRule = null } = {},
) {
if (!Array.isArray(rules)) {
return {
goodsSelectList: [],
selectUpgradeGoodsList: [],
selectPackageGoodsList: [],
};
}
const goodsAgg = [];
const upgradeAgg = [];
const packageAgg = [];
const pushG = (item) => {
if (!item || item.goodsType == null) {
return;
}
goodsAgg.push(deepClone(item));
};
const pushU = (item) => {
if (!item) {
return;
}
const k = getUpgradeGoodsRowKey(item);
if (!k) {
return;
}
upgradeAgg.push(deepClone(item));
};
const pushP = (item) => {
if (!item || item.packageCode == null) {
return;
}
packageAgg.push(deepClone(item));
};
const sinks = { pushG, pushU, pushP };
for (let i = 0; i < rules.length; i++) {
accumulateFromReconciliationRule(rules[i], sinks);
}
if (
includePhantom
&& phantomRule
&& typeof phantomRule === "object"
) {
accumulateFromReconciliationRule(phantomRule, sinks);
}
// 使用最细一级类目值做唯一性判断(与 classificationCpt 禁用逻辑一致)
const getFinestValue = (item) => {
if (item && item.thirdLevelGoodsType) {
return item.thirdLevelGoodsType;
}
if (item && item.secondaryGoodsType) {
return item.secondaryGoodsType;
}
return item ? item.goodsType : null;
};
return {
goodsSelectList: uniqueBy(goodsAgg, x => getFinestValue(x)),
selectUpgradeGoodsList: uniqueBy(
upgradeAgg,
x => getUpgradeGoodsRowKey(x),
),
selectPackageGoodsList: uniqueBy(
packageAgg,
x => x && x.packageCode,
),
};
}
/**
* 财务分账页:从 goodsTypeGroupRules 聚合跨 Tab 已选(浅拷贝条目,与 classificationCpt 禁用列表语义一致)。
* @see rebuildCrossTabReconciliationSelections — 对账侧聚合;两函数刻意分叉,合并前需专项测试。
*/
export function rebuildFinancialCrossTabGoodsSelections(rules) {
if (!Array.isArray(rules) || !rules.length) {
return {
goodsSelectList: [],
selectUpgradeGoodsList: [],
selectPackageGoodsList: [],
};
}
const hashG = {};
const hashU = {};
const hashP = {};
const goodsAgg = [];
const upgradeAgg = [];
const packageAgg = [];
// 使用最细一级类目值做唯一性判断(与 classificationCpt 禁用逻辑一致)
const getFinestValue = (item) => {
if (item && item.thirdLevelGoodsType) {
return item.thirdLevelGoodsType;
}
if (item && item.secondaryGoodsType) {
return item.secondaryGoodsType;
}
return item ? item.goodsType : null;
};
const pushG = (item) => {
if (!item) {
return;
}
const k = getFinestValue(item);
if (!k) {
return;
}
if (!hashG[k]) {
hashG[k] = true;
goodsAgg.push(typeof item === "object" ? { ...item } : item);
}
};
const pushU = (item) => {
if (!item) {
return;
}
const k = getUpgradeGoodsRowKey(item);
if (!k) {
return;
}
if (!hashU[k]) {
hashU[k] = true;
upgradeAgg.push(typeof item === "object" ? { ...item } : item);
}
};
const pushP = (item) => {
if (!item || item.packageCode == null) {
return;
}
const k = item.packageCode;
if (!hashP[k]) {
hashP[k] = true;
packageAgg.push(typeof item === "object" ? { ...item } : item);
}
};
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
if (!rule || typeof rule !== "object") {
continue;
}
let g = Array.isArray(rule.goodsTypes) ? rule.goodsTypes : [];
let u = Array.isArray(rule.upgradeGoods) ? rule.upgradeGoods : [];
const pRaw = rule.packageGoods;
let pList = [];
if (Array.isArray(pRaw)) {
pList = pRaw;
} else if (pRaw && typeof pRaw === "object") {
pList = [pRaw];
}
const rel = rule.goodsTypeGroupRelations;
if (
Array.isArray(rel)
&& rel.length
&& !g.length
&& !u.length
&& !pList.length
) {
const inferred = rel[0] && rel[0].categoryType;
const ct = inferred != null ? inferred : rule.categoryType;
if (ct === 2) {
u = rel;
} else if (ct === 3) {
pList = rel;
} else {
g = rel;
}
}
g.forEach(pushG);
u.forEach(pushU);
pList.forEach(pushP);
}
return {
goodsSelectList: goodsAgg,
selectUpgradeGoodsList: upgradeAgg,
selectPackageGoodsList: packageAgg,
};
}