完善代码生成项目功能和测试数据

This commit is contained in:
王鹏
2026-05-21 19:35:00 +08:00
parent a8bd6c53be
commit 23b6cbcdac
9 changed files with 2366 additions and 258 deletions

View File

@@ -117,10 +117,7 @@ public class GenProjectServiceImpl implements IGenProjectService {
*/
@Override
public List<Map<String, Object>> getProjectStructure(Long projectId, String type) {
GenProject project = genProjectMapper.selectGenProjectById(projectId);
if (project == null) {
return new ArrayList<>();
}
GenProject project = getRequiredProject(projectId);
List<GenTable> projectTables = genTableMapper.selectGenTablesByProjectId(projectId);
project.setTables(projectTables);
@@ -128,95 +125,39 @@ public class GenProjectServiceImpl implements IGenProjectService {
List<Map<String, Object>> structure = new ArrayList<>();
// 获取项目模板
SysProjectTemplate projectTemplate = sysProjectTemplateService.selectSysProjectTemplateByProjectAndType(project.getProjectId(), type);
if (projectTemplate == null) {
return structure;
}
SysProjectTemplate projectTemplate = getRequiredProjectTemplate(project, type);
// 获取模板关联的项目结构节点
List<SysProjectStructure> structureNodes = sysProjectStructureService.selectProjectStructureListByModule(project.getProjectId(), projectTemplate.getTemplateId());
// 构建树形结构
Map<Long, Map<String, Object>> nodeMap = new HashMap<>();
Map<Long, SysProjectStructure> sourceNodeMap = new HashMap<>();
Map<String, Map<String, Object>> nodeMap = new LinkedHashMap<>();
for (SysProjectStructure node : structureNodes) {
Map<String, Object> nodeData = new HashMap<>();
String nodeName = node.getNodeName();
if(nodeName.contains("{projectName}")){
nodeName = nodeName.replace("{projectName}", project.getProjectFileName());
} else if(nodeName.contains("{packageName}")){
nodeName = nodeName.replace("{packageName}", project.getPackageName());
}
Long tableId = node.getTableId();
if(tableId != null && tableId == 0){
for(GenTable table : projectTables){
Map<String, Object> tableNodeData = new HashMap<>();
String tableNodeName = nodeName;
if(tableNodeName.contains("{ClassName}")){
tableNodeName = tableNodeName.replace("{ClassName}", table.getClassName());
}
Long nodeId = node.getNodeId() * 1000 + table.getTableId();
tableNodeData.put("id", nodeId);
tableNodeData.put("name", tableNodeName);
tableNodeData.put("type", node.getNodeType());
tableNodeData.put("category", node.getCategory());
tableNodeData.put("tableId", table.getTableId());
nodeMap.put(nodeId, tableNodeData);
sourceNodeMap.put(node.getNodeId(), node);
if (isTableScopedNode(node)) {
for (GenTable table : projectTables) {
Map<String, Object> tableNodeData = createStructureNodeData(node, project, table);
nodeMap.put(getNodeKey(node.getNodeId(), table.getTableId()), tableNodeData);
}
} else{
nodeData.put("id", node.getNodeId());
nodeData.put("name", nodeName);
nodeData.put("type", node.getNodeType());
nodeData.put("category", node.getCategory());
nodeData.put("tableId", tableId);
nodeMap.put(node.getNodeId(), nodeData);
}
if ("folder".equals(node.getNodeType())) {
nodeData.put("children", new ArrayList<>());
nodeMap.put(node.getNodeId(), nodeData);
} else {
Map<String, Object> nodeData = createStructureNodeData(node, project, null);
nodeMap.put(getNodeKey(node.getNodeId(), null), nodeData);
}
}
// 构建父子关系
for (SysProjectStructure node : structureNodes) {
if (node.getTableId() != null && node.getTableId() == 0) {
if (isTableScopedNode(node)) {
// 处理表特定的节点
for (GenTable table : projectTables) {
Long nodeId = node.getNodeId() * 1000 + table.getTableId();
Map<String, Object> nodeData = nodeMap.get(nodeId);
if (nodeData != null) {
if (node.getParentId() == 0) {
structure.add(nodeData);
} else {
Map<String, Object> parentNode = nodeMap.get(node.getParentId());
if (parentNode != null) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.get("children");
if (children != null) {
children.add(nodeData);
}
}
}
}
Map<String, Object> nodeData = nodeMap.get(getNodeKey(node.getNodeId(), table.getTableId()));
addNodeToParent(structure, nodeData, node, nodeMap, sourceNodeMap, table);
}
} else {
// 处理普通节点
Map<String, Object> nodeData = nodeMap.get(node.getNodeId());
if (nodeData != null) {
if (node.getParentId() == 0) {
structure.add(nodeData);
} else {
Map<String, Object> parentNode = nodeMap.get(node.getParentId());
if (parentNode != null) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.get("children");
if (children != null) {
children.add(nodeData);
}
}
}
}
Map<String, Object> nodeData = nodeMap.get(getNodeKey(node.getNodeId(), null));
addNodeToParent(structure, nodeData, node, nodeMap, sourceNodeMap, null);
}
}
@@ -225,10 +166,7 @@ public class GenProjectServiceImpl implements IGenProjectService {
@Override
public byte[] downloadStructure(Long projectId, String type) {
GenProject project = genProjectMapper.selectGenProjectById(projectId);
if (project == null) {
throw new ServiceException("项目不存在");
}
GenProject project = getRequiredProject(projectId);
List<GenTable> projectTables = genTableMapper.selectGenTablesByProjectId(projectId);
project.setTables(projectTables);
@@ -296,7 +234,7 @@ public class GenProjectServiceImpl implements IGenProjectService {
public Map<String, String> previewCode(Long projectId, Long tableId, String keyword, String type)
{
Map<String, String> dataMap = new LinkedHashMap<>();
GenProject project = genProjectMapper.selectGenProjectById(projectId);
GenProject project = getRequiredProject(projectId);
List<GenTable> projectTables = genTableMapper.selectGenTablesByProjectId(projectId);
project.setTables(projectTables);
@@ -305,38 +243,15 @@ public class GenProjectServiceImpl implements IGenProjectService {
List<SysProjectModule> modules = sysProjectModuleService.selectSysProjectModuleList(projectModuleFilter);
VelocityInitializer.initVelocity();
VelocityContext context = null;
String tplCategory = "";
if(!Objects.isNull(tableId) && tableId != -1){
// 查询表信息
GenTable table = genTableMapper.selectGenTableById(tableId);
table.setModules(modules);
tplCategory = table.getTplCategory();
table.setPackageName(project.getPackageName());
// 设置主子表信息
setSubTable(table);
// 设置主键列信息
setPkColumn(table);
// 设置代码片段
setCodeSnippet(table);
context = VelocityUtils.prepareContext(table);
} else{
context = VelocityUtils.prepareContextProject(project);
}
// 获取模板列表
SysProjectTemplate projectTemplate = sysProjectTemplateService.selectSysProjectTemplateByProjectAndType(project.getProjectId(), type);
List<TemplateFile> templateFileList = templateFileService.selectTemplateFilesByTemplateId(projectTemplate.getTemplateId());
SysProjectTemplate projectTemplate = getRequiredProjectTemplate(project, type);
List<TemplateFile> templateFileList = getRequiredTemplateFiles(projectTemplate.getTemplateId());
for (TemplateFile templateFile : templateFileList)
{
if(templateFile.getFileName().contains(keyword)){
if(matchesTemplateFile(templateFile, keyword)){
// 渲染模板
StringWriter sw = new StringWriter();
String tplContent = templateFile.getFileContent();
Velocity.evaluate(context, sw, "", tplContent);
dataMap.put(templateFile.getFileName(), sw.toString());
dataMap.put(templateFile.getFileName(), renderTemplate(project, tableId, modules, templateFile));
}
}
return dataMap;
@@ -351,67 +266,219 @@ public class GenProjectServiceImpl implements IGenProjectService {
List<SysProjectModule> modules = sysProjectModuleService.selectSysProjectModuleList(projectModuleFilter);
VelocityInitializer.initVelocity();
VelocityContext context = null;
String tplCategory = "";
if(!Objects.isNull(tableId) && tableId != -1){
// 查询表信息
GenTable table = genTableMapper.selectGenTableById(tableId);
table.setModules(modules);
tplCategory = table.getTplCategory();
table.setPackageName(project.getPackageName());
// 设置主子表信息
setSubTable(table);
// 设置主键列信息
setPkColumn(table);
// 设置代码片段
setCodeSnippet(table);
context = VelocityUtils.prepareContext(table);
} else {
context = VelocityUtils.prepareContextProject(project);
}
// 获取模板列表
SysProjectTemplate projectTemplate = sysProjectTemplateService.selectSysProjectTemplateByProjectAndType(project.getProjectId(), type);
List<TemplateFile> templateFileList = templateFileService.selectTemplateFilesByTemplateId(projectTemplate.getTemplateId());
SysProjectTemplate projectTemplate = getRequiredProjectTemplate(project, type);
List<TemplateFile> templateFileList = getRequiredTemplateFiles(projectTemplate.getTemplateId());
for (TemplateFile templateFile : templateFileList)
{
if(templateFile.getFileName().contains(category)){
if(matchesTemplateFile(templateFile, category)){
try {
StringWriter sw = new StringWriter();
String tplContent = templateFile.getFileContent();
Velocity.evaluate(context, sw, "", tplContent);
return sw.toString();
return renderTemplate(project, tableId, modules, templateFile);
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("渲染模板失败,表名:" + tableId);
}
}
}
return null;
throw new ServiceException("未找到模板文件:" + category);
}
/**
* 设置代码片段
* @param table
*/
private void setCodeSnippet(GenTable table) {
private GenProject getRequiredProject(Long projectId) {
GenProject project = genProjectMapper.selectGenProjectById(projectId);
if (project == null) {
throw new ServiceException("项目不存在");
}
return project;
}
private SysProjectTemplate getRequiredProjectTemplate(GenProject project, String type) {
SysProjectTemplate projectTemplate = sysProjectTemplateService.selectSysProjectTemplateByProjectAndType(project.getProjectId(), type);
if (projectTemplate == null || projectTemplate.getTemplateId() == null) {
throw new ServiceException("项目未配置" + type + "模板");
}
return projectTemplate;
}
private List<TemplateFile> getRequiredTemplateFiles(Long templateId) {
List<TemplateFile> templateFileList = templateFileService.selectTemplateFilesByTemplateId(templateId);
if (StringUtils.isEmpty(templateFileList)) {
throw new ServiceException("模板未配置文件");
}
return templateFileList;
}
private boolean isTableScopedNode(SysProjectStructure node) {
return node.getTableId() != null && node.getTableId() == 0;
}
private String getNodeKey(Long nodeId, Long tableId) {
return tableId == null ? String.valueOf(nodeId) : nodeId + "-" + tableId;
}
private Map<String, Object> createStructureNodeData(SysProjectStructure node, GenProject project, GenTable table) {
Map<String, Object> nodeData = new LinkedHashMap<>();
nodeData.put("id", table == null ? node.getNodeId() : getNodeKey(node.getNodeId(), table.getTableId()));
nodeData.put("name", replaceNodeName(node.getNodeName(), project, table));
nodeData.put("type", node.getNodeType());
nodeData.put("category", node.getCategory());
nodeData.put("tableId", table == null ? node.getTableId() : table.getTableId());
if ("folder".equals(node.getNodeType())) {
nodeData.put("children", new ArrayList<>());
}
return nodeData;
}
private String replaceNodeName(String nodeName, GenProject project, GenTable table) {
if (nodeName == null) {
return "";
}
String result = nodeName;
result = result.replace("{projectName}", StringUtils.defaultString(project.getProjectFileName()));
result = result.replace("{packageName}", StringUtils.defaultString(project.getPackageName()));
if (table != null) {
result = result.replace("{ClassName}", StringUtils.defaultString(table.getClassName()));
result = result.replace("{className}", StringUtils.uncapitalize(StringUtils.defaultString(table.getClassName())));
result = result.replace("{tableName}", StringUtils.defaultString(table.getTableName()));
result = result.replace("{businessName}", StringUtils.defaultString(table.getBusinessName()));
}
return result;
}
private void addNodeToParent(List<Map<String, Object>> roots, Map<String, Object> nodeData,
SysProjectStructure node, Map<String, Map<String, Object>> nodeMap,
Map<Long, SysProjectStructure> sourceNodeMap, GenTable table) {
if (nodeData == null) {
return;
}
if (node.getParentId() == null || node.getParentId() == 0) {
roots.add(nodeData);
return;
}
SysProjectStructure parentSource = sourceNodeMap.get(node.getParentId());
Long parentTableId = parentSource != null && isTableScopedNode(parentSource) && table != null ? table.getTableId() : null;
Map<String, Object> parentNode = nodeMap.get(getNodeKey(node.getParentId(), parentTableId));
if (parentNode == null) {
return;
}
@SuppressWarnings("unchecked")
List<Map<String, Object>> children = (List<Map<String, Object>>) parentNode.get("children");
if (children != null) {
children.add(nodeData);
}
}
private boolean matchesTemplateFile(TemplateFile templateFile, String templateFileName) {
if (templateFile == null || StringUtils.isEmpty(templateFileName)) {
return false;
}
return StringUtils.equals(templateFile.getFileName(), templateFileName)
|| StringUtils.equals(templateFile.getFilePath(), templateFileName);
}
private String renderTemplate(GenProject project, Long tableId, List<SysProjectModule> modules, TemplateFile templateFile) {
VelocityContext context = prepareVelocityContext(project, tableId, modules, templateFile);
StringWriter sw = new StringWriter();
Velocity.evaluate(context, sw, "", StringUtils.defaultString(templateFile.getFileContent()));
return sw.toString();
}
private VelocityContext prepareVelocityContext(GenProject project, Long tableId, List<SysProjectModule> modules, TemplateFile templateFile) {
if (tableId != null && tableId != -1) {
GenTable table = getRequiredTable(tableId);
table.setModules(modules);
table.setPackageName(project.getPackageName());
// 设置主子表信息
setSubTable(table);
// 设置主键列信息
setPkColumn(table);
VelocityContext context = VelocityUtils.prepareContext(table);
putSnippetVariables(context, table, modules, templateFile);
return context;
}
return VelocityUtils.prepareContextProject(project);
}
private GenTable getRequiredTable(Long tableId) {
GenTable table = genTableMapper.selectGenTableById(tableId);
if (table == null) {
throw new ServiceException("业务表不存在:" + tableId);
}
if (StringUtils.isEmpty(table.getColumns())) {
throw new ServiceException("业务表未配置字段:" + table.getTableName());
}
return table;
}
private void putSnippetVariables(VelocityContext context, GenTable table, List<SysProjectModule> modules, TemplateFile templateFile) {
SysCodeSnippet codeSnippet = new SysCodeSnippet();
// codeSnippet.setRelationTableName(table.getTableName());
codeSnippet.setStatus("0");
List<SysCodeSnippet> codeSnippetList = codeSnippetService.selectSysCodeSnippetList(codeSnippet);
table.setCodeSnippetList(codeSnippetList);
if (StringUtils.isEmpty(codeSnippetList)) {
return;
}
Map<String, StringBuilder> snippetContentMap = new LinkedHashMap<>();
for (SysCodeSnippet snippet : codeSnippetList) {
if (StringUtils.isNotEmpty(snippet.getInsertPoint())) {
snippetContentMap.putIfAbsent(snippet.getInsertPoint(), new StringBuilder());
}
}
for (SysCodeSnippet snippet : codeSnippetList) {
if (matchesSnippet(snippet, table, modules, templateFile)) {
StringBuilder content = snippetContentMap.get(snippet.getInsertPoint());
if (content.length() > 0) {
content.append(System.lineSeparator());
}
content.append(snippet.getSnippetContent());
}
}
for (Map.Entry<String, StringBuilder> entry : snippetContentMap.entrySet()) {
context.put(entry.getKey(), entry.getValue().toString());
}
}
/**
* 获取包前缀
*
* @param packageName 包名称
* @return 包前缀名称
*/
private String getPackagePrefix(String packageName) {
int lastIndex = packageName.lastIndexOf(".");
return StringUtils.substring(packageName, 0, lastIndex);
private boolean matchesSnippet(SysCodeSnippet snippet, GenTable table, List<SysProjectModule> modules, TemplateFile templateFile) {
return snippet != null
&& StringUtils.isNotEmpty(snippet.getInsertPoint())
&& StringUtils.isNotEmpty(snippet.getSnippetContent())
&& isSnippetEnabled(snippet)
&& matchesSnippetModule(snippet, modules)
&& matchesSnippetTable(snippet, table)
&& matchesSnippetTemplate(snippet, templateFile);
}
private boolean isSnippetEnabled(SysCodeSnippet snippet) {
return (StringUtils.isEmpty(snippet.getStatus()) || "0".equals(snippet.getStatus()))
&& (StringUtils.isEmpty(snippet.getDelFlag()) || "0".equals(snippet.getDelFlag()));
}
private boolean matchesSnippetModule(SysCodeSnippet snippet, List<SysProjectModule> modules) {
if (snippet.getModuleId() == null) {
return true;
}
if (StringUtils.isEmpty(modules)) {
return false;
}
for (SysProjectModule module : modules) {
if (snippet.getModuleId().equals(module.getModuleId())
&& (StringUtils.isEmpty(module.getStatus()) || "0".equals(module.getStatus()))) {
return true;
}
}
return false;
}
private boolean matchesSnippetTable(SysCodeSnippet snippet, GenTable table) {
return StringUtils.isEmpty(snippet.getRelationTableName())
|| StringUtils.equals(snippet.getRelationTableName(), table.getTableName());
}
private boolean matchesSnippetTemplate(SysCodeSnippet snippet, TemplateFile templateFile) {
return StringUtils.isEmpty(snippet.getTemplateFileName())
|| matchesTemplateFile(templateFile, snippet.getTemplateFileName());
}
/**
@@ -442,4 +509,4 @@ public class GenProjectServiceImpl implements IGenProjectService {
table.setSubTable(genTableMapper.selectGenTableByName(subTableName));
}
}
}
}

View File

@@ -78,17 +78,44 @@ public class VelocityUtils {
List<SysProjectModule> modules = genTable.getModules();
if (!Objects.isNull(codeList)) {
for (SysCodeSnippet code : codeList) {
if (modules.stream().anyMatch(module -> module.getModuleId().equals(code.getModuleId()))
&& code.getRelationTableName().equals(genTable.getTableName())) {
velocityContext.put(code.getInsertPoint(), code.getSnippetContent());
} else {
if (StringUtils.isNotEmpty(code.getInsertPoint())) {
velocityContext.put(code.getInsertPoint(), "");
}
}
for (SysCodeSnippet code : codeList) {
if (matchesSnippet(code, modules, genTable)) {
String existing = StringUtils.defaultString((String) velocityContext.get(code.getInsertPoint()));
String value = StringUtils.isEmpty(existing) ? code.getSnippetContent() : existing + System.lineSeparator() + code.getSnippetContent();
velocityContext.put(code.getInsertPoint(), value);
}
}
}
return velocityContext;
}
private static boolean matchesSnippet(SysCodeSnippet code, List<SysProjectModule> modules, GenTable genTable) {
if (code == null || StringUtils.isEmpty(code.getInsertPoint()) || StringUtils.isEmpty(code.getSnippetContent())) {
return false;
}
if (StringUtils.isNotEmpty(code.getStatus()) && !"0".equals(code.getStatus())) {
return false;
}
if (StringUtils.isNotEmpty(code.getDelFlag()) && !"0".equals(code.getDelFlag())) {
return false;
}
if (StringUtils.isNotEmpty(code.getRelationTableName()) && !code.getRelationTableName().equals(genTable.getTableName())) {
return false;
}
if (code.getModuleId() == null) {
return true;
}
if (StringUtils.isEmpty(modules)) {
return false;
}
return modules.stream().anyMatch(module -> code.getModuleId().equals(module.getModuleId())
&& (StringUtils.isEmpty(module.getStatus()) || "0".equals(module.getStatus())));
}
/**
* 设置模板变量信息
*