RoleServiceImpl.java
8.88 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.bsth.service.sys.impl;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
import com.bsth.security.util.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bsth.common.ResponseCode;
import com.bsth.entity.sys.Module;
import com.bsth.entity.sys.Role;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.sys.ModuleRepository;
import com.bsth.repository.sys.RoleRepository;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.sys.RoleService;
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, Integer> implements
RoleService {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
RoleRepository roleRepository;
@Autowired
ModuleRepository moduleRepository;
SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
@Override
public Map<String, Object> findSubordinate() {
SysUser user = SecurityUtils.getCurrentUser();
Iterator<Role> itRole = user.getRoles().iterator();
Role ro = new Role();
while(itRole.hasNext()){//判断是否有下一个
ro = itRole.next();
}
Map<String, Object> map = new HashMap<>();
List<Map<String, Object>> rsRoleList = new ArrayList<>();
try {
// 读取层次数据结果集列表
Iterator<Role> roleList = roleRepository.findAll().iterator();
// 节点列表(散列表,用于临时存储节点对象)
Map<String, Object> nodeList = new HashMap<>();
// 根节点
List rootlist = new ArrayList();
while(roleList.hasNext()){
Role role = roleList.next();
HashMap map0 = new HashMap();
map0.put("id", role.getId());
map0.put("roleCode", role.getRoleCode());
map0.put("upCode", role.getUpCode());
map0.put("roleName", role.getRoleName());
map0.put("codeName", role.getCodeName());
map0.put("level", role.getLevel());
map0.put("levelCode", role.getLevelCode());
nodeList.put(role.getLevelCode(), map0);
}
// 构造无序的多叉树
Set entrySet = nodeList.entrySet();
for (Iterator it = entrySet.iterator(); it.hasNext();) {
Map<String, Object> map1 = (HashMap) ((Map.Entry) it.next()).getValue();
// Map<String, Object> map1 = objectToMap(it.next());
if (map1.get("upCode") == null || map1.get("upCode").equals("")
|| Integer.parseInt(map1.get("upCode").toString()) == 0) {
// root = node;
rootlist.add(map1);
} else {
Map<String, Object> tempmap = ((HashMap)nodeList.get((Integer.parseInt(map1.get("level").toString())-1)+"_"+map1.get("upCode")));
// Map<String, Object> tempmap = objectToMap(nodeList.get((Integer.parseInt(map1.get("level").toString())-1)+"_"+map1.get("upCode")));
System.out.println(tempmap);
List templist = (List) tempmap.get("children");
if (null != templist) {
templist.add(map1);
} else {
templist = new ArrayList();
templist.add(map1);
}
tempmap.put("children", templist);
}
}
getRoleList(rootlist,ro.getLevelCode(),rsRoleList,false);
// 排序后输出
// ComparatorSysrole(rootlist);
map.put("list", rsRoleList);
map.put("status", ResponseCode.SUCCESS);
map.put("msg", "成功");
} catch (Exception e) {
map.put("status", ResponseCode.ERROR);
map.put("msg", e);
logger.error("error",e);
}
return map;
}
private void getRoleList(List<Map<String, Object>> list, String levelCode, List<Map<String, Object>> roleList,boolean isChildren){
try{
if(isChildren){
for (Map<String, Object> map : list) {
roleList.add(map);
List mapList = (List) map.get("children");
if (mapList != null && mapList.size() > 0) {
getRoleList(mapList,levelCode,roleList,isChildren);
}
}
} else {
for (Map<String, Object> map : list) {
if(map.get("levelCode").equals(levelCode)){
isChildren = true;
List mapList = (List) map.get("children");
if (mapList != null && mapList.size() > 0) {
getRoleList(mapList,levelCode,roleList,isChildren);
}
break;
} else {
List mapList = (List) map.get("children");
if (mapList != null && mapList.size() > 0) {
getRoleList(mapList,levelCode,roleList,isChildren);
}
}
}
}
} catch (Exception e) {
logger.error("error",e);
}
}
public Map<String, Object> objectToMap(Object obj) throws Exception {
if(obj == null)
return null;
Map<String, Object> map = new HashMap<>();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
map.put(key, value);
}
return map;
}
private void ComparatorSysrole(List<HashMap> list) {
ComparatorSysrole comparator = new ComparatorSysrole();
Collections.sort(list, comparator);
for (HashMap map : list) {
List mapList = (List) map.get("children");
if (mapList != null && mapList.size() > 0) {
ComparatorSysrole(mapList);
}
}
}
private class ComparatorSysrole implements Comparator {
public int compare(Object arg0, Object arg1) {
HashMap role0 = (HashMap) arg0;
HashMap role1 = (HashMap) arg1;
// 首先比较父节点 相同比较 子 位序
Long role0Pid = (Long) (role0.get("upCode") == null ? (long) 0 : role0.get("upCode"));
Long role1Pid = (Long) (role1.get("upCode") == null ? (long) 0 : role1.get("upCode"));
int flag = role0Pid.compareTo(role1Pid);
if (flag == 0) {
return (Integer.valueOf(role0.get("roleIndex").toString())).compareTo(Integer.valueOf(role1.get(
"roleIndex").toString()));
} else {
return flag;
}
}
}
@Override
public Map<String, Object> add(Role role) {
Map<String, Object> rs = new HashMap();
try{
SysUser user = SecurityUtils.getCurrentUser();
Iterator<Role> itRole = user.getRoles().iterator();
Role ro = new Role();
while(itRole.hasNext()){//判断是否有下一个
ro = itRole.next();
}
int id = roleRepository.roleMaxId()+1;
role.setUpCode(ro.getRoleCode());
role.setLevel(ro.getLevel()+1);
role.setRoleCode(id);
role.setId(id);
return super.save(role);
}catch (Exception e){
logger.error("", e);
rs.put("status", ResponseCode.ERROR);
rs.put("msg", e.getMessage());
}
return rs;
}
@Override
public Map<String, Object> save(Role t) {
if (t.getId() != null) {
// 更新
Map<String, Object> map = new HashMap<>();
try {
roleRepository.update(t.getCodeName(), t.getRoleName(),
t.isEnable(), t.getDescriptions(), t.getId());
map.put("status", ResponseCode.SUCCESS);
} catch (Exception e) {
map.put("status", ResponseCode.ERROR);
}
return map;
}
return super.save(t);
}
@Override
public Map<String, Object> settRoleModules(Integer roleCode, String mIds) {
Map<String, Object> map = new HashMap<>();
try {
Role role = roleRepository.findById(roleCode).get();
List<Integer> idList = new ArrayList<>();
String[] array = mIds.split(",");
for (String id : array) {
if (null == id || id.trim().equals(""))
continue;
idList.add(Integer.parseInt(id));
}
Set<Module> mList = moduleRepository.findByIds(idList);
role.setModules(mList);
roleRepository.save(role);
map.put("status", ResponseCode.SUCCESS);
} catch (Exception e) {
logger.error("", e);
map.put("status", ResponseCode.ERROR);
}
return map;
}
@Override
public Map<String, Object> roleInfo(Integer id) {
Map<String, Object> map = new HashMap<>();
Role role = roleRepository.findById(id).get();
map.put("codeName", role.getCodeName());
map.put("roleName", role.getRoleName());
map.put("createDate", sdfMinute.format(role.getCreateDate()));
map.put("updateDate", sdfMinute.format(role.getUpdateDate()));
map.put("enable", role.isEnable()==true?1:0);
map.put("descriptions", role.getDescriptions());
map.put("modules", role.getModules().size());
map.put("resources", role.getResources().size());
String userNames = "";
Set<SysUser> users = role.getUsers();
if(!users.isEmpty()){
Iterator<SysUser> it = users.iterator();
while(it.hasNext()){
SysUser user = it.next();
userNames = user.getUserName()+"...";
}
}
map.put("userNames", userNames);
return map;
}
@Override
public List<Role> findAllByIds(String ids) {
return roleRepository.findAllById(ids);
}
@Override
public boolean checkOperationLegality(Integer operationRoleId){
boolean isLegality = false;
Map<String, Object> roleMap = findSubordinate();
isLegality = (roleMap.get(operationRoleId) == null ? true:false );
return isLegality;
}
}