RoleService.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.bosshand.virgo.service;
  2. import com.bosshand.virgo.core.dao.MgrRoleDao;
  3. import com.bosshand.virgo.core.model.MgrRole;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. @Service
  9. public class RoleService {
  10. @Autowired
  11. MgrRoleDao mgrRoleDao;
  12. public int save(MgrRole mgrRole) {
  13. return mgrRoleDao.insert(mgrRole);
  14. }
  15. public int update(MgrRole mgrRole) {
  16. return mgrRoleDao.update(mgrRole);
  17. }
  18. public int delete(long id) {
  19. return mgrRoleDao.delete(id);
  20. }
  21. public List<MgrRole> getList(long projectId, long organizationId) {
  22. List<MgrRole> list = mgrRoleDao.getList(projectId, organizationId);
  23. List<Long> ids = new ArrayList<>();
  24. list.forEach(ls -> {
  25. if (ls.getParentId() == -1) {
  26. ids.add(ls.getId());
  27. }
  28. });
  29. List<MgrRole> hierarchy = new ArrayList<>();
  30. if (ids.size() > 0) {
  31. ids.forEach(ll -> hierarchy.add(getNodeTreeById(ll, list)));
  32. }
  33. return hierarchy;
  34. }
  35. private MgrRole getNodeTreeById(long id, List<MgrRole> list) {
  36. MgrRole node = null;
  37. List<MgrRole> roleList = list;
  38. for (MgrRole data : roleList) {
  39. if (data.getId() == id) {
  40. node = data;
  41. break;
  42. }
  43. }
  44. if (node != null) {
  45. node.setChildren(getChildNode(node.getId(), roleList));
  46. }
  47. return node;
  48. }
  49. private List<MgrRole> getChildNode(long parentId, List<MgrRole> list) {
  50. List<MgrRole> nodeList = new ArrayList<MgrRole>();
  51. nodeList.clear();
  52. for (MgrRole data : list) {
  53. if (data.getParentId() == parentId) {
  54. nodeList.add(data);
  55. }
  56. }
  57. if (nodeList.size() > 0) {
  58. for (MgrRole data : nodeList) {
  59. List<MgrRole> childList = getChildNode(data.getId(), list);
  60. data.setChildren(childList);
  61. }
  62. }
  63. return nodeList;
  64. }
  65. }