refactor: 代码生成生成信息表单
parent
54152d65f2
commit
01a4c9dfe1
|
|
@ -243,8 +243,6 @@ export function useTreeTableFormSchema(columns: InfraCodegenApi.CodegenColumn[]
|
|||
export function useSubTableFormSchema(
|
||||
columns: InfraCodegenApi.CodegenColumn[] = [],
|
||||
tables: InfraCodegenApi.CodegenTable[] = [],
|
||||
subColumns: InfraCodegenApi.CodegenColumn[] = [],
|
||||
onSubTableNameSelected: (value: string) => void,
|
||||
): VbenFormSchema[] {
|
||||
return [
|
||||
{
|
||||
|
|
@ -258,87 +256,55 @@ export function useSubTableFormSchema(
|
|||
},
|
||||
formItemClass: 'md:col-span-2',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'subJoinColumnId',
|
||||
label: '关联子表的成员变量名',
|
||||
help: '子表的成员变量名,默认为子表的名称',
|
||||
componentProps: {
|
||||
placeholder: '请输入',
|
||||
},
|
||||
formItemClass: 'md:col-span-2',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
fieldName: 'subTableName',
|
||||
label: '子表的表名',
|
||||
help: '关联的子表的表名, 如:sys_user_post',
|
||||
fieldName: 'masterTableId',
|
||||
label: '关联的主表',
|
||||
help: '关联主表(父表)的表名, 如:system_user',
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
allowClear: true,
|
||||
placeholder: '请选择',
|
||||
options: tables.map((table) => ({
|
||||
label: `${table.tableName}:${table.tableComment}`,
|
||||
value: table.tableName,
|
||||
})),
|
||||
onChange: onSubTableNameSelected,
|
||||
},
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'subTableClassName',
|
||||
label: '子表的类名',
|
||||
help: '子表的类名, 如:SysUserPost',
|
||||
componentProps: {
|
||||
placeholder: '请输入',
|
||||
},
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
fieldName: 'subTableFkColumnId',
|
||||
label: '子表的外键字段',
|
||||
help: '子表的外键字段的编号,对应主表的主键',
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
allowClear: true,
|
||||
placeholder: '请选择',
|
||||
options: subColumns.map((column) => ({
|
||||
label: column.columnName,
|
||||
value: column.id,
|
||||
value: table.id,
|
||||
})),
|
||||
},
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'mainTableClassName',
|
||||
label: '主表的类名',
|
||||
help: '主表的类名, 如:SysUser',
|
||||
componentProps: {
|
||||
placeholder: '请输入',
|
||||
},
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
fieldName: 'mainTableFKColumnId',
|
||||
label: '主表的外键字段',
|
||||
help: '子表的外键关联到主表的主键字段编号',
|
||||
fieldName: 'subJoinColumnId',
|
||||
label: '子表关联的字段',
|
||||
help: '子表关联的字段, 如:user_id',
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
allowClear: true,
|
||||
placeholder: '请选择',
|
||||
options: columns.map((column) => ({
|
||||
label: column.columnName,
|
||||
label: `${column.columnName}:${column.columnComment}`,
|
||||
value: column.id,
|
||||
})),
|
||||
},
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'subTableClassComment',
|
||||
label: '子表的类描述',
|
||||
help: '子表的描述,例如 用户岗位',
|
||||
component: 'RadioGroup',
|
||||
fieldName: 'subJoinMany',
|
||||
label: '关联关系',
|
||||
help: '主表与子表的关联关系',
|
||||
componentProps: {
|
||||
placeholder: '请输入',
|
||||
class: 'w-full',
|
||||
allowClear: true,
|
||||
placeholder: '请选择',
|
||||
options: [
|
||||
{
|
||||
label: '一对多',
|
||||
value: true,
|
||||
},
|
||||
{
|
||||
label: '一对一',
|
||||
value: 'false',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ const props = defineProps<{
|
|||
table?: InfraCodegenApi.CodegenTable;
|
||||
}>();
|
||||
|
||||
const subColumns = ref<InfraCodegenApi.CodegenColumn[]>([]);
|
||||
const tables = ref<InfraCodegenApi.CodegenTable[]>([]);
|
||||
const menus = ref<SystemMenuApi.SystemMenu[]>([]);
|
||||
const currentTemplateType = ref<number>();
|
||||
|
|
@ -56,48 +55,21 @@ const [SubForm, subFormApi] = useVbenForm({
|
|||
schema: [],
|
||||
});
|
||||
|
||||
/** 子表名称选中事件 */
|
||||
async function subTableNameSelected(value: string): Promise<void> {
|
||||
// 使用表ID获取子表的字段
|
||||
const subTableColumns = await getSubCodegenColumns(props.table?.dataSourceConfigId, value);
|
||||
subColumns.value = subTableColumns;
|
||||
// 重置已经选择的子表字段
|
||||
await subFormApi.setFieldValue('subTableFkColumnId', undefined);
|
||||
await subFormApi.setFieldValue('mainTableFKColumnId', undefined);
|
||||
}
|
||||
|
||||
/** 获取子表的列 */
|
||||
async function getSubCodegenColumns(
|
||||
dataSourceConfigId: number | undefined,
|
||||
tableName: string,
|
||||
): Promise<InfraCodegenApi.CodegenColumn[]> {
|
||||
if (!dataSourceConfigId || !tableName) return [];
|
||||
|
||||
const tableList = await getCodegenTableList(dataSourceConfigId);
|
||||
const subTable = tableList.find((item) => item.tableName === tableName);
|
||||
if (subTable) {
|
||||
// 这里需要实际API来获取子表的列信息
|
||||
// 由于没有完整接口信息,这里仅模拟返回
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/** 更新基础表单schema */
|
||||
/** 更新基础表单 schema */
|
||||
function updateBaseSchema(): void {
|
||||
const schema = useGenerationInfoBaseFormSchema(menus.value);
|
||||
baseFormApi.setState({ schema });
|
||||
}
|
||||
|
||||
/** 更新树表信息表单schema */
|
||||
/** 更新树表信息表单 schema */
|
||||
function updateTreeSchema(): void {
|
||||
const schema = useTreeTableFormSchema(props.columns);
|
||||
treeFormApi.setState({ schema });
|
||||
}
|
||||
|
||||
/** 更新主子表信息表单schema */
|
||||
/** 更新主子表信息表单 schema */
|
||||
function updateSubSchema(): void {
|
||||
const schema = useSubTableFormSchema(props.columns, tables.value, subColumns.value, subTableNameSelected);
|
||||
const schema = useSubTableFormSchema(props.columns, tables.value);
|
||||
subFormApi.setState({ schema });
|
||||
}
|
||||
|
||||
|
|
@ -151,16 +123,18 @@ function setAllFormValues(values: Record<string, any>): void {
|
|||
/** 监听表格数据变化 */
|
||||
watch(
|
||||
() => props.table,
|
||||
(val) => {
|
||||
async (val) => {
|
||||
if (!val) return;
|
||||
|
||||
// 初始化所有表单的schema
|
||||
updateBaseSchema();
|
||||
// 初始化树表的schema
|
||||
updateTreeSchema();
|
||||
updateSubSchema();
|
||||
|
||||
// 设置表单值
|
||||
setAllFormValues(val);
|
||||
// 获取表数据,用于主子表选择
|
||||
if (typeof val.dataSourceConfigId !== undefined) {
|
||||
tables.value = await getCodegenTableList(val.dataSourceConfigId);
|
||||
// 初始化子表 schema
|
||||
updateSubSchema();
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
|
@ -169,21 +143,8 @@ watch(
|
|||
onMounted(async () => {
|
||||
// 获取菜单列表
|
||||
menus.value = await getSimpleMenusList();
|
||||
|
||||
// 获取表数据,用于主子表选择
|
||||
if (props.table?.dataSourceConfigId) {
|
||||
tables.value = await getCodegenTableList(props.table.dataSourceConfigId);
|
||||
}
|
||||
|
||||
// 初始化所有表单的schema
|
||||
// 初始基础信息 schema
|
||||
updateBaseSchema();
|
||||
updateTreeSchema();
|
||||
updateSubSchema();
|
||||
|
||||
// 如果有初始数据,设置表单值
|
||||
if (props.table) {
|
||||
setAllFormValues(props.table);
|
||||
}
|
||||
});
|
||||
|
||||
/** 暴露出表单校验方法和表单值获取方法 */
|
||||
|
|
|
|||
Loading…
Reference in New Issue