|
@@ -3,6 +3,7 @@ package com.ruoyi.generator.util;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
+
|
|
|
import org.apache.velocity.VelocityContext;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.ruoyi.common.constant.GenConstants;
|
|
@@ -13,18 +14,23 @@ import com.ruoyi.generator.domain.GenTableColumn;
|
|
|
|
|
|
/**
|
|
|
* 模板处理工具类
|
|
|
- *
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
-public class VelocityUtils
|
|
|
-{
|
|
|
- /** 项目空间路径 */
|
|
|
+public class VelocityUtils {
|
|
|
+ /**
|
|
|
+ * 项目空间路径
|
|
|
+ */
|
|
|
private static final String PROJECT_PATH = "main/java";
|
|
|
|
|
|
- /** mybatis空间路径 */
|
|
|
+ /**
|
|
|
+ * mybatis空间路径
|
|
|
+ */
|
|
|
private static final String MYBATIS_PATH = "main/resources/mapper";
|
|
|
|
|
|
- /** 默认上级菜单,系统工具 */
|
|
|
+ /**
|
|
|
+ * 默认上级菜单,系统工具
|
|
|
+ */
|
|
|
private static final String DEFAULT_PARENT_MENU_ID = "3";
|
|
|
|
|
|
/**
|
|
@@ -32,8 +38,7 @@ public class VelocityUtils
|
|
|
*
|
|
|
* @return 模板列表
|
|
|
*/
|
|
|
- public static VelocityContext prepareContext(GenTable genTable)
|
|
|
- {
|
|
|
+ public static VelocityContext prepareContext(GenTable genTable) {
|
|
|
String moduleName = genTable.getModuleName();
|
|
|
String businessName = genTable.getBusinessName();
|
|
|
String packageName = genTable.getPackageName();
|
|
@@ -59,23 +64,20 @@ public class VelocityUtils
|
|
|
velocityContext.put("columns", genTable.getColumns());
|
|
|
velocityContext.put("table", genTable);
|
|
|
setMenuVelocityContext(velocityContext, genTable);
|
|
|
- if (GenConstants.TPL_TREE.equals(tplCategory))
|
|
|
- {
|
|
|
+ if (GenConstants.TPL_TREE.equals(tplCategory)) {
|
|
|
setTreeVelocityContext(velocityContext, genTable);
|
|
|
}
|
|
|
return velocityContext;
|
|
|
}
|
|
|
|
|
|
- public static void setMenuVelocityContext(VelocityContext context, GenTable genTable)
|
|
|
- {
|
|
|
+ public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) {
|
|
|
String options = genTable.getOptions();
|
|
|
JSONObject paramsObj = JSONObject.parseObject(options);
|
|
|
String parentMenuId = getParentMenuId(paramsObj);
|
|
|
context.put("parentMenuId", parentMenuId);
|
|
|
}
|
|
|
|
|
|
- public static void setTreeVelocityContext(VelocityContext context, GenTable genTable)
|
|
|
- {
|
|
|
+ public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) {
|
|
|
String options = genTable.getOptions();
|
|
|
JSONObject paramsObj = JSONObject.parseObject(options);
|
|
|
String treeCode = getTreecode(paramsObj);
|
|
@@ -86,12 +88,10 @@ public class VelocityUtils
|
|
|
context.put("treeParentCode", treeParentCode);
|
|
|
context.put("treeName", treeName);
|
|
|
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));
|
|
|
}
|
|
|
- if (paramsObj.containsKey(GenConstants.TREE_NAME))
|
|
|
- {
|
|
|
+ if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
|
|
|
context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME));
|
|
|
}
|
|
|
}
|
|
@@ -101,8 +101,7 @@ public class VelocityUtils
|
|
|
*
|
|
|
* @return 模板列表
|
|
|
*/
|
|
|
- public static List<String> getTemplateList(String tplCategory)
|
|
|
- {
|
|
|
+ public static List<String> getTemplateList(String tplCategory) {
|
|
|
List<String> templates = new ArrayList<String>();
|
|
|
templates.add("vm/java/domain.java.vm");
|
|
|
templates.add("vm/java/mapper.java.vm");
|
|
@@ -112,12 +111,9 @@ public class VelocityUtils
|
|
|
templates.add("vm/xml/mapper.xml.vm");
|
|
|
templates.add("vm/sql/sql.vm");
|
|
|
templates.add("vm/js/api.js.vm");
|
|
|
- if (GenConstants.TPL_CRUD.equals(tplCategory))
|
|
|
- {
|
|
|
+ if (GenConstants.TPL_CRUD.equals(tplCategory)) {
|
|
|
templates.add("vm/vue/index.vue.vm");
|
|
|
- }
|
|
|
- else if (GenConstants.TPL_TREE.equals(tplCategory))
|
|
|
- {
|
|
|
+ } else if (GenConstants.TPL_TREE.equals(tplCategory)) {
|
|
|
templates.add("vm/vue/index-tree.vue.vm");
|
|
|
}
|
|
|
return templates;
|
|
@@ -126,8 +122,7 @@ public class VelocityUtils
|
|
|
/**
|
|
|
* 获取文件名
|
|
|
*/
|
|
|
- public static String getFileName(String template, GenTable genTable)
|
|
|
- {
|
|
|
+ public static String getFileName(String template, GenTable genTable) {
|
|
|
// 文件名称
|
|
|
String fileName = "";
|
|
|
// 包路径
|
|
@@ -143,44 +138,25 @@ public class VelocityUtils
|
|
|
String mybatisPath = MYBATIS_PATH + "/" + moduleName;
|
|
|
String vuePath = "vue";
|
|
|
|
|
|
- if (template.contains("domain.java.vm"))
|
|
|
- {
|
|
|
+ if (template.contains("domain.java.vm")) {
|
|
|
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);
|
|
|
- }
|
|
|
- else if (template.contains("service.java.vm"))
|
|
|
- {
|
|
|
+ } else if (template.contains("service.java.vm")) {
|
|
|
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);
|
|
|
- }
|
|
|
- else if (template.contains("controller.java.vm"))
|
|
|
- {
|
|
|
+ } else if (template.contains("controller.java.vm")) {
|
|
|
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);
|
|
|
- }
|
|
|
- else if (template.contains("sql.vm"))
|
|
|
- {
|
|
|
+ } else if (template.contains("sql.vm")) {
|
|
|
fileName = businessName + "Menu.sql";
|
|
|
- }
|
|
|
- else if (template.contains("api.js.vm"))
|
|
|
- {
|
|
|
+ } else if (template.contains("api.js.vm")) {
|
|
|
fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
|
|
|
- }
|
|
|
- else if (template.contains("index.vue.vm"))
|
|
|
- {
|
|
|
+ } else if (template.contains("index.vue.vm")) {
|
|
|
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
|
|
|
- }
|
|
|
- else if (template.contains("index-tree.vue.vm"))
|
|
|
- {
|
|
|
+ } else if (template.contains("index-tree.vue.vm")) {
|
|
|
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
|
|
|
}
|
|
|
return fileName;
|
|
@@ -192,8 +168,7 @@ public class VelocityUtils
|
|
|
* @param packageName 包名称
|
|
|
* @return 包前缀名称
|
|
|
*/
|
|
|
- public static String getPackagePrefix(String packageName)
|
|
|
- {
|
|
|
+ public static String getPackagePrefix(String packageName) {
|
|
|
int lastIndex = packageName.lastIndexOf(".");
|
|
|
String basePackage = StringUtils.substring(packageName, 0, lastIndex);
|
|
|
return basePackage;
|
|
@@ -205,18 +180,13 @@ public class VelocityUtils
|
|
|
* @param columns 列集合
|
|
|
* @return 返回需要导入的包列表
|
|
|
*/
|
|
|
- public static HashSet<String> getImportList(List<GenTableColumn> columns)
|
|
|
- {
|
|
|
+ public static HashSet<String> getImportList(List<GenTableColumn> columns) {
|
|
|
HashSet<String> importList = new HashSet<String>();
|
|
|
- for (GenTableColumn column : columns)
|
|
|
- {
|
|
|
- if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType()))
|
|
|
- {
|
|
|
+ for (GenTableColumn column : columns) {
|
|
|
+ if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) {
|
|
|
importList.add("java.util.Date");
|
|
|
importList.add("com.fasterxml.jackson.annotation.JsonFormat");
|
|
|
- }
|
|
|
- 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");
|
|
|
}
|
|
|
}
|
|
@@ -226,12 +196,11 @@ public class VelocityUtils
|
|
|
/**
|
|
|
* 获取权限前缀
|
|
|
*
|
|
|
- * @param moduleName 模块名称
|
|
|
+ * @param moduleName 模块名称
|
|
|
* @param businessName 业务名称
|
|
|
* @return 返回权限前缀
|
|
|
*/
|
|
|
- public static String getPermissionPrefix(String moduleName, String businessName)
|
|
|
- {
|
|
|
+ public static String getPermissionPrefix(String moduleName, String businessName) {
|
|
|
return StringUtils.format("{}:{}", moduleName, businessName);
|
|
|
}
|
|
|
|
|
@@ -241,10 +210,8 @@ public class VelocityUtils
|
|
|
* @param paramsObj 生成其他选项
|
|
|
* @return 上级菜单ID字段
|
|
|
*/
|
|
|
- public static String getParentMenuId(JSONObject paramsObj)
|
|
|
- {
|
|
|
- if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID))
|
|
|
- {
|
|
|
+ public static String getParentMenuId(JSONObject paramsObj) {
|
|
|
+ if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)) {
|
|
|
return paramsObj.getString(GenConstants.PARENT_MENU_ID);
|
|
|
}
|
|
|
return DEFAULT_PARENT_MENU_ID;
|
|
@@ -256,10 +223,8 @@ public class VelocityUtils
|
|
|
* @param paramsObj 生成其他选项
|
|
|
* @return 树编码
|
|
|
*/
|
|
|
- public static String getTreecode(JSONObject paramsObj)
|
|
|
- {
|
|
|
- if (paramsObj.containsKey(GenConstants.TREE_CODE))
|
|
|
- {
|
|
|
+ public static String getTreecode(JSONObject paramsObj) {
|
|
|
+ if (paramsObj.containsKey(GenConstants.TREE_CODE)) {
|
|
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
|
|
|
}
|
|
|
return StringUtils.EMPTY;
|
|
@@ -271,10 +236,8 @@ public class VelocityUtils
|
|
|
* @param paramsObj 生成其他选项
|
|
|
* @return 树父编码
|
|
|
*/
|
|
|
- public static String getTreeParentCode(JSONObject paramsObj)
|
|
|
- {
|
|
|
- if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
|
|
|
- {
|
|
|
+ public static String getTreeParentCode(JSONObject paramsObj) {
|
|
|
+ if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
|
|
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
|
|
|
}
|
|
|
return StringUtils.EMPTY;
|
|
@@ -286,10 +249,8 @@ public class VelocityUtils
|
|
|
* @param paramsObj 生成其他选项
|
|
|
* @return 树名称
|
|
|
*/
|
|
|
- public static String getTreeName(JSONObject paramsObj)
|
|
|
- {
|
|
|
- if (paramsObj.containsKey(GenConstants.TREE_NAME))
|
|
|
- {
|
|
|
+ public static String getTreeName(JSONObject paramsObj) {
|
|
|
+ if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
|
|
|
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
|
|
|
}
|
|
|
return StringUtils.EMPTY;
|
|
@@ -301,20 +262,16 @@ public class VelocityUtils
|
|
|
* @param genTable 业务表对象
|
|
|
* @return 展开按钮列序号
|
|
|
*/
|
|
|
- public static int getExpandColumn(GenTable genTable)
|
|
|
- {
|
|
|
+ public static int getExpandColumn(GenTable genTable) {
|
|
|
String options = genTable.getOptions();
|
|
|
JSONObject paramsObj = JSONObject.parseObject(options);
|
|
|
String treeName = paramsObj.getString(GenConstants.TREE_NAME);
|
|
|
int num = 0;
|
|
|
- for (GenTableColumn column : genTable.getColumns())
|
|
|
- {
|
|
|
- if (column.isList())
|
|
|
- {
|
|
|
+ for (GenTableColumn column : genTable.getColumns()) {
|
|
|
+ if (column.isList()) {
|
|
|
num++;
|
|
|
String columnName = column.getColumnName();
|
|
|
- if (columnName.equals(treeName))
|
|
|
- {
|
|
|
+ if (columnName.equals(treeName)) {
|
|
|
break;
|
|
|
}
|
|
|
}
|