mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-04-03 17:23:25 +08:00
update 优化 代码生成器方法
This commit is contained in:
@@ -332,23 +332,9 @@ public class GenTableServiceImpl implements IGenTableService {
|
||||
@Override
|
||||
public Map<String, String> previewCode(Long tableId) {
|
||||
Map<String, String> dataMap = new LinkedHashMap<>();
|
||||
// 查询表信息
|
||||
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());
|
||||
for (PathNamedTemplate template : templates) {
|
||||
// 渲染模板
|
||||
String render = template.render(context);
|
||||
dataMap.put(template.getPathName(), render);
|
||||
RenderContext rc = buildRenderContext(tableId);
|
||||
for (PathNamedTemplate template : rc.templates()) {
|
||||
dataMap.put(template.getPathName(), template.render(rc.context()));
|
||||
}
|
||||
return dataMap;
|
||||
}
|
||||
@@ -474,25 +460,12 @@ public class GenTableServiceImpl implements IGenTableService {
|
||||
* @param zip 代码压缩输出流
|
||||
*/
|
||||
private void generatorCode(Long tableId, ZipOutputStream zip) {
|
||||
// 查询表信息
|
||||
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());
|
||||
for (PathNamedTemplate template : templates) {
|
||||
RenderContext rc = buildRenderContext(tableId);
|
||||
GenTable table = rc.table();
|
||||
for (PathNamedTemplate template : rc.templates()) {
|
||||
String pathName = template.getPathName();
|
||||
// 渲染模板
|
||||
try {
|
||||
String render = template.render(context);
|
||||
// 添加到zip
|
||||
String render = template.render(rc.context());
|
||||
zip.putNextEntry(new ZipEntry(TemplateEngineUtils.getFileName(pathName, table)));
|
||||
IoUtil.write(zip, StandardCharsets.UTF_8, false, render);
|
||||
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) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改保存参数校验
|
||||
*
|
||||
|
||||
@@ -111,7 +111,7 @@ public class TemplateEngineUtils {
|
||||
|
||||
// 向树形模板上下文写入树字段相关变量
|
||||
if (GenConstants.TPL_TREE.equals(tplCategory)) {
|
||||
setTreeContext(context, genTable);
|
||||
setTreeContext(context, genTable, paramsObj);
|
||||
}
|
||||
|
||||
return context;
|
||||
@@ -120,12 +120,11 @@ public class TemplateEngineUtils {
|
||||
/**
|
||||
* 向树形模板上下文写入树字段相关变量。
|
||||
*
|
||||
* @param context 模板上下文
|
||||
* @param genTable 代码生成业务表对象
|
||||
* @param context 模板上下文
|
||||
* @param genTable 代码生成业务表对象
|
||||
* @param paramsObj 已解析的 options 参数(避免重复解析)
|
||||
*/
|
||||
public static void setTreeContext(Dict context, GenTable genTable) {
|
||||
String options = genTable.getOptions();
|
||||
Dict paramsObj = JsonUtils.parseMap(options);
|
||||
public static void setTreeContext(Dict context, GenTable genTable, Dict paramsObj) {
|
||||
String treeCode = getTreeCode(paramsObj);
|
||||
String treeParentCode = getTreeParentCode(paramsObj);
|
||||
String treeName = getTreeName(paramsObj);
|
||||
@@ -133,7 +132,17 @@ public class TemplateEngineUtils {
|
||||
context.put("treeCode", treeCode);
|
||||
context.put("treeParentCode", treeParentCode);
|
||||
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)) {
|
||||
context.put("tree_parent_code", paramsObj.get(GenConstants.TREE_PARENT_CODE));
|
||||
}
|
||||
@@ -209,14 +218,11 @@ public class TemplateEngineUtils {
|
||||
// genFilePathFormat
|
||||
if (template.contains("domain.java.vm")) {
|
||||
fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
|
||||
}
|
||||
if (template.contains("vo.java.vm")) {
|
||||
} else if (template.contains("vo.java.vm")) {
|
||||
fileName = StringUtils.format("{}/domain/vo/{}Vo.java", javaPath, className);
|
||||
}
|
||||
if (template.contains("bo.java.vm")) {
|
||||
} else if (template.contains("bo.java.vm")) {
|
||||
fileName = StringUtils.format("{}/domain/bo/{}Bo.java", javaPath, className);
|
||||
}
|
||||
if (template.contains("mapper.java.vm")) {
|
||||
} else if (template.contains("mapper.java.vm")) {
|
||||
fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
|
||||
} else if (template.contains("service.java.vm")) {
|
||||
fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
|
||||
@@ -370,27 +376,4 @@ public class TemplateEngineUtils {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user