/**
* @ClassName: FunctionService
* @Description: 功能菜單
* @author: yaozhenhua
* @date: 2018/12/26 12:29
*/
@Service
public class MenuService {
@Resource
MenuDao menuDao;
/**
*查詢可用且上線的菜單
*
* @param
* @author: yaozhenhua 2019/3/27 17:24
*/
public List<MenuVO> listMenu() throws InstantiationException, IllegalAccessException, AobpException {
//結果處理容器
List<MenuVO> menuVOS = new ArrayList<>();
//查詢一級菜單
List<MenuBO> menuBOS = menuDao.listParentMenu();
if(!EmptyUtils.isEmpty(menuBOS)){
//一級菜單排序
Collections.sort(menuBOS, new Comparator<MenuBO>() {
@Override
public int compare(MenuBO o1, MenuBO o2) {
int ret = 0;
ret = o1.getParentSequence().compareTo(o2.getParentSequence());
return ret;
}
});
menuVOS = BeanCopyUtils.copyList(menuBOS,MenuVO.class);
for(int i=0; i<menuBOS.size(); i++){
MenuBO menuBO = menuBOS.get(i);
MenuVO menuVO = menuVOS.get(i);
//查詢子菜單並排序
List<MenuVO> subMenuVOS = listSubMenu(menuBO.getId(),1);
menuVO.setSubs(subMenuVOS);
}
}
return menuVOS;
}
/**
*根據父id查詢子菜單
*
* @param parentId
* @author: yaozhenhua 2019/3/27 17:24
*/
public List<MenuVO> listSubMenu(Long parentId, int alarm) throws InstantiationException, IllegalAccessException, AobpException {
//計數器 遞歸20次拋出異常,菜單層級最大沒有超過20層的。
if(alarm>20){
throw new AobpException(ExceptionConstant.INTERNAL_ERR,"遞歸次數超過20次異常");
}
//查詢出此父菜單下的子菜單
List<MenuBO> menuBOS= menuDao.listMenuByParentId(parentId);
//結果容器
List<MenuVO> menuVOS = new ArrayList<>();
//查看子菜單是否是還有下一級菜單(斷定條件 用id做爲父id去查 )
if (!EmptyUtils.isEmpty(menuBOS)){
Collections.sort(menuBOS, new Comparator<MenuBO>() {
@Override
public int compare(MenuBO o1, MenuBO o2) {
int ret = 0;
ret = o1.getChildSequence().compareTo(o2.getChildSequence());
return ret;
}
});
menuVOS = BeanCopyUtils.copyList(menuBOS,MenuVO.class);
for (int i = 0; i < menuBOS.size(); i++) {
MenuBO menuBO = menuBOS.get(i);
MenuVO menuVO = menuVOS.get(i);
List<MenuVO> nextMenuVOS = listSubMenu(menuBO.getId(),alarm+1);
if(!EmptyUtils.isEmpty(nextMenuVOS)){
menuVO.setSubs(nextMenuVOS);
}
}
}
return menuVOS;
}
}
另外一種方式:
/**
* @ClassName: MenuService
* @author: yaozhenhua
* @date: 2019/3/27 15:59
*/
public class MenuService {
@Transactional(readOnly = true)
public List<Menu> selectDocMenuList() {
// 查詢所有文檔
List<Menu> rootMenuList = docDao.selectListExpUnable();
// 最終的菜單
List<Menu> docList = new ArrayList<Menu>();
// 沒有父ID的文檔, 做爲一級菜單
for (Menu rootMenu : rootMenuList) {
if (rootMenu.getDocPid() == null) {
docList.add(rootMenu);
}
}
// 爲一級菜單添加子菜單
for (Menu menu : docList) {
menu.setChildList(getChild(menu.getDocId(), rootMenuList));
}
if (docList != null && docList.size() > 0) {
// 爲list排序
this.mySort(docList, "docOrder", null);
}
return docList;
}
private List<Menu> getChild(String docId, List<Menu> rootMenu) {
if (StringUtils.isBlank(docId)) {
throw new BusinessException("文檔id不能爲空");
}
// 子菜單
List<Menu> childList = new ArrayList<>();
for (Menu menu : rootMenu) {
// 遍歷全部節點,將父菜單id與傳過來的type比較
if (menu.getDocPid() != null) {
if (menu.getDocPid().equals(docId)) {
childList.add(menu);
}
}
}
// 把子菜單的子菜單再循環一遍
for (Menu menu : childList) {// 沒有內容的子菜單還有子菜單
try {
if (menu.getDocContentHref() != null) {
menu.setDocContent(new String(menu.getDocContentHref(), "UTF-8"));
}
if (StringUtils.isBlank(menu.getDocContent())) {
// 遞歸
menu.setChildList(getChild(menu.getDocId(), rootMenu));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} // 遞歸退出條件
if (childList != null && childList.size() > 0) {
this.mySort(childList, "docOrder", null);
} else {
return null;
}
return childList;
}
}
public
class
Menu {
// 菜單id
private
String id;
// 菜單名稱
private
String name;
// 父菜單id
private
String parentId;
// 菜單url
private
String url;
// 菜單圖標
private
String icon;
// 菜單順序
private
int
order;
// 子菜單
private
List<Menu> children;
// ... 省去getter和setter方法以及toString方法
}
|
菜單通常須要排序,咱們根據Menu的order字段進行排序:java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/*
* 排序,根據order排序
*/
public
Comparator<Menu> order(){
Comparator<Menu> comparator =
new
Comparator<Menu>() {
@Override
public
int
compare(Menu o1, Menu o2) {
if
(o1.getOrder() != o2.getOrder()){
return
o1.getOrder() - o2.getOrder();
}
return
0
;
}
};
return
comparator;
}
|
生成樹的方法:ide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public
Map<String,Object> findTree(){
Map<String,Object> data =
new
HashMap<String,Object>();
try
{
//查詢全部菜單
List<Menu> allMenu = menuDao.findTree();
//根節點
List<Menu> rootMenu =
new
ArrayList<Menu>();
for
(Menu nav : allMenu) {
if
(nav.getParentId().equals(
"0"
)){
//父節點是0的,爲根節點。
rootMenu.add(nav);
}
}
/* 根據Menu類的order排序 */
Collections.sort(rootMenu, order());
//爲根菜單設置子菜單,getClild是遞歸調用的
for (Menu nav : rootMenu) {
/* 獲取根節點下的全部子節點 使用getChild方法*/
List<Menu> childList = getChild(nav.getId(), allMenu);
nav.setChildren(childList);//給根節點設置子節點
}
/**
* 輸出構建好的菜單數據。
*
*/
data.put(
"success"
,
"true"
);
data.put(
"list"
, rootMenu);
return
data;
}
catch
(Exception e) {
data.put(
"success"
,
"false"
);
data.put(
"list"
,
new
ArrayList());
return
data;
}
}
|
獲取子菜單:this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/**
* 獲取子節點
* @param id 父節點id
* @param allMenu 全部菜單列表
* @return 每一個根節點下,全部子菜單列表
*/
public
List<Menu> getChild(String id,List<Menu> allMenu){
//子菜單
List<Menu> childList =
new
ArrayList<Menu>();
for
(Menu nav : allMenu) {
// 遍歷全部節點,將全部菜單的父id與傳過來的根節點的id比較
//相等說明:爲該根節點的子節點。
if
(nav.ParentId().equals(id)){
childList.add(nav);
}
}
//遞歸
for
(Menu nav : childList) {
nav.setChildren(getChild(nav.getId(), allMenu));
}
Collections.sort(childList,order());
//排序
//若是節點下沒有子節點,返回一個空List(遞歸退出)
if
(childList.size() ==
0
){
return
new
ArrayList<Menu>();
}
return
childList;
}
|