orderMerchantContext.helpers.js
3.3 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
export const LOW_PRICE_FLAG = 1
const CHILD_LIST_KEYS = [
'shopMerchantList',
'shopList',
'shopWithYqList',
'merchantList',
'storeList',
'shopMerchantInfoList',
'children'
]
function firstDefined(...values) {
return values.find(value => value !== undefined && value !== null && value !== '')
}
function unwrapList(value) {
if (Array.isArray(value)) return value
if (!value || typeof value !== 'object') return []
if (value.data !== undefined) return unwrapList(value.data)
for (const key of ['list', 'records', 'rows', 'result']) {
if (Array.isArray(value[key])) return value[key]
}
return []
}
function childListOf(item) {
for (const key of CHILD_LIST_KEYS) {
if (Array.isArray(item?.[key])) return item[key]
}
return []
}
function toOption(item, parent) {
const hasExplicitShop = item?.shopMerchantId !== undefined
const merchantId = firstDefined(
item?.shopMerchantId,
item?.merchantId,
item?.shopId,
item?.id
)
const merchantName = firstDefined(
item?.shopMerchantName,
item?.merchantName,
item?.shopName,
item?.name,
''
)
const yqMerchantId = firstDefined(
item?.yqMerchantId,
item?.sellerMerchantId,
item?.parkMerchantId,
parent?.yqMerchantId,
parent?.sellerMerchantId,
parent?.parkMerchantId,
parent?.merchantId,
hasExplicitShop ? item?.merchantId : undefined,
merchantId
)
const yqMerchantName = firstDefined(
item?.yqMerchantName,
item?.sellerMerchantName,
item?.parkMerchantName,
parent?.yqMerchantName,
parent?.sellerMerchantName,
parent?.parkMerchantName,
parent?.merchantName,
hasExplicitShop ? item?.merchantName : undefined,
merchantName
)
if (merchantId === undefined || merchantId === null || merchantId === '') {
return null
}
return {
merchantId,
merchantName,
yqMerchantId,
yqMerchantName,
key: `${yqMerchantId}:${merchantId}`
}
}
/**
* 将 shopWithYqList 的园区/4S 店混合返回结构统一为可选 4S 店。
*/
export function normalizeOrderMerchantOptions(payload) {
const result = []
const seen = new Set()
unwrapList(payload).forEach(parent => {
const children = childListOf(parent)
const rows = children.length ? children : [parent]
rows.forEach(row => {
const option = toOption(row, children.length ? parent : null)
if (!option || seen.has(option.key)) return
seen.add(option.key)
result.push(option)
})
})
return result
}
export function filterOrderMerchantOptions(options, keyword) {
const normalizedKeyword = String(keyword || '').trim().toLowerCase()
if (!normalizedKeyword) return [...(options || [])]
return (options || []).filter(item =>
`${item.merchantName || ''} ${item.yqMerchantName || ''}`
.toLowerCase()
.includes(normalizedKeyword)
)
}
export function buildLowPriceMerchantContext(option = {}) {
const yqMerchantId = firstDefined(
option.yqMerchantId,
option.sellerMerchantId
)
const yqMerchantName = firstDefined(
option.yqMerchantName,
option.sellerMerchantName,
''
)
return {
lowPriceFlag: LOW_PRICE_FLAG,
merchantId: option.merchantId,
merchantName: option.merchantName || '',
yqMerchantId,
yqMerchantName,
sellerMerchantId: yqMerchantId,
sellerMerchantName: yqMerchantName
}
}