car-brand-value.js
6.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
/**
* 四级车型值模型 — @see packageDynamicForm/docs/ARCHITECTURE.md
*/
/** 待 App 联调:schema childKey -> 后端 fieldKey 列表 */
const CAR_BRAND_SUBMIT_KEY_MAP = {
// 示例: 'F3-1-6': ['vehicleBrandId', 'vehicleBrandName', ...]
}
/** 计划到店客户导入的三级值兼容元数据(不参与完整性校验) */
export const PARTIAL_FROM_PLAN_IMPORT_KEY = 'partialFromPlanImport'
const CAR_BRAND_FIELD_KEYS = [
'vehicleBrandId',
'vehicleBrandName',
'vehicleSubBrandId',
'vehicleSubBrandName',
'vehicleSeriesId',
'vehicleSeriesName',
'vehicleShapeId',
'vehicleShapeName'
]
/** 拼接品牌/子品牌/车系/车型名称为 displayText */
export function buildDisplayText(carInfo = {}) {
const parts = [
carInfo.vehicleBrandName,
carInfo.vehicleSubBrandName,
carInfo.vehicleSeriesName,
carInfo.vehicleShapeName
].filter(part => part != null && part !== '')
return parts.join('/')
}
/** 将原始值(字符串 JSON / 对象)归一化为 carInfo 对象 */
export function normalizeCarBrandValue(raw) {
if (raw == null || raw === '') {
return null
}
if (typeof raw === 'string') {
try {
const parsed = JSON.parse(raw)
if (parsed && typeof parsed === 'object') {
return normalizeCarBrandObject(parsed)
}
} catch (error) {
return {
displayText: raw
}
}
return { displayText: raw }
}
if (typeof raw === 'object') {
return normalizeCarBrandObject(raw)
}
return null
}
function normalizeCarBrandObject(obj = {}) {
if (Array.isArray(obj)) {
return cascadeLevelsToCarInfo(obj)
}
const next = {}
CAR_BRAND_FIELD_KEYS.forEach(key => {
if (obj[key] != null && obj[key] !== '') {
next[key] = obj[key]
}
})
if (obj.displayText) {
next.displayText = obj.displayText
} else {
next.displayText = buildDisplayText(next)
}
if (obj[PARTIAL_FROM_PLAN_IMPORT_KEY] === true) {
next[PARTIAL_FROM_PLAN_IMPORT_KEY] = true
}
return next
}
/** 从选车组件 pick 结果归一化为 carInfo 对象 */
export function normalizeCarInfoFromPick(carInfo = {}) {
const next = normalizeCarBrandValue(carInfo)
if (!next) return null
if (!next.displayText) {
next.displayText = buildDisplayText(next)
}
return next
}
/**
* 是否为计划到店导入的三级车型(未点编辑前可提交)
* @param {*} value
* @returns {boolean}
*/
export function hasPartialPlanImportMarker(value) {
return (
value != null &&
typeof value === 'object' &&
!Array.isArray(value) &&
value[PARTIAL_FROM_PLAN_IMPORT_KEY] === true
)
}
/**
* 清除导入兼容元数据
* @param {Object} [carInfo]
* @returns {Object|undefined|null}
*/
export function clearPartialPlanImportMarker(carInfo) {
if (!hasPartialPlanImportMarker(carInfo)) {
return carInfo
}
const next = { ...carInfo }
delete next[PARTIAL_FROM_PLAN_IMPORT_KEY]
return next
}
function applyPartialPlanImportMarkerIfPartial(carInfo) {
if (!carInfo) {
return carInfo
}
if (
carInfo.vehicleSeriesId != null &&
carInfo.vehicleSeriesId !== '' &&
(carInfo.vehicleShapeId == null || carInfo.vehicleShapeId === '')
) {
return { ...carInfo, [PARTIAL_FROM_PLAN_IMPORT_KEY]: true }
}
return carInfo
}
/**
* 详情回显:已保存的三级车型还原导入豁免标记(与计划到店导入一致)
* @param {*} raw carInfo 对象 / 四级 JSON 数组 / 字符串
* @returns {*}
*/
export function maybeRestoreCarBrandPlanImportMarker(raw) {
if (raw == null || raw === '') {
return raw
}
const normalized = normalizeCarBrandValue(raw)
if (!normalized) {
return raw
}
if (hasPartialPlanImportMarker(normalized)) {
return normalized
}
if (normalized.vehicleShapeId != null && normalized.vehicleShapeId !== '') {
return normalized
}
return applyPartialPlanImportMarkerIfPartial(normalized)
}
/**
* 判断车型值是否满足必填校验:车系层级即可完整提交。
* partialFromPlanImport 仅保留为旧数据兼容元数据,不作为校验门槛。
*/
export function isCarBrandComplete(value) {
const normalized = normalizeCarBrandValue(value)
return !!(
normalized &&
normalized.vehicleSeriesId != null &&
normalized.vehicleSeriesId !== ''
)
}
/**
* 校验/引擎取值:App 双 key 时优先 field[key] / 行 map,避免仅用 displayText 的 value
* @param {Object} [rowField] cache 中的 FormField
* @param {string} [childKey]
* @param {*} [rowMapValue] values[groupKey][rowIndex][childKey]
*/
export function resolveCarBrandValueForValidation(rowField, childKey, rowMapValue) {
const meta = rowField?.[childKey]
if (meta != null && meta !== '' && typeof meta === 'object') {
return meta
}
if (rowMapValue != null && rowMapValue !== '' && typeof rowMapValue === 'object') {
return rowMapValue
}
if (rowField?.value !== undefined) {
return rowField.value
}
return rowMapValue
}
const CASCADE_LEVEL_SPECS = [
{ idKey: 'vehicleBrandId', nameKey: 'vehicleBrandName' },
{ idKey: 'vehicleSubBrandId', nameKey: 'vehicleSubBrandName' },
{ idKey: 'vehicleSeriesId', nameKey: 'vehicleSeriesName' },
{ idKey: 'vehicleShapeId', nameKey: 'vehicleShapeName' }
]
/** flat carInfo → 四级 [{ id, name }, ...] */
export function carInfoToCascadeLevels(carInfo = {}) {
const normalized = normalizeCarBrandValue(carInfo)
if (!normalized) return []
return CASCADE_LEVEL_SPECS.map(({ idKey, nameKey }) => ({
id: normalized[idKey] != null ? String(normalized[idKey]) : '',
name: normalized[nameKey] != null ? String(normalized[nameKey]) : ''
})).filter(level => level.id !== '' || level.name !== '')
}
/** 四级 [{ id, name }, ...] → flat carInfo */
export function cascadeLevelsToCarInfo(levels = []) {
if (!Array.isArray(levels) || !levels.length) {
return null
}
const next = {}
CASCADE_LEVEL_SPECS.forEach(({ idKey, nameKey }, index) => {
const level = levels[index]
if (!level) return
const id = level.id ?? level.value
const name = level.name ?? level.label
if (id != null && id !== '') {
next[idKey] = String(id)
}
if (name != null && name !== '') {
next[nameKey] = String(name)
}
})
if (!Object.keys(next).length) {
return null
}
next.displayText = buildDisplayText(next)
return applyPartialPlanImportMarkerIfPartial(next)
}
/**
* fieldDataList 单条:四级数组 JSON
*/
export function flattenCarBrandForSubmit(childKey, value) {
const levels = carInfoToCascadeLevels(value)
if (!levels.length) return []
const mapKeys = CAR_BRAND_SUBMIT_KEY_MAP[childKey]
if (Array.isArray(mapKeys) && mapKeys.length) {
const normalized = normalizeCarBrandValue(value)
return mapKeys
.filter(key => normalized[key] != null && normalized[key] !== '')
.map(fieldKey => ({
fieldKey,
fieldValue: normalized[fieldKey]
}))
}
return [
{
fieldKey: childKey,
fieldValue: levels
}
]
}
export { CAR_BRAND_SUBMIT_KEY_MAP, CAR_BRAND_FIELD_KEYS }