package com.aurora.service.impl; import com.aurora.constant.CommonConstant; import com.aurora.model.dto.RoleDTO; import com.aurora.model.dto.UserRoleDTO; import com.aurora.entity.Role; import com.aurora.entity.RoleMenu; import com.aurora.entity.RoleResource; import com.aurora.entity.UserRole; import com.aurora.exception.BizException; import com.aurora.handler.FilterInvocationSecurityMetadataSourceImpl; import com.aurora.mapper.RoleMapper; import com.aurora.mapper.UserRoleMapper; import com.aurora.service.RoleMenuService; import com.aurora.service.RoleResourceService; import com.aurora.service.RoleService; import com.aurora.util.BeanCopyUtil; import com.aurora.util.PageUtil; import com.aurora.model.vo.ConditionVO; import com.aurora.model.dto.PageResultDTO; import com.aurora.model.vo.RoleVO; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.SneakyThrows; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @Service public class RoleServiceImpl extends ServiceImpl implements RoleService { @Autowired private RoleMapper roleMapper; @Autowired private UserRoleMapper userRoleMapper; @Autowired private RoleResourceService roleResourceService; @Autowired private RoleMenuService roleMenuService; @Autowired private FilterInvocationSecurityMetadataSourceImpl filterInvocationSecurityMetadataSource; @Override public List listUserRoles() { List roleList = roleMapper.selectList(new LambdaQueryWrapper() .select(Role::getId, Role::getRoleName)); return BeanCopyUtil.copyList(roleList, UserRoleDTO.class); } @SneakyThrows @Override public PageResultDTO listRoles(ConditionVO conditionVO) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Role::getRoleName, conditionVO.getKeywords()); CompletableFuture asyncCount = CompletableFuture.supplyAsync(() -> roleMapper.selectCount(queryWrapper)); List roleDTOs = roleMapper.listRoles(PageUtil.getLimitCurrent(), PageUtil.getSize(), conditionVO); return new PageResultDTO<>(roleDTOs, asyncCount.get()); } @Transactional(rollbackFor = Exception.class) @Override public void saveOrUpdateRole(RoleVO roleVO) { Role roleCheck = roleMapper.selectOne(new LambdaQueryWrapper() .select(Role::getId) .eq(Role::getRoleName, roleVO.getRoleName())); if (Objects.nonNull(roleCheck) && !(roleCheck.getId().equals(roleVO.getId()))) { throw new BizException("该角色存在"); } Role role = Role.builder() .id(roleVO.getId()) .roleName(roleVO.getRoleName()) .isDisable(CommonConstant.FALSE) .build(); this.saveOrUpdate(role); if (Objects.nonNull(roleVO.getResourceIds())) { if (Objects.nonNull(roleVO.getId())) { roleResourceService.remove(new LambdaQueryWrapper() .eq(RoleResource::getRoleId, roleVO.getId())); } List roleResourceList = roleVO.getResourceIds().stream() .map(resourceId -> RoleResource.builder() .roleId(role.getId()) .resourceId(resourceId) .build()) .collect(Collectors.toList()); roleResourceService.saveBatch(roleResourceList); filterInvocationSecurityMetadataSource.clearDataSource(); } if (Objects.nonNull(roleVO.getMenuIds())) { if (Objects.nonNull(roleVO.getId())) { roleMenuService.remove(new LambdaQueryWrapper().eq(RoleMenu::getRoleId, roleVO.getId())); } List roleMenuList = roleVO.getMenuIds().stream() .map(menuId -> RoleMenu.builder() .roleId(role.getId()) .menuId(menuId) .build()) .collect(Collectors.toList()); roleMenuService.saveBatch(roleMenuList); } } @Override public void deleteRoles(List roleIdList) { Integer count = userRoleMapper.selectCount(new LambdaQueryWrapper() .in(UserRole::getRoleId, roleIdList)); if (count > 0) { throw new BizException("该角色下存在用户"); } roleMapper.deleteBatchIds(roleIdList); } }