You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
6.1 KiB

package com.nov.KgLowDurable.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nov.KgLowDurable.constant.RoleConstant;
import com.nov.KgLowDurable.exception.ServiceException;
import com.nov.KgLowDurable.mapper.RoleMapper;
import com.nov.KgLowDurable.pojo.entity.Role;
import com.nov.KgLowDurable.pojo.entity.RoleMenu;
import com.nov.KgLowDurable.pojo.vo.RoleVO;
import com.nov.KgLowDurable.service.IRoleMenuService;
import com.nov.KgLowDurable.service.IRoleService;
import com.nov.KgLowDurable.util.Func;
import com.nov.KgLowDurable.wrapper.RoleWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isNotEmpty;
/**
* 服务实现类
*
* @author Chill
*/
//@Master
@Service
@Validated
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
private final IRoleMenuService roleMenuService;
//private final IRoleScopeService roleScopeService;
@Override
public IPage<RoleVO> selectRolePage(IPage<RoleVO> page, RoleVO role) {
return page.setRecords(baseMapper.selectRolePage(page, role));
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean grant( List<Long> roleIds, List<Long> menuIds, List<Long> dataScopeIds, List<Long> apiScopeIds) {
return grantRoleMenu(roleIds, menuIds); //&& grantDataScope(roleIds, dataScopeIds) && grantApiScope(roleIds, apiScopeIds);
}
private boolean grantRoleMenu(List<Long> roleIds, List<Long> menuIds) {
// 防止越权配置超管角色
// Integer administratorCount = baseMapper.selectCount(Wrappers.<Role>query().lambda().eq(Role::getCode, RoleConstant.ADMINISTRATOR).in(Role::getId, roleIds));
// if (administratorCount > 0L) {
// throw new ServiceException("无权配置超管角色!");
// }
// 防止越权配置管理员角色
// Integer adminCount = baseMapper.selectCount(Wrappers.<Role>query().lambda().eq(Role::getCode, RoleConstant.ADMIN).in(Role::getId, roleIds));
// if ( adminCount > 0L) {
// throw new ServiceException("无权配置管理员角色!");
// }
// 删除角色配置的菜单集合
roleMenuService.remove(Wrappers.<RoleMenu>update().lambda().in(RoleMenu::getRoleId, roleIds));
// 组装配置
List<RoleMenu> roleMenus = new ArrayList<>();
roleIds.forEach(roleId -> menuIds.forEach(menuId -> {
RoleMenu roleMenu = new RoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(menuId);
roleMenus.add(roleMenu);
}));
// 新增配置
roleMenuService.saveBatch(roleMenus);
// 递归设置下属角色菜单集合
//recursionRoleMenu(roleIds, menuIds);
return true;
}
private void recursionRoleMenu(List<Long> roleIds, List<Long> menuIds) {
roleIds.forEach(roleId -> baseMapper.selectList(Wrappers.<Role>query().lambda()).forEach(role -> {
List<RoleMenu> roleMenuList = roleMenuService.list(Wrappers.<RoleMenu>query().lambda().eq(RoleMenu::getRoleId, role.getId()));
// 子节点过滤出父节点删除的菜单集合
List<Long> collectRoleMenuIds = roleMenuList.stream().map(RoleMenu::getMenuId).filter(menuId -> !menuIds.contains(menuId)).collect(Collectors.toList());
if (collectRoleMenuIds.size() > 0) {
// 删除子节点权限外的菜单集合
roleMenuService.remove(Wrappers.<RoleMenu>update().lambda().eq(RoleMenu::getRoleId, role.getId()).in(RoleMenu::getMenuId, collectRoleMenuIds));
// 递归设置下属角色菜单集合
recursionRoleMenu(Collections.singletonList(role.getId()), menuIds);
}
}));
}
// private boolean grantDataScope(List<Long> roleIds, List<Long> dataScopeIds) {
// // 删除角色配置的数据权限集合
// roleScopeService.remove(Wrappers.<RoleScope>update().lambda().eq(RoleScope::getScopeCategory, DATA_SCOPE_CATEGORY).in(RoleScope::getRoleId, roleIds));
// // 组装配置
// List<RoleScope> roleDataScopes = new ArrayList<>();
// roleIds.forEach(roleId -> dataScopeIds.forEach(scopeId -> {
// RoleScope roleScope = new RoleScope();
// roleScope.setScopeCategory(DATA_SCOPE_CATEGORY);
// roleScope.setRoleId(roleId);
// roleScope.setScopeId(scopeId);
// roleDataScopes.add(roleScope);
// }));
// // 新增配置
// roleScopeService.saveBatch(roleDataScopes);
// return true;
// }
// private boolean grantApiScope(List<Long> roleIds, List<Long> apiScopeIds) {
// // 删除角色配置的接口权限集合
// roleScopeService.remove(Wrappers.<RoleScope>update().lambda().eq(RoleScope::getScopeCategory, API_SCOPE_CATEGORY).in(RoleScope::getRoleId, roleIds));
// // 组装配置
// List<RoleScope> roleApiScopes = new ArrayList<>();
// roleIds.forEach(roleId -> apiScopeIds.forEach(scopeId -> {
// RoleScope roleScope = new RoleScope();
// roleScope.setScopeCategory(API_SCOPE_CATEGORY);
// roleScope.setScopeId(scopeId);
// roleScope.setRoleId(roleId);
// roleApiScopes.add(roleScope);
// }));
// // 新增配置
// roleScopeService.saveBatch(roleApiScopes);
// return true;
// }
@Override
public List<String> getRoleNames(String roleIds) {
return baseMapper.getRoleNames(Func.toLongArray(roleIds));
}
@Override
public boolean submit(Role role) {
return saveOrUpdate(role);
}
@Override
public List<RoleVO> search(String roleName, Long parentId) {
LambdaQueryWrapper<Role> queryWrapper = Wrappers.<Role>query().lambda();
if (isNotEmpty(roleName)) {
queryWrapper.like(Role::getName, roleName);
}
List<Role> roleList = baseMapper.selectList(queryWrapper);
return RoleWrapper.build().listNodeVO(roleList);
}
@Override
public boolean removeRole(String ids) {
Integer cnt = baseMapper.selectCount(Wrappers.<Role>query().lambda());
if (cnt > 0L) {
throw new ServiceException("请先删除子节点!");
}
return removeByIds(Func.toLongList(ids));
}
}