提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.base.pojo.node; |
|
17 |
|
|
18 |
import cn.stylefeng.roses.kernel.model.enums.YesOrNotEnum; |
|
19 |
import lombok.Data; |
|
20 |
|
|
21 |
import java.io.Serializable; |
|
22 |
import java.util.*; |
|
23 |
|
|
24 |
/** |
|
25 |
* @author fengshuonan |
|
26 |
* @Description 菜单的节点 |
|
27 |
* @date 2016年12月6日 上午11:34:17 |
|
28 |
*/ |
|
29 |
@Data |
|
30 |
public class MenuNode implements Comparable, Serializable { |
|
31 |
|
|
32 |
/** |
|
33 |
* 节点id |
|
34 |
*/ |
|
35 |
private Long id; |
|
36 |
|
|
37 |
public Long getId() { |
|
38 |
return id; |
|
39 |
} |
|
40 |
|
|
41 |
public void setId(Long id) { |
|
42 |
this.id = id; |
|
43 |
} |
|
44 |
|
|
45 |
public String getCode() { |
|
46 |
return code; |
|
47 |
} |
|
48 |
|
|
49 |
public void setCode(String code) { |
|
50 |
this.code = code; |
|
51 |
} |
|
52 |
|
|
53 |
public Long getParentId() { |
|
54 |
return parentId; |
|
55 |
} |
|
56 |
|
|
57 |
public void setParentId(Long parentId) { |
|
58 |
this.parentId = parentId; |
|
59 |
} |
|
60 |
|
|
61 |
public String getName() { |
|
62 |
return name; |
|
63 |
} |
|
64 |
|
|
65 |
public void setName(String name) { |
|
66 |
this.name = name; |
|
67 |
} |
|
68 |
|
|
69 |
public Integer getLevels() { |
|
70 |
return levels; |
|
71 |
} |
|
72 |
|
|
73 |
public void setLevels(Integer levels) { |
|
74 |
this.levels = levels; |
|
75 |
} |
|
76 |
|
|
77 |
public String getIsmenu() { |
|
78 |
return ismenu; |
|
79 |
} |
|
80 |
|
|
81 |
public void setIsmenu(String ismenu) { |
|
82 |
this.ismenu = ismenu; |
|
83 |
} |
|
84 |
|
|
85 |
public Integer getNum() { |
|
86 |
return num; |
|
87 |
} |
|
88 |
|
|
89 |
public void setNum(Integer num) { |
|
90 |
this.num = num; |
|
91 |
} |
|
92 |
|
|
93 |
public String getUrl() { |
|
94 |
return url; |
|
95 |
} |
|
96 |
|
|
97 |
public void setUrl(String url) { |
|
98 |
this.url = url; |
|
99 |
} |
|
100 |
|
|
101 |
public String getIcon() { |
|
102 |
return icon; |
|
103 |
} |
|
104 |
|
|
105 |
public void setIcon(String icon) { |
|
106 |
this.icon = icon; |
|
107 |
} |
|
108 |
|
|
109 |
public String getSystemType() { |
|
110 |
return systemType; |
|
111 |
} |
|
112 |
|
|
113 |
public void setSystemType(String systemType) { |
|
114 |
this.systemType = systemType; |
|
115 |
} |
|
116 |
|
|
117 |
public List<MenuNode> getChildren() { |
|
118 |
return children; |
|
119 |
} |
|
120 |
|
|
121 |
public void setChildren(List<MenuNode> children) { |
|
122 |
this.children = children; |
|
123 |
} |
|
124 |
|
|
125 |
public List<MenuNode> getLinkedList() { |
|
126 |
return linkedList; |
|
127 |
} |
|
128 |
|
|
129 |
public void setLinkedList(List<MenuNode> linkedList) { |
|
130 |
this.linkedList = linkedList; |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* 菜单编码 |
|
135 |
*/ |
|
136 |
private String code; |
|
137 |
|
|
138 |
/** |
|
139 |
* 父节点 |
|
140 |
*/ |
|
141 |
private Long parentId; |
|
142 |
|
|
143 |
/** |
|
144 |
* 节点名称 |
|
145 |
*/ |
|
146 |
private String name; |
|
147 |
|
|
148 |
/** |
|
149 |
* 按钮级别 |
|
150 |
*/ |
|
151 |
private Integer levels; |
|
152 |
|
|
153 |
/** |
|
154 |
* 按钮级别(Y N) |
|
155 |
*/ |
|
156 |
private String ismenu; |
|
157 |
|
|
158 |
/** |
|
159 |
* 按钮的排序 |
|
160 |
*/ |
|
161 |
private Integer num; |
|
162 |
|
|
163 |
/** |
|
164 |
* 节点的url |
|
165 |
*/ |
|
166 |
private String url; |
|
167 |
|
|
168 |
/** |
|
169 |
* 节点图标 |
|
170 |
*/ |
|
171 |
private String icon; |
|
172 |
|
|
173 |
/** |
|
174 |
* 系统类型 |
|
175 |
*/ |
|
176 |
private String systemType; |
|
177 |
|
|
178 |
/** |
|
179 |
* 子节点的集合 |
|
180 |
*/ |
|
181 |
private List<MenuNode> children; |
|
182 |
|
|
183 |
/** |
|
184 |
* 查询子节点时候的临时集合 |
|
185 |
*/ |
|
186 |
private List<MenuNode> linkedList = new ArrayList<>(); |
|
187 |
|
|
188 |
public MenuNode() { |
|
189 |
super(); |
|
190 |
} |
|
191 |
|
|
192 |
public MenuNode(Long id, Long parentId) { |
|
193 |
super(); |
|
194 |
this.id = id; |
|
195 |
this.parentId = parentId; |
|
196 |
} |
|
197 |
|
|
198 |
/** |
|
199 |
* 重写排序比较接口,首先根据等级排序,然后更具排序字段排序 |
|
200 |
* |
|
201 |
* @param o |
|
202 |
* @return |
|
203 |
*/ |
|
204 |
@Override |
|
205 |
public int compareTo(Object o) { |
|
206 |
MenuNode menuNode = (MenuNode) o; |
|
207 |
Integer num = menuNode.getNum(); |
|
208 |
Integer levels = menuNode.getLevels(); |
|
209 |
if (num == null) { |
|
210 |
num = 0; |
|
211 |
} |
|
212 |
if (levels == null) { |
|
213 |
levels = 0; |
|
214 |
} |
|
215 |
if (this.levels.compareTo(levels) == 0) { |
|
216 |
return this.num.compareTo(num); |
|
217 |
} else { |
|
218 |
return this.levels.compareTo(levels); |
|
219 |
} |
|
220 |
|
|
221 |
} |
|
222 |
|
|
223 |
/** |
|
224 |
* 构建页面菜单列表 |
|
225 |
*/ |
|
226 |
public static List<MenuNode> buildTitle(List<MenuNode> nodes) { |
|
227 |
if (nodes.size() <= 0) { |
|
228 |
return nodes; |
|
229 |
} |
|
230 |
|
|
231 |
//剔除非菜单 |
|
232 |
nodes.removeIf(node -> !node.getIsmenu().equals(YesOrNotEnum.Y.name())); |
|
233 |
|
|
234 |
//对菜单排序,返回列表按菜单等级,序号的排序方式排列 |
|
235 |
Collections.sort(nodes); |
|
236 |
return mergeList(nodes, nodes.get(nodes.size() - 1).getLevels(), null); |
|
237 |
} |
|
238 |
|
|
239 |
/** |
|
240 |
* 递归合并数组为子数组,最后返回第一层 |
|
241 |
* |
|
242 |
* @param menuList |
|
243 |
* @param listMap |
|
244 |
* @return |
|
245 |
*/ |
|
246 |
private static List<MenuNode> mergeList(List<MenuNode> menuList, int rank, Map<Long, List<MenuNode>> listMap) { |
|
247 |
//保存当次调用总共合并了多少元素 |
|
248 |
int n; |
|
249 |
//保存当次调用总共合并出来的list |
|
250 |
Map<Long, List<MenuNode>> currentMap = new HashMap<>(); |
|
251 |
//由于按等级从小到大排序,需要从后往前排序 |
|
252 |
//判断该节点是否属于当前循环的等级,不等于则跳出循环 |
|
253 |
for (n = menuList.size() - 1; n >= 0 && menuList.get(n).getLevels() == rank; n--) { |
|
254 |
//判断之前的调用是否有返回以该节点的id为key的map,有则设置为children列表。 |
|
255 |
if (listMap != null && listMap.get(menuList.get(n).getId()) != null) { |
|
256 |
menuList.get(n).setChildren(listMap.get(menuList.get(n).getId())); |
|
257 |
} |
|
258 |
if (menuList.get(n).getParentId() != null && menuList.get(n).getParentId() != 0) { |
|
259 |
//判断当前节点所属的pid是否已经创建了以该pid为key的键值对,没有则创建新的链表 |
|
260 |
currentMap.computeIfAbsent(menuList.get(n).getParentId(), k -> new LinkedList<>()); |
|
261 |
//将该节点插入到对应的list的头部 |
|
262 |
currentMap.get(menuList.get(n).getParentId()).add(0, menuList.get(n)); |
|
263 |
} |
|
264 |
} |
|
265 |
if (n < 0) { |
|
266 |
return menuList; |
|
267 |
} else { |
|
268 |
return mergeList(new ArrayList<>(menuList.subList(0, n + 1)), menuList.get(n).getLevels(), currentMap); |
|
269 |
} |
|
270 |
} |
|
271 |
|
|
272 |
|
|
273 |
} |