| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; |
| | | import com.ruoyi.common.core.domain.PageQuery; |
| | | import com.ruoyi.system.domain.SysDept; |
| | | import com.ruoyi.system.domain.SysRole; |
| | | import com.ruoyi.system.domain.SysUser; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.core.service.UserService; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.PageUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.helper.DataBaseHelper; |
| | | import com.ruoyi.common.helper.LoginHelper; |
| | | import com.ruoyi.common.utils.StreamUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.system.domain.SysPost; |
| | | import com.ruoyi.system.domain.SysUserPost; |
| | | import com.ruoyi.system.domain.SysUserRole; |
| | | import com.ruoyi.system.mapper.*; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 用户 业务层处理 |
| | |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | @Service |
| | | public class SysUserServiceImpl extends ServicePlusImpl<SysUserMapper, SysUser, SysUser> implements ISysUserService, UserService { |
| | | public class SysUserServiceImpl implements ISysUserService { |
| | | |
| | | @Autowired |
| | | private SysRoleMapper roleMapper; |
| | | |
| | | @Autowired |
| | | private SysPostMapper postMapper; |
| | | |
| | | @Autowired |
| | | private SysUserRoleMapper userRoleMapper; |
| | | |
| | | @Autowired |
| | | private SysUserPostMapper userPostMapper; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | private final SysUserMapper baseMapper; |
| | | private final SysDeptMapper deptMapper; |
| | | private final SysRoleMapper roleMapper; |
| | | private final SysPostMapper postMapper; |
| | | private final SysUserRoleMapper userRoleMapper; |
| | | private final SysUserPostMapper userPostMapper; |
| | | |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public TableDataInfo<SysUser> selectPageUserList(SysUser user) { |
| | | return PageUtils.buildDataInfo(baseMapper.selectPageUserList(PageUtils.buildPage(), user)); |
| | | public TableDataInfo<SysUser> selectPageUserList(SysUser user, PageQuery pageQuery) { |
| | | Page<SysUser> page = baseMapper.selectPageUserList(pageQuery.build(), this.buildQueryWrapper(user)); |
| | | return TableDataInfo.build(page); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 用户信息集合信息 |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public List<SysUser> selectUserList(SysUser user) { |
| | | return baseMapper.selectUserList(user); |
| | | return baseMapper.selectUserList(this.buildQueryWrapper(user)); |
| | | } |
| | | |
| | | private Wrapper<SysUser> buildQueryWrapper(SysUser user) { |
| | | Map<String, Object> params = user.getParams(); |
| | | QueryWrapper<SysUser> wrapper = Wrappers.query(); |
| | | wrapper.eq("u.del_flag", UserConstants.USER_NORMAL) |
| | | .eq(ObjectUtil.isNotNull(user.getUserId()), "u.user_id", user.getUserId()) |
| | | .like(StringUtils.isNotBlank(user.getUserName()), "u.user_name", user.getUserName()) |
| | | .eq(StringUtils.isNotBlank(user.getStatus()), "u.status", user.getStatus()) |
| | | .like(StringUtils.isNotBlank(user.getPhonenumber()), "u.phonenumber", user.getPhonenumber()) |
| | | .between(params.get("beginTime") != null && params.get("endTime") != null, |
| | | "u.create_time", params.get("beginTime"), params.get("endTime")) |
| | | .and(ObjectUtil.isNotNull(user.getDeptId()), w -> { |
| | | List<SysDept> deptList = deptMapper.selectList(new LambdaQueryWrapper<SysDept>() |
| | | .select(SysDept::getDeptId) |
| | | .apply(DataBaseHelper.findInSet(user.getDeptId(), "ancestors"))); |
| | | List<Long> ids = StreamUtils.toList(deptList, SysDept::getDeptId); |
| | | ids.add(user.getDeptId()); |
| | | w.in("u.dept_id", ids); |
| | | }); |
| | | return wrapper; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 用户信息集合信息 |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public TableDataInfo<SysUser> selectAllocatedList(SysUser user) { |
| | | return PageUtils.buildDataInfo(baseMapper.selectAllocatedList(PageUtils.buildPage(), user)); |
| | | public TableDataInfo<SysUser> selectAllocatedList(SysUser user, PageQuery pageQuery) { |
| | | QueryWrapper<SysUser> wrapper = Wrappers.query(); |
| | | wrapper.eq("u.del_flag", UserConstants.USER_NORMAL) |
| | | .eq(ObjectUtil.isNotNull(user.getRoleId()), "r.role_id", user.getRoleId()) |
| | | .like(StringUtils.isNotBlank(user.getUserName()), "u.user_name", user.getUserName()) |
| | | .eq(StringUtils.isNotBlank(user.getStatus()), "u.status", user.getStatus()) |
| | | .like(StringUtils.isNotBlank(user.getPhonenumber()), "u.phonenumber", user.getPhonenumber()); |
| | | Page<SysUser> page = baseMapper.selectAllocatedList(pageQuery.build(), wrapper); |
| | | return TableDataInfo.build(page); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 用户信息集合信息 |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public TableDataInfo<SysUser> selectUnallocatedList(SysUser user) { |
| | | return PageUtils.buildDataInfo(baseMapper.selectUnallocatedList(PageUtils.buildPage(), user)); |
| | | public TableDataInfo<SysUser> selectUnallocatedList(SysUser user, PageQuery pageQuery) { |
| | | List<Long> userIds = userRoleMapper.selectUserIdsByRoleId(user.getRoleId()); |
| | | QueryWrapper<SysUser> wrapper = Wrappers.query(); |
| | | wrapper.eq("u.del_flag", UserConstants.USER_NORMAL) |
| | | .and(w -> w.ne("r.role_id", user.getRoleId()).or().isNull("r.role_id")) |
| | | .notIn(CollUtil.isNotEmpty(userIds), "u.user_id", userIds) |
| | | .like(StringUtils.isNotBlank(user.getUserName()), "u.user_name", user.getUserName()) |
| | | .like(StringUtils.isNotBlank(user.getPhonenumber()), "u.phonenumber", user.getPhonenumber()); |
| | | Page<SysUser> page = baseMapper.selectUnallocatedList(pageQuery.build(), wrapper); |
| | | return TableDataInfo.build(page); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public SysUser selectUserByUserName(String userName) { |
| | | return baseMapper.selectUserByUserName(userName); |
| | | } |
| | | |
| | | /** |
| | | * 通过手机号查询用户 |
| | | * |
| | | * @param phonenumber 手机号 |
| | | * @return 用户对象信息 |
| | | */ |
| | | @Override |
| | | public SysUser selectUserByPhonenumber(String phonenumber) { |
| | | return baseMapper.selectUserByPhonenumber(phonenumber); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public String selectUserRoleGroup(String userName) { |
| | | List<SysRole> list = roleMapper.selectRolesByUserName(userName); |
| | | StringBuilder idsStr = new StringBuilder(); |
| | | for (SysRole role : list) { |
| | | idsStr.append(role.getRoleName()).append(","); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | if (StringUtils.isNotEmpty(idsStr.toString())) { |
| | | return idsStr.substring(0, idsStr.length() - 1); |
| | | } |
| | | return idsStr.toString(); |
| | | return StreamUtils.join(list, SysRole::getRoleName); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public String selectUserPostGroup(String userName) { |
| | | List<SysPost> list = postMapper.selectPostsByUserName(userName); |
| | | StringBuilder idsStr = new StringBuilder(); |
| | | for (SysPost post : list) { |
| | | idsStr.append(post.getPostName()).append(","); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | if (StringUtils.isNotEmpty(idsStr.toString())) { |
| | | return idsStr.substring(0, idsStr.length() - 1); |
| | | } |
| | | return idsStr.toString(); |
| | | return StreamUtils.join(list, SysPost::getPostName); |
| | | } |
| | | |
| | | /** |
| | | * 校验用户名称是否唯一 |
| | | * |
| | | * @param userName 用户名称 |
| | | * @param user 用户信息 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public String checkUserNameUnique(String userName) { |
| | | long count = count(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, userName)); |
| | | if (count > 0) { |
| | | public String checkUserNameUnique(SysUser user) { |
| | | boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getUserName, user.getUserName()) |
| | | .ne(ObjectUtil.isNotNull(user.getUserId()), SysUser::getUserId, user.getUserId())); |
| | | if (exist) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | */ |
| | | @Override |
| | | public String checkPhoneUnique(SysUser user) { |
| | | Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId(); |
| | | long count = count(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getPhonenumber, user.getPhonenumber()) |
| | | .ne(SysUser::getUserId, userId)); |
| | | if (count > 0) { |
| | | boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getPhonenumber, user.getPhonenumber()) |
| | | .ne(ObjectUtil.isNotNull(user.getUserId()), SysUser::getUserId, user.getUserId())); |
| | | if (exist) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | */ |
| | | @Override |
| | | public String checkEmailUnique(SysUser user) { |
| | | Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId(); |
| | | long count = count(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getEmail, user.getEmail()) |
| | | .ne(SysUser::getUserId, userId)); |
| | | if (count > 0) { |
| | | boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getEmail, user.getEmail()) |
| | | .ne(ObjectUtil.isNotNull(user.getUserId()), SysUser::getUserId, user.getUserId())); |
| | | if (exist) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | */ |
| | | @Override |
| | | public void checkUserAllowed(SysUser user) { |
| | | if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) { |
| | | if (ObjectUtil.isNotNull(user.getUserId()) && user.isAdmin()) { |
| | | throw new ServiceException("不允许操作超级管理员用户"); |
| | | } |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public void checkUserDataScope(Long userId) { |
| | | if (!SysUser.isAdmin(SecurityUtils.getUserId())) { |
| | | if (!LoginHelper.isAdmin()) { |
| | | SysUser user = new SysUser(); |
| | | user.setUserId(userId); |
| | | List<SysUser> users = SpringUtils.getAopProxy(this).selectUserList(user); |
| | | if (StringUtils.isEmpty(users)) { |
| | | List<SysUser> users = this.selectUserList(user); |
| | | if (CollUtil.isEmpty(users)) { |
| | | throw new ServiceException("没有权限访问用户数据!"); |
| | | } |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = { Exception.class }) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int insertUser(SysUser user) { |
| | | // 新增用户信息 |
| | | int rows = baseMapper.insert(user); |
| | |
| | | */ |
| | | @Override |
| | | public boolean registerUser(SysUser user) { |
| | | user.setCreateBy(user.getUserName()); |
| | | user.setUpdateBy(user.getUserName()); |
| | | return baseMapper.insert(user) > 0; |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = { Exception.class }) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int updateUser(SysUser user) { |
| | | Long userId = user.getUserId(); |
| | | // 删除用户与角色关联 |
| | |
| | | * @param roleIds 角色组 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = { Exception.class }) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void insertUserAuth(Long userId, Long[] roleIds) { |
| | | userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() |
| | | .eq(SysUserRole::getUserId, userId)); |
| | | .eq(SysUserRole::getUserId, userId)); |
| | | insertUserRole(userId, roleIds); |
| | | } |
| | | |
| | |
| | | @Override |
| | | public boolean updateUserAvatar(String userName, String avatar) { |
| | | return baseMapper.update(null, |
| | | new LambdaUpdateWrapper<SysUser>() |
| | | .set(SysUser::getAvatar, avatar) |
| | | .eq(SysUser::getUserName, userName)) > 0; |
| | | new LambdaUpdateWrapper<SysUser>() |
| | | .set(SysUser::getAvatar, avatar) |
| | | .eq(SysUser::getUserName, userName)) > 0; |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public int resetUserPwd(String userName, String password) { |
| | | return baseMapper.update(null, |
| | | new LambdaUpdateWrapper<SysUser>() |
| | | .set(SysUser::getPassword, password) |
| | | .eq(SysUser::getUserName, userName)); |
| | | new LambdaUpdateWrapper<SysUser>() |
| | | .set(SysUser::getPassword, password) |
| | | .eq(SysUser::getUserName, userName)); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param user 用户对象 |
| | | */ |
| | | public void insertUserRole(SysUser user) { |
| | | Long[] roles = user.getRoleIds(); |
| | | if (StringUtils.isNotNull(roles)) { |
| | | // 新增用户与角色管理 |
| | | List<SysUserRole> list = new ArrayList<SysUserRole>(); |
| | | for (Long roleId : roles) { |
| | | SysUserRole ur = new SysUserRole(); |
| | | ur.setUserId(user.getUserId()); |
| | | ur.setRoleId(roleId); |
| | | list.add(ur); |
| | | } |
| | | if (list.size() > 0) { |
| | | userRoleMapper.insertAll(list); |
| | | } |
| | | } |
| | | this.insertUserRole(user.getUserId(), user.getRoleIds()); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public void insertUserPost(SysUser user) { |
| | | Long[] posts = user.getPostIds(); |
| | | if (StringUtils.isNotNull(posts)) { |
| | | if (ArrayUtil.isNotEmpty(posts)) { |
| | | // 新增用户与岗位管理 |
| | | List<SysUserPost> list = new ArrayList<SysUserPost>(); |
| | | List<SysUserPost> list = new ArrayList<>(posts.length); |
| | | for (Long postId : posts) { |
| | | SysUserPost up = new SysUserPost(); |
| | | up.setUserId(user.getUserId()); |
| | | up.setPostId(postId); |
| | | list.add(up); |
| | | } |
| | | if (list.size() > 0) { |
| | | userPostMapper.insertAll(list); |
| | | } |
| | | userPostMapper.insertBatch(list); |
| | | } |
| | | } |
| | | |
| | |
| | | * @param roleIds 角色组 |
| | | */ |
| | | public void insertUserRole(Long userId, Long[] roleIds) { |
| | | if (StringUtils.isNotNull(roleIds)) { |
| | | if (ArrayUtil.isNotEmpty(roleIds)) { |
| | | // 新增用户与角色管理 |
| | | List<SysUserRole> list = new ArrayList<SysUserRole>(); |
| | | List<SysUserRole> list = new ArrayList<>(roleIds.length); |
| | | for (Long roleId : roleIds) { |
| | | SysUserRole ur = new SysUserRole(); |
| | | ur.setUserId(userId); |
| | | ur.setRoleId(roleId); |
| | | list.add(ur); |
| | | } |
| | | if (list.size() > 0) { |
| | | userRoleMapper.insertAll(list); |
| | | } |
| | | userRoleMapper.insertBatch(list); |
| | | } |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = { Exception.class }) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deleteUserById(Long userId) { |
| | | // 删除用户与角色关联 |
| | | userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId)); |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = { Exception.class }) |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deleteUserByIds(Long[] userIds) { |
| | | for (Long userId : userIds) { |
| | | checkUserAllowed(new SysUser(userId)); |
| | | checkUserDataScope(userId); |
| | | } |
| | | List<Long> ids = Arrays.asList(userIds); |
| | | // 删除用户与角色关联 |
| | |
| | | return baseMapper.deleteBatchIds(ids); |
| | | } |
| | | |
| | | /** |
| | | * 导入用户数据 |
| | | * |
| | | * @param userList 用户数据列表 |
| | | * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据 |
| | | * @param operName 操作用户 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) { |
| | | if (StringUtils.isNull(userList) || userList.size() == 0) { |
| | | throw new ServiceException("导入用户数据不能为空!"); |
| | | } |
| | | int successNum = 0; |
| | | int failureNum = 0; |
| | | StringBuilder successMsg = new StringBuilder(); |
| | | StringBuilder failureMsg = new StringBuilder(); |
| | | String password = configService.selectConfigByKey("sys.user.initPassword"); |
| | | for (SysUser user : userList) { |
| | | try { |
| | | // 验证是否存在这个用户 |
| | | SysUser u = baseMapper.selectUserByUserName(user.getUserName()); |
| | | if (StringUtils.isNull(u)) { |
| | | user.setPassword(SecurityUtils.encryptPassword(password)); |
| | | user.setCreateBy(operName); |
| | | this.insertUser(user); |
| | | successNum++; |
| | | successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功"); |
| | | } else if (isUpdateSupport) { |
| | | user.setUpdateBy(operName); |
| | | this.updateUser(user); |
| | | successNum++; |
| | | successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功"); |
| | | } else { |
| | | failureNum++; |
| | | failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在"); |
| | | } |
| | | } catch (Exception e) { |
| | | failureNum++; |
| | | String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:"; |
| | | failureMsg.append(msg + e.getMessage()); |
| | | log.error(msg, e); |
| | | } |
| | | } |
| | | if (failureNum > 0) { |
| | | failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); |
| | | throw new ServiceException(failureMsg.toString()); |
| | | } else { |
| | | successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); |
| | | } |
| | | return successMsg.toString(); |
| | | } |
| | | } |