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.entity.SyRoleExample; import com.objecteye.mapper.SyPrivilegeMapper; import com.objecteye.mapper.SyRoleMapper; import com.objecteye.mapper.SyRolePrivilegeMapper; import com.objecteye.service.RoleServices; import com.objecteye.utils.UserTools; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class RoleServicesImpl implements RoleServices { @Autowired public SyRoleMapper syRoleMapper; @Autowired public SyRolePrivilegeMapper syRolePrivilegeMapper; @Autowired public SyPrivilegeMapper syPrivilegeMapper; @Override public int totalNum() { return syRoleMapper.countByExample(new SyRoleExample()); } @Override public JSONObject rolePage(int currentpage, int pagevolume) { int startPage = (currentpage - 1) * pagevolume; List syRoles = syRoleMapper.selectRolePage(startPage, pagevolume); 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); Integer id = jsonRole.getInteger("id"); /*通过id获取相应的权限*/ List integers = syRolePrivilegeMapper.selectByRid(id); StringBuilder sb = new StringBuilder(); for (Integer in : integers) { SyPrivilege syPrivilege = syPrivilegeMapper.selectByPrimaryKey(in); 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(totalNum() / (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 = syRoleMapper.selectByExample(new SyRoleExample()); if (syRoles != null && 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; } }