Commit b47c4fbd8df97c6a5f8230f5a2fc8b71d43ba8ff
1 parent
06649624
代码生成器:增加树形结构的 vm 模版
Showing
15 changed files
with
246 additions
and
20 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/codegen/CodegenTableDO.java
| ... | ... | @@ -138,4 +138,21 @@ public class CodegenTableDO extends BaseDO { |
| 138 | 138 | */ |
| 139 | 139 | private Boolean subJoinMany; |
| 140 | 140 | |
| 141 | + // ========== 树表相关字段 ========== | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * 树表的父字段编号 | |
| 145 | + * | |
| 146 | + * 关联 {@link CodegenColumnDO#getId()} | |
| 147 | + */ | |
| 148 | + private Long treeParentColumnId; | |
| 149 | + /** | |
| 150 | + * 树表的名字字段编号 | |
| 151 | + * | |
| 152 | + * 名字的用途:新增或修改时,select 框展示的字段 | |
| 153 | + * | |
| 154 | + * 关联 {@link CodegenColumnDO#getId()} | |
| 155 | + */ | |
| 156 | + private Long treeNameColumnId; | |
| 157 | + | |
| 141 | 158 | } | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/enums/codegen/CodegenTemplateTypeEnum.java
| ... | ... | @@ -4,6 +4,8 @@ import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| 4 | 4 | import lombok.AllArgsConstructor; |
| 5 | 5 | import lombok.Getter; |
| 6 | 6 | |
| 7 | +import java.util.Objects; | |
| 8 | + | |
| 7 | 9 | /** |
| 8 | 10 | * 代码生成模板类型 |
| 9 | 11 | * |
| ... | ... | @@ -38,4 +40,14 @@ public enum CodegenTemplateTypeEnum { |
| 38 | 40 | MASTER_NORMAL.type, MASTER_ERP.type, MASTER_INNER.type); |
| 39 | 41 | } |
| 40 | 42 | |
| 43 | + /** | |
| 44 | + * 是否为树表 | |
| 45 | + * | |
| 46 | + * @param type 类型 | |
| 47 | + * @return 是否树表 | |
| 48 | + */ | |
| 49 | + public static boolean isTree(Integer type) { | |
| 50 | + return Objects.equals(type, TREE.type); | |
| 51 | + } | |
| 52 | + | |
| 41 | 53 | } | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.codegen.inner; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.lang.Filter; | |
| 4 | 5 | import cn.hutool.core.map.MapUtil; |
| 5 | 6 | import cn.hutool.core.util.ObjectUtil; |
| 6 | 7 | import cn.hutool.core.util.StrUtil; |
| ... | ... | @@ -67,7 +68,7 @@ public class CodegenEngine { |
| 67 | 68 | .put(javaTemplatePath("controller/vo/pageReqVO"), javaModuleImplVOFilePath("PageReqVO")) |
| 68 | 69 | .put(javaTemplatePath("controller/vo/respVO"), javaModuleImplVOFilePath("RespVO")) |
| 69 | 70 | .put(javaTemplatePath("controller/vo/updateReqVO"), javaModuleImplVOFilePath("UpdateReqVO")) |
| 70 | - .put(javaTemplatePath("controller/vo/exportReqVO"), javaModuleImplVOFilePath("ExportReqVO")) | |
| 71 | + .put(javaTemplatePath("controller/vo/listReqVO"), javaModuleImplVOFilePath("ListReqVO")) | |
| 71 | 72 | .put(javaTemplatePath("controller/vo/excelVO"), javaModuleImplVOFilePath("ExcelVO")) |
| 72 | 73 | .put(javaTemplatePath("controller/controller"), javaModuleImplControllerFilePath()) |
| 73 | 74 | .put(javaTemplatePath("convert/convert"), |
| ... | ... | @@ -216,8 +217,14 @@ public class CodegenEngine { |
| 216 | 217 | if (isSubTemplate(vmPath)) { |
| 217 | 218 | generateSubCode(table, subTables, result, vmPath, filePath, bindingMap); |
| 218 | 219 | return; |
| 220 | + // 2.2 特殊:树表专属逻辑 | |
| 221 | + } else if (isPageTemplate(vmPath)) { | |
| 222 | + // 减少多余的类生成,例如说 PageVO.java 类 | |
| 223 | + if (CodegenTemplateTypeEnum.isTree(table.getTemplateType())) { | |
| 224 | + return; | |
| 225 | + } | |
| 219 | 226 | } |
| 220 | - // 2.2 默认生成 | |
| 227 | + // 2.3 默认生成 | |
| 221 | 228 | generateCode(result, vmPath, filePath, bindingMap); |
| 222 | 229 | }); |
| 223 | 230 | return result; |
| ... | ... | @@ -314,6 +321,14 @@ public class CodegenEngine { |
| 314 | 321 | // permission 前缀 |
| 315 | 322 | bindingMap.put("permissionPrefix", table.getModuleName() + ":" + simpleClassNameStrikeCase); |
| 316 | 323 | |
| 324 | + // 特殊:树表专属逻辑 | |
| 325 | + if (CodegenTemplateTypeEnum.isTree(table.getTemplateType())) { | |
| 326 | + bindingMap.put("treeParentColumn", CollUtil.findOne(columns, | |
| 327 | + column -> Objects.equals(column.getId(), table.getTreeParentColumnId()))); | |
| 328 | + bindingMap.put("treeNameColumn", CollUtil.findOne(columns, | |
| 329 | + column -> Objects.equals(column.getId(), table.getTreeNameColumnId()))); | |
| 330 | + } | |
| 331 | + | |
| 317 | 332 | // 特殊:主子表专属逻辑 |
| 318 | 333 | if (CollUtil.isNotEmpty(subTables)) { |
| 319 | 334 | // 创建 bindingMap |
| ... | ... | @@ -454,4 +469,8 @@ public class CodegenEngine { |
| 454 | 469 | return path.contains("_sub"); |
| 455 | 470 | } |
| 456 | 471 | |
| 472 | + private static boolean isPageTemplate(String path) { | |
| 473 | + return path.contains("page"); | |
| 474 | + } | |
| 475 | + | |
| 457 | 476 | } | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/controller/controller.vm
| ... | ... | @@ -85,28 +85,42 @@ public class ${sceneEnum.prefixClass}${table.className}Controller { |
| 85 | 85 | return success(${table.className}Convert.INSTANCE.convert(${classNameVar})); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | +#if ( $table.templateType != 2 ) | |
| 88 | 89 | @GetMapping("/page") |
| 89 | 90 | @Operation(summary = "获得${table.classComment}分页") |
| 90 | 91 | #if ($sceneEnum.scene == 1) |
| 91 | 92 | @PreAuthorize("@ss.hasPermission('${permissionPrefix}:query')") |
| 92 | 93 | #end |
| 93 | - public CommonResult<PageResult<${sceneEnum.prefixClass}${table.className}RespVO>> get${simpleClassName}Page(@Valid ${sceneEnum.prefixClass}${table.className}PageReqVO pageVO) { | |
| 94 | - PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(pageVO); | |
| 94 | + public CommonResult<PageResult<${sceneEnum.prefixClass}${table.className}RespVO>> get${simpleClassName}Page(@Valid ${sceneEnum.prefixClass}${table.className}PageReqVO pageReqVO) { | |
| 95 | + PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(pageReqVO); | |
| 95 | 96 | return success(${table.className}Convert.INSTANCE.convertPage(pageResult)); |
| 96 | 97 | } |
| 97 | 98 | |
| 99 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 100 | +#else | |
| 101 | + @GetMapping("/list") | |
| 102 | + @Operation(summary = "获得${table.classComment}列表") | |
| 103 | +#if ($sceneEnum.scene == 1) | |
| 104 | + @PreAuthorize("@ss.hasPermission('${permissionPrefix}:query')") | |
| 105 | +#end | |
| 106 | + public CommonResult<List<${sceneEnum.prefixClass}${table.className}RespVO>> get${simpleClassName}List(@Valid ${sceneEnum.prefixClass}${table.className}ListReqVO listReqVO) { | |
| 107 | + List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(listReqVO); | |
| 108 | + return success(${table.className}Convert.INSTANCE.convertList(list)); | |
| 109 | + } | |
| 110 | + | |
| 111 | +#end | |
| 98 | 112 | @GetMapping("/export-excel") |
| 99 | 113 | @Operation(summary = "导出${table.classComment} Excel") |
| 100 | 114 | #if ($sceneEnum.scene == 1) |
| 101 | 115 | @PreAuthorize("@ss.hasPermission('${permissionPrefix}:export')") |
| 102 | 116 | #end |
| 103 | 117 | @OperateLog(type = EXPORT) |
| 104 | - public void export${simpleClassName}Excel(@Valid ${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO, | |
| 118 | + public void export${simpleClassName}Excel(@Valid ${sceneEnum.prefixClass}${table.className}ListReqVO listReqVO, | |
| 105 | 119 | HttpServletResponse response) throws IOException { |
| 106 | - List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(exportReqVO); | |
| 120 | + List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(listReqVO); | |
| 107 | 121 | // 导出 Excel |
| 108 | - List<${sceneEnum.prefixClass}${table.className}ExcelVO> datas = ${table.className}Convert.INSTANCE.convertList02(list); | |
| 109 | - ExcelUtils.write(response, "${table.classComment}.xls", "数据", ${sceneEnum.prefixClass}${table.className}ExcelVO.class, datas); | |
| 122 | + List<${sceneEnum.prefixClass}${table.className}ExcelVO> dataList = ${table.className}Convert.INSTANCE.convertList02(list); | |
| 123 | + ExcelUtils.write(response, "${table.classComment}.xls", "数据", ${sceneEnum.prefixClass}${table.className}ExcelVO.class, dataList); | |
| 110 | 124 | } |
| 111 | 125 | |
| 112 | 126 | ## 特殊:主子表专属逻辑 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/controller/vo/exportReqVO.vm renamed to yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/controller/vo/listReqVO.vm
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/convert/convert.vm
| ... | ... | @@ -26,8 +26,10 @@ public interface ${table.className}Convert { |
| 26 | 26 | ${sceneEnum.prefixClass}${table.className}RespVO convert(${table.className}DO bean); |
| 27 | 27 | |
| 28 | 28 | List<${sceneEnum.prefixClass}${table.className}RespVO> convertList(List<${table.className}DO> list); |
| 29 | - | |
| 29 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 30 | +#if ( $table.templateType != 2 ) | |
| 30 | 31 | PageResult<${sceneEnum.prefixClass}${table.className}RespVO> convertPage(PageResult<${table.className}DO> page); |
| 32 | +#end | |
| 31 | 33 | |
| 32 | 34 | List<${sceneEnum.prefixClass}${table.className}ExcelVO> convertList02(List<${table.className}DO> list); |
| 33 | 35 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/dal/mapper.vm
| ... | ... | @@ -49,6 +49,8 @@ import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePack |
| 49 | 49 | @Mapper |
| 50 | 50 | public interface ${table.className}Mapper extends BaseMapperX<${table.className}DO> { |
| 51 | 51 | |
| 52 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 53 | +#if ( $table.templateType != 2 ) | |
| 52 | 54 | default PageResult<${table.className}DO> selectPage(${sceneEnum.prefixClass}${table.className}PageReqVO reqVO) { |
| 53 | 55 | return selectPage(reqVO, new LambdaQueryWrapperX<${table.className}DO>() |
| 54 | 56 | #listCondition() |
| ... | ... | @@ -56,11 +58,20 @@ public interface ${table.className}Mapper extends BaseMapperX<${table.className} |
| 56 | 58 | |
| 57 | 59 | } |
| 58 | 60 | |
| 59 | - default List<${table.className}DO> selectList(${sceneEnum.prefixClass}${table.className}ExportReqVO reqVO) { | |
| 61 | +#end | |
| 62 | + default List<${table.className}DO> selectList(${sceneEnum.prefixClass}${table.className}ListReqVO reqVO) { | |
| 60 | 63 | return selectList(new LambdaQueryWrapperX<${table.className}DO>() |
| 61 | 64 | #listCondition() |
| 62 | 65 | .orderByDesc(${table.className}DO::getId));## 大多数情况下,id 倒序 |
| 63 | 66 | |
| 64 | 67 | } |
| 65 | 68 | |
| 69 | +## 特殊:树表专属逻辑 | |
| 70 | +#if ( $table.templateType == 2 ) | |
| 71 | +#set ($ParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 | |
| 72 | + default Long selectCountBy${ParentJavaField}(${treeParentColumn.javaType} ${treeParentColumn.javaField}) { | |
| 73 | + return selectCount(${table.className}DO::get${ParentJavaField}, ${treeParentColumn.javaField}); | |
| 74 | + } | |
| 75 | + | |
| 76 | +#end | |
| 66 | 77 | } |
| 67 | 78 | \ No newline at end of file | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/enums/errorcode.vm
| 1 | 1 | // TODO 待办:请将下面的错误码复制到 yudao-module-${table.moduleName}-api 模块的 ErrorCodeConstants 类中。注意,请给“TODO 补充编号”设置一个错误码编号!!! |
| 2 | 2 | // ========== ${table.classComment} TODO 补充编号 ========== |
| 3 | -ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在"); | |
| 4 | 3 | \ No newline at end of file |
| 4 | +ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在"); | |
| 5 | +ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_EXITS_CHILDREN = new ErrorCode(TODO 补充编号, "存在存在子${table.classComment},无法删除"); | |
| 5 | 6 | \ No newline at end of file | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/service/service.vm
| ... | ... | @@ -48,6 +48,8 @@ public interface ${table.className}Service { |
| 48 | 48 | */ |
| 49 | 49 | ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id); |
| 50 | 50 | |
| 51 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 52 | +#if ( $table.templateType != 2 ) | |
| 51 | 53 | /** |
| 52 | 54 | * 获得${table.classComment}分页 |
| 53 | 55 | * |
| ... | ... | @@ -56,13 +58,14 @@ public interface ${table.className}Service { |
| 56 | 58 | */ |
| 57 | 59 | PageResult<${table.className}DO> get${simpleClassName}Page(${sceneEnum.prefixClass}${table.className}PageReqVO pageReqVO); |
| 58 | 60 | |
| 61 | +#end | |
| 59 | 62 | /** |
| 60 | - * 获得${table.classComment}列表, 用于 Excel 导出 | |
| 63 | + * 获得${table.classComment}列表 | |
| 61 | 64 | * |
| 62 | - * @param exportReqVO 查询条件 | |
| 65 | + * @param listReqVO 查询条件 | |
| 63 | 66 | * @return ${table.classComment}列表 |
| 64 | 67 | */ |
| 65 | - List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO); | |
| 68 | + List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ListReqVO listReqVO); | |
| 66 | 69 | |
| 67 | 70 | ## 特殊:主子表专属逻辑 |
| 68 | 71 | #foreach ($subTable in $subTables) | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/service/serviceImpl.vm
| ... | ... | @@ -110,6 +110,14 @@ public class ${table.className}ServiceImpl implements ${table.className}Service |
| 110 | 110 | public void delete${simpleClassName}(${primaryColumn.javaType} id) { |
| 111 | 111 | // 校验存在 |
| 112 | 112 | validate${simpleClassName}Exists(id); |
| 113 | +## 特殊:树表专属逻辑 | |
| 114 | +#if ( $table.templateType == 2 ) | |
| 115 | +#set ($ParentJavaField = $treeParentColumn.javaField.substring(0,1).toUpperCase() + ${treeParentColumn.javaField.substring(1)})##首字母大写 | |
| 116 | + // 校验是否有子${table.classComment} | |
| 117 | + if (${classNameVar}Mapper.selectCountBy${ParentJavaField}(id) > 0) { | |
| 118 | + throw exception(${simpleClassName_underlineCase.toUpperCase()}_EXITS_CHILDREN); | |
| 119 | + } | |
| 120 | +#end | |
| 113 | 121 | // 删除 |
| 114 | 122 | ${classNameVar}Mapper.deleteById(id); |
| 115 | 123 | ## 特殊:主子表专属逻辑 |
| ... | ... | @@ -137,14 +145,17 @@ public class ${table.className}ServiceImpl implements ${table.className}Service |
| 137 | 145 | return ${classNameVar}Mapper.selectById(id); |
| 138 | 146 | } |
| 139 | 147 | |
| 148 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 149 | +#if ( $table.templateType != 2 ) | |
| 140 | 150 | @Override |
| 141 | 151 | public PageResult<${table.className}DO> get${simpleClassName}Page(${sceneEnum.prefixClass}${table.className}PageReqVO pageReqVO) { |
| 142 | 152 | return ${classNameVar}Mapper.selectPage(pageReqVO); |
| 143 | 153 | } |
| 144 | 154 | |
| 155 | +#end | |
| 145 | 156 | @Override |
| 146 | - public List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO) { | |
| 147 | - return ${classNameVar}Mapper.selectList(exportReqVO); | |
| 157 | + public List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ListReqVO listReqVO) { | |
| 158 | + return ${classNameVar}Mapper.selectList(listReqVO); | |
| 148 | 159 | } |
| 149 | 160 | |
| 150 | 161 | ## 特殊:主子表专属逻辑 | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/java/test/serviceTest.vm
| ... | ... | @@ -137,6 +137,8 @@ public class ${table.className}ServiceImplTest extends BaseDbUnitTest { |
| 137 | 137 | assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 141 | +#if ( $table.templateType != 2 ) | |
| 140 | 142 | @Test |
| 141 | 143 | @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 |
| 142 | 144 | public void testGet${simpleClassName}Page() { |
| ... | ... | @@ -150,10 +152,11 @@ public class ${table.className}ServiceImplTest extends BaseDbUnitTest { |
| 150 | 152 | assertPojoEquals(db${simpleClassName}, pageResult.getList().get(0)); |
| 151 | 153 | } |
| 152 | 154 | |
| 155 | +#end | |
| 153 | 156 | @Test |
| 154 | 157 | @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 |
| 155 | 158 | public void testGet${simpleClassName}List() { |
| 156 | - #getPageCondition("ExportReqVO") | |
| 159 | + #getPageCondition("ListReqVO") | |
| 157 | 160 | |
| 158 | 161 | // 调用 |
| 159 | 162 | List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(reqVO); | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3/api/api.ts.vm
| ... | ... | @@ -15,10 +15,17 @@ export interface ${simpleClassName}VO { |
| 15 | 15 | #end |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | -// 查询${table.classComment}列表 | |
| 18 | +#if ( $table.templateType != 2 ) | |
| 19 | +// 查询${table.classComment}分页 | |
| 19 | 20 | export const get${simpleClassName}Page = async (params) => { |
| 20 | 21 | return await request.get({ url: `${baseURL}/page`, params }) |
| 21 | 22 | } |
| 23 | +#else | |
| 24 | +// 查询${table.classComment}列表 | |
| 25 | +export const get${simpleClassName}List = async (params) => { | |
| 26 | + return await request.get({ url: `${baseURL}/list`, params }) | |
| 27 | +} | |
| 28 | +#end | |
| 22 | 29 | |
| 23 | 30 | // 查询${table.classComment}详情 |
| 24 | 31 | export const get${simpleClassName} = async (id: number) => { | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3/views/form.vue.vm
| ... | ... | @@ -22,7 +22,22 @@ |
| 22 | 22 | #elseif ($javaType == "Boolean") |
| 23 | 23 | #set ($dictMethod = "getBoolDictOptions") |
| 24 | 24 | #end |
| 25 | - #if ($column.htmlType == "input" && !$column.primaryKey)## 忽略主键,不用在表单里 | |
| 25 | + #if ( $table.templateType == 2 && $column.id == $treeParentColumn.id ) | |
| 26 | + <el-form-item label="${comment}" prop="${javaField}"> | |
| 27 | + <el-tree-select | |
| 28 | + v-model="formData.${javaField}" | |
| 29 | + :data="${classNameVar}Tree" | |
| 30 | + #if ($treeNameColumn.javaField == "name") | |
| 31 | + :props="defaultProps" | |
| 32 | + #else | |
| 33 | + :props="{...defaultProps, label: '$treeNameColumn.javaField'}" | |
| 34 | + #end | |
| 35 | + check-strictly | |
| 36 | + default-expand-all | |
| 37 | + placeholder="请选择${comment}" | |
| 38 | + /> | |
| 39 | + </el-form-item> | |
| 40 | + #elseif ($column.htmlType == "input" && !$column.primaryKey)## 忽略主键,不用在表单里 | |
| 26 | 41 | <el-form-item label="${comment}" prop="${javaField}"> |
| 27 | 42 | <el-input v-model="formData.${javaField}" placeholder="请输入${comment}" /> |
| 28 | 43 | </el-form-item> |
| ... | ... | @@ -126,6 +141,10 @@ |
| 126 | 141 | <script setup lang="ts"> |
| 127 | 142 | import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict' |
| 128 | 143 | import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${table.businessName}' |
| 144 | +## 特殊:树表专属逻辑 | |
| 145 | +#if ( $table.templateType == 2 ) | |
| 146 | +import { defaultProps, handleTree } from '@/utils/tree' | |
| 147 | +#end | |
| 129 | 148 | ## 特殊:主子表专属逻辑 |
| 130 | 149 | #foreach ($subSimpleClassName in $subSimpleClassNames) |
| 131 | 150 | import ${subSimpleClassName}Form from './components/${subSimpleClassName}Form.vue' |
| ... | ... | @@ -158,6 +177,10 @@ const formRules = reactive({ |
| 158 | 177 | #end |
| 159 | 178 | }) |
| 160 | 179 | const formRef = ref() // 表单 Ref |
| 180 | +## 特殊:树表专属逻辑 | |
| 181 | +#if ( $table.templateType == 2 ) | |
| 182 | +const ${classNameVar}Tree = ref() // 树形结构 | |
| 183 | +#end | |
| 161 | 184 | ## 特殊:主子表专属逻辑 |
| 162 | 185 | #if ( $subTables && $subTables.size() > 0 ) |
| 163 | 186 | |
| ... | ... | @@ -183,6 +206,10 @@ const open = async (type: string, id?: number) => { |
| 183 | 206 | formLoading.value = false |
| 184 | 207 | } |
| 185 | 208 | } |
| 209 | +## 特殊:树表专属逻辑 | |
| 210 | +#if ( $table.templateType == 2 ) | |
| 211 | + await get${simpleClassName}Tree() | |
| 212 | +#end | |
| 186 | 213 | } |
| 187 | 214 | defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
| 188 | 215 | |
| ... | ... | @@ -248,4 +275,16 @@ const resetForm = () => { |
| 248 | 275 | } |
| 249 | 276 | formRef.value?.resetFields() |
| 250 | 277 | } |
| 278 | +## 特殊:树表专属逻辑 | |
| 279 | +#if ( $table.templateType == 2 ) | |
| 280 | + | |
| 281 | +/** 获得${table.classComment}树 */ | |
| 282 | +const get${simpleClassName}Tree = async () => { | |
| 283 | + ${classNameVar}Tree.value = [] | |
| 284 | + const data = await ${simpleClassName}Api.get${simpleClassName}List() | |
| 285 | + const root: Tree = { id: 0, name: '顶级${table.classComment}', children: [] } | |
| 286 | + root.children = handleTree(data, 'id', '${treeParentColumn.javaField}') | |
| 287 | + ${classNameVar}Tree.value.push(root) | |
| 288 | +} | |
| 289 | +#end | |
| 251 | 290 | </script> |
| 252 | 291 | \ No newline at end of file | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3/views/index.vue.vm
| ... | ... | @@ -101,6 +101,12 @@ |
| 101 | 101 | > |
| 102 | 102 | <Icon icon="ep:download" class="mr-5px" /> 导出 |
| 103 | 103 | </el-button> |
| 104 | +## 特殊:树表专属逻辑 | |
| 105 | +#if ( $table.templateType == 2 ) | |
| 106 | + <el-button type="danger" plain @click="toggleExpandAll"> | |
| 107 | + <Icon icon="ep:sort" class="mr-5px" /> 展开/折叠 | |
| 108 | + </el-button> | |
| 109 | +#end | |
| 104 | 110 | </el-form-item> |
| 105 | 111 | </el-form> |
| 106 | 112 | </ContentWrap> |
| ... | ... | @@ -117,6 +123,17 @@ |
| 117 | 123 | highlight-current-row |
| 118 | 124 | @current-change="handleCurrentChange" |
| 119 | 125 | > |
| 126 | +## 特殊:树表专属逻辑 | |
| 127 | +#elseif ( $table.templateType == 2 ) | |
| 128 | + <el-table | |
| 129 | + v-loading="loading" | |
| 130 | + :data="list" | |
| 131 | + :stripe="true" | |
| 132 | + :show-overflow-tooltip="true" | |
| 133 | + row-key="id" | |
| 134 | + :default-expand-all="isExpandAll" | |
| 135 | + v-if="refreshTable" | |
| 136 | + > | |
| 120 | 137 | #else |
| 121 | 138 | <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"> |
| 122 | 139 | #end |
| ... | ... | @@ -218,6 +235,10 @@ |
| 218 | 235 | <script setup lang="ts"> |
| 219 | 236 | import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict' |
| 220 | 237 | import { dateFormatter } from '@/utils/formatTime' |
| 238 | +## 特殊:树表专属逻辑 | |
| 239 | +#if ( $table.templateType == 2 ) | |
| 240 | +import { handleTree } from '@/utils/tree' | |
| 241 | +#end | |
| 221 | 242 | import download from '@/utils/download' |
| 222 | 243 | import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${table.businessName}' |
| 223 | 244 | import ${simpleClassName}Form from './${simpleClassName}Form.vue' |
| ... | ... | @@ -232,11 +253,17 @@ const message = useMessage() // 消息弹窗 |
| 232 | 253 | const { t } = useI18n() // 国际化 |
| 233 | 254 | |
| 234 | 255 | const loading = ref(true) // 列表的加载中 |
| 235 | -const total = ref(0) // 列表的总页数 | |
| 236 | 256 | const list = ref([]) // 列表的数据 |
| 257 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 258 | +#if ( $table.templateType != 2 ) | |
| 259 | +const total = ref(0) // 列表的总页数 | |
| 260 | +#end | |
| 237 | 261 | const queryParams = reactive({ |
| 262 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 263 | +#if ( $table.templateType != 2 ) | |
| 238 | 264 | pageNo: 1, |
| 239 | 265 | pageSize: 10, |
| 266 | +#end | |
| 240 | 267 | #foreach ($column in $columns) |
| 241 | 268 | #if ($column.listOperation) |
| 242 | 269 | #if ($column.listOperationCondition != 'BETWEEN') |
| ... | ... | @@ -255,9 +282,15 @@ const exportLoading = ref(false) // 导出的加载中 |
| 255 | 282 | const getList = async () => { |
| 256 | 283 | loading.value = true |
| 257 | 284 | try { |
| 285 | +## 特殊:树表专属逻辑(树不需要分页接口) | |
| 286 | + #if ( $table.templateType == 2 ) | |
| 287 | + const data = await ${simpleClassName}Api.get${simpleClassName}List(queryParams) | |
| 288 | + list.value = handleTree(data, 'id', '${treeParentColumn.javaField}') | |
| 289 | + #else | |
| 258 | 290 | const data = await ${simpleClassName}Api.get${simpleClassName}Page(queryParams) |
| 259 | 291 | list.value = data.list |
| 260 | 292 | total.value = data.total |
| 293 | + #end | |
| 261 | 294 | } finally { |
| 262 | 295 | loading.value = false |
| 263 | 296 | } |
| ... | ... | @@ -317,6 +350,19 @@ const handleCurrentChange = (row) => { |
| 317 | 350 | currentRow.value = row |
| 318 | 351 | } |
| 319 | 352 | #end |
| 353 | +## 特殊:树表专属逻辑 | |
| 354 | +#if ( $table.templateType == 2 ) | |
| 355 | + | |
| 356 | +/** 展开/折叠操作 */ | |
| 357 | +const isExpandAll = ref(true) // 是否展开,默认全部展开 | |
| 358 | +const refreshTable = ref(true) // 重新渲染表格状态 | |
| 359 | +const toggleExpandAll = async () => { | |
| 360 | + refreshTable.value = false | |
| 361 | + isExpandAll.value = !isExpandAll.value | |
| 362 | + await nextTick() | |
| 363 | + refreshTable.value = true | |
| 364 | +} | |
| 365 | +#end | |
| 320 | 366 | |
| 321 | 367 | /** 初始化 **/ |
| 322 | 368 | onMounted(() => { | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineTest.java
| ... | ... | @@ -43,6 +43,47 @@ public class CodegenEngineTest extends BaseMockitoUnitTest { |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | @Test |
| 46 | + public void testExecute_vue3_tree() { | |
| 47 | + // 准备请求参数 | |
| 48 | + CodegenTableDO table = new CodegenTableDO().setScene(CodegenSceneEnum.ADMIN.getScene()).setParentMenuId(888L) | |
| 49 | + .setTableName("infra_demo66_student").setTableComment("学生表") | |
| 50 | + .setModuleName("infra").setBusinessName("demo66").setClassName("InfraDemo66Student") | |
| 51 | + .setClassComment("学生").setAuthor("芋道源码") | |
| 52 | + .setTemplateType(CodegenTemplateTypeEnum.TREE.getType()) | |
| 53 | + .setFrontType(CodegenFrontTypeEnum.VUE3.getType()) | |
| 54 | + .setTreeParentColumnId(22L).setTreeNameColumnId(11L) | |
| 55 | + ; | |
| 56 | + CodegenColumnDO idColumn = new CodegenColumnDO().setColumnName("id").setDataType(JdbcType.BIGINT.name()) | |
| 57 | + .setColumnComment("编号").setNullable(false).setPrimaryKey(true).setAutoIncrement(true) | |
| 58 | + .setJavaType("Long").setJavaField("id").setExample("1024") | |
| 59 | + .setCreateOperation(false).setUpdateOperation(true) | |
| 60 | + .setListOperation(false) | |
| 61 | + .setListOperationResult(true); | |
| 62 | + CodegenColumnDO nameColumn = new CodegenColumnDO().setColumnName("name").setDataType(JdbcType.VARCHAR.name()) | |
| 63 | + .setId(11L) | |
| 64 | + .setColumnComment("名字").setNullable(false) | |
| 65 | + .setJavaType("String").setJavaField("name").setExample("芋头") | |
| 66 | + .setCreateOperation(true).setUpdateOperation(true) | |
| 67 | + .setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.LIKE.getCondition()) | |
| 68 | + .setListOperationResult(true) | |
| 69 | + .setHtmlType(CodegenColumnHtmlTypeEnum.INPUT.getType()); | |
| 70 | + CodegenColumnDO parentIdColumn = new CodegenColumnDO().setColumnName("description").setDataType(JdbcType.VARCHAR.name()) | |
| 71 | + .setId(22L) | |
| 72 | + .setColumnComment("父编号").setNullable(false) | |
| 73 | + .setJavaType("Long").setJavaField("parentId").setExample("2048") | |
| 74 | + .setCreateOperation(true).setUpdateOperation(true) | |
| 75 | + .setListOperation(false) | |
| 76 | + .setListOperationResult(true); | |
| 77 | + List<CodegenColumnDO> columns = Arrays.asList(idColumn, nameColumn, parentIdColumn); | |
| 78 | + | |
| 79 | + // 调用 | |
| 80 | + Map<String, String> result = codegenEngine.execute(table, columns, null, null); | |
| 81 | + | |
| 82 | + // 构建 zip 包 | |
| 83 | + writeFile(result, "/Users/yunai/test/demo66.zip"); | |
| 84 | + } | |
| 85 | + | |
| 86 | + @Test | |
| 46 | 87 | public void testExecute_vue3_crud() { |
| 47 | 88 | // 准备请求参数 |
| 48 | 89 | CodegenTableDO table = new CodegenTableDO().setScene(CodegenSceneEnum.ADMIN.getScene()).setParentMenuId(888L) | ... | ... |