MenuServiceImpl.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package com.ruoyi.project.system.menu.service;
  2. import java.text.MessageFormat;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.LinkedHashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import com.ruoyi.common.constant.UserConstants;
  14. import com.ruoyi.common.utils.StringUtils;
  15. import com.ruoyi.common.utils.TreeUtils;
  16. import com.ruoyi.common.utils.security.ShiroUtils;
  17. import com.ruoyi.project.system.menu.dao.IMenuDao;
  18. import com.ruoyi.project.system.menu.domain.Menu;
  19. import com.ruoyi.project.system.role.dao.IRoleMenuDao;
  20. import com.ruoyi.project.system.role.domain.Role;
  21. /**
  22. * 菜单 业务层处理
  23. *
  24. * @author ruoyi
  25. */
  26. @Service("menuService")
  27. public class MenuServiceImpl implements IMenuService
  28. {
  29. public static final String PREMISSION_STRING = "perms[\"{0}\"]";
  30. @Autowired
  31. private IMenuDao menuDao;
  32. @Autowired
  33. private IRoleMenuDao roleMenuDao;
  34. /**
  35. * 根据用户ID查询菜单
  36. *
  37. * @param userId 用户ID
  38. * @return 菜单列表
  39. */
  40. @Override
  41. public List<Menu> selectMenusByUserId(Long userId)
  42. {
  43. List<Menu> menus = menuDao.selectMenusByUserId(userId);
  44. return TreeUtils.getChildPerms(menus, 0);
  45. }
  46. /**
  47. * 查询菜单集合
  48. *
  49. * @return 所有菜单信息
  50. */
  51. @Override
  52. public List<Menu> selectMenuAll()
  53. {
  54. return menuDao.selectMenuAll();
  55. }
  56. /**
  57. * 根据用户ID查询权限
  58. *
  59. * @param userId 用户ID
  60. * @return 权限列表
  61. */
  62. @Override
  63. public Set<String> selectPermsByUserId(Long userId)
  64. {
  65. List<String> perms = menuDao.selectPermsByUserId(userId);
  66. Set<String> permsSet = new HashSet<>();
  67. for (String perm : perms)
  68. {
  69. if (StringUtils.isNotEmpty(perm))
  70. {
  71. permsSet.addAll(Arrays.asList(perm.trim().split(",")));
  72. }
  73. }
  74. return permsSet;
  75. }
  76. /**
  77. * 根据角色ID查询菜单
  78. *
  79. * @param role 角色对象
  80. * @return 菜单列表
  81. */
  82. @Override
  83. public List<Map<String, Object>> roleMenuTreeData(Role role)
  84. {
  85. Long roleId = role.getRoleId();
  86. List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
  87. List<Menu> menuList = menuDao.selectMenuAll();
  88. if (StringUtils.isNotNull(roleId))
  89. {
  90. List<String> roleMenuList = menuDao.selectMenuTree(roleId);
  91. trees = getTrees(menuList, true, roleMenuList, true);
  92. }
  93. else
  94. {
  95. trees = getTrees(menuList, false, null, true);
  96. }
  97. return trees;
  98. }
  99. /**
  100. * 查询所有菜单
  101. *
  102. * @param role 角色对象
  103. * @return 菜单列表
  104. */
  105. @Override
  106. public List<Map<String, Object>> menuTreeData()
  107. {
  108. List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
  109. List<Menu> menuList = menuDao.selectMenuAll();
  110. trees = getTrees(menuList, false, null, false);
  111. return trees;
  112. }
  113. /**
  114. * 查询系统所有权限
  115. *
  116. * @return 权限列表
  117. */
  118. @Override
  119. public LinkedHashMap<String, String> selectPermsAll()
  120. {
  121. LinkedHashMap<String, String> section = new LinkedHashMap<>();
  122. List<Menu> permissions = menuDao.selectMenuAll();
  123. if (StringUtils.isNotEmpty(permissions))
  124. {
  125. for (Menu menu : permissions)
  126. {
  127. section.put(menu.getUrl(), MessageFormat.format(PREMISSION_STRING, menu.getPerms()));
  128. }
  129. }
  130. return section;
  131. }
  132. /**
  133. * 对象转菜单树
  134. *
  135. * @param menuList 菜单列表
  136. * @param isCheck 是否需要选中
  137. * @param roleMenuList 角色已存在菜单列表
  138. * @param permsFlag 是否需要显示权限标识
  139. * @return
  140. */
  141. public List<Map<String, Object>> getTrees(List<Menu> menuList, boolean isCheck, List<String> roleMenuList,
  142. boolean permsFlag)
  143. {
  144. List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
  145. for (Menu menu : menuList)
  146. {
  147. Map<String, Object> deptMap = new HashMap<String, Object>();
  148. deptMap.put("id", menu.getMenuId());
  149. deptMap.put("pId", menu.getParentId());
  150. deptMap.put("name", transMenuName(menu, roleMenuList, permsFlag));
  151. if (isCheck)
  152. {
  153. deptMap.put("checked", roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
  154. }
  155. else
  156. {
  157. deptMap.put("checked", false);
  158. }
  159. trees.add(deptMap);
  160. }
  161. return trees;
  162. }
  163. public String transMenuName(Menu menu, List<String> roleMenuList, boolean permsFlag)
  164. {
  165. StringBuffer sb = new StringBuffer();
  166. sb.append(menu.getMenuName());
  167. if (permsFlag)
  168. {
  169. sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
  170. }
  171. return sb.toString();
  172. }
  173. /**
  174. * 删除菜单管理信息
  175. *
  176. * @param menuId 菜单ID
  177. * @return 结果
  178. */
  179. @Override
  180. public int deleteMenuById(Long menuId)
  181. {
  182. return menuDao.deleteMenuById(menuId);
  183. }
  184. /**
  185. * 根据菜单ID查询信息
  186. *
  187. * @param menuId 菜单ID
  188. * @return 菜单信息
  189. */
  190. @Override
  191. public Menu selectMenuById(Long menuId)
  192. {
  193. return menuDao.selectMenuById(menuId);
  194. }
  195. /**
  196. * 查询子菜单数量
  197. *
  198. * @param menuId 菜单ID
  199. * @return 结果
  200. */
  201. @Override
  202. public int selectCountMenuByParentId(Long parentId)
  203. {
  204. return menuDao.selectCountMenuByParentId(parentId);
  205. }
  206. /**
  207. * 查询菜单使用数量
  208. *
  209. * @param menuId 菜单ID
  210. * @return 结果
  211. */
  212. @Override
  213. public int selectCountRoleMenuByMenuId(Long menuId)
  214. {
  215. return roleMenuDao.selectCountRoleMenuByMenuId(menuId);
  216. }
  217. /**
  218. * 保存菜单信息
  219. *
  220. * @param menu 菜单信息
  221. * @return 结果
  222. */
  223. @Override
  224. public int saveMenu(Menu menu)
  225. {
  226. Long menuId = menu.getMenuId();
  227. if (StringUtils.isNotNull(menuId))
  228. {
  229. menu.setUpdateBy(ShiroUtils.getLoginName());
  230. return menuDao.updateMenu(menu);
  231. }
  232. else
  233. {
  234. menu.setCreateBy(ShiroUtils.getLoginName());
  235. return menuDao.insertMenu(menu);
  236. }
  237. }
  238. /**
  239. * 校验菜单名称是否唯一
  240. *
  241. * @param menu 菜单信息
  242. * @return 结果
  243. */
  244. @Override
  245. public String checkMenuNameUnique(Menu menu)
  246. {
  247. Long menuId = menu.getMenuId();
  248. Menu info = menuDao.checkMenuNameUnique(menu.getMenuName());
  249. if (StringUtils.isNotNull(info) && StringUtils.isNotNull(info.getMenuId())
  250. && info.getMenuId().longValue() != menuId.longValue())
  251. {
  252. return UserConstants.NAME_NOT_UNIQUE;
  253. }
  254. return UserConstants.NAME_UNIQUE;
  255. }
  256. }