refactor: 代码生成生成信息表单

pull/69/head
puhui999 2025-04-09 12:51:40 +08:00
parent 54152d65f2
commit 01a4c9dfe1
2 changed files with 38 additions and 111 deletions

View File

@ -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',
},
],
},
},
];

View File

@ -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);
}
});
/** 暴露出表单校验方法和表单值获取方法 */