| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | 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.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.PageUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.core.constant.UserConstants; |
| | | import com.ruoyi.common.mybatis.core.page.PageQuery; |
| | | import com.ruoyi.system.domain.SysRole; |
| | | import com.ruoyi.common.mybatis.core.page.TableDataInfo; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.satoken.utils.LoginHelper; |
| | | import com.ruoyi.system.domain.SysRoleDept; |
| | | import com.ruoyi.system.domain.SysRoleMenu; |
| | | import com.ruoyi.system.domain.SysUserRole; |
| | |
| | | import com.ruoyi.system.mapper.SysRoleMenuMapper; |
| | | import com.ruoyi.system.mapper.SysUserRoleMapper; |
| | | import com.ruoyi.system.service.ISysRoleService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @RequiredArgsConstructor |
| | | @Service |
| | | public class SysRoleServiceImpl extends ServicePlusImpl<SysRoleMapper, SysRole, SysRole> implements ISysRoleService { |
| | | public class SysRoleServiceImpl implements ISysRoleService { |
| | | |
| | | @Autowired |
| | | private SysRoleMenuMapper roleMenuMapper; |
| | | |
| | | @Autowired |
| | | private SysUserRoleMapper userRoleMapper; |
| | | |
| | | @Autowired |
| | | private SysRoleDeptMapper roleDeptMapper; |
| | | private final SysRoleMapper baseMapper; |
| | | private final SysRoleMenuMapper roleMenuMapper; |
| | | private final SysUserRoleMapper userRoleMapper; |
| | | private final SysRoleDeptMapper roleDeptMapper; |
| | | |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public TableDataInfo<SysRole> selectPageRoleList(SysRole role) { |
| | | return PageUtils.buildDataInfo(baseMapper.selectPageRoleList(PageUtils.buildPage(), role)); |
| | | public TableDataInfo<SysRole> selectPageRoleList(SysRole role, PageQuery pageQuery) { |
| | | Page<SysRole> page = baseMapper.selectPageRoleList(pageQuery.build(), this.buildQueryWrapper(role)); |
| | | return TableDataInfo.build(page); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 角色数据集合信息 |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public List<SysRole> selectRoleList(SysRole role) { |
| | | return baseMapper.selectRoleList(role); |
| | | return baseMapper.selectRoleList(this.buildQueryWrapper(role)); |
| | | } |
| | | |
| | | private Wrapper<SysRole> buildQueryWrapper(SysRole role) { |
| | | Map<String, Object> params = role.getParams(); |
| | | QueryWrapper<SysRole> wrapper = Wrappers.query(); |
| | | wrapper.eq("r.del_flag", UserConstants.ROLE_NORMAL) |
| | | .eq(ObjectUtil.isNotNull(role.getRoleId()), "r.role_id", role.getRoleId()) |
| | | .like(StringUtils.isNotBlank(role.getRoleName()), "r.role_name", role.getRoleName()) |
| | | .eq(StringUtils.isNotBlank(role.getStatus()), "r.status", role.getStatus()) |
| | | .like(StringUtils.isNotBlank(role.getRoleKey()), "r.role_key", role.getRoleKey()) |
| | | .between(params.get("beginTime") != null && params.get("endTime") != null, |
| | | "r.create_time", params.get("beginTime"), params.get("endTime")) |
| | | .orderByAsc("r.role_sort"); |
| | | return wrapper; |
| | | } |
| | | |
| | | /** |
| | |
| | | List<SysRole> perms = baseMapper.selectRolePermissionByUserId(userId); |
| | | Set<String> permsSet = new HashSet<>(); |
| | | for (SysRole perm : perms) { |
| | | if (StringUtils.isNotNull(perm)) { |
| | | if (ObjectUtil.isNotNull(perm)) { |
| | | permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(","))); |
| | | } |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public List<SysRole> selectRoleAll() { |
| | | return SpringUtils.getAopProxy(this).selectRoleList(new SysRole()); |
| | | return this.selectRoleList(new SysRole()); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public SysRole selectRoleById(Long roleId) { |
| | | return getById(roleId); |
| | | return baseMapper.selectById(roleId); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public String checkRoleNameUnique(SysRole role) { |
| | | Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId(); |
| | | long count = count(new LambdaQueryWrapper<SysRole>() |
| | | .eq(SysRole::getRoleName, role.getRoleName()) |
| | | .ne(SysRole::getRoleId, roleId)); |
| | | if (count > 0) { |
| | | boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysRole>() |
| | | .eq(SysRole::getRoleName, role.getRoleName()) |
| | | .ne(ObjectUtil.isNotNull(role.getRoleId()), SysRole::getRoleId, role.getRoleId())); |
| | | if (exist) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | */ |
| | | @Override |
| | | public String checkRoleKeyUnique(SysRole role) { |
| | | Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId(); |
| | | long count = count(new LambdaQueryWrapper<SysRole>() |
| | | .eq(SysRole::getRoleKey, role.getRoleKey()) |
| | | .ne(SysRole::getRoleId, roleId)); |
| | | if (count > 0) { |
| | | boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysRole>() |
| | | .eq(SysRole::getRoleKey, role.getRoleKey()) |
| | | .ne(ObjectUtil.isNotNull(role.getRoleId()), SysRole::getRoleId, role.getRoleId())); |
| | | if (exist) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | */ |
| | | @Override |
| | | public void checkRoleAllowed(SysRole role) { |
| | | if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) { |
| | | if (ObjectUtil.isNotNull(role.getRoleId()) && role.isAdmin()) { |
| | | throw new ServiceException("不允许操作超级管理员角色"); |
| | | } |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public void checkRoleDataScope(Long roleId) { |
| | | if (!SysUser.isAdmin(SecurityUtils.getUserId())) { |
| | | if (!LoginHelper.isAdmin()) { |
| | | SysRole role = new SysRole(); |
| | | role.setRoleId(roleId); |
| | | List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role); |
| | | if (StringUtils.isEmpty(roles)) { |
| | | List<SysRole> roles = this.selectRoleList(role); |
| | | if (CollUtil.isEmpty(roles)) { |
| | | throw new ServiceException("没有权限访问角色数据!"); |
| | | } |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int insertRole(SysRole role) { |
| | | // 新增角色信息 |
| | | baseMapper.insert(role); |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int updateRole(SysRole role) { |
| | | // 修改角色信息 |
| | | baseMapper.updateById(role); |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int authDataScope(SysRole role) { |
| | | // 修改角色信息 |
| | | baseMapper.updateById(role); |
| | |
| | | list.add(rm); |
| | | } |
| | | if (list.size() > 0) { |
| | | rows = roleMenuMapper.insertAll(list); |
| | | rows = roleMenuMapper.insertBatch(list) ? list.size() : 0; |
| | | } |
| | | return rows; |
| | | } |
| | |
| | | list.add(rd); |
| | | } |
| | | if (list.size() > 0) { |
| | | rows = roleDeptMapper.insertAll(list); |
| | | rows = roleDeptMapper.insertBatch(list) ? list.size() : 0; |
| | | } |
| | | return rows; |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deleteRoleById(Long roleId) { |
| | | // 删除角色与菜单关联 |
| | | roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId)); |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deleteRoleByIds(Long[] roleIds) { |
| | | for (Long roleId : roleIds) { |
| | | checkRoleAllowed(new SysRole(roleId)); |
| | | checkRoleDataScope(roleId); |
| | | SysRole role = selectRoleById(roleId); |
| | | if (countUserRoleByRoleId(roleId) > 0) { |
| | | throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName())); |
| | |
| | | @Override |
| | | public int deleteAuthUser(SysUserRole userRole) { |
| | | return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() |
| | | .eq(SysUserRole::getRoleId, userRole.getRoleId()) |
| | | .eq(SysUserRole::getUserId, userRole.getUserId())); |
| | | .eq(SysUserRole::getRoleId, userRole.getRoleId()) |
| | | .eq(SysUserRole::getUserId, userRole.getUserId())); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public int deleteAuthUsers(Long roleId, Long[] userIds) { |
| | | return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() |
| | | .eq(SysUserRole::getRoleId, roleId) |
| | | .in(SysUserRole::getUserId, Arrays.asList(userIds))); |
| | | .eq(SysUserRole::getRoleId, roleId) |
| | | .in(SysUserRole::getUserId, Arrays.asList(userIds))); |
| | | } |
| | | |
| | | /** |
| | | * 批量选择授权用户角色 |
| | | * |
| | | * @param roleId 角色ID |
| | | * @param userIds 需要删除的用户数据ID |
| | | * @param userIds 需要授权的用户数据ID |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | |
| | | list.add(ur); |
| | | } |
| | | if (list.size() > 0) { |
| | | rows = userRoleMapper.insertAll(list); |
| | | rows = userRoleMapper.insertBatch(list) ? list.size() : 0; |
| | | } |
| | | return rows; |
| | | } |