| | |
| | | package org.dromara.system.service.impl; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.constant.CacheNames; |
| | | import org.dromara.common.core.constant.UserConstants; |
| | | import org.dromara.common.core.domain.dto.UserDTO; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.service.UserService; |
| | | import org.dromara.common.core.utils.MapstructUtils; |
| | |
| | | import org.dromara.common.mybatis.core.page.TableDataInfo; |
| | | import org.dromara.common.mybatis.helper.DataBaseHelper; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.system.domain.SysDept; |
| | | import org.dromara.system.domain.SysUser; |
| | | import org.dromara.system.domain.SysUserPost; |
| | | import org.dromara.system.domain.SysUserRole; |
| | | import org.dromara.system.domain.*; |
| | | import org.dromara.system.domain.bo.SysUserBo; |
| | | import org.dromara.system.domain.vo.*; |
| | | import org.dromara.system.domain.vo.SysPostVo; |
| | | import org.dromara.system.domain.vo.SysRoleVo; |
| | | import org.dromara.system.domain.vo.SysUserExportVo; |
| | | import org.dromara.system.domain.vo.SysUserVo; |
| | | import org.dromara.system.mapper.*; |
| | | import org.dromara.system.service.ISysUserService; |
| | | import org.springframework.cache.annotation.CacheEvict; |
| | | import org.springframework.cache.annotation.Cacheable; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | ids.add(user.getDeptId()); |
| | | w.in("u.dept_id", ids); |
| | | }).orderByAsc("u.user_id"); |
| | | if (StringUtils.isNotBlank(user.getExcludeUserIds())) { |
| | | wrapper.notIn("u.user_id", StringUtils.splitTo(user.getExcludeUserIds(), Convert::toLong)); |
| | | } |
| | | return wrapper; |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @CacheEvict(cacheNames = CacheNames.SYS_NICKNAME, key = "#user.userId") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int updateUser(SysUserBo user) { |
| | | // 新增用户与角色管理 |
| | |
| | | * @param user 用户信息 |
| | | * @return 结果 |
| | | */ |
| | | @CacheEvict(cacheNames = CacheNames.SYS_NICKNAME, key = "#user.userId") |
| | | @Override |
| | | public int updateUserProfile(SysUserBo user) { |
| | | return baseMapper.update(null, |
| | |
| | | */ |
| | | private void insertUserRole(Long userId, Long[] roleIds, boolean clear) { |
| | | if (ArrayUtil.isNotEmpty(roleIds)) { |
| | | // 判断是否具有此角色的操作权限 |
| | | List<SysRoleVo> roles = roleMapper.selectRoleList(new LambdaQueryWrapper<>()); |
| | | if (CollUtil.isEmpty(roles)) { |
| | | throw new ServiceException("没有权限访问角色的数据"); |
| | | } |
| | | List<Long> roleList = StreamUtils.toList(roles, SysRoleVo::getRoleId); |
| | | List<Long> roleList = new ArrayList<>(List.of(roleIds)); |
| | | if (!LoginHelper.isSuperAdmin(userId)) { |
| | | roleList.remove(UserConstants.SUPER_ADMIN_ID); |
| | | } |
| | | List<Long> canDoRoleList = StreamUtils.filter(List.of(roleIds), roleList::contains); |
| | | if (CollUtil.isEmpty(canDoRoleList)) { |
| | | // 判断是否具有此角色的操作权限 |
| | | List<SysRoleVo> roles = roleMapper.selectRoleList( |
| | | new QueryWrapper<SysRole>().in("r.role_id", roleList)); |
| | | if (CollUtil.isEmpty(roles)) { |
| | | throw new ServiceException("没有权限访问角色的数据"); |
| | | } |
| | | if (clear) { |
| | |
| | | userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId)); |
| | | } |
| | | // 新增用户与角色管理 |
| | | List<SysUserRole> list = StreamUtils.toList(canDoRoleList, roleId -> { |
| | | List<SysUserRole> list = StreamUtils.toList(roleList, roleId -> { |
| | | SysUserRole ur = new SysUserRole(); |
| | | ur.setUserId(userId); |
| | | ur.setRoleId(roleId); |
| | |
| | | // 删除用户与岗位表 |
| | | userPostMapper.delete(new LambdaQueryWrapper<SysUserPost>().in(SysUserPost::getUserId, ids)); |
| | | // 防止更新失败导致的数据删除 |
| | | int flag = baseMapper.deleteBatchIds(ids); |
| | | int flag = baseMapper.deleteByIds(ids); |
| | | if (flag < 1) { |
| | | throw new ServiceException("删除用户失败!"); |
| | | } |
| | |
| | | return ObjectUtil.isNull(sysUser) ? null : sysUser.getEmail(); |
| | | } |
| | | |
| | | @Override |
| | | public List<UserDTO> selectListByIds(List<Long> userIds) { |
| | | if (CollUtil.isEmpty(userIds)) { |
| | | return List.of(); |
| | | } |
| | | List<SysUserVo> list = baseMapper.selectVoList(new LambdaQueryWrapper<SysUser>() |
| | | .select(SysUser::getUserId, SysUser::getUserName, SysUser::getNickName, SysUser::getEmail, SysUser::getPhonenumber) |
| | | .eq(SysUser::getStatus, UserConstants.USER_NORMAL) |
| | | .in(CollUtil.isNotEmpty(userIds), SysUser::getUserId, userIds)); |
| | | return BeanUtil.copyToList(list, UserDTO.class); |
| | | } |
| | | |
| | | @Override |
| | | public List<Long> selectUserIdsByRoleIds(List<Long> roleIds) { |
| | | List<SysUserRole> userRoles = userRoleMapper.selectList( |
| | | new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getRoleId, roleIds)); |
| | | return StreamUtils.toList(userRoles, SysUserRole::getUserId); |
| | | } |
| | | |
| | | @Override |
| | | public List<UserDTO> selectUsersByRoleIds(List<Long> roleIds) { |
| | | if (CollUtil.isEmpty(roleIds)) { |
| | | return List.of(); |
| | | } |
| | | List<SysUserRole> userRoles = userRoleMapper.selectList( |
| | | new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getRoleId, roleIds)); |
| | | List<Long> userIds = StreamUtils.toList(userRoles, SysUserRole::getUserId); |
| | | return selectListByIds(userIds); |
| | | } |
| | | |
| | | @Override |
| | | public List<UserDTO> selectUsersByDeptIds(List<Long> deptIds) { |
| | | if (CollUtil.isEmpty(deptIds)) { |
| | | return List.of(); |
| | | } |
| | | List<SysUserVo> list = baseMapper.selectVoList(new LambdaQueryWrapper<SysUser>() |
| | | .select(SysUser::getUserId, SysUser::getUserName, SysUser::getNickName, SysUser::getEmail, SysUser::getPhonenumber) |
| | | .eq(SysUser::getStatus, UserConstants.USER_NORMAL) |
| | | .in(CollUtil.isNotEmpty(deptIds), SysUser::getDeptId, deptIds)); |
| | | return BeanUtil.copyToList(list, UserDTO.class); |
| | | } |
| | | } |