car-brand-merchant-tree.js
2.14 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
/**
* 解析门店主营品牌树 — @see packageDynamicForm/docs/ARCHITECTURE.md
*/
function resolveMerchantRecords(payload) {
if (Array.isArray(payload)) {
return payload
}
if (payload && Array.isArray(payload.data)) {
return payload.data
}
return []
}
/** 从商户接口响应解析 carBrandList 树 */
export function resolveMerchantCarBrandTree(response) {
if (!response) return []
const payload = response.data !== undefined ? response.data : response
const records = resolveMerchantRecords(payload)
if (!records.length || !records[0]?.carBrandList) {
return []
}
const list = records[0].carBrandList
if (!Array.isArray(list)) return []
return list.filter(item => item && (item.name != null || item.id != null))
}
/** carBrandList 各 letter 节点下 childs 展平为子品牌列表 */
export function flattenMerchantCarBrandChilds(carBrandList = []) {
const result = []
;(carBrandList || []).forEach(group => {
;(group?.childs || []).forEach(child => {
if (!child) return
const brandId = child.brandId > 0 ? child.brandId : child.id
result.push({
...child,
brandId,
firstBrandId: group.id ?? group.brandId ?? null
})
})
})
return result
}
function resolveMerchantBrandNodeId(node = {}) {
if (Number(node.brandId) > 0) {
return Number(node.brandId)
}
if (Number(node.id) > 0) {
return Number(node.id)
}
return null
}
/**
* 解析用于表单显隐的门店主营品牌 ID。
* 有子品牌时只使用子品牌 ID,避免把集合型一级品牌误判为“其他品牌”。
*/
export function resolveMerchantOperatingBrandIds(carBrandList = []) {
const ids = new Map()
;(Array.isArray(carBrandList) ? carBrandList : []).forEach(parent => {
const childIds = (Array.isArray(parent?.childs) ? parent.childs : [])
.map(resolveMerchantBrandNodeId)
.filter(id => id != null && id !== '')
if (childIds.length) {
childIds.forEach(id => ids.set(String(id), ids.get(String(id)) ?? id))
return
}
const parentId = resolveMerchantBrandNodeId(parent)
if (parentId != null && parentId !== '') {
ids.set(String(parentId), ids.get(String(parentId)) ?? parentId)
}
})
return [...ids.values()]
}