From 01a4c9dfe105dde44ffcb5fd410433aee1d9d2a8 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Wed, 9 Apr 2025 12:51:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=94=9F=E6=88=90=E4=BF=A1=E6=81=AF=E8=A1=A8=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/views/infra/codegen/data.ts | 84 ++++++------------- .../infra/codegen/modules/generation-info.vue | 65 +++----------- 2 files changed, 38 insertions(+), 111 deletions(-) diff --git a/apps/web-antd/src/views/infra/codegen/data.ts b/apps/web-antd/src/views/infra/codegen/data.ts index 222542c01..d6f6bf3a8 100644 --- a/apps/web-antd/src/views/infra/codegen/data.ts +++ b/apps/web-antd/src/views/infra/codegen/data.ts @@ -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', + }, + ], }, }, ]; diff --git a/apps/web-antd/src/views/infra/codegen/modules/generation-info.vue b/apps/web-antd/src/views/infra/codegen/modules/generation-info.vue index d745a7ea2..408f1ffa4 100644 --- a/apps/web-antd/src/views/infra/codegen/modules/generation-info.vue +++ b/apps/web-antd/src/views/infra/codegen/modules/generation-info.vue @@ -17,7 +17,6 @@ const props = defineProps<{ table?: InfraCodegenApi.CodegenTable; }>(); -const subColumns = ref([]); const tables = ref([]); const menus = ref([]); const currentTemplateType = ref(); @@ -56,48 +55,21 @@ const [SubForm, subFormApi] = useVbenForm({ schema: [], }); -/** 子表名称选中事件 */ -async function subTableNameSelected(value: string): Promise { - // 使用表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 { - 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): 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); - } }); /** 暴露出表单校验方法和表单值获取方法 */