mirror of https://gitee.com/y_project/RuoYi.git
parent
a2c1bfef8f
commit
cd10f48f53
@ -1,315 +1,315 @@
|
|||||||
package com.ruoyi.generator.util;
|
package com.ruoyi.generator.util;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.apache.velocity.VelocityContext;
|
import org.apache.velocity.VelocityContext;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.ruoyi.common.constant.GenConstants;
|
import com.ruoyi.common.constant.GenConstants;
|
||||||
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.generator.config.GenConfig;
|
import com.ruoyi.generator.config.GenConfig;
|
||||||
import com.ruoyi.generator.domain.GenTable;
|
import com.ruoyi.generator.domain.GenTable;
|
||||||
import com.ruoyi.generator.domain.GenTableColumn;
|
import com.ruoyi.generator.domain.GenTableColumn;
|
||||||
|
|
||||||
public class VelocityUtils
|
public class VelocityUtils
|
||||||
{
|
{
|
||||||
/** 项目空间路径 */
|
/** 项目空间路径 */
|
||||||
private static final String PROJECT_PATH = "main/java";
|
private static final String PROJECT_PATH = "main/java";
|
||||||
|
|
||||||
/** mybatis空间路径 */
|
/** mybatis空间路径 */
|
||||||
private static final String MYBATIS_PATH = "main/resources/mapper";
|
private static final String MYBATIS_PATH = "main/resources/mapper";
|
||||||
|
|
||||||
/** html空间路径 */
|
/** html空间路径 */
|
||||||
private static final String TEMPLATES_PATH = "main/resources/templates";
|
private static final String TEMPLATES_PATH = "main/resources/templates";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置模板变量信息
|
* 设置模板变量信息
|
||||||
*
|
*
|
||||||
* @return 模板列表
|
* @return 模板列表
|
||||||
*/
|
*/
|
||||||
public static VelocityContext prepareContext(GenTable genTable)
|
public static VelocityContext prepareContext(GenTable genTable)
|
||||||
{
|
{
|
||||||
String moduleName = genTable.getModuleName();
|
String moduleName = genTable.getModuleName();
|
||||||
String businessName = genTable.getBusinessName();
|
String businessName = genTable.getBusinessName();
|
||||||
String packageName = genTable.getPackageName();
|
String packageName = genTable.getPackageName();
|
||||||
String tplCategory = genTable.getTplCategory();
|
String tplCategory = genTable.getTplCategory();
|
||||||
|
|
||||||
VelocityContext velocityContext = new VelocityContext();
|
VelocityContext velocityContext = new VelocityContext();
|
||||||
velocityContext.put("tplCategory", genTable.getTplCategory());
|
velocityContext.put("tplCategory", genTable.getTplCategory());
|
||||||
velocityContext.put("tableName", genTable.getTableName());
|
velocityContext.put("tableName", genTable.getTableName());
|
||||||
velocityContext.put("functionName", genTable.getFunctionName());
|
velocityContext.put("functionName", genTable.getFunctionName());
|
||||||
velocityContext.put("ClassName", genTable.getClassName());
|
velocityContext.put("ClassName", genTable.getClassName());
|
||||||
velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
|
velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
|
||||||
velocityContext.put("moduleName", genTable.getModuleName());
|
velocityContext.put("moduleName", genTable.getModuleName());
|
||||||
velocityContext.put("businessName", genTable.getBusinessName());
|
velocityContext.put("businessName", genTable.getBusinessName());
|
||||||
velocityContext.put("basePackage", getPackagePrefix(packageName));
|
velocityContext.put("basePackage", getPackagePrefix(packageName));
|
||||||
velocityContext.put("packageName", packageName);
|
velocityContext.put("packageName", packageName);
|
||||||
velocityContext.put("author", genTable.getFunctionAuthor());
|
velocityContext.put("author", genTable.getFunctionAuthor());
|
||||||
velocityContext.put("datetime", DateUtils.getDate());
|
velocityContext.put("datetime", DateUtils.getDate());
|
||||||
velocityContext.put("pkColumn", genTable.getPkColumn());
|
velocityContext.put("pkColumn", genTable.getPkColumn());
|
||||||
velocityContext.put("importList", getImportList(genTable.getColumns()));
|
velocityContext.put("importList", getImportList(genTable.getColumns()));
|
||||||
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
|
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
|
||||||
velocityContext.put("columns", genTable.getColumns());
|
velocityContext.put("columns", genTable.getColumns());
|
||||||
velocityContext.put("table", genTable);
|
velocityContext.put("table", genTable);
|
||||||
if (GenConstants.TPL_TREE.equals(tplCategory))
|
if (GenConstants.TPL_TREE.equals(tplCategory))
|
||||||
{
|
{
|
||||||
setTreeVelocityContext(velocityContext, genTable);
|
setTreeVelocityContext(velocityContext, genTable);
|
||||||
}
|
}
|
||||||
return velocityContext;
|
return velocityContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setTreeVelocityContext(VelocityContext context, GenTable genTable)
|
public static void setTreeVelocityContext(VelocityContext context, GenTable genTable)
|
||||||
{
|
{
|
||||||
String options = genTable.getOptions();
|
String options = genTable.getOptions();
|
||||||
JSONObject paramsObj = JSONObject.parseObject(options);
|
JSONObject paramsObj = JSONObject.parseObject(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);
|
||||||
|
|
||||||
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));
|
context.put("expandColumn", getExpandColumn(genTable));
|
||||||
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
|
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
|
||||||
{
|
{
|
||||||
context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
|
context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取模板信息
|
* 获取模板信息
|
||||||
*
|
*
|
||||||
* @return 模板列表
|
* @return 模板列表
|
||||||
*/
|
*/
|
||||||
public static List<String> getTemplateList(String tplCategory)
|
public static List<String> getTemplateList(String tplCategory)
|
||||||
{
|
{
|
||||||
List<String> templates = new ArrayList<String>();
|
List<String> templates = new ArrayList<String>();
|
||||||
templates.add("vm/java/domain.java.vm");
|
templates.add("vm/java/domain.java.vm");
|
||||||
templates.add("vm/java/mapper.java.vm");
|
templates.add("vm/java/mapper.java.vm");
|
||||||
templates.add("vm/java/service.java.vm");
|
templates.add("vm/java/service.java.vm");
|
||||||
templates.add("vm/java/serviceImpl.java.vm");
|
templates.add("vm/java/serviceImpl.java.vm");
|
||||||
templates.add("vm/java/controller.java.vm");
|
templates.add("vm/java/controller.java.vm");
|
||||||
templates.add("vm/xml/mapper.xml.vm");
|
templates.add("vm/xml/mapper.xml.vm");
|
||||||
if (GenConstants.TPL_CRUD.equals(tplCategory))
|
if (GenConstants.TPL_CRUD.equals(tplCategory))
|
||||||
{
|
{
|
||||||
templates.add("vm/html/list.html.vm");
|
templates.add("vm/html/list.html.vm");
|
||||||
}
|
}
|
||||||
else if (GenConstants.TPL_TREE.equals(tplCategory))
|
else if (GenConstants.TPL_TREE.equals(tplCategory))
|
||||||
{
|
{
|
||||||
templates.add("vm/html/tree.html.vm");
|
templates.add("vm/html/tree.html.vm");
|
||||||
templates.add("vm/html/list-tree.html.vm");
|
templates.add("vm/html/list-tree.html.vm");
|
||||||
}
|
}
|
||||||
templates.add("vm/html/add.html.vm");
|
templates.add("vm/html/add.html.vm");
|
||||||
templates.add("vm/html/edit.html.vm");
|
templates.add("vm/html/edit.html.vm");
|
||||||
templates.add("vm/sql/sql.vm");
|
templates.add("vm/sql/sql.vm");
|
||||||
return templates;
|
return templates;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件名
|
* 获取文件名
|
||||||
*/
|
*/
|
||||||
public static String getFileName(String template, GenTable genTable)
|
public static String getFileName(String template, GenTable genTable)
|
||||||
{
|
{
|
||||||
// 文件名称
|
// 文件名称
|
||||||
String fileName = "";
|
String fileName = "";
|
||||||
// 包路径
|
// 包路径
|
||||||
String packageName = genTable.getPackageName();
|
String packageName = genTable.getPackageName();
|
||||||
// 模块名
|
// 模块名
|
||||||
String moduleName = genTable.getModuleName();
|
String moduleName = genTable.getModuleName();
|
||||||
// 大写类名
|
// 大写类名
|
||||||
String className = genTable.getClassName();
|
String className = genTable.getClassName();
|
||||||
// 业务名称
|
// 业务名称
|
||||||
String businessName = genTable.getBusinessName();
|
String businessName = genTable.getBusinessName();
|
||||||
|
|
||||||
String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
|
String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
|
||||||
String mybatisPath = MYBATIS_PATH + "/" + moduleName;
|
String mybatisPath = MYBATIS_PATH + "/" + moduleName;
|
||||||
String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + businessName;
|
String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + businessName;
|
||||||
|
|
||||||
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("mapper.java.vm"))
|
else 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);
|
||||||
}
|
}
|
||||||
else if (template.contains("serviceImpl.java.vm"))
|
else if (template.contains("serviceImpl.java.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java ", javaPath, className);
|
fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
|
||||||
}
|
}
|
||||||
else if (template.contains("controller.java.vm"))
|
else if (template.contains("controller.java.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/controller/{}Controller.java ", javaPath, className);
|
fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
|
||||||
}
|
}
|
||||||
else if (template.contains("mapper.xml.vm"))
|
else if (template.contains("mapper.xml.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/{}Mapper.xml ", mybatisPath, className);
|
fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
|
||||||
}
|
}
|
||||||
else if (template.contains("list.html.vm"))
|
else if (template.contains("list.html.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/{}.html", htmlPath, businessName);
|
fileName = StringUtils.format("{}/{}.html", htmlPath, businessName);
|
||||||
}
|
}
|
||||||
else if (template.contains("list-tree.html.vm"))
|
else if (template.contains("list-tree.html.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/{}.html", htmlPath, businessName);
|
fileName = StringUtils.format("{}/{}.html", htmlPath, businessName);
|
||||||
}
|
}
|
||||||
else if (template.contains("tree.html.vm"))
|
else if (template.contains("tree.html.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/tree.html", htmlPath);
|
fileName = StringUtils.format("{}/tree.html", htmlPath);
|
||||||
}
|
}
|
||||||
else if (template.contains("add.html.vm"))
|
else if (template.contains("add.html.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/add.html", htmlPath);
|
fileName = StringUtils.format("{}/add.html", htmlPath);
|
||||||
}
|
}
|
||||||
else if (template.contains("edit.html.vm"))
|
else if (template.contains("edit.html.vm"))
|
||||||
{
|
{
|
||||||
fileName = StringUtils.format("{}/edit.html", htmlPath);
|
fileName = StringUtils.format("{}/edit.html", htmlPath);
|
||||||
}
|
}
|
||||||
else if (template.contains("sql.vm"))
|
else if (template.contains("sql.vm"))
|
||||||
{
|
{
|
||||||
fileName = businessName + "Menu.sql";
|
fileName = businessName + "Menu.sql";
|
||||||
}
|
}
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取项目文件路径
|
* 获取项目文件路径
|
||||||
*
|
*
|
||||||
* @return 路径
|
* @return 路径
|
||||||
*/
|
*/
|
||||||
public static String getProjectPath()
|
public static String getProjectPath()
|
||||||
{
|
{
|
||||||
String packageName = GenConfig.getPackageName();
|
String packageName = GenConfig.getPackageName();
|
||||||
StringBuffer projectPath = new StringBuffer();
|
StringBuffer projectPath = new StringBuffer();
|
||||||
projectPath.append("main/java/");
|
projectPath.append("main/java/");
|
||||||
projectPath.append(packageName.replace(".", "/"));
|
projectPath.append(packageName.replace(".", "/"));
|
||||||
projectPath.append("/");
|
projectPath.append("/");
|
||||||
return projectPath.toString();
|
return projectPath.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取包前缀
|
* 获取包前缀
|
||||||
*
|
*
|
||||||
* @param packageName 包名称
|
* @param packageName 包名称
|
||||||
* @return 包前缀名称
|
* @return 包前缀名称
|
||||||
*/
|
*/
|
||||||
public static String getPackagePrefix(String packageName)
|
public static String getPackagePrefix(String packageName)
|
||||||
{
|
{
|
||||||
int lastIndex = packageName.lastIndexOf(".");
|
int lastIndex = packageName.lastIndexOf(".");
|
||||||
String basePackage = StringUtils.substring(packageName, 0, lastIndex);
|
String basePackage = StringUtils.substring(packageName, 0, lastIndex);
|
||||||
return basePackage;
|
return basePackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据列类型获取导入包
|
* 根据列类型获取导入包
|
||||||
*
|
*
|
||||||
* @param column 列集合
|
* @param column 列集合
|
||||||
* @return 返回需要导入的包列表
|
* @return 返回需要导入的包列表
|
||||||
*/
|
*/
|
||||||
public static HashSet<String> getImportList(List<GenTableColumn> columns)
|
public static HashSet<String> getImportList(List<GenTableColumn> columns)
|
||||||
{
|
{
|
||||||
HashSet<String> importList = new HashSet<String>();
|
HashSet<String> importList = new HashSet<String>();
|
||||||
for (GenTableColumn column : columns)
|
for (GenTableColumn column : columns)
|
||||||
{
|
{
|
||||||
if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType()))
|
if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType()))
|
||||||
{
|
{
|
||||||
importList.add("java.util.Date");
|
importList.add("java.util.Date");
|
||||||
}
|
}
|
||||||
else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType()))
|
else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType()))
|
||||||
{
|
{
|
||||||
importList.add("java.math.BigDecimal");
|
importList.add("java.math.BigDecimal");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return importList;
|
return importList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取权限前缀
|
* 获取权限前缀
|
||||||
*
|
*
|
||||||
* @param moduleName 模块名称
|
* @param moduleName 模块名称
|
||||||
* @param businessName 业务名称
|
* @param businessName 业务名称
|
||||||
* @return 返回权限前缀
|
* @return 返回权限前缀
|
||||||
*/
|
*/
|
||||||
public static String getPermissionPrefix(String moduleName, String businessName)
|
public static String getPermissionPrefix(String moduleName, String businessName)
|
||||||
{
|
{
|
||||||
return StringUtils.format("{}:{}", moduleName, businessName);
|
return StringUtils.format("{}:{}", moduleName, businessName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取树编码
|
* 获取树编码
|
||||||
*
|
*
|
||||||
* @param options 生成其他选项
|
* @param options 生成其他选项
|
||||||
* @return 树编码
|
* @return 树编码
|
||||||
*/
|
*/
|
||||||
public static String getTreecode(JSONObject paramsObj)
|
public static String getTreecode(JSONObject paramsObj)
|
||||||
{
|
{
|
||||||
if (paramsObj.containsKey(GenConstants.TREE_CODE))
|
if (paramsObj.containsKey(GenConstants.TREE_CODE))
|
||||||
{
|
{
|
||||||
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取树父编码
|
* 获取树父编码
|
||||||
*
|
*
|
||||||
* @param options 生成其他选项
|
* @param options 生成其他选项
|
||||||
* @return 树父编码
|
* @return 树父编码
|
||||||
*/
|
*/
|
||||||
public static String getTreeParentCode(JSONObject paramsObj)
|
public static String getTreeParentCode(JSONObject paramsObj)
|
||||||
{
|
{
|
||||||
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
|
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
|
||||||
{
|
{
|
||||||
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取树名称
|
* 获取树名称
|
||||||
*
|
*
|
||||||
* @param options 生成其他选项
|
* @param options 生成其他选项
|
||||||
* @return 树名称
|
* @return 树名称
|
||||||
*/
|
*/
|
||||||
public static String getTreeName(JSONObject paramsObj)
|
public static String getTreeName(JSONObject paramsObj)
|
||||||
{
|
{
|
||||||
if (paramsObj.containsKey(GenConstants.TREE_NAME))
|
if (paramsObj.containsKey(GenConstants.TREE_NAME))
|
||||||
{
|
{
|
||||||
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取需要在哪一列上面显示展开按钮
|
* 获取需要在哪一列上面显示展开按钮
|
||||||
*
|
*
|
||||||
* @param genTable 业务表对象
|
* @param genTable 业务表对象
|
||||||
* @return 展开按钮列序号
|
* @return 展开按钮列序号
|
||||||
*/
|
*/
|
||||||
public static int getExpandColumn(GenTable genTable)
|
public static int getExpandColumn(GenTable genTable)
|
||||||
{
|
{
|
||||||
String options = genTable.getOptions();
|
String options = genTable.getOptions();
|
||||||
JSONObject paramsObj = JSONObject.parseObject(options);
|
JSONObject paramsObj = JSONObject.parseObject(options);
|
||||||
String treeName = paramsObj.getString(GenConstants.TREE_NAME);
|
String treeName = paramsObj.getString(GenConstants.TREE_NAME);
|
||||||
int num = 0;
|
int num = 0;
|
||||||
for (GenTableColumn column : genTable.getColumns())
|
for (GenTableColumn column : genTable.getColumns())
|
||||||
{
|
{
|
||||||
if (column.isList())
|
if (column.isList())
|
||||||
{
|
{
|
||||||
num++;
|
num++;
|
||||||
String columnName = column.getColumnName();
|
String columnName = column.getColumnName();
|
||||||
if (columnName.equals(treeName))
|
if (columnName.equals(treeName))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in new issue