mirror of https://github.com/elunez/eladmin
[代码完善](v2.5): v2.5 beta 菜单编辑缓存优化,部门编辑缓存优化
close https://github.com/elunez/eladmin/issues/390pull/394/head
parent
a5880e701c
commit
0f7cefaf4b
|
@ -20,6 +20,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
import org.springframework.data.jpa.repository.Modifying;
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,4 +68,13 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
|
||||||
@Query(value = "select count(1) from sys_role r, sys_roles_depts d where " +
|
@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)
|
"r.role_id = d.role_id and d.dept_id in ?1",nativeQuery = true)
|
||||||
int countByDepts(Set<Long> deptIds);
|
int countByDepts(Set<Long> 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<Role> findInMenuId(List<Long> menuIds);
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,4 +126,11 @@ public interface RoleService {
|
||||||
* @param ids /
|
* @param ids /
|
||||||
*/
|
*/
|
||||||
void verification(Set<Long> ids);
|
void verification(Set<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜单Id查询
|
||||||
|
* @param menuIds /
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
List<Role> findInMenuId(List<Long> menuIds);
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,18 +106,17 @@ public class DeptServiceImpl implements DeptService {
|
||||||
deptRepository.save(resources);
|
deptRepository.save(resources);
|
||||||
// 计算子节点数目
|
// 计算子节点数目
|
||||||
resources.setSubCount(0);
|
resources.setSubCount(0);
|
||||||
if(resources.getPid() != null){
|
// 清理缓存
|
||||||
// 清理缓存
|
redisUtils.del("dept::pid:" + (resources.getPid() == null ? 0 : resources.getPid()));
|
||||||
redisUtils.del("dept::pid:" + resources.getPid());
|
updateSubCnt(resources.getPid());
|
||||||
updateSubCnt(resources.getPid());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void update(Dept resources) {
|
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())) {
|
if(resources.getPid() != null && resources.getId().equals(resources.getPid())) {
|
||||||
throw new BadRequestException("上级不能为自己");
|
throw new BadRequestException("上级不能为自己");
|
||||||
}
|
}
|
||||||
|
@ -125,14 +124,11 @@ public class DeptServiceImpl implements DeptService {
|
||||||
ValidationUtil.isNull( dept.getId(),"Dept","id",resources.getId());
|
ValidationUtil.isNull( dept.getId(),"Dept","id",resources.getId());
|
||||||
resources.setId(dept.getId());
|
resources.setId(dept.getId());
|
||||||
deptRepository.save(resources);
|
deptRepository.save(resources);
|
||||||
if(resources.getPid() == null){
|
// 更新父节点中子节点数目
|
||||||
updateSubCnt(pid);
|
updateSubCnt(oldPid);
|
||||||
} else {
|
updateSubCnt(newPid);
|
||||||
pid = resources.getPid();
|
|
||||||
updateSubCnt(resources.getPid());
|
|
||||||
}
|
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
delCaches(resources.getId(), pid);
|
delCaches(resources.getId(), oldPid, newPid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -140,11 +136,9 @@ public class DeptServiceImpl implements DeptService {
|
||||||
public void delete(Set<DeptDto> deptDtos) {
|
public void delete(Set<DeptDto> deptDtos) {
|
||||||
for (DeptDto deptDto : deptDtos) {
|
for (DeptDto deptDto : deptDtos) {
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
delCaches(deptDto.getId(), deptDto.getPid());
|
delCaches(deptDto.getId(), deptDto.getPid(), null);
|
||||||
deptRepository.deleteById(deptDto.getId());
|
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;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateSubCnt(Long deptId){
|
|
||||||
int count = deptRepository.countByPid(deptId);
|
|
||||||
deptRepository.updateSubCntById(count, deptId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void verification(Set<DeptDto> deptDtos) {
|
public void verification(Set<DeptDto> deptDtos) {
|
||||||
Set<Long> deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet());
|
Set<Long> 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 id /
|
||||||
|
* @param oldPid /
|
||||||
|
* @param newPid /
|
||||||
*/
|
*/
|
||||||
public void delCaches(Long id, Long pid){
|
public void delCaches(Long id, Long oldPid, Long newPid){
|
||||||
List<User> users = userRepository.findByDeptRoleId(id);
|
List<User> users = userRepository.findByDeptRoleId(id);
|
||||||
// 删除数据权限
|
// 删除数据权限
|
||||||
redisUtils.delByKeys("data::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
|
redisUtils.delByKeys("data::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
|
||||||
redisUtils.del("dept::id:" + id);
|
redisUtils.del("dept::id:" + id);
|
||||||
if (pid != null) {
|
redisUtils.del("dept::pid:" + (oldPid == null ? 0 : oldPid));
|
||||||
redisUtils.del("dept::pid:" + pid);
|
redisUtils.del("dept::pid:" + (newPid == null ? 0 : newPid));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -19,6 +19,7 @@ import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import me.zhengjie.modules.system.domain.Menu;
|
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.User;
|
||||||
import me.zhengjie.modules.system.domain.vo.MenuMetaVo;
|
import me.zhengjie.modules.system.domain.vo.MenuMetaVo;
|
||||||
import me.zhengjie.modules.system.domain.vo.MenuVo;
|
import me.zhengjie.modules.system.domain.vo.MenuVo;
|
||||||
|
@ -125,10 +126,8 @@ public class MenuServiceImpl implements MenuService {
|
||||||
menuRepository.save(resources);
|
menuRepository.save(resources);
|
||||||
// 计算子节点数目
|
// 计算子节点数目
|
||||||
resources.setSubCount(0);
|
resources.setSubCount(0);
|
||||||
if(resources.getPid() != null){
|
// 更新父节点菜单数目
|
||||||
// 清理缓存
|
updateSubCnt(resources.getPid());
|
||||||
updateSubCnt(resources.getPid());
|
|
||||||
}
|
|
||||||
redisUtils.del("menu::pid:" + (resources.getPid() == null ? 0 : resources.getPid()));
|
redisUtils.del("menu::pid:" + (resources.getPid() == null ? 0 : resources.getPid()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,8 +138,6 @@ public class MenuServiceImpl implements MenuService {
|
||||||
throw new BadRequestException("上级不能为自己");
|
throw new BadRequestException("上级不能为自己");
|
||||||
}
|
}
|
||||||
Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new);
|
Menu menu = menuRepository.findById(resources.getId()).orElseGet(Menu::new);
|
||||||
// 记录旧的父节点ID
|
|
||||||
Long pid = menu.getPid();
|
|
||||||
ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId());
|
ValidationUtil.isNull(menu.getId(),"Permission","id",resources.getId());
|
||||||
|
|
||||||
if(resources.getIFrame()){
|
if(resources.getIFrame()){
|
||||||
|
@ -158,6 +155,11 @@ public class MenuServiceImpl implements MenuService {
|
||||||
if(resources.getPid().equals(0L)){
|
if(resources.getPid().equals(0L)){
|
||||||
resources.setPid(null);
|
resources.setPid(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 记录的父节点ID
|
||||||
|
Long oldPid = menu.getPid();
|
||||||
|
Long newPid = resources.getPid();
|
||||||
|
|
||||||
if(StringUtils.isNotBlank(resources.getComponentName())){
|
if(StringUtils.isNotBlank(resources.getComponentName())){
|
||||||
menu1 = menuRepository.findByComponentName(resources.getComponentName());
|
menu1 = menuRepository.findByComponentName(resources.getComponentName());
|
||||||
if(menu1 != null && !menu1.getId().equals(menu.getId())){
|
if(menu1 != null && !menu1.getId().equals(menu.getId())){
|
||||||
|
@ -177,15 +179,11 @@ public class MenuServiceImpl implements MenuService {
|
||||||
menu.setPermission(resources.getPermission());
|
menu.setPermission(resources.getPermission());
|
||||||
menu.setType(resources.getType());
|
menu.setType(resources.getType());
|
||||||
menuRepository.save(menu);
|
menuRepository.save(menu);
|
||||||
// 计算子节点数目
|
// 计算父级菜单节点数目
|
||||||
if(resources.getPid() == null){
|
updateSubCnt(oldPid);
|
||||||
updateSubCnt(pid);
|
updateSubCnt(newPid);
|
||||||
} else {
|
|
||||||
pid = resources.getPid();
|
|
||||||
updateSubCnt(resources.getPid());
|
|
||||||
}
|
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
delCaches(resources.getId(), pid);
|
delCaches(resources.getId(), oldPid, newPid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -206,12 +204,10 @@ public class MenuServiceImpl implements MenuService {
|
||||||
public void delete(Set<Menu> menuSet) {
|
public void delete(Set<Menu> menuSet) {
|
||||||
for (Menu menu : menuSet) {
|
for (Menu menu : menuSet) {
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
delCaches(menu.getId(), menu.getPid());
|
delCaches(menu.getId(), menu.getPid(), null);
|
||||||
roleService.untiedMenu(menu.getId());
|
roleService.untiedMenu(menu.getId());
|
||||||
menuRepository.deleteById(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){
|
private void updateSubCnt(Long menuId){
|
||||||
int count = menuRepository.countByPid(menuId);
|
if(menuId != null){
|
||||||
menuRepository.updateSubCntById(count, menuId);
|
int count = menuRepository.countByPid(menuId);
|
||||||
|
menuRepository.updateSubCntById(count, menuId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清理缓存
|
* 清理缓存
|
||||||
* @param id 菜单ID
|
* @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<User> users = userRepository.findByMenuId(id);
|
List<User> users = userRepository.findByMenuId(id);
|
||||||
redisUtils.del("menu::id:" +id);
|
redisUtils.del("menu::id:" +id);
|
||||||
redisUtils.delByKeys("menu::user:",users.stream().map(User::getId).collect(Collectors.toSet()));
|
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<Role> roles = roleService.findInMenuId(new ArrayList<Long>(){{
|
||||||
|
add(id);
|
||||||
|
add(newPid == null ? 0 : newPid);
|
||||||
|
}});
|
||||||
|
redisUtils.delByKeys("role::id:",roles.stream().map(Role::getId).collect(Collectors.toSet()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -210,4 +210,9 @@ public class RoleServiceImpl implements RoleService {
|
||||||
throw new BadRequestException("所选角色存在用户关联,请解除关联再试!");
|
throw new BadRequestException("所选角色存在用户关联,请解除关联再试!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Role> findInMenuId(List<Long> menuIds) {
|
||||||
|
return roleRepository.findInMenuId(menuIds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue