From 5e1c64fcd8da60c382cd8559a36d699d46bcb1bc Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期二, 21 七月 2020 11:21:55 +0800
Subject: [PATCH] 代码生成支持选择上级菜单

---
 src/main/java/com/ruoyi/project/tool/gen/util/VelocityUtils.java          |   33 +++++++++++++++-
 src/main/java/com/ruoyi/common/constant/GenConstants.java                 |    6 +++
 src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java |    5 ++
 src/main/resources/vm/sql/sql.vm                                          |    2 
 src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java             |   26 +++++++++++++
 5 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/ruoyi/common/constant/GenConstants.java b/src/main/java/com/ruoyi/common/constant/GenConstants.java
index 0001785..8e95f37 100644
--- a/src/main/java/com/ruoyi/common/constant/GenConstants.java
+++ b/src/main/java/com/ruoyi/common/constant/GenConstants.java
@@ -22,6 +22,12 @@
     /** 鏍戝悕绉板瓧娈� */
     public static final String TREE_NAME = "treeName";
 
+    /** 涓婄骇鑿滃崟ID瀛楁 */
+    public static final String PARENT_MENU_ID = "parentMenuId";
+
+    /** 涓婄骇鑿滃崟鍚嶇О瀛楁 */
+    public static final String PARENT_MENU_NAME = "parentMenuName";
+
     /** 鏁版嵁搴撳瓧绗︿覆绫诲瀷 */
     public static final String[] COLUMNTYPE_STR = { "char", "varchar", "narchar", "varchar2", "tinytext", "text",
             "mediumtext", "longtext" };
diff --git a/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java b/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java
index 039af5e..98fb5fe 100644
--- a/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java
+++ b/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java
@@ -74,6 +74,12 @@
     /** 鏍戝悕绉板瓧娈� */
     private String treeName;
 
+    /** 涓婄骇鑿滃崟ID瀛楁 */
+    private String parentMenuId;
+
+    /** 涓婄骇鑿滃崟鍚嶇О瀛楁 */
+    private String parentMenuName;
+
     public Long getTableId()
     {
         return tableId;
@@ -234,6 +240,26 @@
         this.treeName = treeName;
     }
 
+    public String getParentMenuId()
+    {
+        return parentMenuId;
+    }
+
+    public void setParentMenuId(String parentMenuId)
+    {
+        this.parentMenuId = parentMenuId;
+    }
+
+    public String getParentMenuName()
+    {
+        return parentMenuName;
+    }
+
+    public void setParentMenuName(String parentMenuName)
+    {
+        this.parentMenuName = parentMenuName;
+    }
+
     public boolean isTree()
     {
         return isTree(this.tplCategory);
diff --git a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
index b10c549..8dc18fb 100644
--- a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
+++ b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
@@ -337,9 +337,14 @@
             String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
             String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE);
             String treeName = paramsObj.getString(GenConstants.TREE_NAME);
+            String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID);
+            String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME);
+            
             genTable.setTreeCode(treeCode);
             genTable.setTreeParentCode(treeParentCode);
             genTable.setTreeName(treeName);
+            genTable.setParentMenuId(parentMenuId);
+            genTable.setParentMenuName(parentMenuName);
         }
     }
 }
\ No newline at end of file
diff --git a/src/main/java/com/ruoyi/project/tool/gen/util/VelocityUtils.java b/src/main/java/com/ruoyi/project/tool/gen/util/VelocityUtils.java
index f3e38a8..854dc9f 100644
--- a/src/main/java/com/ruoyi/project/tool/gen/util/VelocityUtils.java
+++ b/src/main/java/com/ruoyi/project/tool/gen/util/VelocityUtils.java
@@ -19,6 +19,9 @@
     /** mybatis绌洪棿璺緞 */
     private static final String MYBATIS_PATH = "main/resources/mybatis";
 
+    /** 榛樿涓婄骇鑿滃崟锛岀郴缁熷伐鍏� */
+    private static final String DEFAULT_PARENT_MENU_ID = "3";
+
     /**
      * 璁剧疆妯℃澘鍙橀噺淇℃伅
      * 
@@ -50,11 +53,20 @@
         velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
         velocityContext.put("columns", genTable.getColumns());
         velocityContext.put("table", genTable);
+        setMenuVelocityContext(velocityContext, genTable);
         if (GenConstants.TPL_TREE.equals(tplCategory))
         {
             setTreeVelocityContext(velocityContext, genTable);
         }
         return velocityContext;
+    }
+
+    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)
@@ -220,6 +232,21 @@
     }
 
     /**
+     * 鑾峰彇涓婄骇鑿滃崟ID瀛楁
+     * 
+     * @param options 鐢熸垚鍏朵粬閫夐」
+     * @return 涓婄骇鑿滃崟ID瀛楁
+     */
+    public static String getParentMenuId(JSONObject paramsObj)
+    {
+        if (paramsObj.containsKey(GenConstants.PARENT_MENU_ID))
+        {
+            return paramsObj.getString(GenConstants.PARENT_MENU_ID);
+        }
+        return DEFAULT_PARENT_MENU_ID;
+    }
+
+    /**
      * 鑾峰彇鏍戠紪鐮�
      * 
      * @param options 鐢熸垚鍏朵粬閫夐」
@@ -231,7 +258,7 @@
         {
             return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
         }
-        return "";
+        return StringUtils.EMPTY;
     }
 
     /**
@@ -246,7 +273,7 @@
         {
             return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
         }
-        return "";
+        return StringUtils.EMPTY;
     }
 
     /**
@@ -261,7 +288,7 @@
         {
             return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
         }
-        return "";
+        return StringUtils.EMPTY;
     }
 
     /**
diff --git a/src/main/resources/vm/sql/sql.vm b/src/main/resources/vm/sql/sql.vm
index ab17d1b..5e3c682 100644
--- a/src/main/resources/vm/sql/sql.vm
+++ b/src/main/resources/vm/sql/sql.vm
@@ -1,6 +1,6 @@
 -- 鑿滃崟 SQL
 insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
-values('${functionName}', '3', '1', '${businessName}', '${moduleName}/${businessName}/index', 1, 'C', '0', '0', '${permissionPrefix}:list', '#', 'admin', '2018-03-01', 'ry', '2018-03-01', '${functionName}鑿滃崟');
+values('${functionName}', '${parentMenuId}', '1', '${businessName}', '${moduleName}/${businessName}/index', 1, 'C', '0', '0', '${permissionPrefix}:list', '#', 'admin', '2018-03-01', 'ry', '2018-03-01', '${functionName}鑿滃崟');
 
 -- 鎸夐挳鐖惰彍鍗旾D
 SELECT @parentId := LAST_INSERT_ID();

--
Gitblit v1.9.3