测回部门和角色权限逻辑

This commit is contained in:
2025-07-10 18:01:31 +08:00
parent e623855b21
commit 888af08666
5 changed files with 164 additions and 114 deletions

View File

@ -68,6 +68,18 @@ public class SysDeptController extends BaseController {
return R.ok(deptService.selectDeptById(deptId)); return R.ok(deptService.selectDeptById(deptId));
} }
// /**
// * 根据部门编号获取详细信息
// *
// */
// @SaCheckPermission("system:dept:informationRetrieval")
// @GetMapping(value = "informationRetrieval")
// public R<SysDeptBo> getInfo(SysDeptBo dept) {
// deptService.checkDeptDataScope(dept.getDeptId());
// return R.ok(deptService.selectDeptByIdBo(dept));
// }
/** /**
* 新增部门 * 新增部门
*/ */

View File

@ -90,4 +90,9 @@ public class SysDept extends TenantEntity {
*/ */
private String isSubset; private String isSubset;
/**
* 角色ID
*/
private Long roleId;
} }

View File

@ -84,70 +84,60 @@ public class SysDeptBo extends BaseEntity {
private String isSubset; private String isSubset;
/** // /**
* ====================== // * ======================
* 如果子集为0,则需要新增角色 // * 如果子集为0,则需要新增角色
* ====================== // * ======================
*/ // */
//
/** // /**
* 角色ID // * 角色ID
*/ // */
private Long roleId; // private Long roleId;
//
/** // /**
* 角色权限字符串 // * 角色权限字符串
*/ // */
private String roleKey; // private String roleKey;
//
/** // /**
* 显示顺序 // * 显示顺序
*/ // */
private Integer roleSort; // private Integer roleSort;
//
/** // /**
* 数据范围1全部数据权限 2自定数据权限 3本部门数据权限 4本部门及以下数据权限 // * 数据范围1全部数据权限 2自定数据权限 3本部门数据权限 4本部门及以下数据权限
*/ // */
private String dataScope; // private String dataScope;
//
/** // /**
* 菜单树选择项是否关联显示 // * 菜单树选择项是否关联显示
*/ // */
private Boolean menuCheckStrictly; // private Boolean menuCheckStrictly;
//
/** // /**
* 部门树选择项是否关联显示 // * 部门树选择项是否关联显示
*/ // */
private Boolean deptCheckStrictly; // private Boolean deptCheckStrictly;
//
/** // /**
* 角色状态0正常 1停用 // * 角色状态0正常 1停用
*/ // */
private String roleStatus; // private String roleStatus;
//
/** // /**
* 备注 // * 备注
*/ // */
private String remark; // private String remark;
//
/** // /**
* 菜单组 // * 菜单组
*/ // */
private Long[] menuIds; // private Long[] menuIds;
//
/** // /**
* 部门组(数据权限) // * 部门组(数据权限)
*/ // */
private Long[] deptIds; // private Long[] deptIds;
// public SysDeptBo(Long roleId) {
// this.roleId = roleId;
// }
// public boolean isSuperAdmin() {
// return SystemConstants.SUPER_ADMIN_ID.equals(this.roleId);
// }
} }

View File

@ -52,6 +52,14 @@ public interface ISysDeptService {
*/ */
SysDeptVo selectDeptById(Long deptId); SysDeptVo selectDeptById(Long deptId);
// /**
// * 根据部门ID查询信息
// *
// * @return 部门信息
// */
// SysDeptBo selectDeptByIdBo(SysDeptBo dept);
/** /**
* 通过部门名称查询部门ID * 通过部门名称查询部门ID
* *

View File

@ -12,6 +12,7 @@ import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.CacheNames; import org.dromara.common.core.constant.CacheNames;
import org.dromara.common.core.constant.HttpStatus; import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.constant.SystemConstants; import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.dto.DeptDTO; import org.dromara.common.core.domain.dto.DeptDTO;
import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.service.DeptService; import org.dromara.common.core.service.DeptService;
@ -27,12 +28,14 @@ import org.dromara.system.domain.bo.SysRoleBo;
import org.dromara.system.domain.enums.SysDeptIsSubsetEnum; import org.dromara.system.domain.enums.SysDeptIsSubsetEnum;
import org.dromara.system.domain.enums.SysRoleIsSpecialEnum; import org.dromara.system.domain.enums.SysRoleIsSpecialEnum;
import org.dromara.system.domain.vo.SysDeptVo; import org.dromara.system.domain.vo.SysDeptVo;
import org.dromara.system.domain.vo.SysRoleVo;
import org.dromara.system.mapper.SysDeptMapper; import org.dromara.system.mapper.SysDeptMapper;
import org.dromara.system.mapper.SysRoleMapper; import org.dromara.system.mapper.SysRoleMapper;
import org.dromara.system.mapper.SysUserMapper; import org.dromara.system.mapper.SysUserMapper;
import org.dromara.system.service.ISysDeptService; import org.dromara.system.service.ISysDeptService;
import org.dromara.system.service.ISysRoleService; import org.dromara.system.service.ISysRoleService;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching; import org.springframework.cache.annotation.Caching;
@ -164,6 +167,32 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
return dept; return dept;
} }
// /**
// * 根据部门ID查询信息
// *
// * @return 部门信息
// */
// @Cacheable(cacheNames = CacheNames.SYS_DEPT, key = "#deptId")
// @Override
// public SysDeptBo selectDeptByIdBo(SysDeptBo bo) {
// SysDeptBo sysDeptBo = new SysDeptBo();
// SysDeptVo dept = baseMapper.selectVoById(bo.getDeptId());
// if (ObjectUtil.isNull(dept)) {
// return null;
// }
// SysDeptVo parentDept = baseMapper.selectVoOne(new LambdaQueryWrapper<SysDept>()
// .select(SysDept::getDeptName).eq(SysDept::getDeptId, dept.getParentId()));
// dept.setParentName(ObjectUtils.notNullGetter(parentDept, SysDeptVo::getDeptName));
//
// //cory 获取角色信息
// roleService.checkRoleDataScope(bo.getRoleId());
// SysRoleVo sysRoleVo = roleService.selectRoleById(bo.getRoleId());
// BeanUtils.copyProperties(dept,sysDeptBo);
// BeanUtils.copyProperties(sysRoleVo,sysDeptBo);
// sysDeptBo.setRoleSort(sysRoleVo.getRoleSort());
// return sysDeptBo;
// }
/** /**
* 通过部门名称查询部门ID * 通过部门名称查询部门ID
* *
@ -342,42 +371,47 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
if (!SystemConstants.NORMAL.equals(info.getStatus())) { if (!SystemConstants.NORMAL.equals(info.getStatus())) {
throw new ServiceException("部门停用,不允许新增"); throw new ServiceException("部门停用,不允许新增");
} }
if (!"0".equals(info.getIsSubset())){
throw new ServiceException("当前部门不允许有子部门");
}
SysDept dept = MapstructUtils.convert(bo, SysDept.class); SysDept dept = MapstructUtils.convert(bo, SysDept.class);
dept.setAncestors(info.getAncestors() + StringUtils.SEPARATOR + dept.getParentId()); dept.setAncestors(info.getAncestors() + StringUtils.SEPARATOR + dept.getParentId());
int insert = baseMapper.insert(dept); return baseMapper.insert(dept);
if (bo.getIsSubset().equals("1")) { // // cory 判断是否需要直接新增角色
return insert; // SysRoleBo role = getSysRoleBo(bo);
} // roleService.checkRoleAllowed(role);
// cory 判断是否需要直接新增角色 // if (!roleService.checkRoleNameUnique(role)) {
SysRoleBo role = getSysRoleBo(bo); // throw new ServiceException("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
roleService.checkRoleAllowed(role); // } else if (!roleService.checkRoleKeyUnique(role)) {
if (!roleService.checkRoleNameUnique(role)) { // throw new ServiceException("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
throw new ServiceException("新增角色'" + role.getRoleName() + "'失败,角色名称已存在"); // }
} else if (!roleService.checkRoleKeyUnique(role)) { // roleService.insertRole(role);
throw new ServiceException("新增角色'" + role.getRoleName() + "'失败,角色权限已存在"); // SysDept sysDept = new SysDept();
} // sysDept.setDeptId(dept.getDeptId());
return roleService.insertRole(role); // sysDept.setRoleId(role.getRoleId());
// baseMapper.updateById(sysDept);
// return insert;
} }
@NotNull // @NotNull
private static SysRoleBo getSysRoleBo(SysDeptBo bo) { // private static SysRoleBo getSysRoleBo(SysDeptBo bo) {
SysRoleBo role = new SysRoleBo(); // SysRoleBo role = new SysRoleBo();
role.setRoleName(bo.getDeptName()); // role.setRoleName(bo.getDeptName());
role.setRoleKey(bo.getRoleKey()); // role.setRoleKey(bo.getRoleKey());
role.setRoleSort(bo.getRoleSort()); // role.setRoleSort(bo.getRoleSort());
role.setDataScope(bo.getDataScope()); // role.setDataScope(bo.getDataScope());
role.setMenuCheckStrictly(bo.getMenuCheckStrictly()); // role.setMenuCheckStrictly(bo.getMenuCheckStrictly());
role.setDeptCheckStrictly(bo.getDeptCheckStrictly()); // role.setDeptCheckStrictly(bo.getDeptCheckStrictly());
role.setStatus(bo.getRoleStatus()); // role.setStatus(bo.getRoleStatus());
role.setRemark(bo.getRemark()); // role.setRemark(bo.getRemark());
role.setMenuIds(bo.getMenuIds()); // role.setMenuIds(bo.getMenuIds());
role.setDeptIds(bo.getDeptIds()); // role.setDeptIds(bo.getDeptIds());
role.setIsSpecial("1"); //特殊角色(部门无子集标识特殊角色) // role.setIsSpecial("1"); //特殊角色(部门无子集标识特殊角色)
if (bo.getRoleId() != null) { // if (bo.getRoleId() != null) {
role.setRoleId(bo.getRoleId()); // role.setRoleId(bo.getRoleId());
} // }
return role; // return role;
} // }
/** /**
* 修改保存部门信息 * 修改保存部门信息
@ -416,23 +450,24 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
// 如果该部门是启用状态,则启用该部门的所有上级部门 // 如果该部门是启用状态,则启用该部门的所有上级部门
updateParentDeptStatusNormal(dept); updateParentDeptStatusNormal(dept);
} }
if (bo.getIsSubset().equals("1")) { return result;
return result; // if (bo.getIsSubset().equals("1")) {
} // return result;
// cory 角色修改 // }
SysRoleBo role = getSysRoleBo(bo); // // cory 角色修改
roleService.checkRoleAllowed(role); // SysRoleBo role = getSysRoleBo(bo);
roleService.checkRoleDataScope(role.getRoleId()); // roleService.checkRoleAllowed(role);
if (!roleService.checkRoleNameUnique(role)) { // roleService.checkRoleDataScope(role.getRoleId());
throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,角色名称已存在"); // if (!roleService.checkRoleNameUnique(role)) {
} else if (!roleService.checkRoleKeyUnique(role)) { // throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,角色权限已存在"); // } else if (!roleService.checkRoleKeyUnique(role)) {
} // throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
if (roleService.updateRole(role) > 0) { // }
roleService.cleanOnlineUserByRole(role.getRoleId()); // if (roleService.updateRole(role) > 0) {
return result; // roleService.cleanOnlineUserByRole(role.getRoleId());
} // return result;
throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,请联系管理员"); // }
// throw new ServiceException("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
} }
/** /**