| | |
| | | package com.xiaopeng.account.service.impl; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.xiaopeng.account.model.dto.XpOperationsUserDTO; |
| | | import com.xiaopeng.account.model.query.XpOperationsUserPageQuery; |
| | | import com.xiaopeng.account.model.query.XpOperationsUserQuery; |
| | | import com.xiaopeng.account.service.ISysRoleService; |
| | | import com.xiaopeng.account.service.IXpOperationsRoleService; |
| | | import com.xiaopeng.account.service.IXpOperationsUserService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | |
| | | import java.io.InputStream; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | private IXpQualificationAuditService xpQualificationAuditService; |
| | | @Autowired |
| | | private SysRoleMapper sysRoleMapper; |
| | | @Autowired |
| | | private ISysRoleService iSysRoleService; |
| | | |
| | | @Override |
| | | public List<XpOperationsUser> listByName(String nickName) { |
| | |
| | | |
| | | // 8. 查询角色信息 |
| | | List<XpOperationsUserWithRoleVO> userWithRoles = xpOperationsRoleMapper.selectUsersWithRoleByIds(Collections.singletonList(userId)); |
| | | if (!CollectionUtils.isEmpty(userWithRoles)) { |
| | | List<XpOperationsUserProfileVO.RoleInfoVO> roleVOs = userWithRoles.stream() |
| | | .filter(r -> r.getRoleId() != null) |
| | | .map(r -> { |
| | | XpOperationsUserProfileVO.RoleInfoVO roleVO = new XpOperationsUserProfileVO.RoleInfoVO(); |
| | | roleVO.setRoleId(r.getRoleId()); |
| | | roleVO.setRoleName(r.getRoleName()); |
| | | return roleVO; |
| | | }) |
| | | XpOperationsUserWithRoleVO operationsUserWithRoleVO = userWithRoles.get(0); |
| | | if (CollUtil.isNotEmpty(operationsUserWithRoleVO.getRoles())) { |
| | | List<XpOperationsUserProfileVO.RoleInfoVO> roleVOs = operationsUserWithRoleVO.getRoles().stream() |
| | | .map(item -> BeanUtil.copyProperties(item, XpOperationsUserProfileVO.RoleInfoVO.class)) |
| | | .collect(Collectors.toList()); |
| | | vo.setRoles(roleVOs); |
| | | } |
| | | |
| | | return vo; |
| | | } |
| | | |
| | |
| | | ExcelUtil<ExportOperationsUserVo> util = new ExcelUtil<ExportOperationsUserVo>(ExportOperationsUserVo.class); |
| | | util.exportExcel(response); |
| | | } |
| | | |
| | | @Override |
| | | public List<XpOperationsUserWithRoleVO> selectUsersWithRoleByIds(List<Long> ids) { |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | List<XpOperationsUser> operationsUsers = listByIds(ids); |
| | | if (CollUtil.isEmpty(operationsUsers)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | // 查询用户角色信息 |
| | | List<Long> userIds = operationsUsers.stream() |
| | | .map(XpOperationsUser::getId) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (CollUtil.isEmpty(userIds)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | // 获取关联角色ids |
| | | List<XpOperationsRole> operationsRoles = xpOperationsRoleMapper.selectList( |
| | | Wrappers.lambdaQuery(XpOperationsRole.class) |
| | | .in(XpOperationsRole::getOperationsUserId, userIds) |
| | | ); |
| | | |
| | | // userId -> 该用户关联的 roleId 列表 |
| | | Map<Long, List<Long>> userRoleIdsMap = CollUtil.isNotEmpty(operationsRoles) |
| | | ? operationsRoles.stream() |
| | | .filter(r -> r.getOperationsUserId() != null && r.getRoleId() != null) |
| | | .collect(Collectors.groupingBy( |
| | | XpOperationsRole::getOperationsUserId, |
| | | Collectors.mapping(XpOperationsRole::getRoleId, |
| | | Collectors.collectingAndThen(Collectors.toList(), |
| | | list -> list.stream().distinct().collect(Collectors.toList()))) |
| | | )) |
| | | : Collections.emptyMap(); |
| | | |
| | | // 一次性查出所有角色 |
| | | List<Long> roleIds = userRoleIdsMap.values().stream() |
| | | .flatMap(Collection::stream) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | |
| | | Map<Long, SysRole> roleMap = CollUtil.isNotEmpty(roleIds) |
| | | ? iSysRoleService.listByIds(roleIds).stream() |
| | | .collect(Collectors.toMap(SysRole::getId, Function.identity(), (a, b) -> a)) |
| | | : Collections.emptyMap(); |
| | | |
| | | // 组装 VO:每个用户对应角色列表 |
| | | return operationsUsers.stream().map(u -> { |
| | | Long uid = u.getId(); |
| | | |
| | | List<Long> roleIdList = userRoleIdsMap.getOrDefault(uid, Collections.emptyList()); |
| | | List<SysRole> roles = roleIdList.stream() |
| | | .map(roleMap::get) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 根据你的 VO 字段调整 |
| | | XpOperationsUserWithRoleVO vo = BeanUtil.copyProperties(u, XpOperationsUserWithRoleVO.class); |
| | | vo.setRoles(roles); |
| | | |
| | | return vo; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | } |