222 lines
7.4 KiB
Java
222 lines
7.4 KiB
Java
package com.youlai.system.service.impl;
|
||
|
||
import cn.hutool.core.collection.CollectionUtil;
|
||
import cn.hutool.core.lang.Assert;
|
||
import cn.hutool.core.util.StrUtil;
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||
import com.youlai.system.common.constant.SystemConstants;
|
||
import com.youlai.system.common.model.Option;
|
||
import com.youlai.system.converter.RoleConverter;
|
||
import com.youlai.system.base.security.service.PermissionService;
|
||
import com.youlai.system.mapper.SysRoleMapper;
|
||
import com.youlai.system.model.entity.SysRole;
|
||
import com.youlai.system.model.entity.SysRoleMenu;
|
||
import com.youlai.system.model.form.RoleForm;
|
||
import com.youlai.system.model.query.RolePageQuery;
|
||
import com.youlai.system.model.vo.RolePageVO;
|
||
import com.youlai.system.service.SysRoleMenuService;
|
||
import com.youlai.system.service.SysRoleService;
|
||
import com.youlai.system.service.SysUserRoleService;
|
||
import com.youlai.system.common.util.SecurityUtils;
|
||
import lombok.RequiredArgsConstructor;
|
||
import org.springframework.cache.annotation.CacheEvict;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import java.util.*;
|
||
|
||
/**
|
||
* 角色业务实现类
|
||
*
|
||
* @author haoxr
|
||
* @since 2022/6/3
|
||
*/
|
||
@Service
|
||
@RequiredArgsConstructor
|
||
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
|
||
|
||
private final SysRoleMenuService roleMenuService;
|
||
private final SysUserRoleService userRoleService;
|
||
private final RoleConverter roleConverter;
|
||
private final PermissionService permissionService;
|
||
|
||
/**
|
||
* 角色分页列表
|
||
*
|
||
* @param queryParams 角色查询参数
|
||
* @return {@link Page<RolePageVO>} – 角色分页列表
|
||
*/
|
||
@Override
|
||
public Page<RolePageVO> getRolePage(RolePageQuery queryParams) {
|
||
// 查询参数
|
||
int pageNum = queryParams.getPageNum();
|
||
int pageSize = queryParams.getPageSize();
|
||
String keywords = queryParams.getKeywords();
|
||
|
||
// 查询数据
|
||
Page<SysRole> rolePage = this.page(new Page<>(pageNum, pageSize),
|
||
new LambdaQueryWrapper<SysRole>()
|
||
.and(StrUtil.isNotBlank(keywords),
|
||
wrapper ->
|
||
wrapper.like(StrUtil.isNotBlank(keywords), SysRole::getName, keywords)
|
||
.or()
|
||
.like(StrUtil.isNotBlank(keywords), SysRole::getCode, keywords)
|
||
)
|
||
.ne(!SecurityUtils.isRoot(), SysRole::getCode, SystemConstants.ROOT_ROLE_CODE) // 非超级管理员不显示超级管理员角色
|
||
);
|
||
|
||
// 实体转换
|
||
return roleConverter.entity2Page(rolePage);
|
||
}
|
||
|
||
/**
|
||
* 角色下拉列表
|
||
*
|
||
* @return {@link List<Option>} – 角色下拉列表
|
||
*/
|
||
@Override
|
||
public List<Option> listRoleOptions() {
|
||
// 查询数据
|
||
List<SysRole> roleList = this.list(new LambdaQueryWrapper<SysRole>()
|
||
.ne(!SecurityUtils.isRoot(), SysRole::getCode, SystemConstants.ROOT_ROLE_CODE)
|
||
.select(SysRole::getId, SysRole::getName)
|
||
.orderByAsc(SysRole::getSort)
|
||
);
|
||
|
||
// 实体转换
|
||
return roleConverter.entities2Options(roleList);
|
||
}
|
||
|
||
/**
|
||
* 保存角色
|
||
*
|
||
* @param roleForm 角色表单数据
|
||
* @return {@link Boolean}
|
||
*/
|
||
@Override
|
||
public boolean saveRole(RoleForm roleForm) {
|
||
|
||
Long roleId = roleForm.getId();
|
||
String roleCode = roleForm.getCode();
|
||
|
||
long count = this.count(new LambdaQueryWrapper<SysRole>()
|
||
.ne(roleId != null, SysRole::getId, roleId)
|
||
.and(wrapper ->
|
||
wrapper.eq(SysRole::getCode, roleCode).or().eq(SysRole::getName, roleCode)
|
||
));
|
||
Assert.isTrue(count == 0, "角色名称或角色编码重复,请检查!");
|
||
|
||
// 实体转换
|
||
SysRole role = roleConverter.form2Entity(roleForm);
|
||
|
||
return this.saveOrUpdate(role);
|
||
}
|
||
|
||
/**
|
||
* 获取角色表单数据
|
||
*
|
||
* @param roleId 角色ID
|
||
* @return {@link RoleForm} – 角色表单数据
|
||
*/
|
||
@Override
|
||
public RoleForm getRoleForm(Long roleId) {
|
||
SysRole entity = this.getById(roleId);
|
||
return roleConverter.entity2Form(entity);
|
||
}
|
||
|
||
/**
|
||
* 修改角色状态
|
||
*
|
||
* @param roleId 角色ID
|
||
* @param status 角色状态(1:启用;0:禁用)
|
||
* @return {@link Boolean}
|
||
*/
|
||
@Override
|
||
public boolean updateRoleStatus(Long roleId, Integer status) {
|
||
return this.update(new LambdaUpdateWrapper<SysRole>()
|
||
.eq(SysRole::getId, roleId)
|
||
.set(SysRole::getStatus, status));
|
||
}
|
||
|
||
/**
|
||
* 批量删除角色
|
||
*
|
||
* @param ids 角色ID,多个使用英文逗号(,)分割
|
||
* @return {@link Boolean}
|
||
*/
|
||
@Override
|
||
public boolean deleteRoles(String ids) {
|
||
Assert.isTrue(StrUtil.isNotBlank(ids), "删除的角色ID不能为空");
|
||
List<Long> roleIds = Arrays.stream(ids.split(","))
|
||
.map(Long::parseLong)
|
||
.toList();
|
||
|
||
for (Long roleId : roleIds) {
|
||
SysRole role = this.getById(roleId);
|
||
Assert.isTrue(role != null, "角色不存在");
|
||
|
||
// 判断角色是否被用户关联
|
||
boolean isRoleAssigned = userRoleService.isRoleAssignedToUser(roleId);
|
||
Assert.isTrue(!isRoleAssigned, "角色【{}】已分配用户,请先解除关联后删除", role.getName());
|
||
|
||
boolean deleteResult = this.removeById(roleId);
|
||
if(deleteResult) {
|
||
// 删除成功,刷新权限缓存
|
||
permissionService.refreshPermissionCache(role.getCode());
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 获取角色的菜单ID集合
|
||
*
|
||
* @param roleId 角色ID
|
||
* @return 菜单ID集合(包括按钮权限ID)
|
||
*/
|
||
@Override
|
||
public List<Long> getRoleMenuIds(Long roleId) {
|
||
return roleMenuService.listMenuIdsByRoleId(roleId);
|
||
}
|
||
|
||
/**
|
||
* 修改角色的资源权限
|
||
*
|
||
* @param roleId 角色ID
|
||
* @param menuIds 菜单ID集合
|
||
* @return {@link Boolean}
|
||
*/
|
||
@Override
|
||
@Transactional
|
||
@CacheEvict(cacheNames = "system", key = "'routes'")
|
||
public boolean assignMenusToRole(Long roleId, List<Long> menuIds) {
|
||
// 删除角色菜单
|
||
roleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
|
||
// 新增角色菜单
|
||
if (CollectionUtil.isNotEmpty(menuIds)) {
|
||
List<SysRoleMenu> roleMenus = menuIds
|
||
.stream()
|
||
.map(menuId -> new SysRoleMenu(roleId, menuId))
|
||
.toList();
|
||
roleMenuService.saveBatch(roleMenus);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 获取最大范围的数据权限
|
||
*
|
||
* @param roles 角色编码集合
|
||
* @return {@link Integer} – 数据权限范围
|
||
*/
|
||
@Override
|
||
public Integer getMaximumDataScope(Set<String> roles) {
|
||
Integer dataScope = this.baseMapper.getMaximumDataScope(roles);
|
||
return dataScope;
|
||
}
|
||
|
||
}
|