From 88aa9d8e787457826660e3e7b7c9d8e235a81320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E6=9C=B5=E6=A2=A8=E8=8A=B1=E5=8E=8B=E6=B5=B7?= =?UTF-8?q?=E6=A3=A0?= Date: Fri, 9 Oct 2020 14:59:28 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=94=9F=E6=88=90=E4=BB=A3=E7=A0=81=E9=A2=84?= =?UTF-8?q?=E8=A7=88=20https://gitee.com/log4j/pig/issues/I1WQ3C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GeneratorController.java | 10 +++ .../pig/codegen/service/GeneratorService.java | 7 +++ .../service/impl/GeneratorServiceImpl.java | 32 ++++++++++ .../pig/codegen/util/CodeGenUtils.java | 62 +++++++++++++++---- 4 files changed, 99 insertions(+), 12 deletions(-) diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/controller/GeneratorController.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/controller/GeneratorController.java index 4e078593..9dfd5366 100755 --- a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/controller/GeneratorController.java +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/controller/GeneratorController.java @@ -54,6 +54,16 @@ public class GeneratorController { return R.ok(generatorService.getPage(page, tableName, dsName)); } + /** + * 预览代码 + * @param genConfig 数据表配置 + * @return + */ + @GetMapping("/preview") + public R previewCode(GenConfig genConfig) { + return R.ok(generatorService.previewCode(genConfig)); + } + /** * 生成代码 */ diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/GeneratorService.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/GeneratorService.java index efa03a0c..380db098 100644 --- a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/GeneratorService.java +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/GeneratorService.java @@ -45,4 +45,11 @@ public interface GeneratorService { */ IPage>> getPage(Page page, String tableName, String name); + /** + * 预览代码 + * @param genConfig 查询条件 + * @return + */ + Map previewCode(GenConfig genConfig); + } diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java index da3ee9b9..71864c57 100755 --- a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java @@ -18,8 +18,10 @@ package com.pig4cloud.pig.codegen.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -63,6 +65,36 @@ public class GeneratorServiceImpl implements GeneratorService { return generatorMapper.queryList(page, tableName); } + /** + * 预览代码 + * @param genConfig 查询条件 + * @return + */ + @Override + public Map previewCode(GenConfig genConfig) { + // 根据tableName 查询最新的表单配置 + List formConfList = genFormConfMapper.selectList(Wrappers.lambdaQuery() + .eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime)); + + DynamicDataSourceContextHolder.push(genConfig.getDsName()); + + String tableNames = genConfig.getTableName(); + for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) { + // 查询表信息 + Map table = generatorMapper.queryTable(tableName, genConfig.getDsName()); + // 查询列信息 + List> columns = generatorMapper.queryColumns(tableName, genConfig.getDsName()); + // 生成代码 + if (CollUtil.isNotEmpty(formConfList)) { + return CodeGenUtils.generatorCode(genConfig, table, columns, null, formConfList.get(0)); + } + else { + return CodeGenUtils.generatorCode(genConfig, table, columns, null, null); + } + } + return MapUtil.empty(); + } + /** * 生成代码 * @param genConfig 生成配置 diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/util/CodeGenUtils.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/util/CodeGenUtils.java index 0314e0e8..e4dc26d4 100644 --- a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/util/CodeGenUtils.java +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/util/CodeGenUtils.java @@ -39,6 +39,7 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import java.io.File; +import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.*; @@ -102,8 +103,8 @@ public class CodeGenUtils { * 生成代码 */ @SneakyThrows - public void generatorCode(GenConfig genConfig, Map table, List> columns, - ZipOutputStream zip, GenFormConf formConf) { + public Map generatorCode(GenConfig genConfig, Map table, + List> columns, ZipOutputStream zip, GenFormConf formConf) { // 配置信息 Configuration config = getConfig(); boolean hasBigDecimal = false; @@ -211,18 +212,48 @@ public class CodeGenUtils { map.put("package", config.getString("package")); map.put("mainPath", config.getString("mainPath")); } + + // 渲染数据 + return renderData(genConfig, zip, formConf, tableEntity, map); + } + + /** + * 渲染数据 + * @param genConfig 配置信息 + * @param zip 流 (为空,直接返回Map) + * @param formConf 表单信息 + * @param tableEntity 表基本信息 + * @param map 模板参数 + * @return map key-filename value-contents + * @throws IOException + */ + private Map renderData(GenConfig genConfig, ZipOutputStream zip, GenFormConf formConf, + TableEntity tableEntity, Map map) throws IOException { + // 设置velocity资源加载器 + Properties prop = new Properties(); + prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + Velocity.init(prop); VelocityContext context = new VelocityContext(map); // 获取模板列表 List templates = getTemplates(); + Map resultMap = new HashMap<>(8); + for (String template : templates) { // 如果是crud if (template.contains(AVUE_CRUD_JS_VM) && formConf != null) { - zip.putNextEntry( - new ZipEntry(Objects.requireNonNull(getFileName(template, tableEntity.getCaseClassName(), - map.get("package").toString(), map.get("moduleName").toString())))); - IoUtil.write(zip, StandardCharsets.UTF_8, false, CRUD_PREFIX + formConf.getFormInfo()); - zip.closeEntry(); + + String fileName = getFileName(template, tableEntity.getCaseClassName(), map.get("package").toString(), + map.get("moduleName").toString()); + String contents = CRUD_PREFIX + formConf.getFormInfo(); + + if (zip != null) { + zip.putNextEntry(new ZipEntry(Objects.requireNonNull(fileName))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, contents); + zip.closeEntry(); + } + + resultMap.put(template, contents); continue; } @@ -232,12 +263,19 @@ public class CodeGenUtils { tpl.merge(context, sw); // 添加到zip - zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getFileName(template, tableEntity.getCaseClassName(), - map.get("package").toString(), map.get("moduleName").toString())))); - IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString()); - IoUtil.close(sw); - zip.closeEntry(); + String fileName = getFileName(template, tableEntity.getCaseClassName(), map.get("package").toString(), + map.get("moduleName").toString()); + + if (zip != null) { + zip.putNextEntry(new ZipEntry(Objects.requireNonNull(fileName))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString()); + IoUtil.close(sw); + zip.closeEntry(); + } + resultMap.put(template, sw.toString()); } + + return resultMap; } /**