123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- package com.ruoyi.project.system.menu.service;
- import java.text.MessageFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedHashMap;
- 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.constant.UserConstants;
- import com.ruoyi.common.utils.StringUtils;
- import com.ruoyi.common.utils.TreeUtils;
- import com.ruoyi.common.utils.security.ShiroUtils;
- import com.ruoyi.project.system.menu.dao.IMenuDao;
- import com.ruoyi.project.system.menu.domain.Menu;
- import com.ruoyi.project.system.role.dao.IRoleMenuDao;
- import com.ruoyi.project.system.role.domain.Role;
- /**
- * 菜单 业务层处理
- *
- * @author ruoyi
- */
- @Service("menuService")
- public class MenuServiceImpl implements IMenuService
- {
- public static final String PREMISSION_STRING = "perms[\"{0}\"]";
- @Autowired
- private IMenuDao menuDao;
- @Autowired
- private IRoleMenuDao roleMenuDao;
- /**
- * 根据用户ID查询菜单
- *
- * @param userId 用户ID
- * @return 菜单列表
- */
- @Override
- public List<Menu> selectMenusByUserId(Long userId)
- {
- List<Menu> menus = menuDao.selectMenusByUserId(userId);
- return TreeUtils.getChildPerms(menus, 0);
- }
- /**
- * 查询菜单集合
- *
- * @return 所有菜单信息
- */
- @Override
- public List<Menu> selectMenuAll()
- {
- return menuDao.selectMenuAll();
- }
- /**
- * 根据用户ID查询权限
- *
- * @param userId 用户ID
- * @return 权限列表
- */
- @Override
- public Set<String> selectPermsByUserId(Long userId)
- {
- List<String> perms = menuDao.selectPermsByUserId(userId);
- Set<String> permsSet = new HashSet<>();
- for (String perm : perms)
- {
- if (StringUtils.isNotEmpty(perm))
- {
- permsSet.addAll(Arrays.asList(perm.trim().split(",")));
- }
- }
- return permsSet;
- }
- /**
- * 根据角色ID查询菜单
- *
- * @param role 角色对象
- * @return 菜单列表
- */
- @Override
- public List<Map<String, Object>> roleMenuTreeData(Role role)
- {
- Long roleId = role.getRoleId();
- List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
- List<Menu> menuList = menuDao.selectMenuAll();
- if (StringUtils.isNotNull(roleId))
- {
- List<String> roleMenuList = menuDao.selectMenuTree(roleId);
- trees = getTrees(menuList, true, roleMenuList, true);
- }
- else
- {
- trees = getTrees(menuList, false, null, true);
- }
- return trees;
- }
- /**
- * 查询所有菜单
- *
- * @param role 角色对象
- * @return 菜单列表
- */
- @Override
- public List<Map<String, Object>> menuTreeData()
- {
- List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
- List<Menu> menuList = menuDao.selectMenuAll();
- trees = getTrees(menuList, false, null, false);
- return trees;
- }
- /**
- * 查询系统所有权限
- *
- * @return 权限列表
- */
- @Override
- public LinkedHashMap<String, String> selectPermsAll()
- {
- LinkedHashMap<String, String> section = new LinkedHashMap<>();
- List<Menu> permissions = menuDao.selectMenuAll();
- if (StringUtils.isNotEmpty(permissions))
- {
- for (Menu menu : permissions)
- {
- section.put(menu.getUrl(), MessageFormat.format(PREMISSION_STRING, menu.getPerms()));
- }
- }
- return section;
- }
- /**
- * 对象转菜单树
- *
- * @param menuList 菜单列表
- * @param isCheck 是否需要选中
- * @param roleMenuList 角色已存在菜单列表
- * @param permsFlag 是否需要显示权限标识
- * @return
- */
- public List<Map<String, Object>> getTrees(List<Menu> menuList, boolean isCheck, List<String> roleMenuList,
- boolean permsFlag)
- {
- List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
- for (Menu 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));
- if (isCheck)
- {
- deptMap.put("checked", roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
- }
- else
- {
- deptMap.put("checked", false);
- }
- trees.add(deptMap);
- }
- return trees;
- }
- public String transMenuName(Menu menu, List<String> roleMenuList, boolean permsFlag)
- {
- StringBuffer sb = new StringBuffer();
- sb.append(menu.getMenuName());
- if (permsFlag)
- {
- sb.append("<font color=\"#888\"> " + menu.getPerms() + "</font>");
- }
- return sb.toString();
- }
- /**
- * 删除菜单管理信息
- *
- * @param menuId 菜单ID
- * @return 结果
- */
- @Override
- public int deleteMenuById(Long menuId)
- {
- return menuDao.deleteMenuById(menuId);
- }
- /**
- * 根据菜单ID查询信息
- *
- * @param menuId 菜单ID
- * @return 菜单信息
- */
- @Override
- public Menu selectMenuById(Long menuId)
- {
- return menuDao.selectMenuById(menuId);
- }
- /**
- * 查询子菜单数量
- *
- * @param menuId 菜单ID
- * @return 结果
- */
- @Override
- public int selectCountMenuByParentId(Long parentId)
- {
- return menuDao.selectCountMenuByParentId(parentId);
- }
- /**
- * 查询菜单使用数量
- *
- * @param menuId 菜单ID
- * @return 结果
- */
- @Override
- public int selectCountRoleMenuByMenuId(Long menuId)
- {
- return roleMenuDao.selectCountRoleMenuByMenuId(menuId);
- }
- /**
- * 保存菜单信息
- *
- * @param menu 菜单信息
- * @return 结果
- */
- @Override
- public int saveMenu(Menu menu)
- {
- Long menuId = menu.getMenuId();
- if (StringUtils.isNotNull(menuId))
- {
- menu.setUpdateBy(ShiroUtils.getLoginName());
- return menuDao.updateMenu(menu);
- }
- else
- {
- menu.setCreateBy(ShiroUtils.getLoginName());
- return menuDao.insertMenu(menu);
- }
- }
- /**
- * 校验菜单名称是否唯一
- *
- * @param menu 菜单信息
- * @return 结果
- */
- @Override
- public String checkMenuNameUnique(Menu menu)
- {
- Long menuId = menu.getMenuId();
- Menu info = menuDao.checkMenuNameUnique(menu.getMenuName());
- if (StringUtils.isNotNull(info) && StringUtils.isNotNull(info.getMenuId())
- && info.getMenuId().longValue() != menuId.longValue())
- {
- return UserConstants.NAME_NOT_UNIQUE;
- }
- return UserConstants.NAME_UNIQUE;
- }
- }
|