Commit 0ae7c188 by fsn

增加对菜单的排序

parent 48d36cc7
package com.stylefeng.guns.common.node; package com.stylefeng.guns.common.node;
import com.stylefeng.guns.core.util.ToolUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import com.stylefeng.guns.core.util.ToolUtil;
/** /**
* @Description 菜单的节点
* @author fengshuonan * @author fengshuonan
* @Description 菜单的节点
* @date 2016年12月6日 上午11:34:17 * @date 2016年12月6日 上午11:34:17
*/ */
public class MenuNode { public class MenuNode implements Comparable {
/** /**
* 节点id * 节点id
*/ */
private Integer id; private Integer id;
/** /**
* 父节点 * 父节点
*/ */
private Integer parentId; private Integer parentId;
/** /**
* 节点名称 * 节点名称
*/ */
private String name; private String name;
/** /**
* 按钮级别 * 按钮级别
*/ */
private Integer levels; private Integer levels;
/** /**
* 节点的url * 按钮的排序
*/ */
private String url; private Integer num;
/** /**
* 节点图标 * 节点的url
*/ */
private String icon; private String url;
/** /**
* 子节点的集合 * 节点图标
*/ */
private List<MenuNode> children; private String icon;
/** /**
* 查询子节点时候的临时集合 * 子节点的集合
*/ */
private List<MenuNode> linkedList = new ArrayList<MenuNode>(); private List<MenuNode> children;
public MenuNode() { /**
super(); * 查询子节点时候的临时集合
} */
private List<MenuNode> linkedList = new ArrayList<MenuNode>();
public MenuNode(Integer id, Integer parentId) {
super(); public MenuNode() {
this.id = id; super();
this.parentId = parentId; }
}
public MenuNode(Integer id, Integer parentId) {
public Integer getLevels() { super();
return levels; this.id = id;
} this.parentId = parentId;
}
public void setLevels(Integer levels) {
this.levels = levels; public Integer getLevels() {
} return levels;
}
public String getIcon() {
return icon; public void setLevels(Integer levels) {
} this.levels = levels;
}
public void setIcon(String icon) {
this.icon = icon; public String getIcon() {
} return icon;
}
public static MenuNode createRoot() {
return new MenuNode(0, -1); public void setIcon(String icon) {
} this.icon = icon;
}
public Integer getId() {
return id; public static MenuNode createRoot() {
} return new MenuNode(0, -1);
}
public void setId(Integer id) {
this.id = id; public Integer getId() {
} return id;
}
public Integer getParentId() {
return parentId; public void setId(Integer id) {
} this.id = id;
}
public void setParentId(Integer parentId) {
this.parentId = parentId; public Integer getParentId() {
} return parentId;
}
public String getName() {
return name; public void setParentId(Integer parentId) {
} this.parentId = parentId;
}
public void setName(String name) {
this.name = name; public String getName() {
} return name;
}
public String getUrl() {
return url; public void setName(String name) {
} this.name = name;
}
public void setUrl(String url) {
this.url = url; public String getUrl() {
} return url;
}
public List<MenuNode> getChildren() {
return children; public void setUrl(String url) {
} this.url = url;
}
public void setChildren(List<MenuNode> children) {
this.children = children; public List<MenuNode> getChildren() {
} return children;
}
/**
* @Description 构建整个菜单树 public void setChildren(List<MenuNode> children) {
* @param list this.children = children;
* 需要被构建的节点集合 }
* @author fengshuonan
*/ public Integer getNum() {
public void buildNodeTree(List<MenuNode> nodeList) { return num;
for (MenuNode treeNode : nodeList) { }
List<MenuNode> linkedList = treeNode.findChildNodes(nodeList, treeNode.getId());
if (linkedList.size() > 0) { public void setNum(Integer num) {
treeNode.setChildren(linkedList); this.num = num;
} }
}
} @Override
public String toString() {
/** return "MenuNode{" +
* @Description 查询子节点的集合 "id=" + id +
* @author fengshuonan ", parentId=" + parentId +
*/ ", name='" + name + '\'' +
public List<MenuNode> findChildNodes(List<MenuNode> nodeList, Integer parentId) { ", levels=" + levels +
if (nodeList == null && parentId == null) ", num=" + num +
return null; ", url='" + url + '\'' +
for (Iterator<MenuNode> iterator = nodeList.iterator(); iterator.hasNext();) { ", icon='" + icon + '\'' +
MenuNode node = (MenuNode) iterator.next(); ", children=" + children +
// 根据传入的某个父节点ID,遍历该父节点的所有子节点 ", linkedList=" + linkedList +
if (node.getParentId() != 0 && parentId == node.getParentId()) { '}';
recursionFn(nodeList, node, parentId); }
}
} @Override
return linkedList; public int compareTo(Object o) {
} MenuNode menuNode = (MenuNode) o;
Integer num = menuNode.getNum();
/** if (num == null) {
* @Description 遍历一个节点的子节点 num = 0;
* @author fengshuonan }
*/ return this.num.compareTo(num);
public void recursionFn(List<MenuNode> nodeList, MenuNode node, Integer pId) { }
List<MenuNode> childList = getChildList(nodeList, node);// 得到子节点列表
if (childList.size() > 0) {// 判断是否有子节点 /**
if (node.getParentId() == pId) { * 构建整个菜单树
linkedList.add(node); *
} * @author fengshuonan
Iterator<MenuNode> it = childList.iterator(); */
while (it.hasNext()) { public void buildNodeTree(List<MenuNode> nodeList) {
MenuNode n = (MenuNode) it.next(); for (MenuNode treeNode : nodeList) {
recursionFn(nodeList, n, pId); List<MenuNode> linkedList = treeNode.findChildNodes(nodeList, treeNode.getId());
} if (linkedList.size() > 0) {
} else { treeNode.setChildren(linkedList);
if (node.getParentId() == pId) { }
linkedList.add(node); }
} }
}
} /**
* 查询子节点的集合
/** *
* @Description 得到子节点列表 * @author fengshuonan
* @author fengshuonan */
*/ public List<MenuNode> findChildNodes(List<MenuNode> nodeList, Integer parentId) {
private List<MenuNode> getChildList(List<MenuNode> list, MenuNode node) { if (nodeList == null && parentId == null)
List<MenuNode> nodeList = new ArrayList<MenuNode>(); return null;
Iterator<MenuNode> it = list.iterator(); for (Iterator<MenuNode> iterator = nodeList.iterator(); iterator.hasNext(); ) {
while (it.hasNext()) { MenuNode node = (MenuNode) iterator.next();
MenuNode n = (MenuNode) it.next(); // 根据传入的某个父节点ID,遍历该父节点的所有子节点
if (n.getParentId() == node.getId()) { if (node.getParentId() != 0 && parentId == node.getParentId()) {
nodeList.add(n); recursionFn(nodeList, node, parentId);
} }
} }
return nodeList; return linkedList;
} }
@Override /**
public String toString() { * 遍历一个节点的子节点
return "MenuNode [id=" + id + ", parentId=" + parentId + ", name=" + name + ", url=" + url + ", children=" *
+ children + "]"; * @author fengshuonan
} */
public void recursionFn(List<MenuNode> nodeList, MenuNode node, Integer pId) {
/** List<MenuNode> childList = getChildList(nodeList, node);// 得到子节点列表
* 清除掉按钮级别的资源 if (childList.size() > 0) {// 判断是否有子节点
* @param nodes if (node.getParentId() == pId) {
* @return linkedList.add(node);
* @date 2017年2月19日 下午11:04:11 }
*/ Iterator<MenuNode> it = childList.iterator();
public static List<MenuNode> clearBtn(List<MenuNode> nodes){ while (it.hasNext()) {
ArrayList<MenuNode> noBtns = new ArrayList<MenuNode>(); MenuNode n = (MenuNode) it.next();
for(MenuNode node : nodes){ recursionFn(nodeList, n, pId);
if(node.getLevels() < 3){ }
noBtns.add(node); } else {
} if (node.getParentId() == pId) {
} linkedList.add(node);
return noBtns; }
} }
}
/**
* 清除不包含子节点的节点 /**
* @return * 得到子节点列表
* @date 2017年2月19日 下午11:18:19 *
*/ * @author fengshuonan
public static List<MenuNode> clearNoChild(List<MenuNode> nodes){ */
ArrayList<MenuNode> results = new ArrayList<MenuNode>(); private List<MenuNode> getChildList(List<MenuNode> list, MenuNode node) {
for(MenuNode node : nodes){ List<MenuNode> nodeList = new ArrayList<MenuNode>();
if(ToolUtil.isNotEmpty(node.getChildren())){ Iterator<MenuNode> it = list.iterator();
results.add(node); while (it.hasNext()) {
} MenuNode n = (MenuNode) it.next();
} if (n.getParentId() == node.getId()) {
return results; nodeList.add(n);
} }
}
/** return nodeList;
* 构建菜单列表 }
* @return
* @date 2017年2月19日 下午11:18:19 /**
*/ * 清除掉按钮级别的资源
public static List<MenuNode> buildTitle(List<MenuNode> nodes){ *
List<MenuNode> clearBtn = clearBtn(nodes); * @date 2017年2月19日 下午11:04:11
new MenuNode().buildNodeTree(clearBtn); */
return clearNoChild(clearBtn); public static List<MenuNode> clearBtn(List<MenuNode> nodes) {
} ArrayList<MenuNode> noBtns = new ArrayList<MenuNode>();
for (MenuNode node : nodes) {
if (node.getLevels() < 3) {
noBtns.add(node);
}
}
return noBtns;
}
/**
* 清除不包含子节点的节点
*
* @date 2017年2月19日 下午11:18:19
*/
public static List<MenuNode> clearNoChild(List<MenuNode> nodes) {
ArrayList<MenuNode> results = new ArrayList<MenuNode>();
for (MenuNode node : nodes) {
if (ToolUtil.isNotEmpty(node.getChildren())) {
results.add(node);
}
}
return results;
}
/**
* 构建菜单列表
*
* @date 2017年2月19日 下午11:18:19
*/
public static List<MenuNode> buildTitle(List<MenuNode> nodes) {
List<MenuNode> clearBtn = clearBtn(nodes);
new MenuNode().buildNodeTree(clearBtn);
List<MenuNode> menuNodes = clearNoChild(clearBtn);
//对菜单排序
Collections.sort(menuNodes);
//对菜单的子菜单进行排序
for (MenuNode menuNode : menuNodes) {
if (menuNode.getChildren() != null && menuNode.getChildren().size() > 0) {
Collections.sort(menuNode.getChildren());
}
}
return menuNodes;
}
} }
...@@ -4,24 +4,17 @@ ...@@ -4,24 +4,17 @@
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id, code, pcode, alias, name, icon, url, num, levels, path, id, code, pcode, name, icon, url, num, levels,
tips, status, tips, status,isopen
isopen,
istemplate, version
</sql>
<sql id="Blob_Column_List">
source
</sql> </sql>
<select id="selectMenus" resultType="map"> <select id="selectMenus" resultType="map">
select select
<include refid="Base_Column_List" /> <include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from _menu from _menu
where status = 1
<if test="condition != null and condition != ''"> <if test="condition != null and condition != ''">
where name = #{condition} and name = #{condition}
</if> </if>
</select> </select>
...@@ -134,7 +127,8 @@ ...@@ -134,7 +127,8 @@
) AS parentId, ) AS parentId,
m1.NAME as name, m1.NAME as name,
m1.url as url, m1.url as url,
m1.levels as levels m1.levels as levels,
m1.num as num
FROM FROM
_menu m1 _menu m1
LEFT JOIN _menu m2 ON m1.pcode = m2. CODE LEFT JOIN _menu m2 ON m1.pcode = m2. CODE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment