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,
|
};
|
}
|