| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package com.example.service.system.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.example.entity.database.system.Role;
- import com.example.entity.database.system.SysRoleDept;
- import com.example.entity.database.system.SysUserRole;
- import com.example.mapper.system.RoleMapper;
- import com.example.mapper.system.SysRoleDeptMapper;
- import com.example.mapper.system.SysRoleMenuMapper;
- import com.example.mapper.system.SysUserRoleMapper;
- import com.example.service.BaseServiceImpl;
- import com.example.service.system.IRoleService;
- import com.example.util.helper.StringHelper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.StringUtils;
- import java.util.ArrayList;
- import java.util.List;
- @Transactional
- @Service
- public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements IRoleService{
- public RoleServiceImpl(){ super(false); }
- @Autowired
- private SysUserRoleMapper sysUserRoleMapper;
- @Autowired
- private SysRoleDeptMapper sysRoleDeptMapper;
- @Autowired
- private IRoleService roleService;
- @Autowired
- private SysRoleMenuMapper roleMenuMapper;
- @Autowired
- private SysRoleDeptMapper roleDeptMapper;
- @Autowired
- private SysUserRoleMapper userRoleMapper;
- /**
- * 修改数据权限信息
- * @param role 角色信息
- * @return 结果
- */
- @Override
- @Transactional
- public int authDataScope(Role role)
- {
- // 修改角色信息
- roleService.update(role);
- // 删除角色与部门关联
- sysRoleDeptMapper.deleteRoleDeptByRoleId(role.getRole_id());
- // 新增角色和部门信息(数据权限)
- return insertRoleDept(role);
- }
- /**
- * 新增角色部门信息(数据权限)
- * @param role 角色对象
- */
- public int insertRoleDept(Role role)
- {
- int rows = 1;
- // 新增角色与部门(数据权限)管理
- List<SysRoleDept> list = new ArrayList<SysRoleDept>();
- for (Long deptId : role.getDeptIds())
- {
- SysRoleDept rd = new SysRoleDept();
- rd.setRole_id(role.getRole_id());
- rd.setDept_id(deptId);
- list.add(rd);
- }
- if (list.size() > 0)
- {
- rows = sysRoleDeptMapper.batchRoleDept(list);
- }
- return rows;
- }
- /**
- * 取消授权用户角色
- *
- * @param userRole 用户和角色关联信息
- * @return 结果
- */
- @Override
- public int deleteAuthUser(SysUserRole userRole)
- {
- return sysUserRoleMapper.deleteUserRoleInfo(userRole);
- }
- /**
- * 批量取消授权用户角色
- *
- * @param roleId 角色ID
- * @param userIds 需要取消授权的用户数据ID
- * @return 结果
- */
- @Override
- public int deleteAuthUsers(Long roleId, Long[] userIds)
- {
- return sysUserRoleMapper.deleteUserRoleInfos(roleId, userIds);
- }
- /**
- * 批量选择授权用户角色
- *
- * @param roleId 角色ID
- * @param userIds 需要授权的用户数据ID
- * @return 结果
- */
- @Override
- public int insertAuthUsers(Long roleId, Long[] userIds)
- {
- // 新增用户与角色管理
- List<SysUserRole> list = new ArrayList<SysUserRole>();
- for (Long userId : userIds)
- {
- SysUserRole ur = new SysUserRole();
- ur.setUser_id(userId);
- ur.setRole_id(roleId);
- list.add(ur);
- }
- return sysUserRoleMapper.batchUserRole(list);
- }
- /**
- * 批量删除角色信息
- *
- * @param roleIds 需要删除的角色ID
- * @return 结果
- */
- @Override
- public void deleteRoleByIds(Long[] roleIds)
- {
- for (Long role_id : roleIds)
- {
- checkRoleAllowed(new Role(role_id));
- checkRoleDataScope(role_id);
- //Role role = selectRoleById(roleId);
- LambdaQueryWrapper<Role> qw = new LambdaQueryWrapper<>();
- qw.eq(Role::getRole_id,role_id);
- Role role = this.getEntity(qw);
- if (countUserRoleByRoleId(role_id) > 0)
- {
- throw new RuntimeException(String.format("%1$s已分配,不能删除", role.getRole_name()));
- }
- }
- // 删除角色与菜单关联
- roleMenuMapper.deleteRoleMenu(roleIds);
- // 删除角色与部门关联
- roleDeptMapper.deleteRoleDept(roleIds);
- }
- /**
- * 校验角色是否允许操作
- *
- * @param role 角色信息
- */
- @Override
- public void checkRoleAllowed(Role role)
- {
- if (!StringHelper.isNull(role.getRole_id()) && role.isAdmin())
- {
- // throw new ServiceException("不允许操作超级管理员角色");
- throw new RuntimeException("不允许操作超级管理员角色");
- }
- }
- /**
- * 校验角色是否有数据权限
- *
- * @param roleId 角色id
- */
- @Override
- public void checkRoleDataScope(Long roleId)
- {
- //if (!User.isAdmin(SecurityUtils.getUserId()))
- // {
- Role role = new Role();
- role.setRole_id(roleId);
- //List<Role> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
- //List<Role> roles = this.selectRoleList(role);
- List<Role> roles = this.getList();
- if (StringUtils.isEmpty(roles))
- {
- throw new RuntimeException("没有权限访问角色数据!");
- }
- //}
- }
- /**
- * 通过角色ID查询角色使用数量
- *
- * @param roleId 角色ID
- * @return 结果
- */
- @Override
- public int countUserRoleByRoleId(Long roleId)
- {
- return userRoleMapper.countUserRoleByRoleId(roleId);
- }
- }
|