Commit b3a40af214aba8121795779c186c8e41de279b07

Authored by puhui999
1 parent 436eb29d

【代码优化】代码生成: vue3_vben5_antd schema 主子表模板优化

yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
... ... @@ -154,15 +154,15 @@ public class CodegenEngine {
154 154 .put(CodegenFrontTypeEnum.VUE3_VBEN5_ANTD_SCHEMA.getType(), vue3Vben5AntdSchemaTemplatePath("api/api.ts"),
155 155 vue3FilePath("api/${table.moduleName}/${table.businessName}/index.ts"))
156 156 .put(CodegenFrontTypeEnum.VUE3_VBEN5_ANTD_SCHEMA.getType(), vue3Vben5AntdSchemaTemplatePath("views/modules/form_sub_normal.vue"), // 特殊:主子表专属逻辑
157   - vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName}Form.vue"))
  157 + vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName_strikeCase}-form.vue"))
158 158 .put(CodegenFrontTypeEnum.VUE3_VBEN5_ANTD_SCHEMA.getType(), vue3Vben5AntdSchemaTemplatePath("views/modules/form_sub_inner.vue"), // 特殊:主子表专属逻辑
159   - vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName}Form.vue"))
  159 + vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName_strikeCase}-form.vue"))
160 160 .put(CodegenFrontTypeEnum.VUE3_VBEN5_ANTD_SCHEMA.getType(), vue3Vben5AntdSchemaTemplatePath("views/modules/form_sub_erp.vue"), // 特殊:主子表专属逻辑
161   - vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName}Form.vue"))
  161 + vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName_strikeCase}-form.vue"))
162 162 .put(CodegenFrontTypeEnum.VUE3_VBEN5_ANTD_SCHEMA.getType(), vue3Vben5AntdSchemaTemplatePath("views/modules/list_sub_inner.vue"), // 特殊:主子表专属逻辑
163   - vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName}List.vue"))
  163 + vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName_strikeCase}-list.vue"))
164 164 .put(CodegenFrontTypeEnum.VUE3_VBEN5_ANTD_SCHEMA.getType(), vue3Vben5AntdSchemaTemplatePath("views/modules/list_sub_erp.vue"), // 特殊:主子表专属逻辑
165   - vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName}List.vue"))
  165 + vue3FilePath("views/${table.moduleName}/${table.businessName}/modules/${subSimpleClassName_strikeCase}-list.vue"))
166 166 .build();
167 167  
168 168 @Resource
... ... @@ -451,6 +451,8 @@ public class CodegenEngine {
451 451 filePath = StrUtil.replace(filePath, "${subTable.className}", subTable.getClassName());
452 452 filePath = StrUtil.replace(filePath, "${subSimpleClassName}",
453 453 ((List<String>) bindingMap.get("subSimpleClassNames")).get(subIndex));
  454 + filePath = StrUtil.replace(filePath, "${subSimpleClassName_strikeCase}",
  455 + ((List<String>) bindingMap.get("subSimpleClassName_strikeCases")).get(subIndex));
454 456 }
455 457 return filePath;
456 458 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3_vben5_antd/schema/api/api.ts.vm
... ... @@ -23,6 +23,7 @@ export namespace ${simpleClassName}Api {
23 23 #end
24 24 #end
25 25 }
  26 +
26 27 #end
27 28 /** ${table.classComment}信息 */
28 29 export interface ${simpleClassName} {
... ... @@ -104,6 +105,7 @@ export function export${simpleClassName}(params: any) {
104 105 #set ($subClassNameVar = $subClassNameVars.get($index))
105 106  
106 107 // ==================== 子表($subTable.classComment) ====================
  108 +
107 109 ## 情况一:MASTER_ERP 时,需要分查询页子表
108 110 #if ( $table.templateType == 11 )
109 111 /** 获得${subTable.classComment}分页 */
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3_vben5_antd/schema/views/data.ts.vm
... ... @@ -88,7 +88,7 @@ export function useFormSchema(): VbenFormSchema[] {
88 88 maxCount: 1,
89 89 },
90 90 #elseif($column.htmlType == "editor")## 文本编辑器
91   - component: 'Editor',
  91 + component: 'RichTextarea',
92 92 #elseif($column.htmlType == "select")## 下拉框
93 93 component: 'Select',
94 94 componentProps: {
... ... @@ -279,6 +279,7 @@ export function useGridColumns(
279 279 #set ($subJoinColumn = $subJoinColumns.get($index))##当前 join 字段
280 280 #set ($subSimpleClassName_strikeCase = $subSimpleClassName_strikeCases.get($index))
281 281 // ==================== 子表($subTable.classComment) ====================
  282 +
282 283 #if ($table.templateType == 11) ## erp 情况
283 284 /** 新增/修改的表单 */
284 285 export function use${subSimpleClassName}FormSchema(): VbenFormSchema[] {
... ... @@ -331,7 +332,7 @@ export function use${subSimpleClassName}FormSchema(): VbenFormSchema[] {
331 332 maxCount: 1,
332 333 },
333 334 #elseif($column.htmlType == "editor")## 文本编辑器
334   - component: 'Editor',
  335 + component: 'RichTextarea',
335 336 #elseif($column.htmlType == "select")## 下拉框
336 337 component: 'Select',
337 338 componentProps: {
... ... @@ -391,6 +392,7 @@ export function use${subSimpleClassName}FormSchema(): VbenFormSchema[] {
391 392 #end
392 393 ];
393 394 }
  395 +
394 396 /** 列表的字段 */
395 397 export function use${subSimpleClassName}GridColumns(
396 398 onActionClick?: OnActionClickFn<${simpleClassName}Api.${subSimpleClassName}>,
... ... @@ -445,6 +447,7 @@ export function use${subSimpleClassName}GridColumns(
445 447 },
446 448 ];
447 449 }
  450 +
448 451 #else
449 452 #if ($subTable.subJoinMany) ## 一对多
450 453 /** 新增/修改列表的字段 */
... ... @@ -510,6 +513,7 @@ export function use${subSimpleClassName}GridColumns(
510 513 },
511 514 ];
512 515 }
  516 +
513 517 #else
514 518 /** 新增/修改的表单 */
515 519 export function use${subSimpleClassName}FormSchema(): VbenFormSchema[] {
... ... @@ -562,7 +566,7 @@ export function use${subSimpleClassName}GridColumns(
562 566 maxCount: 1,
563 567 },
564 568 #elseif($column.htmlType == "editor")## 文本编辑器
565   - component: 'Editor',
  569 + component: 'RichTextarea',
566 570 #elseif($column.htmlType == "select")## 下拉框
567 571 component: 'Select',
568 572 componentProps: {
... ... @@ -622,6 +626,7 @@ export function use${subSimpleClassName}GridColumns(
622 626 #end
623 627 ];
624 628 }
  629 +
625 630 #end
626 631 #if ($table.templateType == 12) ## 内嵌情况
627 632 /** 列表的字段 */
... ... @@ -649,6 +654,7 @@ export function use${subSimpleClassName}GridColumns(
649 654 #end
650 655 ];
651 656 }
  657 +
652 658 #end
653 659 #end
654 660 #end
655 661 \ No newline at end of file
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3_vben5_antd/schema/views/form.vue.vm
... ... @@ -6,7 +6,9 @@ import { message, Tabs, Checkbox, Input, Textarea, Select,RadioGroup,CheckboxGro
6 6 ## 特殊:主子表专属逻辑
7 7 #if ( $table.templateType == 10 || $table.templateType == 12 )
8 8 #foreach ($subSimpleClassName in $subSimpleClassNames)
9   - import ${subSimpleClassName}Form from './${subSimpleClassName}Form.vue'
  9 + #set ($index = $foreach.count - 1)
  10 + #set ($subSimpleClassName_strikeCase = $subSimpleClassName_strikeCases.get($index))
  11 + import ${subSimpleClassName}Form from './${subSimpleClassName_strikeCase}-form.vue'
10 12 #end
11 13 #end
12 14  
... ... @@ -125,9 +127,7 @@ const [Modal, modalApi] = useVbenModal({
125 127 if (!data) {
126 128 return;
127 129 }
128   -
129 130 if (data.id) {
130   - // 编辑
131 131 modalApi.lock();
132 132 try {
133 133 data = await get${simpleClassName}(data.id);
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3_vben5_antd/schema/views/index.vue.vm
... ... @@ -10,7 +10,9 @@ import Form from &#39;./modules/form.vue&#39;;
10 10 ## 特殊:主子表专属逻辑
11 11 #if ( $table.templateType == 11 || $table.templateType == 12 )
12 12 #foreach ($subSimpleClassName in $subSimpleClassNames)
13   - import ${subSimpleClassName}List from './modules/${subSimpleClassName}List.vue'
  13 + #set ($index = $foreach.count - 1)
  14 + #set ($subSimpleClassName_strikeCase = $subSimpleClassName_strikeCases.get($index))
  15 + import ${subSimpleClassName}List from './modules/${subSimpleClassName_strikeCase}-list.vue'
14 16 #end
15 17 #end
16 18  
... ... @@ -54,12 +56,6 @@ function onRefresh() {
54 56 #end
55 57 }
56 58  
57   -/** 导出表格 */
58   -async function onExport() {
59   - const data = await export${simpleClassName}(await gridApi.formApi.getValues());
60   - downloadByData(data, '${table.classComment}.xls');
61   -}
62   -
63 59 /** 创建${table.classComment} */
64 60 function onCreate() {
65 61 formModalApi.setData({}).open();
... ... @@ -96,6 +92,12 @@ async function onDelete(row: ${simpleClassName}Api.${simpleClassName}) {
96 92 }
97 93 }
98 94  
  95 +/** 导出表格 */
  96 +async function onExport() {
  97 + const data = await export${simpleClassName}(await gridApi.formApi.getValues());
  98 + downloadByData(data, '${table.classComment}.xls');
  99 +}
  100 +
99 101 /** 表格操作按钮的回调函数 */
100 102 function onActionClick({
101 103 code,
... ... @@ -108,14 +110,14 @@ function onActionClick({
108 110 break;
109 111 }
110 112 #end
111   - case 'delete': {
112   - onDelete(row);
113   - break;
114   - }
115 113 case 'edit': {
116 114 onEdit(row);
117 115 break;
118 116 }
  117 + case 'delete': {
  118 + onDelete(row);
  119 + break;
  120 + }
119 121 }
120 122 }
121 123  
... ... @@ -179,7 +181,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
179 181 #if ($table.templateType == 12) ## 内嵌情况
180 182 <template #expand_content="{ row }">
181 183 <!-- 子表的表单 -->
182   - <Tabs v-model:active-key="subTabsName">
  184 + <Tabs v-model:active-key="subTabsName" class="mx-8">
183 185 #foreach ($subTable in $subTables)
184 186 #set ($index = $foreach.count - 1)
185 187 #set ($subClassNameVar = $subClassNameVars.get($index))
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3_vben5_antd/schema/views/modules/form_sub_normal.vue.vm
... ... @@ -24,7 +24,7 @@ import { get${subSimpleClassName}By${SubJoinColumnName} } from &#39;#/api/${table.mo
24 24 #end
25 25  
26 26 const props = defineProps<{
27   - ${subJoinColumn.javaField}?: any // ${subJoinColumn.columnComment}(主表的关联字段)
  27 + ${subJoinColumn.javaField}?: number // ${subJoinColumn.columnComment}(主表的关联字段)
28 28 }>()
29 29  
30 30 #if ($subTable.subJoinMany) ## 一对多
... ... @@ -41,7 +41,7 @@ function onActionClick({
41 41 }
42 42 }
43 43  
44   -const [${subSimpleClassName}Grid, ${subClassNameVar}GridApi] = useVbenVxeGrid({
  44 +const [Grid, gridApi] = useVbenVxeGrid({
45 45 gridOptions: {
46 46 columns: use${subSimpleClassName}GridEditColumns(onActionClick),
47 47 border: true,
... ... @@ -60,34 +60,25 @@ gridOptions: {
60 60 },
61 61 });
62 62  
63   -/** 删除${subTable.classComment} */
64   -const onDelete = async (row: ${simpleClassName}Api.${subSimpleClassName}) => {
65   - await ${subClassNameVar}GridApi.grid.remove(row);
  63 +/** 添加${subTable.classComment} */
  64 +const onAdd = async () => {
  65 + await gridApi.grid.insertAt({} as ${simpleClassName}Api.${subSimpleClassName}, -1);
66 66 }
67 67  
68   -/** 添加${subTable.classComment} */
69   -const handleAdd = async () => {
70   - await ${subClassNameVar}GridApi.grid.insertAt({} as ${simpleClassName}Api.${subSimpleClassName}, -1);
  68 +/** 删除${subTable.classComment} */
  69 +const onDelete = async (row: ${simpleClassName}Api.${subSimpleClassName}) => {
  70 + await gridApi.grid.remove(row);
71 71 }
72 72  
73 73 /** 提供获取表格数据的方法供父组件调用 */
74 74 defineExpose({
75 75 getData: (): ${simpleClassName}Api.${subSimpleClassName}[] => {
76   - // 获取当前数据,但排除已删除的记录
77   - const allData = ${subClassNameVar}GridApi.grid.getData();
78   - const removedData = ${subClassNameVar}GridApi.grid.getRemoveRecords();
79   - const removedIds = new Set(removedData.map((row) => row.id));
80   -
81   - // 过滤掉已删除的记录
82   - const currentData = allData.filter((row) => !removedIds.has(row.id));
83   -
84   - // 获取新插入的记录并移除id
85   - const insertedData = ${subClassNameVar}GridApi.grid.getInsertRecords().map((row) => {
86   - delete row.id;
87   - return row;
88   - });
89   -
90   - return [...currentData, ...insertedData];
  76 + const data = gridApi.grid.getData() as ${simpleClassName}Api.${subSimpleClassName}[];
  77 + const removeRecords = gridApi.grid.getRemoveRecords() as ${simpleClassName}Api.${subSimpleClassName}[];
  78 + const insertRecords = gridApi.grid.getInsertRecords() as ${simpleClassName}Api.${subSimpleClassName}[];
  79 + return data
  80 + .filter((row) => !removeRecords.some((removed) => removed.id === row.id))
  81 + .concat(insertRecords.map((row: any) => ({ ...row, id: undefined })));
91 82 },
92 83 });
93 84  
... ... @@ -98,14 +89,13 @@ watch(
98 89 if (!val) {
99 90 return;
100 91 }
101   -
102 92 await nextTick();
103   - await ${subClassNameVar}GridApi.grid.loadData(await get${subSimpleClassName}ListBy${SubJoinColumnName}(props.${subJoinColumn.javaField}!));
  93 + await gridApi.grid.loadData(await get${subSimpleClassName}ListBy${SubJoinColumnName}(props.${subJoinColumn.javaField}!));
104 94 },
105 95 { immediate: true },
106 96 );
107 97 #else
108   -const [${subSimpleClassName}Form, ${subClassNameVar}FormApi] = useVbenForm({
  98 +const [Form, formApi] = useVbenForm({
109 99 layout: 'horizontal',
110 100 schema: use${subSimpleClassName}FormSchema(),
111 101 showDefaultActions: false
... ... @@ -114,10 +104,10 @@ showDefaultActions: false
114 104 /** 暴露出表单校验方法和表单值获取方法 */
115 105 defineExpose({
116 106 validate: async () => {
117   - const { valid } = await ${subClassNameVar}FormApi.validate();
  107 + const { valid } = await formApi.validate();
118 108 return valid;
119 109 },
120   - getValues: ${subClassNameVar}FormApi.getValues,
  110 + getValues: formApi.getValues,
121 111 });
122 112  
123 113 /** 监听主表的关联字段的变化,加载对应的子表数据 */
... ... @@ -127,9 +117,8 @@ watch(
127 117 if (!val) {
128 118 return;
129 119 }
130   -
131 120 await nextTick();
132   - await ${subClassNameVar}FormApi.setValues(await get${subSimpleClassName}By${SubJoinColumnName}(props.${subJoinColumn.javaField}!));
  121 + await formApi.setValues(await get${subSimpleClassName}By${SubJoinColumnName}(props.${subJoinColumn.javaField}!));
133 122 },
134 123 { immediate: true },
135 124 );
... ... @@ -138,7 +127,7 @@ watch(
138 127  
139 128 <template>
140 129 #if ($subTable.subJoinMany) ## 一对多
141   - <${subSimpleClassName}Grid class="mx-4">
  130 + <Grid class="mx-4">
142 131 #foreach($column in $subColumns)
143 132 #if ($column.createOperation || $column.updateOperation)
144 133 #set ($javaField = $column.javaField)
... ... @@ -191,13 +180,13 @@ watch(
191 180 #end
192 181 #end
193 182 #end
194   - </${subSimpleClassName}Grid>
195   - <div class="flex justify-center">
196   - <Button :icon="h(Plus)" type="primary" ghost @click="handleAdd" v-access:code="['${subTable.moduleName}:${simpleClassName_strikeCase}:create']">
  183 + </Grid>
  184 + <div class="flex justify-center -mt-4">
  185 + <Button :icon="h(Plus)" type="primary" ghost @click="onAdd" v-access:code="['${subTable.moduleName}:${simpleClassName_strikeCase}:create']">
197 186 {{ $t('ui.actionTitle.create', ['${subTable.classComment}']) }}
198 187 </Button>
199 188 </div>
200 189 #else
201   - <${subSimpleClassName}Form class="mx-4" />
  190 + <Form class="mx-4" />
202 191 #end
203 192 </template>
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3_vben5_antd/schema/views/modules/list_sub_erp.vue.vm
... ... @@ -28,7 +28,7 @@ import { get${subSimpleClassName}By${SubJoinColumnName} } from &#39;#/api/${table.mo
28 28 #end
29 29  
30 30 const props = defineProps<{
31   - ${subJoinColumn.javaField}?: any // ${subJoinColumn.columnComment}(主表的关联字段)
  31 + ${subJoinColumn.javaField}?: number // ${subJoinColumn.columnComment}(主表的关联字段)
32 32 }>()
33 33  
34 34 #if ($table.templateType == 11) ## erp
... ... @@ -46,6 +46,7 @@ function onCreate() {
46 46 function onEdit(row: ${simpleClassName}Api.${subSimpleClassName}) {
47 47 formModalApi.setData(row).open();
48 48 }
  49 +
49 50 /** 删除${subTable.classComment} */
50 51 async function onDelete(row: ${simpleClassName}Api.${subSimpleClassName}) {
51 52 const hideLoading = message.loading({
... ... @@ -71,16 +72,17 @@ function onActionClick({
71 72 row,
72 73 }: OnActionClickParams<${simpleClassName}Api.${subSimpleClassName}>) {
73 74 switch (code) {
74   - case 'delete': {
75   - onDelete(row);
76   - break;
77   - }
78 75 case 'edit': {
79 76 onEdit(row);
80 77 break;
81 78 }
  79 + case 'delete': {
  80 + onDelete(row);
  81 + break;
  82 + }
82 83 }
83 84 }
  85 +
84 86 #end
85 87 const [Grid, gridApi] = useVbenVxeGrid({
86 88 gridOptions: {
... ... @@ -119,7 +121,6 @@ const onRefresh = async ()=&gt; {
119 121 if (!val) {
120 122 return;
121 123 }
122   -
123 124 await nextTick();
124 125 await onRefresh()
125 126 },
... ... @@ -128,18 +129,16 @@ const onRefresh = async ()=&gt; {
128 129 </script>
129 130  
130 131 <template>
131   - <div class="mx-4">
132   - #if ($table.templateType == 11) ## erp
133   - <FormModal @success="onRefresh" />
134   - <Grid table-title="${subTable.classComment}列表">
135   - <template #toolbar-tools>
136   - <Button :icon="h(Plus)" type="primary" @click="onCreate" v-access:code="['${subTable.moduleName}:${subSimpleClassName_strikeCase}:create']">
137   - {{ $t('ui.actionTitle.create', ['${subTable.classComment}']) }}
138   - </Button>
139   - </template>
140   - </Grid>
141   - #else
142   - <Grid table-title="${subTable.classComment}列表" />
143   - #end
144   - </div>
  132 + #if ($table.templateType == 11) ## erp
  133 + <FormModal @success="onRefresh" />
  134 + <Grid table-title="${subTable.classComment}列表">
  135 + <template #toolbar-tools>
  136 + <Button :icon="h(Plus)" type="primary" @click="onCreate" v-access:code="['${subTable.moduleName}:${subSimpleClassName_strikeCase}:create']">
  137 + {{ $t('ui.actionTitle.create', ['${subTable.classComment}']) }}
  138 + </Button>
  139 + </template>
  140 + </Grid>
  141 + #else
  142 + <Grid table-title="${subTable.classComment}列表" />
  143 + #end
145 144 </template>
146 145 \ No newline at end of file
... ...