com.zznote.basecommon.service.impl.TRoleServiceImpl Maven / Gradle / Ivy
The newest version!
package com.zznote.basecommon.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.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zznote.basecommon.common.constant.UserConstants;
import com.zznote.basecommon.common.exception.ServiceException;
import com.zznote.basecommon.common.page.PageInfo;
import com.zznote.basecommon.common.page.PageQuery;
import com.zznote.basecommon.common.util.LoginHelper;
import com.zznote.basecommon.common.util.Rm;
import com.zznote.basecommon.dao.TRoleDao;
import com.zznote.basecommon.dao.TRoleDeptDao;
import com.zznote.basecommon.dao.TRoleMenuDao;
import com.zznote.basecommon.dao.TUserRoleDao;
import com.zznote.basecommon.entity.system.TRole;
import com.zznote.basecommon.entity.system.TRoleDept;
import com.zznote.basecommon.entity.system.TRoleMenu;
import com.zznote.basecommon.entity.system.TUserRole;
import com.zznote.basecommon.service.TRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@RequiredArgsConstructor
@Service
public class TRoleServiceImpl extends ServiceImpl implements TRoleService {
private final TRoleDao baseMapper;
private final TRoleMenuDao roleMenuMapper;
private final TUserRoleDao userRoleMapper;
private final TRoleDeptDao roleDeptMapper;
@Override
public PageInfo selectPageRoleList(TRole role, PageQuery pageQuery) {
Page page = baseMapper.selectPageRoleList(pageQuery.build(), this.buildQueryWrapper(role));
return PageInfo.build(page);
}
/**
* 根据用户ID查询权限
*
* @param userId 用户ID
* @return 权限列表
*/
@Override
public Set selectRolePermissionByUserId(Long userId) {
List perms = baseMapper.selectRolePermissionByUserId(userId);
Set permsSet = new HashSet<>();
for (TRole perm : perms) {
if (ObjectUtil.isNotNull(perm)) {
permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
}
}
return permsSet;
}
/**
* 根据条件分页查询角色数据
*
* @param role 角色信息
* @return 角色数据集合信息
*/
@Override
public List selectRoleList(TRole role) {
return baseMapper.selectRoleList(this.buildQueryWrapper(role));
}
private Wrapper buildQueryWrapper(TRole role) {
Map params = role.getParams();
QueryWrapper 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;
}
@Override
public void checkRoleDataScope(Long roleId) {
if (!LoginHelper.isAdmin()) {
TRole role = new TRole();
role.setRoleId(roleId);
List roles = this.selectRoleList(role);
if (CollUtil.isEmpty(roles)) {
throw new ServiceException("没有权限访问角色数据!");
}
}
}
/**
* 通过角色ID查询角色
*
* @param roleId 角色ID
* @return 角色对象信息
*/
@Override
public TRole selectRoleById(Long roleId) {
return baseMapper.selectById(roleId);
}
@Override
public String checkRoleNameUnique(TRole role) {
Long count = lambdaQuery()
.eq(TRole::getRoleName, role.getRoleName())
.ne(ObjectUtil.isNotNull(role.getRoleId()), TRole::getRoleId, role.getRoleId())
.count();
boolean exist = (count != null && count > 0);
if (exist) {
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
}
@Override
public String checkRoleKeyUnique(TRole role) {
Long count = lambdaQuery()
.eq(TRole::getRoleKey, role.getRoleKey())
.ne(ObjectUtil.isNotNull(role.getRoleId()), TRole::getRoleId, role.getRoleId())
.count();
boolean exist = (count != null && count > 0);
if (exist) {
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
}
@Override
public Integer insertRole(TRole role) {
baseMapper.insert(role);
return insertRoleMenu(role);
}
/**
* 校验角色是否允许操作
*
* @param role 角色信息
*/
@Override
public void checkRoleAllowed(TRole role) {
if (ObjectUtil.isNotNull(role.getRoleId()) && role.isAdmin()) {
throw new ServiceException("不允许操作超级管理员角色");
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public int authDataScope(TRole role) {
// 修改角色信息
baseMapper.updateById(role);
// 删除角色与部门关联
roleDeptMapper.delete(new LambdaQueryWrapper().eq(TRoleDept::getRoleId, role.getRoleId()));
// 新增角色和部门信息(数据权限)
return insertRoleDept(role);
}
/**
* 新增角色部门信息(数据权限)
*
* @param role 角色对象
*/
public int insertRoleDept(TRole role) {
int rows = 1;
// 新增角色与部门(数据权限)管理
List list = new ArrayList();
for (Long deptId : role.getDeptIds()) {
TRoleDept rd = new TRoleDept();
rd.setRoleId(role.getRoleId());
rd.setDeptId(deptId);
list.add(rd);
}
if (list.size() > 0) {
rows = roleDeptMapper.insertBatch(list) ? list.size() : 0;
}
return rows;
}
/**
* 修改保存角色信息
*
* @param role 角色信息
* @return 结果
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int updateRole(TRole role) {
// 修改角色信息
baseMapper.updateById(role);
// 删除角色与菜单关联
roleMenuMapper.delete(new LambdaQueryWrapper().eq(TRoleMenu::getRoleId,role.getRoleId()));
return insertRoleMenu(role);
}
private Integer insertRoleMenu(TRole role) {
int rows = 1;
// 新增用户与角色管理
List list = new ArrayList<>();
for (Long menuId : role.getMenuIds()) {
TRoleMenu rm = new TRoleMenu();
rm.setRoleId(role.getRoleId());
rm.setMenuId(menuId);
list.add(rm);
}
if (list.size() > 0) {
rows = roleMenuMapper.insertBatch(list) ? list.size() : 0;
}
return rows;
}
/**
* 批量删除角色信息
*
* @param roleIds 需要删除的角色ID
* @return 结果
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Rm deleteRoleByIds(Long[] roleIds) {
for (Long roleId : roleIds) {
checkRoleAllowed(new TRole(roleId));
checkRoleDataScope(roleId);
TRole role = selectRoleById(roleId);
if (countUserRoleByRoleId(roleId) > 0) {
throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName()));
}
}
List ids = Arrays.asList(roleIds);
// 删除角色与菜单关联
roleMenuMapper.delete(new LambdaQueryWrapper().in(TRoleMenu::getRoleId, ids));
// 删除角色与部门关联
roleDeptMapper.delete(new LambdaQueryWrapper().in(TRoleDept::getRoleId, ids));
return removeByIds(ids) ? Rm.success("删除成功") : Rm.error("删除失败");
}
/**
* 通过角色ID查询角色使用数量
*
* @param roleId 角色ID
* @return 结果
*/
@Override
public Long countUserRoleByRoleId(Long roleId) {
return userRoleMapper.selectCount(new LambdaQueryWrapper().eq(TUserRole::getRoleId, roleId));
}
/**
* 根据用户ID查询角色
*
* @param userId 用户ID
* @return 角色列表
*/
@Override
public List selectRolesByUserId(Long userId) {
List userRoles = baseMapper.selectRolePermissionByUserId(userId);
List roles = selectRoleAll();
for (TRole role : roles) {
for (TRole userRole : userRoles) {
if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) {
role.setFlag(true);
break;
}
}
}
return roles;
}
/**
* 查询所有角色
*
* @return 角色列表
*/
@Override
public List selectRoleAll() {
return this.selectRoleList(new TRole());
}
/**
* 取消授权用户角色
*
* @param userRole 用户和角色关联信息
* @return 结果
*/
@Override
public int deleteAuthUser(TUserRole userRole) {
return userRoleMapper.delete(new LambdaQueryWrapper()
.eq(TUserRole::getRoleId, userRole.getRoleId())
.eq(TUserRole::getUserId, userRole.getUserId()));
}
/**
* 批量取消授权用户角色
*
* @param roleId 角色ID
* @param userIds 需要取消授权的用户数据ID
* @return 结果
*/
@Override
public int deleteAuthUsers(Long roleId, Long[] userIds) {
return userRoleMapper.delete(new LambdaQueryWrapper()
.eq(TUserRole::getRoleId, roleId)
.in(TUserRole::getUserId, Arrays.asList(userIds)));
}
/**
* 批量选择授权用户角色
*
* @param roleId 角色ID
* @param userIds 需要授权的用户数据ID
* @return 结果
*/
@Override
public int insertAuthUsers(Long roleId, Long[] userIds) {
// 新增用户与角色管理
int rows = 1;
List list = new ArrayList();
for (Long userId : userIds) {
TUserRole ur = new TUserRole();
ur.setUserId(userId);
ur.setRoleId(roleId);
list.add(ur);
}
if (list.size() > 0) {
rows = userRoleMapper.insertBatch(list) ? list.size() : 0;
}
return rows;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy