刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
import { cloneDeep } from 'lodash-es';
 
export const BATCH_ROWS_FIELD = '__batchRows';
 
const layoutComponents = new Set(['card', 'collapse', 'divider', 'groupTitle', 'row', 'steps', 'tab', 'tableGrid', 'text']);
 
function collectFields(fields: any[], result: any[], state: { complex: boolean }) {
  for (const source of fields || []) {
    const config = source?.__config__;
    if (!config) continue;
    if (source.__vModel__ && !layoutComponents.has(config.jnpfKey)) {
      result.push(cloneDeep(source));
      if (config.jnpfKey === 'table') state.complex = true;
      continue;
    }
    if (Array.isArray(config.children)) {
      state.complex = true;
      collectFields(config.children, result, state);
    }
  }
}
 
function prefixRelationField(value: any, fieldNames: Set<string>) {
  if (typeof value !== 'string' || !fieldNames.has(value)) return value;
  return `${BATCH_ROWS_FIELD}-${value}`;
}
 
function adaptField(field: any, fieldNames: Set<string>, overrideMap: Map<string, any>) {
  const config = field.__config__ || {};
  const override = overrideMap.get(config.formId) || {};
  config.isSubTable = true;
  config.parentVModel = BATCH_ROWS_FIELD;
  config.columnWidth = override.width || config.columnWidth || 180;
  if (override.visible === false) config.noShow = true;
  if (override.visible === true) config.noShow = false;
  if (override.readonly === true) field.disabled = true;
 
  field.relationField = prefixRelationField(field.relationField, fieldNames);
  config.relationField = prefixRelationField(config.relationField, fieldNames);
  config.startRelationField = prefixRelationField(config.startRelationField, fieldNames);
  config.endRelationField = prefixRelationField(config.endRelationField, fieldNames);
  for (const key of ['templateJson', 'propsTemplateJson']) {
    const list = field[key] || config[key];
    if (!Array.isArray(list)) continue;
    for (const item of list) item.relationField = prefixRelationField(item.relationField, fieldNames);
  }
  return field;
}
 
export function createBatchFormConf(sourceFormConf: any, rows: any[], mode: 'create' | 'edit', onRowAdd?: (row: any, rowIndex: number) => void) {
  const source = cloneDeep(sourceFormConf || {});
  const fields: any[] = [];
  const state = { complex: false };
  collectFields(source.fields, fields, state);
  const batchConfig = source.batchConfig || {};
  const overrides = Array.isArray(batchConfig.fieldOverrides) ? batchConfig.fieldOverrides : [];
  const overrideMap = new Map<string, any>(overrides.map((item: any) => [item.formId, item]));
  const fieldNames = new Set<string>(fields.map((field) => field.__vModel__).filter(Boolean));
  const adaptedFields = fields
    .map((field) => adaptField(field, fieldNames, overrideMap))
    .sort((left, right) => {
      const leftOrder = overrideMap.get(left.__config__?.formId)?.order ?? Number.MAX_SAFE_INTEGER;
      const rightOrder = overrideMap.get(right.__config__?.formId)?.order ?? Number.MAX_SAFE_INTEGER;
      return leftOrder - rightOrder;
    });
  const requestedLayout = batchConfig.layout === 'list' ? 'list' : 'table';
  const layoutType = state.complex ? 'list' : requestedLayout;
  const maxRows = Math.min(Math.max(Number(batchConfig.maxRows) || 20, 1), 100);
 
  const table = {
    __vModel__: BATCH_ROWS_FIELD,
    __config__: {
      children: adaptedFields,
      defaultValue: cloneDeep(rows),
      formId: `batch-table-${Date.now()}`,
      isSubTable: false,
      jnpfKey: 'table',
      label: '批量数据',
      layout: 'rowFormItem',
      noShow: false,
      required: true,
      showLabel: false,
      showTitle: false,
      span: 24,
      tag: 'JnpfInputTable',
      visibility: ['pc'],
    },
    batchMainForm: true,
    batchOnRowAdd: onRowAdd,
    columnBtnsList: [
      { value: 'copy', label: '复制', show: mode === 'create' && batchConfig.allowCopy !== false, btnType: 'primary' },
      { value: 'remove', label: '删除', show: true, btnType: 'danger', showConfirm: true },
    ],
    defaultValue: cloneDeep(rows),
    disabled: false,
    footerBtnsList: [{ value: 'add', label: '新增', show: mode === 'create', btnType: 'primary', btnIcon: 'icon-ym icon-ym-btn-add' }],
    hasPage: layoutType === 'table',
    isNumLimit: true,
    layoutType,
    numLimit: maxRows,
    pageSize: Math.min(Number(batchConfig.pageSize) || 20, maxRows),
    showSummary: false,
    summaryField: [],
  };
 
  return {
    ...source,
    confirmBtnConfig: { ...source.confirmBtnConfig, biz_sign_enabled: false },
    fields: [table],
    formData: { [BATCH_ROWS_FIELD]: cloneDeep(rows) },
    funcs: { ...source.funcs, afterSubmit: '', beforeSubmit: '', customSubmit: false, onLoad: '' },
    hasReviewBtn: false,
  };
}