| | |
| | | package com.project.system.service.impl; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.project.common.annotation.DataScope; |
| | | import com.project.common.constant.UserConstants; |
| | | import com.project.common.core.domain.entity.SysDept; |
| | | import com.project.common.core.domain.entity.SysRole; |
| | | import com.project.common.core.domain.entity.SysUser; |
| | | import com.project.common.core.domain.model.ImportError; |
| | | import com.project.common.exception.ServiceException; |
| | | import com.project.common.exception.base.BaseException; |
| | | import com.project.common.utils.SecurityUtils; |
| | | import com.project.common.utils.StringUtils; |
| | | import com.project.common.utils.bean.BeanValidators; |
| | | import com.project.common.utils.spring.SpringUtils; |
| | | import com.project.system.domain.SysPost; |
| | | import com.project.system.domain.SysUserPost; |
| | | import com.project.system.domain.SysUserRole; |
| | | import com.project.system.domain.*; |
| | | import com.project.system.domain.bo.editBo.UserDeptBo; |
| | | import com.project.system.domain.vo.SysUserResultVo; |
| | | import com.project.system.domain.vo.SysUserVo; |
| | | import com.project.system.mapper.*; |
| | | import com.project.system.service.ISysConfigService; |
| | | import com.project.system.service.ISysDeptService; |
| | | import com.project.system.service.ISysUserDeptService; |
| | | import com.project.system.service.ISysUserService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.cache.annotation.CacheEvict; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | |
| | | private final SysUserRoleMapper userRoleMapper; |
| | | private final SysUserPostMapper userPostMapper; |
| | | private final ISysConfigService configService; |
| | | private final ISysUserDeptService userDeptService; |
| | | private final ISysDeptService deptService; |
| | | protected final Validator validator; |
| | | |
| | | /** |
| | | * 根据条件分页查询用户列表 |
| | | * |
| | | * @param user 用户信息 |
| | | * @return 用户信息集合信息 |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u") |
| | | public List<SysUser> selectUserListCommon(SysUser user) |
| | | { |
| | | return userMapper.selectUserListCommon(user); |
| | | } |
| | | |
| | | /** |
| | | * 根据条件分页查询用户列表 |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @CacheEvict(value = "sysUser:peerList",allEntries = true) |
| | | public int insertUser(SysUser user) |
| | | { |
| | | // 新增用户信息 |
| | |
| | | insertUserPost(user); |
| | | // 新增用户与角色管理 |
| | | insertUserRole(user); |
| | | // 新增用户与部门管理 |
| | | if (!"02".equals(user.getUserType())){ |
| | | insertUserDept(user); |
| | | } |
| | | return rows; |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @CacheEvict(value = "sysUser:peerList",allEntries = true) |
| | | public int updateUser(SysUser user) |
| | | { |
| | | Long userId = user.getUserId(); |
| | |
| | | userPostMapper.deleteUserPostByUserId(userId); |
| | | // 新增用户与岗位管理 |
| | | insertUserPost(user); |
| | | |
| | | SysUser old = this.selectUserById(userId); |
| | | // 新增用户与部门管理 |
| | | if (!"02".equals(user.getUserType()) && !old.getDeptId().equals(user.getDeptId())){ |
| | | insertUserDept(user); |
| | | } |
| | | return userMapper.updateUser(user); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 新增用户部门 |
| | | * |
| | | * @param user 用户对象 |
| | | */ |
| | | public void insertUserDept(SysUser user) |
| | | { |
| | | if (deptService.getCheckDeptIdByLoginDeptId(user.getDeptId()).equals(user.getDeptId())) { //分管局内所有部门 |
| | | List<SysDept> list = deptService.list(deptService.lq().eq(SysDept::getParentId, user.getDeptId())); |
| | | List<Long> deptIds = list.stream().map(SysDept::getDeptId).collect(Collectors.toList()); |
| | | deptIds.add(user.getDeptId()); |
| | | UserDeptBo bo = new UserDeptBo(); |
| | | bo.setUserId(user.getUserId()); |
| | | bo.setDeptIds(deptIds); |
| | | userDeptService.batchUserDeptSave(bo); |
| | | } else { //分管单独部门 |
| | | userDeptService.remove(userDeptService.lq().eq(SysUserDept::getUserId, user.getUserId())); |
| | | SysUserDept userDept = new SysUserDept().setUserId(user.getUserId()).setDeptId(user.getDeptId()); |
| | | userDeptService.save(userDept); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 新增用户岗位信息 |
| | | * |
| | | * @param user 用户对象 |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @CacheEvict(value = "sysUser:peerList",allEntries = true) |
| | | public int deleteUserById(Long userId) |
| | | { |
| | | // 删除用户与角色关联 |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | @CacheEvict(value = "sysUser:peerList",allEntries = true) |
| | | public int deleteUserByIds(Long[] userIds) |
| | | { |
| | | for (Long userId : userIds) |
| | |
| | | /** |
| | | * 批量删除用户信息 |
| | | * |
| | | * @param deptId 需要删除的用户ID |
| | | * @param phone 需要删除的用户ID |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int deleteUserByDeptId(Long deptId) |
| | | public int deleteUserByCompanyPhone(String phone) |
| | | { |
| | | List<SysUser> sysUsers = userMapper.selectList(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeptId, deptId)); |
| | | List<SysUser> sysUsers = userMapper.selectList(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber, phone)); |
| | | Long[] ids = sysUsers.stream().map(SysUser::getUserId).toArray(Long[]::new); |
| | | return this.deleteUserByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | public List<Long> getNumByRoleKey(String roleKey, Long deptId) |
| | | public List<String> getPhonesByRoleKey(String roleKey, Long deptId) |
| | | { |
| | | return userMapper.getNumByRoleKey(roleKey, deptId); |
| | | return userMapper.getPhonesByRoleKey(roleKey, deptId); |
| | | } |
| | | |
| | | @Override |
| | | public int resetPhone(Long userId, String phone) { |
| | | return userMapper.resetPhone(userId, phone); |
| | | } |
| | | |
| | | /** |
| | | * 处理导入数据 |
| | | * @param list 数据 |
| | | * @param deptId 导入部门id |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public SysUserResultVo doImport(List<SysUserVo> list, Long deptId) |
| | | { |
| | | if (list==null || list.size()<1){ |
| | | throw new BaseException("未获取到导入信息!"); |
| | | } |
| | | |
| | | SysUserResultVo resultVo = new SysUserResultVo(); |
| | | list.forEach(sysUserVo -> { |
| | | sysUserVo.setDeptId(deptId); |
| | | }); |
| | | resultVo.setVoList(list); |
| | | return this.checkImport(resultVo); |
| | | } |
| | | |
| | | /** |
| | | * 校验导入数据 |
| | | * @param resultVo 数据 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public SysUserResultVo checkImport(SysUserResultVo resultVo) |
| | | { |
| | | if (resultVo==null || StringUtils.isEmpty(resultVo.getVoList())){ |
| | | throw new BaseException("未获取到导入信息!"); |
| | | } |
| | | for (SysUserVo vo : resultVo.getVoList()) { |
| | | List<ImportError> errors = new ArrayList<>(); |
| | | validEntityBeforeImport(vo, errors); |
| | | vo.setErrorList(errors); |
| | | } |
| | | return resultVo; |
| | | } |
| | | |
| | | /** |
| | | * 验证参数 |
| | | * @param entity 数据 |
| | | * @param errorList 错误数据 |
| | | */ |
| | | private void validEntityBeforeImport(SysUserVo entity, List<ImportError> errorList) |
| | | { |
| | | if (StringUtils.isEmpty(entity.getNickName())){ |
| | | errorList.add(new ImportError(0, "请填姓名!")); |
| | | } |
| | | if (StringUtils.isEmpty(entity.getDeptName())){ |
| | | errorList.add(new ImportError(1, "请填写部门信息!")); |
| | | } else { |
| | | SysDept sysDept = deptService.selectDeptById(entity.getDeptId()); |
| | | if (sysDept==null || !sysDept.getDeptName().equals(entity.getDeptName())){ |
| | | errorList.add(new ImportError(1, "部门信息不匹配!")); |
| | | } |
| | | } |
| | | if (StringUtils.isEmpty(entity.getPhonenumber())){ |
| | | errorList.add(new ImportError(2, "请填写手机号!")); |
| | | } else { |
| | | SysUser user = this.selectUserByUserName(entity.getPhonenumber()); |
| | | if (user!=null){ |
| | | entity.setUserId(user.getUserId()); |
| | | } else { |
| | | SysUser check = new SysUser(); |
| | | check.setPhonenumber(entity.getPhonenumber()); |
| | | String s = checkPhoneUnique(check); |
| | | if (UserConstants.NOT_UNIQUE.equals(s)) { |
| | | errorList.add(new ImportError(2, "手机号已存在!")); |
| | | } |
| | | } |
| | | } |
| | | if (StringUtils.isNotEmpty(entity.getEmail())){ |
| | | SysUser check = new SysUser(); |
| | | check.setPhonenumber(entity.getPhonenumber()); |
| | | String s = checkEmailUnique(check); |
| | | if (UserConstants.NOT_UNIQUE.equals(s)) { |
| | | errorList.add(new ImportError(3, "邮箱已存在!")); |
| | | } |
| | | } |
| | | if (StringUtils.isNotEmpty(entity.getImportSex())){ |
| | | switch (entity.getImportSex().trim()){ |
| | | case "男": |
| | | entity.setSex("0"); |
| | | break; |
| | | case "女": |
| | | entity.setSex("1"); |
| | | break; |
| | | case "未知": |
| | | entity.setSex("2"); |
| | | break; |
| | | } |
| | | } else { |
| | | entity.setSex("2"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存导入结果 |
| | | * @param resultVo 数据 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public Boolean saveImport(SysUserResultVo resultVo) |
| | | { |
| | | if (resultVo==null || StringUtils.isEmpty(resultVo.getVoList())){ |
| | | throw new BaseException("未获取到导入信息!"); |
| | | } |
| | | resultVo.getVoList().forEach(e-> { |
| | | if (StringUtils.isNotEmpty(e.getErrorList())) { |
| | | throw new BaseException("还有未处理错误信息!"); |
| | | } |
| | | }); |
| | | List<SysUser> users = Convert.toList(SysUser.class, resultVo.getVoList()); |
| | | for (SysUser user : users) { |
| | | this.insertUser(user); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public String getDeptNameByUserId(Long userId) |
| | | { |
| | | return deptService.selectDeptById(selectUserById(userId).getDeptId()).getDeptName(); |
| | | } |
| | | } |