Ztree树结构优化

This commit is contained in:
RuoYi
2019-03-01 15:26:04 +08:00
parent 161c0f1a67
commit 1d01d20b49
10 changed files with 234 additions and 120 deletions

View File

@ -1,7 +1,7 @@
package com.ruoyi.system.service;
import java.util.List;
import java.util.Map;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysRole;
@ -26,7 +26,7 @@ public interface ISysDeptService
* @param dept 部门信息
* @return 所有部门信息
*/
public List<Map<String, Object>> selectDeptTree(SysDept dept);
public List<Ztree> selectDeptTree(SysDept dept);
/**
* 根据角色ID查询菜单
@ -34,7 +34,7 @@ public interface ISysDeptService
* @param role 角色对象
* @return 菜单列表
*/
public List<Map<String, Object>> roleDeptTreeData(SysRole role);
public List<Ztree> roleDeptTreeData(SysRole role);
/**
* 查询部门人数

View File

@ -3,6 +3,7 @@ package com.ruoyi.system.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
@ -51,14 +52,14 @@ public interface ISysMenuService
* @param role 角色对象
* @return 菜单列表
*/
public List<Map<String, Object>> roleMenuTreeData(SysRole role);
public List<Ztree> roleMenuTreeData(SysRole role);
/**
* 查询所有菜单信息
*
* @return 菜单列表
*/
public List<Map<String, Object>> menuTreeData();
public List<Ztree> menuTreeData();
/**
* 查询系统所有权限

View File

@ -1,12 +1,11 @@
package com.ruoyi.system.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
@ -47,12 +46,11 @@ public class SysDeptServiceImpl implements ISysDeptService
*/
@Override
@DataScope(tableAlias = "d")
public List<Map<String, Object>> selectDeptTree(SysDept dept)
public List<Ztree> selectDeptTree(SysDept dept)
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<SysDept> deptList = deptMapper.selectDeptList(dept);
trees = getTrees(deptList, false, null);
return trees;
List<Ztree> ztrees = initZtree(deptList);
return ztrees;
}
/**
@ -62,56 +60,63 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 部门列表(数据权限)
*/
@Override
public List<Map<String, Object>> roleDeptTreeData(SysRole role)
public List<Ztree> roleDeptTreeData(SysRole role)
{
Long roleId = role.getRoleId();
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
List<SysDept> deptList = selectDeptList(new SysDept());
if (StringUtils.isNotNull(roleId))
{
List<String> roleDeptList = deptMapper.selectRoleDeptTree(roleId);
trees = getTrees(deptList, true, roleDeptList);
ztrees = initZtree(deptList, roleDeptList);
}
else
{
trees = getTrees(deptList, false, null);
ztrees = initZtree(deptList);
}
return trees;
return ztrees;
}
/**
* 对象转部门树
*
* @param deptList 部门列表
* @param isCheck 是否需要选中
* @param roleDeptList 角色已存在菜单列表
* @return
* @return 树结构列表
*/
public List<Map<String, Object>> getTrees(List<SysDept> deptList, boolean isCheck, List<String> roleDeptList)
public List<Ztree> initZtree(List<SysDept> deptList)
{
return initZtree(deptList, null);
}
/**
* 对象转部门树
*
* @param deptList 部门列表
* @param roleDeptList 角色已存在菜单列表
* @return 树结构列表
*/
public List<Ztree> initZtree(List<SysDept> deptList, List<String> roleDeptList)
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
boolean isCheck = StringUtils.isNotNull(roleDeptList);
for (SysDept dept : deptList)
{
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
{
Map<String, Object> deptMap = new HashMap<String, Object>();
deptMap.put("id", dept.getDeptId());
deptMap.put("pId", dept.getParentId());
deptMap.put("name", dept.getDeptName());
deptMap.put("title", dept.getDeptName());
Ztree ztree = new Ztree();
ztree.setId(dept.getDeptId());
ztree.setpId(dept.getParentId());
ztree.setName(dept.getDeptName());
ztree.setTitle(dept.getDeptName());
if (isCheck)
{
deptMap.put("checked", roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
}
else
{
deptMap.put("checked", false);
}
trees.add(deptMap);
ztrees.add(ztree);
}
}
return trees;
return ztrees;
}
/**

View File

@ -3,16 +3,15 @@ package com.ruoyi.system.service.impl;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysMenu;
@ -110,21 +109,21 @@ public class SysMenuServiceImpl implements ISysMenuService
* @return 菜单列表
*/
@Override
public List<Map<String, Object>> roleMenuTreeData(SysRole role)
public List<Ztree> roleMenuTreeData(SysRole role)
{
Long roleId = role.getRoleId();
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
List<SysMenu> menuList = menuMapper.selectMenuAll();
if (StringUtils.isNotNull(roleId))
{
List<String> roleMenuList = menuMapper.selectMenuTree(roleId);
trees = getTrees(menuList, true, roleMenuList, true);
ztrees = initZtree(menuList, roleMenuList, true);
}
else
{
trees = getTrees(menuList, false, null, true);
ztrees = initZtree(menuList, null, true);
}
return trees;
return ztrees;
}
/**
@ -133,12 +132,11 @@ public class SysMenuServiceImpl implements ISysMenuService
* @return 菜单列表
*/
@Override
public List<Map<String, Object>> menuTreeData()
public List<Ztree> menuTreeData()
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<SysMenu> menuList = menuMapper.selectMenuAll();
trees = getTrees(menuList, false, null, false);
return trees;
List<Ztree> ztrees = initZtree(menuList);
return ztrees;
}
/**
@ -165,33 +163,39 @@ public class SysMenuServiceImpl implements ISysMenuService
* 对象转菜单树
*
* @param menuList 菜单列表
* @param isCheck 是否需要选中
* @return 树结构列表
*/
public List<Ztree> initZtree(List<SysMenu> menuList)
{
return initZtree(menuList, null, false);
}
/**
* 对象转菜单树
*
* @param menuList 菜单列表
* @param roleMenuList 角色已存在菜单列表
* @param permsFlag 是否需要显示权限标识
* @return
* @return 树结构列表
*/
public List<Map<String, Object>> getTrees(List<SysMenu> menuList, boolean isCheck, List<String> roleMenuList,
boolean permsFlag)
public List<Ztree> initZtree(List<SysMenu> menuList, List<String> roleMenuList, boolean permsFlag)
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
boolean isCheck = StringUtils.isNotNull(roleMenuList);
for (SysMenu menu : menuList)
{
Map<String, Object> deptMap = new HashMap<String, Object>();
deptMap.put("id", menu.getMenuId());
deptMap.put("pId", menu.getParentId());
deptMap.put("name", transMenuName(menu, roleMenuList, permsFlag));
deptMap.put("title", menu.getMenuName());
Ztree ztree = new Ztree();
ztree.setId(menu.getMenuId());
ztree.setpId(menu.getParentId());
ztree.setName(transMenuName(menu, roleMenuList, permsFlag));
ztree.setTitle(menu.getMenuName());
if (isCheck)
{
deptMap.put("checked", roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
ztree.setChecked(roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
}
else
{
deptMap.put("checked", false);
}
trees.add(deptMap);
ztrees.add(ztree);
}
return trees;
return ztrees;
}
public String transMenuName(SysMenu menu, List<String> roleMenuList, boolean permsFlag)