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 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 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 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; } }