RoleServicesImpl.java
3.76 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
package com.objecteye.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.objecteye.entity.SyPrivilege;
import com.objecteye.entity.SyRole;
import com.objecteye.service.RoleServices;
import com.objecteye.utils.UserTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class RoleServicesImpl implements RoleServices {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public JSONObject rolePage(int currentpage, int pagevolume) {
List<SyRole> syRoles = mongoTemplate.find(new Query().limit(pagevolume).skip((currentpage - 1) * pagevolume), SyRole.class);
long count = mongoTemplate.count(new Query(), SyRole.class);
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", 200);
jsonObject.put("message", "success");
JSONArray rowOld = UserTools.getJSONArrayByList(syRoles);
JSONArray rowNew = new JSONArray();
for (int i = 0; i < rowOld.size(); i++) {
JSONObject jsonRole = rowOld.getJSONObject(i);
String id = jsonRole.getString("id");
/*通过id获取相应的权限*/
List<String> privilegeIdList = mongoTemplate.find(Query.query(Criteria.where("rid").is(id)), SyPrivilege.class)
.stream().map(SyPrivilege::getId).collect(Collectors.toList());
StringBuilder sb = new StringBuilder();
for (String in : privilegeIdList) {
SyPrivilege syPrivilege = mongoTemplate.findOne(Query.query(Criteria.where("id").is(in)), SyPrivilege.class);
sb.append(syPrivilege.getPrivilegeName()).append(",");
}
String substring = sb.toString().substring(0, sb.length() - 1);
jsonRole.put("jurisdiction", substring);
jsonRole.put("rolename", jsonRole.getString("roleName"));
jsonRole.put("createdate", jsonRole.getString("createDate"));
jsonRole.put("isdelete", jsonRole.getString("isDelete"));
jsonRole.remove("roleName");
jsonRole.remove("createDate");
jsonRole.remove("isDelete");
rowNew.add(jsonRole);
}
int total = (int) Math.ceil(count / (double) pagevolume);
JSONObject data = new JSONObject();
data.put("total", total);
data.put("row", rowNew);
jsonObject.put("data", data);
return jsonObject;
}
@Override
public JSONObject roleDis() {
JSONObject jsonObject = new JSONObject();
try {
List<SyRole> syRoles = mongoTemplate.find(new Query(), SyRole.class);
if (syRoles.size() > 0) {
jsonObject.put("code", 200);
jsonObject.put("message", "success");
JSONArray array = new JSONArray();
for (SyRole sr : syRoles) {
JSONObject kv = new JSONObject();
kv.put("name", sr.getRoleName());
kv.put("value", sr.getId());
array.add(kv);
}
jsonObject.put("data", array);
} else {
jsonObject.put("code", 201);
jsonObject.put("message", "列表信息错误");
jsonObject.put("date", null);
}
} catch (Exception e) {
jsonObject.put("code", 202);
jsonObject.put("message", "下拉列表失败");
jsonObject.put("date", null);
}
return jsonObject;
}
}