| | |
| | | package com.ruoyi.system.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.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.common.utils.PageUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.system.domain.SysRoleDept; |
| | | import com.ruoyi.system.domain.SysRoleMenu; |
| | |
| | | * @author ruoyi |
| | | */ |
| | | @Service |
| | | public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService { |
| | | public class SysRoleServiceImpl extends ServicePlusImpl<SysRoleMapper, SysRole, SysRole> implements ISysRoleService { |
| | | |
| | | @Autowired |
| | | private SysRoleMenuMapper roleMenuMapper; |
| | |
| | | |
| | | @Autowired |
| | | private SysRoleDeptMapper roleDeptMapper; |
| | | |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public TableDataInfo<SysRole> selectPageRoleList(SysRole role) { |
| | | return PageUtils.buildDataInfo(baseMapper.selectPageRoleList(PageUtils.buildPage(), role)); |
| | | } |
| | | |
| | | /** |
| | | * 根据条件分页查询角色数据 |
| | |
| | | @DataScope(deptAlias = "d") |
| | | public List<SysRole> selectRoleList(SysRole role) { |
| | | return baseMapper.selectRoleList(role); |
| | | } |
| | | |
| | | /** |
| | | * 根据用户ID查询角色 |
| | | * |
| | | * @param userId 用户ID |
| | | * @return 角色列表 |
| | | */ |
| | | @Override |
| | | public List<SysRole> selectRolesByUserId(Long userId) { |
| | | List<SysRole> userRoles = baseMapper.selectRolePermissionByUserId(userId); |
| | | List<SysRole> roles = selectRoleAll(); |
| | | for (SysRole role : roles) { |
| | | for (SysRole userRole : userRoles) { |
| | | if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) { |
| | | role.setFlag(true); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return roles; |
| | | } |
| | | |
| | | /** |
| | |
| | | list.add(rm); |
| | | } |
| | | if (list.size() > 0) { |
| | | for (SysRoleMenu sysRoleMenu : list) { |
| | | rows += roleMenuMapper.insert(sysRoleMenu); |
| | | } |
| | | rows = roleMenuMapper.insertAll(list); |
| | | } |
| | | return rows; |
| | | } |
| | |
| | | list.add(rd); |
| | | } |
| | | if (list.size() > 0) { |
| | | for (SysRoleDept sysRoleDept : list) { |
| | | rows += roleDeptMapper.insert(sysRoleDept); |
| | | } |
| | | rows = roleDeptMapper.insertAll(list); |
| | | } |
| | | return rows; |
| | | } |
| | |
| | | roleDeptMapper.delete(new LambdaQueryWrapper<SysRoleDept>().in(SysRoleDept::getRoleId, ids)); |
| | | return baseMapper.deleteBatchIds(ids); |
| | | } |
| | | |
| | | /** |
| | | * 取消授权用户角色 |
| | | * |
| | | * @param userRole 用户和角色关联信息 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteAuthUser(SysUserRole userRole) { |
| | | return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() |
| | | .eq(SysUserRole::getRoleId, userRole.getRoleId()) |
| | | .eq(SysUserRole::getUserId, userRole.getUserId())); |
| | | } |
| | | |
| | | /** |
| | | * 批量取消授权用户角色 |
| | | * |
| | | * @param roleId 角色ID |
| | | * @param userIds 需要取消授权的用户数据ID |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteAuthUsers(Long roleId, Long[] userIds) { |
| | | return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() |
| | | .eq(SysUserRole::getRoleId, roleId) |
| | | .in(SysUserRole::getUserId, Arrays.asList(userIds))); |
| | | } |
| | | |
| | | /** |
| | | * 批量选择授权用户角色 |
| | | * |
| | | * @param roleId 角色ID |
| | | * @param userIds 需要删除的用户数据ID |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int insertAuthUsers(Long roleId, Long[] userIds) { |
| | | // 新增用户与角色管理 |
| | | int rows = 1; |
| | | List<SysUserRole> list = new ArrayList<SysUserRole>(); |
| | | for (Long userId : userIds) { |
| | | SysUserRole ur = new SysUserRole(); |
| | | ur.setUserId(userId); |
| | | ur.setRoleId(roleId); |
| | | list.add(ur); |
| | | } |
| | | if (list.size() > 0) { |
| | | rows = userRoleMapper.insertAll(list); |
| | | } |
| | | return rows; |
| | | } |
| | | } |