package com.ots.project.system.menu.service; import com.ots.common.constant.UserConstants; import com.ots.common.utils.MessageUtils; import com.ots.common.utils.StringUtils; import com.ots.common.utils.TreeUtils; import com.ots.common.utils.security.ShiroUtils; import com.ots.framework.web.domain.Ztree; import com.ots.project.system.menu.domain.Menu; import com.ots.project.system.menu.mapper.MenuMapper; import com.ots.project.system.role.domain.Role; import com.ots.project.system.role.mapper.RoleMenuMapper; import com.ots.project.system.user.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.MessageFormat; import java.util.*; @Service public class MenuServiceImpl implements IMenuService { public static final String PREMISSION_STRING = "perms[\"{0}\"]"; @Autowired private MenuMapper menuMapper; @Autowired private RoleMenuMapper roleMenuMapper; @Override public List selectMenusByUser(User user) { List menus; if (user.isAdmin()) { menus = menuMapper.selectMenuNormalAll(); } else { menus = menuMapper.selectMenusByUserId(user.getUserId()); } for (int i = 0; i < menus.size(); i++) { if (StringUtils.isNotBlank(menus.get(i).getMenuNameCode())) { menus.get(i).setMenuName(MessageUtils.message(menus.get(i).getMenuNameCode())); } } return TreeUtils.getChildPerms(menus, 0); } @Override public List selectMenuList(Menu menu) { List menuList = null; User user = ShiroUtils.getSysUser(); if (user.isAdmin()) { menuList = menuMapper.selectMenuList(menu); } else { menu.getParams().put("userId", user.getUserId()); menuList = menuMapper.selectMenuListByUserId(menu); } return menuList; } @Override public List selectMenuAll() { List menuList = null; User user = ShiroUtils.getSysUser(); if (user.isAdmin()) { menuList = menuMapper.selectMenuAll(); } else { menuList = menuMapper.selectMenuAllByUserId(user.getUserId()); } return menuList; } @Override public Set selectPermsByUserId(Long userId) { List perms = menuMapper.selectPermsByUserId(userId); Set permsSet = new HashSet<>(); for (String perm : perms) { if (StringUtils.isNotEmpty(perm)) { permsSet.addAll(Arrays.asList(perm.trim().split(","))); } } return permsSet; } @Override public List roleMenuTreeData(Role role) { Long roleId = role.getRoleId(); List ztrees = new ArrayList(); List menuList = selectMenuAll(); if (StringUtils.isNotNull(roleId)) { List roleMenuList = menuMapper.selectMenuTree(roleId); ztrees = initZtree(menuList, roleMenuList, true); } else { ztrees = initZtree(menuList, null, true); } return ztrees; } @Override public List menuTreeData() { List menuList = selectMenuAll(); List ztrees = initZtree(menuList); return ztrees; } @Override public LinkedHashMap selectPermsAll() { LinkedHashMap section = new LinkedHashMap<>(); List permissions = selectMenuAll(); if (StringUtils.isNotEmpty(permissions)) { for (Menu menu : permissions) { section.put(menu.getUrl(), MessageFormat.format(PREMISSION_STRING, menu.getPerms())); } } return section; } public List initZtree(List menuList) { return initZtree(menuList, null, false); } public List initZtree(List menuList, List roleMenuList, boolean permsFlag) { List ztrees = new ArrayList(); boolean isCheck = StringUtils.isNotNull(roleMenuList); for (Menu menu : menuList) { Ztree ztree = new Ztree(); ztree.setId(menu.getMenuId()); ztree.setpId(menu.getParentId()); ztree.setName(transMenuName(menu, permsFlag)); ztree.setTitle(menu.getMenuName()); if (isCheck) { ztree.setChecked(roleMenuList.contains(menu.getMenuId() + menu.getPerms())); } ztrees.add(ztree); } return ztrees; } public String transMenuName(Menu menu, boolean permsFlag) { StringBuffer sb = new StringBuffer(); sb.append(menu.getMenuName()); if (permsFlag) { sb.append("   " + menu.getPerms() + ""); } return sb.toString(); } @Override public int deleteMenuById(Long menuId) { ShiroUtils.clearCachedAuthorizationInfo(); return menuMapper.deleteMenuById(menuId); } @Override public Menu selectMenuById(Long menuId) { return menuMapper.selectMenuById(menuId); } @Override public int selectCountMenuByParentId(Long parentId) { return menuMapper.selectCountMenuByParentId(parentId); } @Override public int selectCountRoleMenuByMenuId(Long menuId) { return roleMenuMapper.selectCountRoleMenuByMenuId(menuId); } @Override public int insertMenu(Menu menu) { menu.setCreateBy(ShiroUtils.getLoginName()); ShiroUtils.clearCachedAuthorizationInfo(); return menuMapper.insertMenu(menu); } @Override public int updateMenu(Menu menu) { menu.setUpdateBy(ShiroUtils.getLoginName()); ShiroUtils.clearCachedAuthorizationInfo(); return menuMapper.updateMenu(menu); } @Override public String checkMenuNameUnique(Menu menu) { Long menuId = StringUtils.isNull(menu.getMenuId()) ? -1L : menu.getMenuId(); Menu info = menuMapper.checkMenuNameUnique(menu.getMenuName(), menu.getParentId()); if (StringUtils.isNotNull(info) && info.getMenuId().longValue() != menuId.longValue()) { return UserConstants.MENU_NAME_NOT_UNIQUE; } return UserConstants.MENU_NAME_UNIQUE; } }