submit-adapter.js
17.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
* 提交与回显适配 — jsonContent / fieldDataList / subformDataList
* @see docs/features/packageDynamicForm/ARCHITECTURE.md
* @see docs/features/packageDynamicForm/group-repeater.md
*
* buildDynamicSavePayload:visibleFormList + normalized 字段列表
* parseDetailValues:jsonContent + fieldDataList + subformDataList → values;radio/select/multipleSelect 归一化
*/
import { getVisibleList, getVisibleLeafFields } from './visibility.js'
import { normalizeRadioValuesInMap } from './radio-value.js'
import { normalizeSelectValuesInMap, coerceSelectValue } from './select-value.js'
import { normalizeMultipleSelectValuesInMap, coerceMultipleSelectValue } from './multiple-select-value.js'
import { flattenCarBrandForSubmit } from './car-brand-value.js'
import {
getGroupRows,
mergeSubformDataListIntoValues,
mergeFieldDataListIntoValues
} from './group-values.js'
import { getSubformRowTemplate, getStaticGroupChildren } from './field-types.js'
import {
rowsToValueMaps,
extractSubformValuesFromField,
getRowTemplate,
valueMapsToRowFieldsList
} from './group-row-adapter.js'
function cloneJson(data) {
return JSON.parse(JSON.stringify(data))
}
function applyStaticGroupValues(field = {}, values = {}) {
const next = { ...field }
const children = getStaticGroupChildren(field).map(child => {
const nextChild = { ...child }
if (values[child.key] !== undefined) {
nextChild.value = values[child.key]
}
return nextChild
})
return { ...next, children }
}
function applyValuesToFormList(list = [], values = {}) {
return (list || []).map(field => {
if (field.type === 'subform') {
const nextGroup = { ...field }
const template = getRowTemplate(field)
const maps = getGroupRows(values, field.key)
if (maps.length && template.length) {
nextGroup.value = valueMapsToRowFieldsList(template, maps)
} else if (values[field.key] !== undefined) {
nextGroup.value = cloneJson(values[field.key])
}
return nextGroup
}
if (field.type === 'group') {
return applyStaticGroupValues(field, values)
}
const nextField = { ...field }
if (values[field.key] !== undefined) {
nextField.value = values[field.key]
}
return nextField
})
}
/** 将 values 回写到 schema list 各字段的 value 属性 */
export function mergeValuesIntoSchema(schemaRoot = {}, values = {}) {
const nextSchema = cloneJson(schemaRoot)
nextSchema.list = applyValuesToFormList(nextSchema.list || [], values)
return nextSchema
}
/** 构建带当前值的可见 formList(用于 jsonContent 提交) */
export function buildVisibleFormList(visibleList = [], values = {}) {
return applyValuesToFormList(cloneJson(visibleList), values)
}
/** 保存前收集 engine 最新 values(subform 以 groupRowsCache 为准与 UI 对齐) */
export function collectSaveValues(engine, visibleList = []) {
const values = { ...(engine.getValues?.() || {}) }
const cache = engine.getGroupRowsCache?.() || {}
;(visibleList || []).forEach(field => {
if (field.type !== 'subform') {
return
}
const rowFieldsList = cache[field.key]
if (Array.isArray(rowFieldsList) && rowFieldsList.length) {
values[field.key] = rowsToValueMaps(rowFieldsList)
}
})
return values
}
/** 从 formList 提取 values(含 subform 行与静态 group 子字段) */
export function extractValuesFromFormList(list = []) {
const values = {}
;(list || []).forEach(field => {
if (field.type === 'subform') {
const maps = extractSubformValuesFromField(field)
if (maps?.length) {
values[field.key] = maps
}
return
}
if (field.type === 'group') {
getStaticGroupChildren(field).forEach(child => {
if (
child.value !== undefined &&
child.value !== null &&
child.value !== ''
) {
values[child.key] = child.value
}
})
return
}
if (
field.value !== undefined &&
field.value !== null &&
field.value !== '' &&
field.type !== 'divider'
) {
values[field.key] =
field.type === 'multipleSelect'
? coerceMultipleSelectValue(field.value)
: field.type === 'select'
? coerceSelectValue(field.value)
: field.value
}
})
return values
}
function pushFieldDataItem(items, item) {
items.push(item)
}
/** schema 字段类型映射为后端存储类型(INT/DECIMAL/STRING/JSON 等) */
export function mapSchemaFieldToStorageType(field = {}) {
const type = field.type
if (type === 'numberInput') {
if (field.rules?.decimal) return 'DECIMAL'
return 'INT'
}
if (type === 'radio' || type === 'textarea' || type === 'select' || type === 'text') {
return 'STRING'
}
if (type === 'carBrandSelect') return 'JSON'
if (type === 'switch' || type === 'checkbox') return 'BOOL'
if (type === 'date') return 'DATE'
if (type === 'datetime') return 'DATETIME'
return 'STRING'
}
/** 按 storageType 序列化字段值为字符串 */
export function serializeFieldValueForStorage(value, storageType = 'STRING') {
if (value === undefined || value === null || value === '') {
return ''
}
if (storageType === 'JSON' || typeof value === 'object') {
return JSON.stringify(value)
}
return String(value)
}
/** select 强制标量 STRING;multipleSelect 强制 JSON 数组 */
function resolveStorageFieldValue(field = {}, fieldValue) {
if (field.type === 'select') {
const value = coerceSelectValue(fieldValue)
if (value === '' || value == null) {
return { value: undefined, storageType: 'STRING' }
}
return { value, storageType: 'STRING' }
}
if (field.type === 'multipleSelect') {
return {
value: coerceMultipleSelectValue(fieldValue),
storageType: 'JSON'
}
}
if (typeof fieldValue === 'object' && fieldValue !== null) {
return { value: fieldValue, storageType: 'JSON' }
}
return { value: fieldValue, storageType: mapSchemaFieldToStorageType(field) }
}
function buildNormalizedFieldDataEntry({
field,
fieldKey,
displayName,
fieldValue,
groupKey,
rowIndex
}) {
const { value: normalizedValue, storageType } = resolveStorageFieldValue(field, fieldValue)
if (normalizedValue === undefined || normalizedValue === null || normalizedValue === '') {
return null
}
const serialized = serializeFieldValueForStorage(normalizedValue, storageType)
if (serialized === '') return null
const entry = {
fieldKey,
displayName,
fieldName: displayName,
fieldType: storageType,
fieldValue: serialized
}
if (groupKey != null) entry.groupKey = groupKey
if (rowIndex != null) entry.rowIndex = rowIndex
return entry
}
/** 构建规范化 fieldDataList(固定字段 + subform 行字段,与驻店 F2-1 同样写入 fieldDataList) */
export function buildNormalizedFieldDataList(visibleList = [], values = {}) {
const items = []
;(visibleList || []).forEach(field => {
if (field.type === 'subform') {
return
}
if (field.type === 'group') {
getStaticGroupChildren(field).forEach(child => {
const fieldValue = values[child.key]
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return
}
if (child.type === 'carBrandSelect' && typeof fieldValue === 'object') {
flattenCarBrandForSubmit(child.key, fieldValue).forEach(item => {
const entry = buildNormalizedFieldDataEntry({
field: child,
fieldKey: item.fieldKey,
displayName: child.label,
fieldValue: item.fieldValue
})
if (entry) pushFieldDataItem(items, entry)
})
return
}
const entry = buildNormalizedFieldDataEntry({
field: child,
fieldKey: child.key,
displayName: child.label,
fieldValue
})
if (entry) pushFieldDataItem(items, entry)
})
return
}
if (field.type === 'divider') {
return
}
const fieldValue = values[field.key]
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return
}
if (field.type === 'carBrandSelect' && typeof fieldValue === 'object') {
flattenCarBrandForSubmit(field.key, fieldValue).forEach(item => {
const entry = buildNormalizedFieldDataEntry({
field,
fieldKey: item.fieldKey,
displayName: field.label,
fieldValue: item.fieldValue
})
if (entry) pushFieldDataItem(items, entry)
})
return
}
const entry = buildNormalizedFieldDataEntry({
field,
fieldKey: field.key,
displayName: field.label,
fieldValue
})
if (entry) pushFieldDataItem(items, entry)
})
appendNormalizedSubformFieldDataList(items, visibleList, values)
return items
}
/** subform 行内字段写入 fieldDataList(groupKey/rowIndex),与 subformDataList 双写便于回显 */
function appendNormalizedSubformFieldDataList(items, visibleList = [], values = {}) {
;(visibleList || []).forEach(field => {
if (field.type !== 'subform') {
return
}
const rows = getGroupRows(values, field.key)
const children = getSubformRowTemplate(field)
rows.forEach((row, rowIndex) => {
children.forEach(child => {
const fieldValue = row[child.key]
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return
}
if (child.type === 'carBrandSelect' && typeof fieldValue === 'object') {
flattenCarBrandForSubmit(child.key, fieldValue).forEach(item => {
const entry = buildNormalizedFieldDataEntry({
field: child,
fieldKey: item.fieldKey,
displayName: child.label,
fieldValue: item.fieldValue,
groupKey: field.key,
rowIndex
})
if (entry) pushFieldDataItem(items, entry)
})
return
}
const entry = buildNormalizedFieldDataEntry({
field: child,
fieldKey: child.key,
displayName: child.label,
fieldValue,
groupKey: field.key,
rowIndex
})
if (entry) pushFieldDataItem(items, entry)
})
})
})
}
function buildNormalizedSubformDataEntry({ parentField, rowIndex, child, fieldValue }) {
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return null
}
const { value: normalizedValue, storageType } = resolveStorageFieldValue(child, fieldValue)
if (normalizedValue === undefined || normalizedValue === null || normalizedValue === '') {
return null
}
const serialized = serializeFieldValueForStorage(normalizedValue, storageType)
if (serialized === '') return null
return {
parentFieldKey: parentField.key,
parentFieldName: parentField.label,
rowIndex,
columnFieldKey: child.key,
columnFieldName: child.label,
fieldType: storageType,
fieldValue: serialized
}
}
/** 构建规范化 subformDataList(subform 行内字段) */
export function buildNormalizedSubformDataList(visibleList = [], values = {}) {
const items = []
;(visibleList || []).forEach(field => {
if (field.type !== 'subform') {
return
}
const rows = getGroupRows(values, field.key)
const children = getSubformRowTemplate(field)
rows.forEach((row, rowIndex) => {
children.forEach(child => {
const fieldValue = row[child.key]
const entry = buildNormalizedSubformDataEntry({
parentField: field,
rowIndex,
child,
fieldValue
})
if (entry) pushFieldDataItem(items, entry)
})
})
})
return items
}
/** 构建原始 fieldDataList(含 groupKey/rowIndex,非规范化存储类型) */
export function buildFieldDataList(visibleList = [], values = {}) {
const items = []
;(visibleList || []).forEach(field => {
if (field.type === 'subform') {
const rows = getGroupRows(values, field.key)
const children = getSubformRowTemplate(field)
rows.forEach((row, rowIndex) => {
children.forEach(child => {
const fieldValue = row[child.key]
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return
}
if (child.type === 'carBrandSelect' && typeof fieldValue === 'object') {
flattenCarBrandForSubmit(child.key, fieldValue).forEach(item => {
pushFieldDataItem(items, {
groupKey: field.key,
rowIndex,
fieldKey: item.fieldKey,
fieldName: child.label,
fieldType: child.type,
fieldValue: item.fieldValue
})
})
return
}
pushFieldDataItem(items, {
groupKey: field.key,
rowIndex,
fieldKey: child.key,
fieldName: child.label,
fieldType: child.type,
fieldValue
})
})
})
return
}
if (field.type === 'group') {
getStaticGroupChildren(field).forEach(child => {
const fieldValue = values[child.key]
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return
}
if (child.type === 'carBrandSelect' && typeof fieldValue === 'object') {
flattenCarBrandForSubmit(child.key, fieldValue).forEach(item => {
pushFieldDataItem(items, {
fieldKey: item.fieldKey,
fieldName: child.label,
fieldType: child.type,
fieldValue: item.fieldValue
})
})
return
}
pushFieldDataItem(items, {
fieldKey: child.key,
fieldName: child.label,
fieldType: child.type,
fieldValue
})
})
return
}
if (field.type === 'divider') {
return
}
const fieldValue = values[field.key]
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return
}
if (field.type === 'carBrandSelect' && typeof fieldValue === 'object') {
flattenCarBrandForSubmit(field.key, fieldValue).forEach(item => {
pushFieldDataItem(items, {
fieldKey: item.fieldKey,
fieldName: field.label,
fieldType: field.type,
fieldValue: item.fieldValue
})
})
return
}
pushFieldDataItem(items, {
fieldKey: field.key,
fieldName: field.label,
fieldType: field.type,
fieldValue
})
})
return items
}
/** 组装动态表单保存 payload(jsonContent + fieldDataList + subformDataList) */
export function buildDynamicSavePayload({
reportTemplateId,
dailyReportRecordId,
engine,
reportDate,
normalizedFieldDataList = false
}) {
const visibleList = engine.getVisibleList()
let saveValues = collectSaveValues(engine, visibleList)
saveValues = normalizeSelectValuesInMap(visibleList, saveValues)
const visibleFormList = buildVisibleFormList(visibleList, saveValues)
const payload = {
reportTemplateId,
jsonContent: JSON.stringify(visibleFormList),
fieldDataList: normalizedFieldDataList
? buildNormalizedFieldDataList(visibleList, saveValues)
: buildFieldDataList(visibleList, saveValues),
subformDataList: normalizedFieldDataList
? buildNormalizedSubformDataList(visibleList, saveValues)
: []
}
if (dailyReportRecordId) {
payload.dailyReportRecordId = dailyReportRecordId
}
return payload
}
/** 将 fieldDataList 条目值按 fieldType 反序列化为 JS 值 */
export function coerceFieldDataListValue(item = {}) {
const { fieldValue, fieldType } = item
if (fieldValue === undefined || fieldValue === null || fieldValue === '') {
return undefined
}
if (fieldType === 'JSON' && typeof fieldValue === 'string') {
try {
return JSON.parse(fieldValue)
} catch (error) {
return fieldValue
}
}
if (fieldType === 'INT' || fieldType === 'DECIMAL') {
if (typeof fieldValue === 'string') {
const num = Number(fieldValue)
return Number.isNaN(num) ? fieldValue : num
}
}
if (fieldType === 'BOOL' && typeof fieldValue === 'string') {
if (fieldValue === 'true') return true
if (fieldValue === 'false') return false
}
return fieldValue
}
/** 从详情 jsonContent/fieldDataList/subformDataList 解析 values 并归一化 radio/select/multipleSelect */
export function parseDetailValues(detail = {}, schemaList = []) {
let values = {}
if (detail.jsonContent) {
try {
const parsed =
typeof detail.jsonContent === 'string'
? JSON.parse(detail.jsonContent)
: detail.jsonContent
if (Array.isArray(parsed)) {
values = extractValuesFromFormList(parsed)
}
} catch (error) {
console.error('parseDetailValues jsonContent error', error)
}
}
if (Array.isArray(detail.fieldDataList) && detail.fieldDataList.length) {
if (Array.isArray(schemaList) && schemaList.length) {
values = mergeFieldDataListIntoValues(schemaList, detail.fieldDataList, values)
} else {
values = mergeTopLevelFieldDataList(detail.fieldDataList, values)
}
}
if (Array.isArray(detail.subformDataList) && detail.subformDataList.length) {
values = mergeSubformDataListIntoValues(
schemaList,
detail.subformDataList,
values
)
}
if (Array.isArray(schemaList) && schemaList.length) {
values = normalizeRadioValuesInMap(schemaList, values)
values = normalizeSelectValuesInMap(schemaList, values)
values = normalizeMultipleSelectValuesInMap(schemaList, values)
}
return values
}
function mergeTopLevelFieldDataList(fieldDataList = [], values = {}) {
const next = { ...values }
fieldDataList.forEach(item => {
if (!item?.fieldKey || item.groupKey != null || item.rowIndex != null) {
return
}
const coerced = coerceFieldDataListValue(item)
if (coerced !== undefined) {
next[item.fieldKey] = coerced
}
})
return next
}
/** 从品牌列表提取 brandId 数组(含 -1 占位取 firstBrandId) */
export function mapBrandListToBrandIds(list = []) {
const brandIds = []
list.forEach(item => {
if (item.brandId > 0) {
brandIds.push(item.brandId)
} else if (item.brandId === -1 && item.firstBrandId) {
brandIds.push(item.firstBrandId)
}
})
return brandIds
}
export default {
buildFieldDataList,
buildNormalizedFieldDataList,
buildNormalizedSubformDataList,
buildDynamicSavePayload,
buildVisibleFormList,
collectSaveValues,
mergeValuesIntoSchema,
extractValuesFromFormList,
parseDetailValues,
mapBrandListToBrandIds,
mapSchemaFieldToStorageType,
serializeFieldValueForStorage,
coerceFieldDataListValue,
getVisibleLeafFields
}