|
|
@@ -0,0 +1,425 @@
|
|
|
+package com.zhongzheng.modules.store.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.zhongzheng.common.constant.UserConstants;
|
|
|
+import com.zhongzheng.common.core.domain.entity.store.StoreMenu;
|
|
|
+import com.zhongzheng.common.core.domain.entity.store.StoreRole;
|
|
|
+import com.zhongzheng.common.core.domain.entity.store.StoreRoleMenu;
|
|
|
+import com.zhongzheng.common.core.domain.entity.store.StoreUser;
|
|
|
+import com.zhongzheng.common.utils.SecurityUtils;
|
|
|
+import com.zhongzheng.modules.store.mapper.StoreMenuMapper;
|
|
|
+import com.zhongzheng.modules.store.mapper.StoreRoleMapper;
|
|
|
+import com.zhongzheng.modules.store.mapper.StoreRoleMenuMapper;
|
|
|
+import com.zhongzheng.modules.store.service.IStoreMenuService;
|
|
|
+import com.zhongzheng.modules.store.service.IStoreUserService;
|
|
|
+import com.zhongzheng.modules.system.domain.vo.MetaVo;
|
|
|
+import com.zhongzheng.modules.system.domain.vo.RouterVo;
|
|
|
+import com.zhongzheng.modules.top.util.TreeSelectStore;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 【请填写功能名称】Service业务层处理
|
|
|
+ *
|
|
|
+ * @author ruoyi
|
|
|
+ * @date 2023-05-25
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class StoreMenuServiceImpl extends ServiceImpl<StoreMenuMapper, StoreMenu> implements IStoreMenuService {
|
|
|
+
|
|
|
+ public static final String PREMISSION_STRING = "perms[\"{0}\"]";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StoreRoleMapper storeRoleMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StoreRoleMenuMapper storeRoleMenuMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IStoreUserService storeUserService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户查询系统菜单列表
|
|
|
+ *
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @return 菜单列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<StoreMenu> selectMenuList(Long userId) {
|
|
|
+ return selectMenuList(new StoreMenu(), userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询系统菜单列表
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 菜单列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<StoreMenu> selectMenuList(StoreMenu menu, Long userId) {
|
|
|
+ List<StoreMenu> menuList = null;
|
|
|
+ StoreUser sysUser = storeUserService.selectUserById(userId);
|
|
|
+ // 管理员显示所有菜单信息
|
|
|
+ if (sysUser.isAdmin()) {
|
|
|
+ menuList = list(new LambdaQueryWrapper<StoreMenu>()
|
|
|
+ .like(StrUtil.isNotBlank(menu.getMenuName()),StoreMenu::getMenuName,menu.getMenuName())
|
|
|
+ .eq(StrUtil.isNotBlank(menu.getVisible()),StoreMenu::getVisible,menu.getVisible())
|
|
|
+ .eq(StrUtil.isNotBlank(menu.getStatus()),StoreMenu::getStatus,menu.getStatus())
|
|
|
+ .orderByAsc(StoreMenu::getParentId)
|
|
|
+ .orderByAsc(StoreMenu::getOrderNum));
|
|
|
+ } else {
|
|
|
+ menu.getParams().put("userId", userId);
|
|
|
+ menuList = baseMapper.selectMenuListByUserId(menu);
|
|
|
+ }
|
|
|
+ return menuList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户ID查询权限
|
|
|
+ *
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @return 权限列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Set<String> selectMenuPermsByUserId(Long userId) {
|
|
|
+ List<String> perms = baseMapper.selectMenuPermsByUserId(userId);
|
|
|
+ Set<String> permsSet = new HashSet<>();
|
|
|
+ for (String perm : perms) {
|
|
|
+ if (Validator.isNotEmpty(perm)) {
|
|
|
+ permsSet.addAll(Arrays.asList(perm.trim().split(",")));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return permsSet;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户ID查询菜单
|
|
|
+ *
|
|
|
+ * @param userId 用户名称
|
|
|
+ * @return 菜单列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<StoreMenu> selectMenuTreeByUserId(Long userId) {
|
|
|
+ List<StoreMenu> menus = null;
|
|
|
+ if (SecurityUtils.isTopAdmin(userId)) {
|
|
|
+ menus = baseMapper.selectMenuTreeAll();
|
|
|
+ } else {
|
|
|
+ menus = baseMapper.selectMenuTreeByUserId(userId);
|
|
|
+ }
|
|
|
+ return getChildPerms(menus, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据角色ID查询菜单树信息
|
|
|
+ *
|
|
|
+ * @param roleId 角色ID
|
|
|
+ * @return 选中菜单列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<Integer> selectMenuListByRoleId(Long roleId) {
|
|
|
+ StoreRole role = storeRoleMapper.selectById(roleId);
|
|
|
+ return baseMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建前端路由所需要的菜单
|
|
|
+ *
|
|
|
+ * @param menus 菜单列表
|
|
|
+ * @return 路由列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<RouterVo> buildMenus(List<StoreMenu> menus) {
|
|
|
+ List<RouterVo> routers = new LinkedList<RouterVo>();
|
|
|
+ for (StoreMenu menu : menus) {
|
|
|
+ RouterVo router = new RouterVo();
|
|
|
+ router.setMenuId(menu.getMenuId());
|
|
|
+ router.setHidden("1".equals(menu.getVisible()));
|
|
|
+ router.setName(getRouteName(menu));
|
|
|
+ router.setPath(getRouterPath(menu));
|
|
|
+ router.setComponent(getComponent(menu));
|
|
|
+ router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StrUtil.equals("1", menu.getIsCache().toString())));
|
|
|
+ List<StoreMenu> cMenus = menu.getChildren();
|
|
|
+ if (!cMenus.isEmpty() && cMenus.size() > 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType())) {
|
|
|
+ router.setAlwaysShow(true);
|
|
|
+ router.setRedirect("noRedirect");
|
|
|
+ router.setChildren(buildMenus(cMenus));
|
|
|
+ } else if (isMenuFrame(menu)) {
|
|
|
+ router.setMeta(null);
|
|
|
+ List<RouterVo> childrenList = new ArrayList<RouterVo>();
|
|
|
+ RouterVo children = new RouterVo();
|
|
|
+ children.setPath(menu.getPath());
|
|
|
+ children.setComponent(menu.getComponent());
|
|
|
+ children.setName(StrUtil.upperFirst(menu.getPath()));
|
|
|
+ children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StrUtil.equals("1", menu.getIsCache().toString())));
|
|
|
+ childrenList.add(children);
|
|
|
+ router.setChildren(childrenList);
|
|
|
+ }
|
|
|
+ routers.add(router);
|
|
|
+ }
|
|
|
+ return routers;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建前端所需要树结构
|
|
|
+ *
|
|
|
+ * @param menus 菜单列表
|
|
|
+ * @return 树结构列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<StoreMenu> buildMenuTree(List<StoreMenu> menus) {
|
|
|
+ List<StoreMenu> returnList = new ArrayList<StoreMenu>();
|
|
|
+ List<Long> tempList = new ArrayList<Long>();
|
|
|
+ for (StoreMenu dept : menus) {
|
|
|
+ tempList.add(dept.getMenuId());
|
|
|
+ }
|
|
|
+ for (Iterator<StoreMenu> iterator = menus.iterator(); iterator.hasNext(); ) {
|
|
|
+ StoreMenu menu = (StoreMenu) iterator.next();
|
|
|
+ // 如果是顶级节点, 遍历该父节点的所有子节点
|
|
|
+ if (!tempList.contains(menu.getParentId())) {
|
|
|
+ recursionFn(menus, menu);
|
|
|
+ returnList.add(menu);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (returnList.isEmpty()) {
|
|
|
+ returnList = menus;
|
|
|
+ }
|
|
|
+ return returnList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建前端所需要下拉树结构
|
|
|
+ *
|
|
|
+ * @param menus 菜单列表
|
|
|
+ * @return 下拉树结构列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<TreeSelectStore> buildMenuTreeSelect(List<StoreMenu> menus) {
|
|
|
+ List<StoreMenu> menuTrees = buildMenuTree(menus);
|
|
|
+ return menuTrees.stream().map(TreeSelectStore::new).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据菜单ID查询信息
|
|
|
+ *
|
|
|
+ * @param menuId 菜单ID
|
|
|
+ * @return 菜单信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public StoreMenu selectMenuById(Long menuId) {
|
|
|
+ return getById(menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否存在菜单子节点
|
|
|
+ *
|
|
|
+ * @param menuId 菜单ID
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean hasChildByMenuId(Long menuId) {
|
|
|
+ int result = count(new LambdaQueryWrapper<StoreMenu>().eq(StoreMenu::getParentId,menuId));
|
|
|
+ return result > 0 ? true : false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询菜单使用数量
|
|
|
+ *
|
|
|
+ * @param menuId 菜单ID
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean checkMenuExistRole(Long menuId) {
|
|
|
+ int result = storeRoleMenuMapper.selectCount(new LambdaQueryWrapper<StoreRoleMenu>().eq(StoreRoleMenu::getMenuId,menuId));
|
|
|
+ return result > 0 ? true : false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增保存菜单信息
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int insertMenu(StoreMenu menu) {
|
|
|
+ return baseMapper.insert(menu);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改保存菜单信息
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int updateMenu(StoreMenu menu) {
|
|
|
+ return baseMapper.updateById(menu);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除菜单管理信息
|
|
|
+ *
|
|
|
+ * @param menuId 菜单ID
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int deleteMenuById(Long menuId) {
|
|
|
+ return baseMapper.deleteById(menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验菜单名称是否唯一
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String checkMenuNameUnique(StoreMenu menu) {
|
|
|
+ Long menuId = Validator.isNull(menu.getMenuId()) ? -1L : menu.getMenuId();
|
|
|
+ StoreMenu info = getOne(new LambdaQueryWrapper<StoreMenu>()
|
|
|
+ .eq(StoreMenu::getMenuName,menu.getMenuName())
|
|
|
+ .eq(StoreMenu::getParentId,menu.getParentId())
|
|
|
+ .last("limit 1"));
|
|
|
+ if (Validator.isNotNull(info) && info.getMenuId().longValue() != menuId.longValue()) {
|
|
|
+ return UserConstants.NOT_UNIQUE;
|
|
|
+ }
|
|
|
+ return UserConstants.UNIQUE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取路由名称
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 路由名称
|
|
|
+ */
|
|
|
+ public String getRouteName(StoreMenu menu) {
|
|
|
+ String routerName = StrUtil.upperFirst(menu.getPath());
|
|
|
+ // 非外链并且是一级目录(类型为目录)
|
|
|
+ if (isMenuFrame(menu)) {
|
|
|
+ routerName = StrUtil.EMPTY;
|
|
|
+ }
|
|
|
+ return routerName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取路由地址
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 路由地址
|
|
|
+ */
|
|
|
+ public String getRouterPath(StoreMenu menu) {
|
|
|
+ String routerPath = menu.getPath();
|
|
|
+ // 非外链并且是一级目录(类型为目录)
|
|
|
+ if (0 == menu.getParentId().intValue() && UserConstants.TYPE_DIR.equals(menu.getMenuType())
|
|
|
+ && UserConstants.NO_FRAME.equals(menu.getIsFrame())) {
|
|
|
+ routerPath = "/" + menu.getPath();
|
|
|
+ }
|
|
|
+ // 非外链并且是一级目录(类型为菜单)
|
|
|
+ else if (isMenuFrame(menu)) {
|
|
|
+ routerPath = "/";
|
|
|
+ }
|
|
|
+ return routerPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取组件信息
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 组件信息
|
|
|
+ */
|
|
|
+ public String getComponent(StoreMenu menu) {
|
|
|
+ String component = UserConstants.LAYOUT;
|
|
|
+ if (StrUtil.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu)) {
|
|
|
+ component = menu.getComponent();
|
|
|
+ } else if (StrUtil.isEmpty(menu.getComponent()) && isParentView(menu)) {
|
|
|
+ component = UserConstants.PARENT_VIEW;
|
|
|
+ }
|
|
|
+ return component;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否为菜单内部跳转
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ public boolean isMenuFrame(StoreMenu menu) {
|
|
|
+ return menu.getParentId().intValue() == 0 && UserConstants.TYPE_MENU.equals(menu.getMenuType())
|
|
|
+ && menu.getIsFrame().equals(UserConstants.NO_FRAME);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否为parent_view组件
|
|
|
+ *
|
|
|
+ * @param menu 菜单信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ public boolean isParentView(StoreMenu menu) {
|
|
|
+ return menu.getParentId().intValue() != 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据父节点的ID获取所有子节点
|
|
|
+ *
|
|
|
+ * @param list 分类表
|
|
|
+ * @param parentId 传入的父节点ID
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public List<StoreMenu> getChildPerms(List<StoreMenu> list, int parentId) {
|
|
|
+ List<StoreMenu> returnList = new ArrayList<StoreMenu>();
|
|
|
+ for (Iterator<StoreMenu> iterator = list.iterator(); iterator.hasNext(); ) {
|
|
|
+ StoreMenu t = (StoreMenu) iterator.next();
|
|
|
+ // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
|
|
|
+ if (t.getParentId() == parentId) {
|
|
|
+ recursionFn(list, t);
|
|
|
+ returnList.add(t);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return returnList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归列表
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @param t
|
|
|
+ */
|
|
|
+ private void recursionFn(List<StoreMenu> list, StoreMenu t) {
|
|
|
+ // 得到子节点列表
|
|
|
+ List<StoreMenu> childList = getChildList(list, t);
|
|
|
+ t.setChildren(childList);
|
|
|
+ for (StoreMenu tChild : childList) {
|
|
|
+ if (hasChild(list, tChild)) {
|
|
|
+ recursionFn(list, tChild);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到子节点列表
|
|
|
+ */
|
|
|
+ private List<StoreMenu> getChildList(List<StoreMenu> list, StoreMenu t) {
|
|
|
+ List<StoreMenu> tlist = new ArrayList<StoreMenu>();
|
|
|
+ Iterator<StoreMenu> it = list.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ StoreMenu n = (StoreMenu) it.next();
|
|
|
+ if (n.getParentId().longValue() == t.getMenuId().longValue()) {
|
|
|
+ tlist.add(n);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return tlist;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否有子节点
|
|
|
+ */
|
|
|
+ private boolean hasChild(List<StoreMenu> list, StoreMenu t) {
|
|
|
+ return getChildList(list, t).size() > 0 ? true : false;
|
|
|
+ }
|
|
|
+}
|