package com.bosshand.virgo.service; import com.bosshand.virgo.core.dao.MgrRoleDao; import com.bosshand.virgo.core.model.MgrRole; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class RoleService { @Autowired MgrRoleDao mgrRoleDao; public int save(MgrRole mgrRole) { return mgrRoleDao.insert(mgrRole); } public int update(MgrRole mgrRole) { return mgrRoleDao.update(mgrRole); } public int delete(long id) { return mgrRoleDao.delete(id); } public List getList(long projectId, long organizationId) { List list = mgrRoleDao.getList(projectId, organizationId); List ids = new ArrayList<>(); list.forEach(ls -> { if (ls.getParentId() == -1) { ids.add(ls.getId()); } }); List hierarchy = new ArrayList<>(); if (ids.size() > 0) { ids.forEach(ll -> hierarchy.add(getNodeTreeById(ll, list))); } return hierarchy; } private MgrRole getNodeTreeById(long id, List list) { MgrRole node = null; List roleList = list; for (MgrRole data : roleList) { if (data.getId() == id) { node = data; break; } } if (node != null) { node.setChildren(getChildNode(node.getId(), roleList)); } return node; } private List getChildNode(long parentId, List list) { List nodeList = new ArrayList(); nodeList.clear(); for (MgrRole data : list) { if (data.getParentId() == parentId) { nodeList.add(data); } } if (nodeList.size() > 0) { for (MgrRole data : nodeList) { List childList = getChildNode(data.getId(), list); data.setChildren(childList); } } return nodeList; } }