MenuServiceImpl.java
5.05 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.bootdo.system.service.impl;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.system.dao.MenuDao;
import com.bootdo.system.dao.RoleMenuDao;
import com.bootdo.system.domain.MenuDO;
import com.bootdo.system.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@Service
@Transactional(readOnly = true,rollbackFor = Exception.class)
public class MenuServiceImpl implements MenuService {
@Autowired
MenuDao menuMapper;
@Autowired
RoleMenuDao roleMenuMapper;
/**
* @param
* @return 树形菜单
*/
@Cacheable
@Override
public Tree<MenuDO> getSysMenuTree(Long id) {
List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
List<MenuDO> menuDOs = menuMapper.listMenuByUserId(id);
for (MenuDO sysMenuDO : menuDOs) {
Tree<MenuDO> tree = new Tree<MenuDO>();
tree.setId(sysMenuDO.getMenuId().toString());
tree.setParentId(sysMenuDO.getParentId().toString());
tree.setText(sysMenuDO.getName());
Map<String, Object> attributes = new HashMap<>(16);
attributes.put("url", sysMenuDO.getUrl());
attributes.put("icon", sysMenuDO.getIcon());
tree.setAttributes(attributes);
trees.add(tree);
}
// 默认顶级菜单为0,根据数据库实际情况调整
Tree<MenuDO> t = BuildTree.build(trees);
return t;
}
@Override
public List<MenuDO> list(Map<String, Object> params) {
List<MenuDO> menus = menuMapper.list(params);
return menus;
}
@Transactional(readOnly = false,rollbackFor = Exception.class)
@Override
public int remove(Long id) {
int result = menuMapper.remove(id);
return result;
}
@Transactional(readOnly = false,rollbackFor = Exception.class)
@Override
public int save(MenuDO menu) {
int r = menuMapper.save(menu);
return r;
}
@Transactional(readOnly = false,rollbackFor = Exception.class)
@Override
public int update(MenuDO menu) {
int r = menuMapper.update(menu);
return r;
}
@Override
public MenuDO get(Long id) {
MenuDO menuDO = menuMapper.get(id);
return menuDO;
}
@Override
public Tree<MenuDO> getTree() {
List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
List<MenuDO> menuDOs = menuMapper.list(new HashMap<>(16));
for (MenuDO sysMenuDO : menuDOs) {
Tree<MenuDO> tree = new Tree<MenuDO>();
tree.setId(sysMenuDO.getMenuId().toString());
tree.setParentId(sysMenuDO.getParentId().toString());
tree.setText(sysMenuDO.getName());
trees.add(tree);
}
// 默认顶级菜单为0,根据数据库实际情况调整
Tree<MenuDO> t = BuildTree.build(trees);
return t;
}
@Override
public Tree<MenuDO> getTree(Long id) {
// 根据roleId查询权限
List<MenuDO> menus = menuMapper.list(new HashMap<String, Object>(16));
List<Long> menuIds = roleMenuMapper.listMenuIdByRoleId(id);
List<Long> temp = menuIds;
for (MenuDO menu : menus) {
if (temp.contains(menu.getParentId())) {
menuIds.remove(menu.getParentId());
}
}
List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
List<MenuDO> menuDOs = menuMapper.list(new HashMap<String, Object>(16));
for (MenuDO sysMenuDO : menuDOs) {
Tree<MenuDO> tree = new Tree<MenuDO>();
tree.setId(sysMenuDO.getMenuId().toString());
tree.setParentId(sysMenuDO.getParentId().toString());
tree.setText(sysMenuDO.getName());
Map<String, Object> state = new HashMap<>(16);
Long menuId = sysMenuDO.getMenuId();
if (menuIds.contains(menuId)) {
state.put("selected", true);
} else {
state.put("selected", false);
}
tree.setState(state);
trees.add(tree);
}
// 默认顶级菜单为0,根据数据库实际情况调整
Tree<MenuDO> t = BuildTree.build(trees);
return t;
}
@Override
public Set<String> listPerms(Long userId) {
List<String> perms = menuMapper.listUserPerms(userId);
Set<String> permsSet = new HashSet<>();
for (String perm : perms) {
if (StringUtils.isNotBlank(perm)) {
permsSet.addAll(Arrays.asList(perm.trim().split(",")));
}
}
return permsSet;
}
@Override
public List<Tree<MenuDO>> listMenuTree(Long id) {
List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
List<MenuDO> menuDOs = menuMapper.listMenuByUserId(id);
for (MenuDO sysMenuDO : menuDOs) {
Tree<MenuDO> tree = new Tree<MenuDO>();
tree.setId(sysMenuDO.getMenuId().toString());
tree.setParentId(sysMenuDO.getParentId().toString());
tree.setText(sysMenuDO.getName());
Map<String, Object> attributes = new HashMap<>(16);
attributes.put("url", sysMenuDO.getUrl());
attributes.put("icon", sysMenuDO.getIcon());
tree.setAttributes(attributes);
trees.add(tree);
}
// 默认顶级菜单为0,根据数据库实际情况调整
List<Tree<MenuDO>> list = BuildTree.buildList(trees, "0");
return list;
}
}