From 7aa5a4ebe2be589f69004eb6d548cac29419246d Mon Sep 17 00:00:00 2001 From: lbw Date: Mon, 30 Oct 2023 15:15:21 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20Improving=20structure=20/=20format=20of?= =?UTF-8?q?=20the=20code.=20=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/6pigxx_config.sql | 2 +- .../service/impl/GeneratorServiceImpl.java | 470 +++++++++--------- 2 files changed, 245 insertions(+), 227 deletions(-) diff --git a/db/6pigxx_config.sql b/db/6pigxx_config.sql index 159af5889..9c12940ad 100644 --- a/db/6pigxx_config.sql +++ b/db/6pigxx_config.sql @@ -38,7 +38,7 @@ INSERT INTO `config_info` VALUES (3, 'pigx-codegen-dev.yml', 'DEFAULT_GROUP', '# INSERT INTO `config_info` VALUES (4, 'pigx-daemon-elastic-job-dev.yml', 'DEFAULT_GROUP', '# 数据源\nspring:\n datasource:\n type: com.alibaba.druid.pool.DruidDataSource\n druid:\n driver-class-name: com.mysql.cj.jdbc.Driver\n username: ${MYSQL_USER:root}\n password: ${MYSQL_PWD:root}\n url: jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx_job}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true', '09996f46e0b80d7b2aa320ef4cb31c82', '2022-12-16 10:44:25', '2023-04-07 14:14:50', 'nacos', '127.0.0.1', '', '', '', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (5, 'pigx-gateway-dev.yml', 'DEFAULT_GROUP', 'gateway:\n encode-key: \'pigxpigxpigxpigx\'\n\n# 验证码相关配置参考: http://t.cn/A647jEdu\naj:\n captcha:\n cache-type: redis\n water-mark: pig4cloud\n\n# 固定路由转发配置 无修改\nspring:\n cloud:\n gateway:\n routes:\n - id: openapi\n uri: lb://pigx-gateway\n predicates:\n - Path=/v3/api-docs/**\n filters:\n - RewritePath=/v3/api-docs/(?.*), /$\\{path}/$\\{path}/v3/api-docs\n\n# gateway 刷新端点\nmanagement:\n endpoint:\n gateway:\n enabled: true\n', 'd138f5bafa91ab76dca8c4c1a01c1de2', '2022-12-16 10:44:25', '2023-07-28 16:49:29', 'nacos', '0:0:0:0:0:0:0:1', '', '', '', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (6, 'pigx-monitor-dev.yml', 'DEFAULT_GROUP', 'spring:\n # 安全配置\n security:\n user:\n name: ENC(rZHA4LW5hHmhLAAzJoFNag==) # pigx\n password: ENC(bjeyh+Aeii3kHXkoo00ZUw==) # pigx\n autoconfigure:\n exclude: com.pig4cloud.pigx.common.core.config.JacksonConfiguration\n boot:\n admin:\n ui:\n title: \'pigx 服务状态监控\'\n brand: \'pigx 服务状态监控\'\n external-views:\n - label: \"SQL监控\"\n url: /druid/sql.html\n order: 2000\n iframe: true\nmanagement:\n endpoints:\n web:\n exposure:\n include: \'*\'\n endpoint:\n health:\n show-details: ALWAYS #显示详细信息\n\n\n# druid 监控的服务\nmonitor:\n applications:\n - pigx-upms-biz\n', '2bb39e4dee3f90d4186d1e42aa666d2a', '2022-12-16 10:44:25', '2023-04-07 14:12:09', 'nacos', '127.0.0.1', '', '', '', '', '', 'yaml', '', ''); -INSERT INTO `config_info` VALUES (7, 'pigx-upms-biz-dev.yml', 'DEFAULT_GROUP', '## spring security 配置\nsecurity:\n oauth2:\n client:\n ignore-urls:\n - /druid/**\n\n# 数据源\nspring:\n autoconfigure:\n exclude: org.springframework.cloud.gateway.config.GatewayAutoConfiguration,org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration\n datasource:\n type: com.alibaba.druid.pool.DruidDataSource\n druid:\n driver-class-name: com.mysql.cj.jdbc.Driver\n username: ${MYSQL_USER:root}\n password: ${MYSQL_PWD:root}\n url: jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true\n stat-view-servlet:\n enabled: true\n allow: \"\"\n url-pattern: /druid/*\n #login-username: admin\n #login-password: admin\n filter:\n stat:\n enabled: true\n log-slow-sql: true\n slow-sql-millis: 10000\n merge-sql: false\n wall:\n config:\n multi-statement-allow: true\n\n# 本地文件系统\nfile:\n local:\n enable: true\n basePath: /Users/lengleng/Downloads/files\n\n# Logger Config\nlogging:\n level:\n com.pig4cloud.pigx.admin.mapper: debug\n\n# 租户表维护\npigx:\n tenant:\n column: tenant_id\n tables:\n - sys_user\n - sys_role\n - sys_menu\n - sys_dept\n - sys_log\n - sys_social_details\n - sys_dict\n - sys_dict_item\n - sys_public_param\n - sys_log\n - sys_file\n - sys_file_group\n - sys_oauth_client_details\n - sys_post', 'a37e3dc8cf649be64bd35cc5e5e355a9', '2022-12-16 10:44:25', '2023-07-28 13:47:59', 'nacos', '0:0:0:0:0:0:0:1', '', '', '', '', '', 'yaml', '', ''); +INSERT INTO `config_info` VALUES (7, 'pigx-upms-biz-dev.yml', 'DEFAULT_GROUP', '## spring security 配置\nsecurity:\n oauth2:\n client:\n ignore-urls:\n - /druid/**\n\n# 数据源\nspring:\n autoconfigure:\n exclude: org.springframework.cloud.gateway.config.GatewayAutoConfiguration,org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration\n datasource:\n type: com.alibaba.druid.pool.DruidDataSource\n druid:\n driver-class-name: com.mysql.cj.jdbc.Driver\n username: ${MYSQL_USER:root}\n password: ${MYSQL_PWD:root}\n url: jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true\n stat-view-servlet:\n enabled: true\n allow: \"\"\n url-pattern: /druid/*\n #login-username: admin\n #login-password: admin\n filter:\n stat:\n enabled: true\n log-slow-sql: true\n slow-sql-millis: 10000\n merge-sql: false\n wall:\n config:\n multi-statement-allow: true\n\n# 本地文件系统\nfile:\n local:\n enable: true\n basePath: /Users/lengleng/Downloads/files\n\n# Logger Config\nlogging:\n level:\n com.pig4cloud.pigx.admin.mapper: debug\n\n# 租户表维护\npigx:\n tenant:\n column: tenant_id\n tables:\n - sys_user\n - sys_role\n - sys_menu\n - sys_dept\n - sys_log\n - sys_social_details\n - sys_dict\n - sys_dict_item\n - sys_public_param\n - sys_log\n - sys_file\n - sys_file_group\n - sys_oauth_client_details\n - sys_post\n - sys_message\n - sys_message_relation', '90d96cb006f342d06da031cff829615c', '2022-12-16 10:44:25', '2023-10-28 21:59:30', 'nacos', '127.0.0.1', '', '', '', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (8, 'pigx-daemon-quartz-dev.yml', 'DEFAULT_GROUP', '# 数据源\nspring:\n datasource:\n type: com.alibaba.druid.pool.DruidDataSource\n druid:\n driver-class-name: com.mysql.cj.jdbc.Driver\n username: ${MYSQL_USER:root}\n password: ${MYSQL_PWD:root}\n url: jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx_job}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true\n resources:\n static-locations: classpath:/static/,classpath:/views/\n quartz:\n #相关属性配置\n properties:\n org:\n quartz:\n scheduler:\n instanceName: clusteredScheduler\n instanceId: AUTO\n jobStore:\n class: org.springframework.scheduling.quartz.LocalDataSourceJobStore\n driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate\n tablePrefix: QRTZ_\n isClustered: true\n clusterCheckinInterval: 10000\n useProperties: false\n threadPool:\n class: org.quartz.simpl.SimpleThreadPool\n threadCount: 50\n threadPriority: 5\n threadsInheritContextClassLoaderOfInitializingThread: true\n #数据库方式\n job-store-type: jdbc\n #初始化表结构\n #jdbc:\n #initialize-schema: never\n\n', '5f7a45c377318ce767433a1e210e8584', '2022-12-16 10:44:25', '2023-04-07 14:13:08', 'nacos', '127.0.0.1', '', '', '', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (9, 'pigx-pay-platform-dev.yml', 'DEFAULT_GROUP', '# 数据源\nspring:\n datasource:\n type: com.alibaba.druid.pool.DruidDataSource\n druid:\n driver-class-name: com.mysql.cj.jdbc.Driver\n username: ${MYSQL_USER:root}\n password: ${MYSQL_PWD:root}\n url: jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx_pay}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true\n freemarker:\n allow-request-override: false\n allow-session-override: false\n cache: true\n charset: UTF-8\n check-template-location: true\n content-type: text/html\n enabled: true\n expose-request-attributes: false\n expose-session-attributes: false\n expose-spring-macro-helpers: true\n prefer-file-system-access: true\n suffix: .ftl\n template-loader-path: classpath:/templates/\n# 租户表维护\npigx:\n pay:\n test: lengleng\n aliPayConfig:\n expire-time: 30\n return-url: http://pig4cloud.com\n notify-url: http://payx.yunjihuitong.com/pay/notify/ali/callbak\n wxPayConfig:\n notify-url: https://admin.pig4cloud.com/pay/notify/wx/callbak\n mergePayConfig:\n return-url: http://pig4cloud.com\n notify-url: http://wechat.pigx.top/pay/notify/merge/callbak\n xsequence: #发号器相关配置\n db:\n retry-times: 3\n table-name: pay_sequence\n tenant:\n column: tenant_id\n tables:\n - pay_channel\n - pay_trade_order\n - pay_goods_order\n - pay_notify_record\n - pay_refund_order', 'c70a66d3c300eab1afef0ed4ad9d42d5', '2022-12-16 10:44:25', '2023-04-07 14:12:40', 'nacos', '127.0.0.1', '', '', '', '', '', 'yaml', '', ''); INSERT INTO `config_info` VALUES (11, 'pigx-mp-platform-dev.yml', 'DEFAULT_GROUP', '# 数据源\nspring:\n datasource:\n type: com.alibaba.druid.pool.DruidDataSource\n druid:\n driver-class-name: com.mysql.cj.jdbc.Driver\n username: ${MYSQL_USER:root}\n password: ${MYSQL_PWD:root}\n url: jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx_mp}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true\n resources:\n static-locations: classpath:/static/,classpath:/views/\n\n\n# 租户表维护\npigx:\n tenant:\n column: tenant_id\n tables:\n - wx_mp_menu\n - wx_account\n - wx_account_tag\n - wx_account_fans\n - wx_msg\n - wx_auto_reply', 'ea4e59c98b8aeb4dd78ec989e7f197a4', '2022-12-16 10:44:25', '2023-01-29 13:12:59', 'nacos', '0:0:0:0:0:0:0:1', '', '', '', '', '', 'yaml', '', ''); diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java index 0c0cb5ac2..976f849c9 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java @@ -36,6 +36,7 @@ import com.pig4cloud.pigx.codegen.util.vo.GroupVo; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.SpringBootVersion; import org.springframework.stereotype.Service; import java.util.*; @@ -54,276 +55,293 @@ import java.util.zip.ZipOutputStream; @RequiredArgsConstructor public class GeneratorServiceImpl implements GeneratorService { - private final GenTableColumnService columnService; + private final GenTableColumnService columnService; - private final GenFormConfService formConfService; + private final GenFormConfService formConfService; - private final GenFieldTypeService fieldTypeService; + private final GenFieldTypeService fieldTypeService; - private final GenTableService tableService; + private final GenTableService tableService; - private final GenGroupService genGroupService; + private final GenGroupService genGroupService; - private final GenTemplateService genTemplateService; + private final GenTemplateService genTemplateService; - /** - * 生成代码zip写出 - * @param tableId 表 - * @param zip 输出流 - */ - @Override - @SneakyThrows - public void downloadCode(Long tableId, ZipOutputStream zip) { - // 数据模型 - Map dataModel = getDataModel(tableId); + /** + * 生成代码zip写出 + * + * @param tableId 表 + * @param zip 输出流 + */ + @Override + @SneakyThrows + public void downloadCode(Long tableId, ZipOutputStream zip) { + // 数据模型 + Map dataModel = getDataModel(tableId); - Long style = (Long) dataModel.get("style"); + Long style = (Long) dataModel.get("style"); - GroupVo groupVo = genGroupService.getGroupVoById(style); - List templateList = groupVo.getTemplateList(); + GroupVo groupVo = genGroupService.getGroupVoById(style); + List templateList = groupVo.getTemplateList(); - Map generatorConfig = tableService.getGeneratorConfig(); - JSONObject project = (JSONObject) generatorConfig.get("project"); - String frontendPath = project.getStr("frontendPath"); - String backendPath = project.getStr("backendPath"); + Map generatorConfig = tableService.getGeneratorConfig(); + JSONObject project = (JSONObject) generatorConfig.get("project"); + String frontendPath = project.getStr("frontendPath"); + String backendPath = project.getStr("backendPath"); - for (GenTemplateEntity template : templateList) { - String templateCode = template.getTemplateCode(); - String generatorPath = template.getGeneratorPath(); + for (GenTemplateEntity template : templateList) { + String templateCode = template.getTemplateCode(); + String generatorPath = template.getGeneratorPath(); - dataModel.put("frontendPath", frontendPath); - dataModel.put("backendPath", backendPath); - String content = VelocityKit.renderStr(templateCode, dataModel); - String path = VelocityKit.renderStr(generatorPath, dataModel); + dataModel.put("frontendPath", frontendPath); + dataModel.put("backendPath", backendPath); + String content = VelocityKit.renderStr(templateCode, dataModel); + String path = VelocityKit.renderStr(generatorPath, dataModel); - // 添加到zip - zip.putNextEntry(new ZipEntry(path)); - IoUtil.writeUtf8(zip, false, content); - zip.flush(); - zip.closeEntry(); - } + // 添加到zip + zip.putNextEntry(new ZipEntry(path)); + IoUtil.writeUtf8(zip, false, content); + zip.flush(); + zip.closeEntry(); + } - } + } - /** - * 表达式优化的预览代码方法 - * @param tableId 表 - * @return [{模板名称:渲染结果}] - */ - @Override - @SneakyThrows - public List> preview(Long tableId) { - // 数据模型 - Map dataModel = getDataModel(tableId); + /** + * 表达式优化的预览代码方法 + * + * @param tableId 表 + * @return [{模板名称:渲染结果}] + */ + @Override + @SneakyThrows + public List> preview(Long tableId) { + // 数据模型 + Map dataModel = getDataModel(tableId); - Long style = (Long) dataModel.get("style"); + Long style = (Long) dataModel.get("style"); - // 获取模板列表,Lambda 表达式简化代码 - List templateList = genGroupService.getGroupVoById(style).getTemplateList(); + // 获取模板列表,Lambda 表达式简化代码 + List templateList = genGroupService.getGroupVoById(style).getTemplateList(); - Map generatorConfig = tableService.getGeneratorConfig(); - JSONObject project = (JSONObject) generatorConfig.get("project"); - String frontendPath = project.getStr("frontendPath"); - String backendPath = project.getStr("backendPath"); + Map generatorConfig = tableService.getGeneratorConfig(); + JSONObject project = (JSONObject) generatorConfig.get("project"); + String frontendPath = project.getStr("frontendPath"); + String backendPath = project.getStr("backendPath"); - return templateList.stream().map(template -> { - String templateCode = template.getTemplateCode(); - String generatorPath = template.getGeneratorPath(); + return templateList.stream().map(template -> { + String templateCode = template.getTemplateCode(); + String generatorPath = template.getGeneratorPath(); - // 预览模式下, 使用相对路径展示 - dataModel.put("frontendPath", frontendPath); - dataModel.put("backendPath", backendPath); - String content = VelocityKit.renderStr(templateCode, dataModel); - String path = VelocityKit.renderStr(generatorPath, dataModel); + // 预览模式下, 使用相对路径展示 + dataModel.put("frontendPath", frontendPath); + dataModel.put("backendPath", backendPath); + String content = VelocityKit.renderStr(templateCode, dataModel); + String path = VelocityKit.renderStr(generatorPath, dataModel); - // 使用 map 简化代码 - return new HashMap(4) { - { - put("code", content); - put("codePath", path); - } - }; - }).collect(Collectors.toList()); - } + // 使用 map 简化代码 + return new HashMap(4) { + { + put("code", content); + put("codePath", path); + } + }; + }).collect(Collectors.toList()); + } - /** - * 目标目录写入渲染结果方法 - * @param tableId 表 - */ - @Override - public void generatorCode(Long tableId) { - // 数据模型 - Map dataModel = getDataModel(tableId); - Long style = (Long) dataModel.get("style"); + /** + * 目标目录写入渲染结果方法 + * + * @param tableId 表 + */ + @Override + public void generatorCode(Long tableId) { + // 数据模型 + Map dataModel = getDataModel(tableId); + Long style = (Long) dataModel.get("style"); - // 获取模板列表,Lambda 表达式简化代码 - List templateList = genGroupService.getGroupVoById(style).getTemplateList(); + // 获取模板列表,Lambda 表达式简化代码 + List templateList = genGroupService.getGroupVoById(style).getTemplateList(); - templateList.forEach(template -> { - String templateCode = template.getTemplateCode(); - String generatorPath = template.getGeneratorPath(); - String content = VelocityKit.renderStr(templateCode, dataModel); - String path = VelocityKit.renderStr(generatorPath, dataModel); - FileUtil.writeUtf8String(content, path); - }); - } + templateList.forEach(template -> { + String templateCode = template.getTemplateCode(); + String generatorPath = template.getGeneratorPath(); + String content = VelocityKit.renderStr(templateCode, dataModel); + String path = VelocityKit.renderStr(generatorPath, dataModel); + FileUtil.writeUtf8String(content, path); + }); + } - /** - * 获取表单设计器需要的 JSON 方法 - * @param dsName 数据源名称 - * @param tableName 表名称 - * @return JSON 字符串 - */ - @SneakyThrows - @Override - public String vform(String dsName, String tableName) { - // 查询表的元数据 - GenTable genTable = tableService.queryOrBuildTable(dsName, tableName); + /** + * 获取表单设计器需要的 JSON 方法 + * + * @param dsName 数据源名称 + * @param tableName 表名称 + * @return JSON 字符串 + */ + @SneakyThrows + @Override + public String vform(String dsName, String tableName) { + // 查询表的元数据 + GenTable genTable = tableService.queryOrBuildTable(dsName, tableName); - // 获取数据模型 - Map dataModel = getDataModel(genTable.getId()); + // 获取数据模型 + Map dataModel = getDataModel(genTable.getId()); - // 获取模板信息,Lambda 表达式简化代码 - GenTemplateEntity genTemplateEntity = Optional - .ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_JSON.getTemplateId())) - .orElseThrow(() -> new Exception("模板不存在")); + // 获取模板信息,Lambda 表达式简化代码 + GenTemplateEntity genTemplateEntity = Optional + .ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_JSON.getTemplateId())) + .orElseThrow(() -> new Exception("模板不存在")); - // 渲染模板并返回结果 - return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel); - } + // 渲染模板并返回结果 + return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel); + } - /** - * 获取表单设计器需要的 JSON 方法 - * @param id 表单配置 ID - * @return JSON 字符串 - */ - @SneakyThrows - @Override - public String vformSfc(Long id) { - // 获取表单配置信息 - GenFormConf formConf = formConfService.getById(id); + /** + * 获取表单设计器需要的 JSON 方法 + * + * @param id 表单配置 ID + * @return JSON 字符串 + */ + @SneakyThrows + @Override + public String vformSfc(Long id) { + // 获取表单配置信息 + GenFormConf formConf = formConfService.getById(id); - // 查询表的元数据 - GenTable genTable = tableService.queryOrBuildTable(formConf.getDsName(), formConf.getTableName()); + // 查询表的元数据 + GenTable genTable = tableService.queryOrBuildTable(formConf.getDsName(), formConf.getTableName()); - // 获取数据模型 - Map dataModel = getDataModel(genTable.getId()); + // 获取数据模型 + Map dataModel = getDataModel(genTable.getId()); - // 解析组件列表 - Map> resultMap = formConfService.parse(formConf.getFormInfo()); + // 解析组件列表 + Map> resultMap = formConfService.parse(formConf.getFormInfo()); - // 遍历 widgetList - dataModel.put("resultMap", resultMap); + // 遍历 widgetList + dataModel.put("resultMap", resultMap); - // 获取模板信息 - GenTemplateEntity genTemplateEntity = Optional - .ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_FORM.getTemplateId())) - .orElseThrow(() -> new Exception("模板不存在")); + // 获取模板信息 + GenTemplateEntity genTemplateEntity = Optional + .ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_FORM.getTemplateId())) + .orElseThrow(() -> new Exception("模板不存在")); - // 渲染模板并返回结果 - return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel); - } + // 渲染模板并返回结果 + return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel); + } - /** - * 通过 Lambda 表达式优化的获取数据模型方法 - * @param tableId 表格 ID - * @return 数据模型 Map 对象 - */ - private Map getDataModel(Long tableId) { - // 获取表格信息 - GenTable table = tableService.getById(tableId); - // 获取字段列表 - List fieldList = columnService.lambdaQuery() - .eq(GenTableColumnEntity::getDsName, table.getDsName()) - .eq(GenTableColumnEntity::getTableName, table.getTableName()) - .orderByAsc(GenTableColumnEntity::getSort) - .list(); + /** + * 通过 Lambda 表达式优化的获取数据模型方法 + * + * @param tableId 表格 ID + * @return 数据模型 Map 对象 + */ + private Map getDataModel(Long tableId) { + // 获取表格信息 + GenTable table = tableService.getById(tableId); + // 获取字段列表 + List fieldList = columnService.lambdaQuery() + .eq(GenTableColumnEntity::getDsName, table.getDsName()) + .eq(GenTableColumnEntity::getTableName, table.getTableName()) + .orderByAsc(GenTableColumnEntity::getSort) + .list(); - table.setFieldList(fieldList); + table.setFieldList(fieldList); - // 创建数据模型对象 - Map dataModel = new HashMap<>(); + // 创建数据模型对象 + Map dataModel = new HashMap<>(); - // 填充数据模型 - dataModel.put("dbType", table.getDbType()); - dataModel.put("package", table.getPackageName()); - dataModel.put("packagePath", table.getPackageName().replace(".", "/")); - dataModel.put("version", table.getVersion()); - dataModel.put("moduleName", table.getModuleName()); - dataModel.put("ModuleName", StrUtil.upperFirst(table.getModuleName())); - dataModel.put("functionName", table.getFunctionName()); - dataModel.put("FunctionName", StrUtil.upperFirst(table.getFunctionName())); - dataModel.put("formLayout", table.getFormLayout()); - dataModel.put("style", table.getStyle()); - dataModel.put("author", table.getAuthor()); - dataModel.put("datetime", DateUtil.now()); - dataModel.put("date", DateUtil.today()); - setFieldTypeList(dataModel, table); + // 填充数据模型 + dataModel.put("isSpringBoot3", isSpringBoot3()); + dataModel.put("dbType", table.getDbType()); + dataModel.put("package", table.getPackageName()); + dataModel.put("packagePath", table.getPackageName().replace(".", "/")); + dataModel.put("version", table.getVersion()); + dataModel.put("moduleName", table.getModuleName()); + dataModel.put("ModuleName", StrUtil.upperFirst(table.getModuleName())); + dataModel.put("functionName", table.getFunctionName()); + dataModel.put("FunctionName", StrUtil.upperFirst(table.getFunctionName())); + dataModel.put("formLayout", table.getFormLayout()); + dataModel.put("style", table.getStyle()); + dataModel.put("author", table.getAuthor()); + dataModel.put("datetime", DateUtil.now()); + dataModel.put("date", DateUtil.today()); + setFieldTypeList(dataModel, table); - // 获取导入的包列表 - Set importList = fieldTypeService.getPackageByTableId(table.getDsName(), table.getTableName()); - dataModel.put("importList", importList); - dataModel.put("tableName", table.getTableName()); - dataModel.put("tableComment", table.getTableComment()); - dataModel.put("className", StrUtil.lowerFirst(table.getClassName())); - dataModel.put("ClassName", table.getClassName()); - dataModel.put("fieldList", table.getFieldList()); + // 获取导入的包列表 + Set importList = fieldTypeService.getPackageByTableId(table.getDsName(), table.getTableName()); + dataModel.put("importList", importList); + dataModel.put("tableName", table.getTableName()); + dataModel.put("tableComment", table.getTableComment()); + dataModel.put("className", StrUtil.lowerFirst(table.getClassName())); + dataModel.put("ClassName", table.getClassName()); + dataModel.put("fieldList", table.getFieldList()); - dataModel.put("backendPath", table.getBackendPath()); - dataModel.put("frontendPath", table.getFrontendPath()); + dataModel.put("backendPath", table.getBackendPath()); + dataModel.put("frontendPath", table.getFrontendPath()); - // 设置子表 - String childTableName = table.getChildTableName(); - if (StrUtil.isNotBlank(childTableName)) { - List childFieldList = columnService.lambdaQuery() - .eq(GenTableColumnEntity::getDsName, table.getDsName()) - .eq(GenTableColumnEntity::getTableName, table.getChildTableName()) - .list(); - dataModel.put("childFieldList", childFieldList); - dataModel.put("childTableName", childTableName); - dataModel.put("mainField", NamingCase.toCamelCase(table.getMainField())); - dataModel.put("childField", NamingCase.toCamelCase(table.getChildField())); - dataModel.put("ChildClassName", NamingCase.toPascalCase(childTableName)); - dataModel.put("childClassName", StrUtil.lowerFirst(NamingCase.toPascalCase(childTableName))); - } + // 设置子表 + String childTableName = table.getChildTableName(); + if (StrUtil.isNotBlank(childTableName)) { + List childFieldList = columnService.lambdaQuery() + .eq(GenTableColumnEntity::getDsName, table.getDsName()) + .eq(GenTableColumnEntity::getTableName, table.getChildTableName()) + .list(); + dataModel.put("childFieldList", childFieldList); + dataModel.put("childTableName", childTableName); + dataModel.put("mainField", NamingCase.toCamelCase(table.getMainField())); + dataModel.put("childField", NamingCase.toCamelCase(table.getChildField())); + dataModel.put("ChildClassName", NamingCase.toPascalCase(childTableName)); + dataModel.put("childClassName", StrUtil.lowerFirst(NamingCase.toPascalCase(childTableName))); + } - return dataModel; - } + return dataModel; + } - /** - * 将表字段按照类型分组并存储到数据模型中 - * @param dataModel 存储数据的 Map 对象 - * @param table 表信息对象 - */ - private void setFieldTypeList(Map dataModel, GenTable table) { - // 按字段类型分组,使用 Map 存储不同类型的字段列表 - Map> typeMap = table.getFieldList() - .stream() - .collect(Collectors - .partitioningBy(columnEntity -> BooleanUtil.toBoolean(columnEntity.getPrimaryPk().toString()))); + /** + * 判断当前是否是 SpringBoot3 版本 + * + * @return true/fasle + */ + private boolean isSpringBoot3() { + return StrUtil.startWith(SpringBootVersion.getVersion(), "3"); + } - // 从分组后的 Map 中获取不同类型的字段列表 - List primaryList = typeMap.get(true); - List formList = typeMap.get(false) - .stream() - .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getFormItem())) - .collect(Collectors.toList()); - List gridList = typeMap.get(false) - .stream() - .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getGridItem())) - .collect(Collectors.toList()); - List queryList = typeMap.get(false) - .stream() - .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getQueryItem())) - .collect(Collectors.toList()); + /** + * 将表字段按照类型分组并存储到数据模型中 + * + * @param dataModel 存储数据的 Map 对象 + * @param table 表信息对象 + */ + private void setFieldTypeList(Map d ataModel, GenTable table) { + // 按字段类型分组,使用 Map 存储不同类型的字段列表 + Map> typeMap = table.getFieldList() + .stream() + .collect(Collectors + .partitioningBy(columnEntity -> BooleanUtil.toBoolean(columnEntity.getPrimaryPk().toString()))); - if (CollUtil.isNotEmpty(primaryList)) { - dataModel.put("pk", primaryList.get(0)); - } - dataModel.put("primaryList", primaryList); - dataModel.put("formList", formList); - dataModel.put("gridList", gridList); - dataModel.put("queryList", queryList); - } + // 从分组后的 Map 中获取不同类型的字段列表 + List primaryList = typeMap.get(true); + List formList = typeMap.get(false) + .stream() + .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getFormItem())) + .collect(Collectors.toList()); + List gridList = typeMap.get(false) + .stream() + .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getGridItem())) + .collect(Collectors.toList()); + List queryList = typeMap.get(false) + .stream() + .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getQueryItem())) + .collect(Collectors.toList()); + + if (CollUtil.isNotEmpty(primaryList)) { + dataModel.put("pk", primaryList.get(0)); + } + dataModel.put("primaryList", primaryList); + dataModel.put("formList", formList); + dataModel.put("gridList", gridList); + dataModel.put("queryList", queryList); + } }