From 0f7cefaf4b6c2b516d2bf6e8e3783c1677cf18f6 Mon Sep 17 00:00:00 2001 From: ZhengJie <201507802@qq.com> Date: Mon, 8 Jun 2020 12:00:43 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BB=A3=E7=A0=81=E5=AE=8C=E5=96=84](v2.5):?= =?UTF-8?q?=20v2.5=20beta=20=E8=8F=9C=E5=8D=95=E7=BC=96=E8=BE=91=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E4=BC=98=E5=8C=96=EF=BC=8C=E9=83=A8=E9=97=A8=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E7=BC=93=E5=AD=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close https://github.com/elunez/eladmin/issues/390 --- .../system/repository/RoleRepository.java | 11 ++++ .../modules/system/service/RoleService.java | 7 +++ .../system/service/impl/DeptServiceImpl.java | 49 ++++++++--------- .../system/service/impl/MenuServiceImpl.java | 52 +++++++++++-------- .../system/service/impl/RoleServiceImpl.java | 5 ++ 5 files changed, 75 insertions(+), 49 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/RoleRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/RoleRepository.java index d761cee2..8e76cc55 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/RoleRepository.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/repository/RoleRepository.java @@ -20,6 +20,8 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; + +import java.util.List; import java.util.Set; /** @@ -66,4 +68,13 @@ public interface RoleRepository extends JpaRepository, JpaSpecificat @Query(value = "select count(1) from sys_role r, sys_roles_depts d where " + "r.role_id = d.role_id and d.dept_id in ?1",nativeQuery = true) int countByDepts(Set deptIds); + + /** + * 根据菜单Id查询 + * @param menuIds / + * @return / + */ + @Query(value = "SELECT r.* FROM sys_role r, sys_roles_menus m WHERE " + + "r.role_id = m.role_id AND m.menu_id in ?1",nativeQuery = true) + List findInMenuId(List menuIds); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java index a157a191..65e4f587 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java @@ -126,4 +126,11 @@ public interface RoleService { * @param ids / */ void verification(Set ids); + + /** + * 根据菜单Id查询 + * @param menuIds / + * @return / + */ + List findInMenuId(List menuIds); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java index f572d889..520da6ef 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java @@ -106,18 +106,17 @@ public class DeptServiceImpl implements DeptService { deptRepository.save(resources); // 计算子节点数目 resources.setSubCount(0); - if(resources.getPid() != null){ - // 清理缓存 - redisUtils.del("dept::pid:" + resources.getPid()); - updateSubCnt(resources.getPid()); - } + // 清理缓存 + redisUtils.del("dept::pid:" + (resources.getPid() == null ? 0 : resources.getPid())); + updateSubCnt(resources.getPid()); } @Override @Transactional(rollbackFor = Exception.class) public void update(Dept resources) { // 旧的部门 - Long pid = findById(resources.getId()).getPid(); + Long oldPid = findById(resources.getId()).getPid(); + Long newPid = resources.getPid(); if(resources.getPid() != null && resources.getId().equals(resources.getPid())) { throw new BadRequestException("上级不能为自己"); } @@ -125,14 +124,11 @@ public class DeptServiceImpl implements DeptService { ValidationUtil.isNull( dept.getId(),"Dept","id",resources.getId()); resources.setId(dept.getId()); deptRepository.save(resources); - if(resources.getPid() == null){ - updateSubCnt(pid); - } else { - pid = resources.getPid(); - updateSubCnt(resources.getPid()); - } + // 更新父节点中子节点数目 + updateSubCnt(oldPid); + updateSubCnt(newPid); // 清理缓存 - delCaches(resources.getId(), pid); + delCaches(resources.getId(), oldPid, newPid); } @Override @@ -140,11 +136,9 @@ public class DeptServiceImpl implements DeptService { public void delete(Set deptDtos) { for (DeptDto deptDto : deptDtos) { // 清理缓存 - delCaches(deptDto.getId(), deptDto.getPid()); + delCaches(deptDto.getId(), deptDto.getPid(), null); deptRepository.deleteById(deptDto.getId()); - if(deptDto.getPid() != null){ - updateSubCnt(deptDto.getPid()); - } + updateSubCnt(deptDto.getPid()); } } @@ -235,11 +229,6 @@ public class DeptServiceImpl implements DeptService { return map; } - private void updateSubCnt(Long deptId){ - int count = deptRepository.countByPid(deptId); - deptRepository.updateSubCntById(count, deptId); - } - @Override public void verification(Set deptDtos) { Set deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet()); @@ -251,17 +240,25 @@ public class DeptServiceImpl implements DeptService { } } + private void updateSubCnt(Long deptId){ + if(deptId != null){ + int count = deptRepository.countByPid(deptId); + deptRepository.updateSubCntById(count, deptId); + } + } + /** * 清理缓存 * @param id / + * @param oldPid / + * @param newPid / */ - public void delCaches(Long id, Long pid){ + public void delCaches(Long id, Long oldPid, Long newPid){ List users = userRepository.findByDeptRoleId(id); // 删除数据权限 redisUtils.delByKeys("data::user:",users.stream().map(User::getId).collect(Collectors.toSet())); redisUtils.del("dept::id:" + id); - if (pid != null) { - redisUtils.del("dept::pid:" + pid); - } + redisUtils.del("dept::pid:" + (oldPid == null ? 0 : oldPid)); + redisUtils.del("dept::pid:" + (newPid == null ? 0 : newPid)); } } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MenuServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MenuServiceImpl.java index 33642f7d..386d06c3 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MenuServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MenuServiceImpl.java @@ -19,6 +19,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.system.domain.Menu; +import me.zhengjie.modules.system.domain.Role; import me.zhengjie.modules.system.domain.User; import me.zhengjie.modules.system.domain.vo.MenuMetaVo; import me.zhengjie.modules.system.domain.vo.MenuVo; @@ -125,10 +126,8 @@ public class MenuServiceImpl implements MenuService { menuRepository.save(resources); // 计算子节点数目 resources.setSubCount(0); - if(resources.getPid() != null){ - // 清理缓存 - updateSubCnt(resources.getPid()); - } + // 更新父节点菜单数目 + updateSubCnt(resources.getPid()); redisUtils.del("menu::pid:" + (resources.getPid() == null ? 0 : resources.getPid())); } @@ -139,8 +138,6 @@ public class MenuServiceImpl implements MenuService { throw new BadRequestException("上级不能为自己"); } Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new); - // 记录旧的父节点ID - Long pid = menu.getPid(); ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId()); if(resources.getIFrame()){ @@ -158,6 +155,11 @@ public class MenuServiceImpl implements MenuService { if(resources.getPid().equals(0L)){ resources.setPid(null); } + + // 记录的父节点ID + Long oldPid = menu.getPid(); + Long newPid = resources.getPid(); + if(StringUtils.isNotBlank(resources.getComponentName())){ menu1 = menuRepository.findByComponentName(resources.getComponentName()); if(menu1 != null && !menu1.getId().equals(menu.getId())){ @@ -177,15 +179,11 @@ public class MenuServiceImpl implements MenuService { menu.setPermission(resources.getPermission()); menu.setType(resources.getType()); menuRepository.save(menu); - // 计算子节点数目 - if(resources.getPid() == null){ - updateSubCnt(pid); - } else { - pid = resources.getPid(); - updateSubCnt(resources.getPid()); - } + // 计算父级菜单节点数目 + updateSubCnt(oldPid); + updateSubCnt(newPid); // 清理缓存 - delCaches(resources.getId(), pid); + delCaches(resources.getId(), oldPid, newPid); } @Override @@ -206,12 +204,10 @@ public class MenuServiceImpl implements MenuService { public void delete(Set menuSet) { for (Menu menu : menuSet) { // 清理缓存 - delCaches(menu.getId(), menu.getPid()); + delCaches(menu.getId(), menu.getPid(), null); roleService.untiedMenu(menu.getId()); menuRepository.deleteById(menu.getId()); - if(menu.getPid() != null){ - updateSubCnt(menu.getPid()); - } + updateSubCnt(menu.getPid()); } } @@ -336,19 +332,29 @@ public class MenuServiceImpl implements MenuService { } private void updateSubCnt(Long menuId){ - int count = menuRepository.countByPid(menuId); - menuRepository.updateSubCntById(count, menuId); + if(menuId != null){ + int count = menuRepository.countByPid(menuId); + menuRepository.updateSubCntById(count, menuId); + } } /** * 清理缓存 * @param id 菜单ID - * @param pid 菜单父级ID + * @param oldPid 旧的菜单父级ID + * @param newPid 新的菜单父级ID */ - public void delCaches(Long id, Long pid){ + public void delCaches(Long id, Long oldPid, Long newPid){ List users = userRepository.findByMenuId(id); redisUtils.del("menu::id:" +id); redisUtils.delByKeys("menu::user:",users.stream().map(User::getId).collect(Collectors.toSet())); - redisUtils.del("menu::pid:" + (pid == null ? 0 : pid)); + redisUtils.del("menu::pid:" + (oldPid == null ? 0 : oldPid)); + redisUtils.del("menu::pid:" + (newPid == null ? 0 : newPid)); + // 清除 Role 缓存 + List roles = roleService.findInMenuId(new ArrayList(){{ + add(id); + add(newPid == null ? 0 : newPid); + }}); + redisUtils.delByKeys("role::id:",roles.stream().map(Role::getId).collect(Collectors.toSet())); } } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java index 5ad362a0..a2f0cdba 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java @@ -210,4 +210,9 @@ public class RoleServiceImpl implements RoleService { throw new BadRequestException("所选角色存在用户关联,请解除关联再试!"); } } + + @Override + public List findInMenuId(List menuIds) { + return roleRepository.findInMenuId(menuIds); + } }