SysRoleMenuRelationService.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package cc.uncarbon.module.sys.service;
  2. import cc.uncarbon.framework.crud.service.impl.HelioBaseServiceImpl;
  3. import cc.uncarbon.module.sys.entity.SysRoleMenuRelationEntity;
  4. import cc.uncarbon.module.sys.mapper.SysRoleMenuRelationMapper;
  5. import cn.hutool.core.collection.CollUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import javax.annotation.Resource;
  11. import java.util.List;
  12. import java.util.stream.Collectors;
  13. /**
  14. * 后台角色-可见菜单关联
  15. *
  16. * @author Uncarbon
  17. */
  18. @Slf4j
  19. @Service
  20. public class SysRoleMenuRelationService extends HelioBaseServiceImpl<SysRoleMenuRelationMapper, SysRoleMenuRelationEntity> {
  21. @Resource
  22. private SysMenuService sysMenuService;
  23. /**
  24. * 根据角色Ids取菜单Ids
  25. *
  26. * @param roleIds 角色Ids
  27. * @return 菜单Ids
  28. */
  29. public List<Long> listMenuIdByRoleIds(List<Long> roleIds) throws IllegalArgumentException {
  30. if (CollUtil.isEmpty(roleIds)) {
  31. throw new IllegalArgumentException("roleIds不能为空");
  32. }
  33. List<Long> menuIds = this.list(
  34. new QueryWrapper<SysRoleMenuRelationEntity>()
  35. .select(" DISTINCT menu_id ")
  36. .lambda()
  37. .in(SysRoleMenuRelationEntity::getRoleId, roleIds)
  38. ).stream().map(SysRoleMenuRelationEntity::getMenuId).collect(Collectors.toList());
  39. return sysMenuService.filterDisabledIds(menuIds);
  40. }
  41. /**
  42. * 先清理角色ID所有关联关系, 再绑定角色ID与菜单ID
  43. */
  44. @Transactional(rollbackFor = Exception.class)
  45. public void cleanAndBind(Long roleId, List<Long> menuIds) {
  46. this.remove(
  47. new QueryWrapper<SysRoleMenuRelationEntity>()
  48. .lambda()
  49. .eq(SysRoleMenuRelationEntity::getRoleId, roleId)
  50. );
  51. if (CollUtil.isNotEmpty(menuIds)) {
  52. // 需要绑定菜单
  53. menuIds.forEach(
  54. menuId -> this.save(
  55. SysRoleMenuRelationEntity.builder()
  56. .roleId(roleId)
  57. .menuId(menuId)
  58. .build()
  59. )
  60. );
  61. }
  62. }
  63. }