|
|
|
@ -1,31 +1,37 @@
|
|
|
|
|
package com.ruoyi.gen.util;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import org.apache.velocity.VelocityContext;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.ruoyi.common.core.constant.GenConstants;
|
|
|
|
|
import com.ruoyi.common.core.utils.DateUtils;
|
|
|
|
|
import com.ruoyi.common.core.utils.StringUtils;
|
|
|
|
|
import com.ruoyi.gen.domain.GenTable;
|
|
|
|
|
import com.ruoyi.gen.domain.GenTableColumn;
|
|
|
|
|
import org.apache.velocity.VelocityContext;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 模板工具类
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @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";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -33,8 +39,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();
|
|
|
|
@ -61,27 +66,23 @@ public class VelocityUtils
|
|
|
|
|
velocityContext.put("table", genTable);
|
|
|
|
|
velocityContext.put("dicts", getDicts(genTable));
|
|
|
|
|
setMenuVelocityContext(velocityContext, genTable);
|
|
|
|
|
if (GenConstants.TPL_TREE.equals(tplCategory))
|
|
|
|
|
{
|
|
|
|
|
if (GenConstants.TPL_TREE.equals(tplCategory)) {
|
|
|
|
|
setTreeVelocityContext(velocityContext, genTable);
|
|
|
|
|
}
|
|
|
|
|
if (GenConstants.TPL_SUB.equals(tplCategory))
|
|
|
|
|
{
|
|
|
|
|
if (GenConstants.TPL_SUB.equals(tplCategory)) {
|
|
|
|
|
setSubVelocityContext(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);
|
|
|
|
@ -92,18 +93,15 @@ 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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setSubVelocityContext(VelocityContext context, GenTable genTable)
|
|
|
|
|
{
|
|
|
|
|
public static void setSubVelocityContext(VelocityContext context, GenTable genTable) {
|
|
|
|
|
GenTable subTable = genTable.getSubTable();
|
|
|
|
|
String subTableName = genTable.getSubTableName();
|
|
|
|
|
String subTableFkName = genTable.getSubTableFkName();
|
|
|
|
@ -125,8 +123,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");
|
|
|
|
@ -136,16 +133,11 @@ 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");
|
|
|
|
|
}
|
|
|
|
|
else if (GenConstants.TPL_SUB.equals(tplCategory))
|
|
|
|
|
{
|
|
|
|
|
} else if (GenConstants.TPL_SUB.equals(tplCategory)) {
|
|
|
|
|
templates.add("vm/vue/index.vue.vm");
|
|
|
|
|
templates.add("vm/java/sub-domain.java.vm");
|
|
|
|
|
}
|
|
|
|
@ -155,8 +147,7 @@ public class VelocityUtils
|
|
|
|
|
/**
|
|
|
|
|
* 获取文件名
|
|
|
|
|
*/
|
|
|
|
|
public static String getFileName(String template, GenTable genTable)
|
|
|
|
|
{
|
|
|
|
|
public static String getFileName(String template, GenTable genTable) {
|
|
|
|
|
// 文件名称
|
|
|
|
|
String fileName = "";
|
|
|
|
|
// 包路径
|
|
|
|
@ -172,48 +163,30 @@ public class VelocityUtils
|
|
|
|
|
String mybatisPath = MYBATIS_PATH + "/" + moduleName;
|
|
|
|
|
String vuePath = "vue";
|
|
|
|
|
|
|
|
|
|
if (template.contains("domain.java.vm"))
|
|
|
|
|
{
|
|
|
|
|
String domainVm = "domain.java.vm";
|
|
|
|
|
if (template.contains(domainVm)) {
|
|
|
|
|
fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
|
|
|
|
|
}
|
|
|
|
|
if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory()))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory())) {
|
|
|
|
|
fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName());
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
@ -225,36 +198,29 @@ public class VelocityUtils
|
|
|
|
|
* @param packageName 包名称
|
|
|
|
|
* @return 包前缀名称
|
|
|
|
|
*/
|
|
|
|
|
public static String getPackagePrefix(String packageName)
|
|
|
|
|
{
|
|
|
|
|
public static String getPackagePrefix(String packageName) {
|
|
|
|
|
int lastIndex = packageName.lastIndexOf(".");
|
|
|
|
|
return StringUtils.substring(packageName, 0, lastIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据列类型获取导入包
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param genTable 业务表对象
|
|
|
|
|
* @return 返回需要导入的包列表
|
|
|
|
|
*/
|
|
|
|
|
public static HashSet<String> getImportList(GenTable genTable)
|
|
|
|
|
{
|
|
|
|
|
public static HashSet<String> getImportList(GenTable genTable) {
|
|
|
|
|
List<GenTableColumn> columns = genTable.getColumns();
|
|
|
|
|
GenTable subGenTable = genTable.getSubTable();
|
|
|
|
|
HashSet<String> importList = new HashSet<String>();
|
|
|
|
|
if (StringUtils.isNotNull(subGenTable))
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isNotNull(subGenTable)) {
|
|
|
|
|
importList.add("java.util.List");
|
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -263,17 +229,15 @@ public class VelocityUtils
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据列类型获取字典组
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param genTable 业务表对象
|
|
|
|
|
* @return 返回字典组
|
|
|
|
|
*/
|
|
|
|
|
public static String getDicts(GenTable genTable)
|
|
|
|
|
{
|
|
|
|
|
public static String getDicts(GenTable genTable) {
|
|
|
|
|
List<GenTableColumn> columns = genTable.getColumns();
|
|
|
|
|
Set<String> dicts = new HashSet<String>();
|
|
|
|
|
addDicts(dicts, columns);
|
|
|
|
|
if (StringUtils.isNotNull(genTable.getSubTable()))
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isNotNull(genTable.getSubTable())) {
|
|
|
|
|
List<GenTableColumn> subColumns = genTable.getSubTable().getColumns();
|
|
|
|
|
addDicts(dicts, subColumns);
|
|
|
|
|
}
|
|
|
|
@ -282,18 +246,15 @@ public class VelocityUtils
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加字典列表
|
|
|
|
|
*
|
|
|
|
|
* @param dicts 字典列表
|
|
|
|
|
*
|
|
|
|
|
* @param dicts 字典列表
|
|
|
|
|
* @param columns 列集合
|
|
|
|
|
*/
|
|
|
|
|
public static void addDicts(Set<String> dicts, List<GenTableColumn> columns)
|
|
|
|
|
{
|
|
|
|
|
for (GenTableColumn column : columns)
|
|
|
|
|
{
|
|
|
|
|
public static void addDicts(Set<String> dicts, List<GenTableColumn> columns) {
|
|
|
|
|
for (GenTableColumn column : columns) {
|
|
|
|
|
if (!column.isSuperColumn() && StringUtils.isNotEmpty(column.getDictType()) && StringUtils.equalsAny(
|
|
|
|
|
column.getHtmlType(),
|
|
|
|
|
new String[] { GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX }))
|
|
|
|
|
{
|
|
|
|
|
new String[]{GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX})) {
|
|
|
|
|
dicts.add("'" + column.getDictType() + "'");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -302,12 +263,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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -317,11 +277,9 @@ public class VelocityUtils
|
|
|
|
|
* @param paramsObj 生成其他选项
|
|
|
|
|
* @return 上级菜单ID字段
|
|
|
|
|
*/
|
|
|
|
|
public static String getParentMenuId(JSONObject paramsObj)
|
|
|
|
|
{
|
|
|
|
|
public static String getParentMenuId(JSONObject paramsObj) {
|
|
|
|
|
if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)
|
|
|
|
|
&& StringUtils.isNotEmpty(paramsObj.getString(GenConstants.PARENT_MENU_ID)))
|
|
|
|
|
{
|
|
|
|
|
&& StringUtils.isNotEmpty(paramsObj.getString(GenConstants.PARENT_MENU_ID))) {
|
|
|
|
|
return paramsObj.getString(GenConstants.PARENT_MENU_ID);
|
|
|
|
|
}
|
|
|
|
|
return DEFAULT_PARENT_MENU_ID;
|
|
|
|
@ -333,10 +291,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;
|
|
|
|
@ -348,10 +304,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;
|
|
|
|
@ -363,10 +317,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;
|
|
|
|
@ -378,20 +330,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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|