update 优化 代码生成器方法

This commit is contained in:
疯狂的狮子Li
2026-03-31 17:58:18 +08:00
parent 37e1ed0bf3
commit eb850fb8cf
2 changed files with 48 additions and 70 deletions

View File

@@ -332,23 +332,9 @@ public class GenTableServiceImpl implements IGenTableService {
@Override @Override
public Map<String, String> previewCode(Long tableId) { public Map<String, String> previewCode(Long tableId) {
Map<String, String> dataMap = new LinkedHashMap<>(); Map<String, String> dataMap = new LinkedHashMap<>();
// 查询表信息 RenderContext rc = buildRenderContext(tableId);
GenTable table = getGenTable(tableId); for (PathNamedTemplate template : rc.templates()) {
List<Long> menuIds = new ArrayList<>(); dataMap.put(template.getPathName(), template.render(rc.context()));
for (int i = 0; i < 6; i++) {
menuIds.add(IdGeneratorUtil.nextLongId());
}
table.setMenuIds(menuIds);
// 设置主键列信息
setPkColumn(table);
Dict context = TemplateEngineUtils.buildContext(table);
// 获取模板列表
List<PathNamedTemplate> templates = TemplateEngineUtils.getTemplateList(table.getTplCategory());
for (PathNamedTemplate template : templates) {
// 渲染模板
String render = template.render(context);
dataMap.put(template.getPathName(), render);
} }
return dataMap; return dataMap;
} }
@@ -474,25 +460,12 @@ public class GenTableServiceImpl implements IGenTableService {
* @param zip 代码压缩输出流 * @param zip 代码压缩输出流
*/ */
private void generatorCode(Long tableId, ZipOutputStream zip) { private void generatorCode(Long tableId, ZipOutputStream zip) {
// 查询表信息 RenderContext rc = buildRenderContext(tableId);
GenTable table = getGenTable(tableId); GenTable table = rc.table();
List<Long> menuIds = new ArrayList<>(); for (PathNamedTemplate template : rc.templates()) {
for (int i = 0; i < 6; i++) {
menuIds.add(IdGeneratorUtil.nextLongId());
}
table.setMenuIds(menuIds);
// 设置主键列信息
setPkColumn(table);
Dict context = TemplateEngineUtils.buildContext(table);
// 获取模板列表
List<PathNamedTemplate> templates = TemplateEngineUtils.getTemplateList(table.getTplCategory());
for (PathNamedTemplate template : templates) {
String pathName = template.getPathName(); String pathName = template.getPathName();
// 渲染模板
try { try {
String render = template.render(context); String render = template.render(rc.context());
// 添加到zip
zip.putNextEntry(new ZipEntry(TemplateEngineUtils.getFileName(pathName, table))); zip.putNextEntry(new ZipEntry(TemplateEngineUtils.getFileName(pathName, table)));
IoUtil.write(zip, StandardCharsets.UTF_8, false, render); IoUtil.write(zip, StandardCharsets.UTF_8, false, render);
zip.flush(); zip.flush();
@@ -503,6 +476,28 @@ public class GenTableServiceImpl implements IGenTableService {
} }
} }
/**
* 构建代码渲染上下文含表信息、菜单ID、主键列、模板列表
*
* @param tableId 业务表主键
* @return 渲染上下文
*/
private RenderContext buildRenderContext(Long tableId) {
GenTable table = getGenTable(tableId);
List<Long> menuIds = new ArrayList<>();
for (int i = 0; i < 6; i++) {
menuIds.add(IdGeneratorUtil.nextLongId());
}
table.setMenuIds(menuIds);
setPkColumn(table);
Dict context = TemplateEngineUtils.buildContext(table);
List<PathNamedTemplate> templates = TemplateEngineUtils.getTemplateList(table.getTplCategory());
return new RenderContext(table, context, templates);
}
private record RenderContext(GenTable table, Dict context, List<PathNamedTemplate> templates) {
}
/** /**
* 修改保存参数校验 * 修改保存参数校验
* *

View File

@@ -111,7 +111,7 @@ public class TemplateEngineUtils {
// 向树形模板上下文写入树字段相关变量 // 向树形模板上下文写入树字段相关变量
if (GenConstants.TPL_TREE.equals(tplCategory)) { if (GenConstants.TPL_TREE.equals(tplCategory)) {
setTreeContext(context, genTable); setTreeContext(context, genTable, paramsObj);
} }
return context; return context;
@@ -120,12 +120,11 @@ public class TemplateEngineUtils {
/** /**
* 向树形模板上下文写入树字段相关变量。 * 向树形模板上下文写入树字段相关变量。
* *
* @param context 模板上下文 * @param context 模板上下文
* @param genTable 代码生成业务表对象 * @param genTable 代码生成业务表对象
* @param paramsObj 已解析的 options 参数(避免重复解析)
*/ */
public static void setTreeContext(Dict context, GenTable genTable) { public static void setTreeContext(Dict context, GenTable genTable, Dict paramsObj) {
String options = genTable.getOptions();
Dict paramsObj = JsonUtils.parseMap(options);
String treeCode = getTreeCode(paramsObj); String treeCode = getTreeCode(paramsObj);
String treeParentCode = getTreeParentCode(paramsObj); String treeParentCode = getTreeParentCode(paramsObj);
String treeName = getTreeName(paramsObj); String treeName = getTreeName(paramsObj);
@@ -133,7 +132,17 @@ public class TemplateEngineUtils {
context.put("treeCode", treeCode); context.put("treeCode", treeCode);
context.put("treeParentCode", treeParentCode); context.put("treeParentCode", treeParentCode);
context.put("treeName", treeName); context.put("treeName", treeName);
context.put("expandColumn", getExpandColumn(genTable)); String expandTreeName = paramsObj.getStr(GenConstants.TREE_NAME);
int expandColumn = 0;
for (GenTableColumn column : genTable.getColumns()) {
if (column.isList()) {
expandColumn++;
if (column.getColumnName().equals(expandTreeName)) {
break;
}
}
}
context.put("expandColumn", expandColumn);
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) { if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
context.put("tree_parent_code", paramsObj.get(GenConstants.TREE_PARENT_CODE)); context.put("tree_parent_code", paramsObj.get(GenConstants.TREE_PARENT_CODE));
} }
@@ -209,14 +218,11 @@ public class TemplateEngineUtils {
// genFilePathFormat // genFilePathFormat
if (template.contains("domain.java.vm")) { if (template.contains("domain.java.vm")) {
fileName = StringUtils.format("{}/domain/{}.java", javaPath, className); fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
} } else if (template.contains("vo.java.vm")) {
if (template.contains("vo.java.vm")) {
fileName = StringUtils.format("{}/domain/vo/{}Vo.java", javaPath, className); fileName = StringUtils.format("{}/domain/vo/{}Vo.java", javaPath, className);
} } else if (template.contains("bo.java.vm")) {
if (template.contains("bo.java.vm")) {
fileName = StringUtils.format("{}/domain/bo/{}Bo.java", javaPath, className); fileName = StringUtils.format("{}/domain/bo/{}Bo.java", javaPath, className);
} } else if (template.contains("mapper.java.vm")) {
if (template.contains("mapper.java.vm")) {
fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className); fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
} else if (template.contains("service.java.vm")) { } else if (template.contains("service.java.vm")) {
fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className); fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
@@ -370,27 +376,4 @@ public class TemplateEngineUtils {
} }
return StringUtils.EMPTY; return StringUtils.EMPTY;
} }
/**
* 获取需要在哪一列上面显示展开按钮
*
* @param genTable 业务表对象
* @return 展开按钮列序号
*/
public static int getExpandColumn(GenTable genTable) {
String options = genTable.getOptions();
Dict paramsObj = JsonUtils.parseMap(options);
String treeName = paramsObj.getStr(GenConstants.TREE_NAME);
int num = 0;
for (GenTableColumn column : genTable.getColumns()) {
if (column.isList()) {
num++;
String columnName = column.getColumnName();
if (columnName.equals(treeName)) {
break;
}
}
}
return num;
}
} }