| | |
| | | import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils; |
| | | import cn.iocoder.yudao.module.infra.api.config.ConfigApi; |
| | | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthRegisterReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; |
| | | import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO; |
| | |
| | | } |
| | | // 1.2 系统参数中的初始密码,作为 Excel 未填写密码时的兜底 |
| | | String initPassword = configApi.getConfigValueByKey(USER_INIT_PASSWORD_KEY); |
| | | // 1.3 预加载部门:名称 -> 部门,用于按名称解析(模板要求填部门名称,而非数据库主键 ID) |
| | | List<DeptDO> allDepts = deptService.getDeptList(new DeptListReqVO()); |
| | | Map<String, DeptDO> deptByName = new HashMap<>(Math.max(allDepts.size(), 1)); |
| | | for (DeptDO dept : allDepts) { |
| | | deptByName.put(dept.getName().trim(), dept); |
| | | } |
| | | |
| | | // 2. 遍历,逐个创建 or 更新 |
| | | UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()) |
| | |
| | | importUsers.forEach(importUser -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String key = StrUtil.blankToDefault(importUser.getUsername(), "第 " + currentIndex + " 行"); |
| | | // 2.0 解析部门:按部门名称精确匹配,匹配不到给出明确提示(不再要求填数据库主键 ID) |
| | | DeptDO importDept = resolveImportDept(importUser.getDeptName(), deptByName); |
| | | if (importDept == null && StrUtil.isNotBlank(importUser.getDeptName())) { |
| | | respVO.getFailureUsernames().put(key, |
| | | "部门「" + importUser.getDeptName().trim() + "」不存在,请填写部门管理中的完整部门名称"); |
| | | return; |
| | | } |
| | | Long importDeptId = importDept != null ? importDept.getId() : null; |
| | | // 2.1.0 解析该行初始密码:Excel 填写了就用填写的,否则读取系统参数 |
| | | String effectivePassword = StrUtil.isNotBlank(importUser.getPassword()) |
| | | ? importUser.getPassword() : initPassword; |
| | |
| | | } |
| | | // 2.1.1 校验字段是否符合要求 |
| | | try { |
| | | ValidationUtils.validate(BeanUtils.toBean(importUser, UserSaveReqVO.class).setPassword(effectivePassword)); |
| | | ValidationUtils.validate(BeanUtils.toBean(importUser, UserSaveReqVO.class) |
| | | .setPassword(effectivePassword).setDeptId(importDeptId)); |
| | | } catch (ConstraintViolationException ex) { |
| | | respVO.getFailureUsernames().put(key, ex.getMessage()); |
| | | return; |
| | |
| | | // 2.1.2 校验,判断是否有不符合的原因 |
| | | try { |
| | | validateUserForCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), |
| | | importUser.getDeptId(), null); |
| | | importDeptId, null); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage()); |
| | | return; |
| | |
| | | AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); |
| | | if (existUser == null) { |
| | | userMapper.insert(BeanUtils.toBean(importUser, AdminUserDO.class) |
| | | .setDeptId(importDeptId) // 部门已按名称解析,需显式回填 ID |
| | | .setPassword(encodePassword(effectivePassword)).setPostIds(new HashSet<>())); // 设置初始密码及空岗位编号数组 |
| | | respVO.getCreateUsernames().add(importUser.getUsername()); |
| | | return; |
| | |
| | | } |
| | | AdminUserDO updateUser = BeanUtils.toBean(importUser, AdminUserDO.class); |
| | | updateUser.setId(existUser.getId()); |
| | | updateUser.setDeptId(importDeptId); // 部门已按名称解析,需显式回填 ID |
| | | // 密码单独处理:Excel 填写了才更新;未填写则置空,保持原密码不变(避免把明文写进库) |
| | | updateUser.setPassword(StrUtil.isNotBlank(importUser.getPassword()) |
| | | ? encodePassword(importUser.getPassword()) : null); |
| | |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 解析导入行里的部门名称 |
| | | * |
| | | * <p>只按部门名称精确匹配(不再要求填写数据库主键 ID), |
| | | * 避免使用者无从得知部门 ID 导致导入必然失败。</p> |
| | | * |
| | | * @param raw 导入填写的部门名称 |
| | | * @param byName 部门名称 -> 部门 |
| | | * @return 匹配到的部门;未填写或匹配不到返回 null |
| | | */ |
| | | private DeptDO resolveImportDept(String raw, Map<String, DeptDO> byName) { |
| | | if (StrUtil.isBlank(raw)) { |
| | | return null; |
| | | } |
| | | return byName.get(raw.trim()); |
| | | } |
| | | |
| | | @Override |
| | | public List<AdminUserDO> getUserListByStatus(Integer status) { |
| | | return userMapper.selectListByStatus(status); |