用户批量导入

main
zhangyue 2025-11-22 17:15:17 +08:00
parent f27fb33063
commit 9c84548f12
6 changed files with 78 additions and 56 deletions

View File

@ -60,7 +60,6 @@ public class UserController {
@ApiOperation("新增用户") @ApiOperation("新增用户")
@PostMapping("/save") @PostMapping("/save")
public SingleResponse<UserCO> add(@Validated @RequestBody UserAddCmd cmd) { public SingleResponse<UserCO> add(@Validated @RequestBody UserAddCmd cmd) {
SSOUser ssoUser = AuthContext.getCurrentUser();
return userService.add(cmd); return userService.add(cmd);
} }

View File

@ -8,6 +8,7 @@ import com.jjb.saas.config.client.dicttree.facade.ConfDictTreeFacade;
import com.jjb.saas.config.client.dicttree.response.ConfDictTreeCO; import com.jjb.saas.config.client.dicttree.response.ConfDictTreeCO;
import com.jjb.saas.framework.auth.model.SSOUser; import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext; import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.system.client.role.response.RoleCO;
import com.zcloud.basic.info.command.convertor.UserCoConvertor; import com.zcloud.basic.info.command.convertor.UserCoConvertor;
import com.zcloud.basic.info.domain.gateway.CorpInfoGateway; import com.zcloud.basic.info.domain.gateway.CorpInfoGateway;
import com.zcloud.basic.info.domain.gateway.UserEmploymentLogGateway; import com.zcloud.basic.info.domain.gateway.UserEmploymentLogGateway;
@ -129,6 +130,9 @@ public class UserAddExe {
UserE userE = new UserE(); UserE userE = new UserE();
Long corpinfoId = AuthContext.getTenantId(); Long corpinfoId = AuthContext.getTenantId();
Map<String, Long> roleMap = userRepository.getRoleMap();
CorpInfoDO corpInfoDO = corpInfoRepository.getById(corpinfoId); CorpInfoDO corpInfoDO = corpInfoRepository.getById(corpinfoId);
// 部门数据 // 部门数据
List<DepartmentDO> departmentDOList = departmentRepository.listByCorpInfoId(corpinfoId); List<DepartmentDO> departmentDOList = departmentRepository.listByCorpInfoId(corpinfoId);
@ -142,11 +146,11 @@ public class UserAddExe {
if(CollectionUtil.isNotEmpty(confDictCOList)){ if(CollectionUtil.isNotEmpty(confDictCOList)){
dictMap = confDictCOList.stream().collect(Collectors.toMap(ConfDictTreeCO::getDictLabel, ConfDictTreeCO::getDictValue)); dictMap = confDictCOList.stream().collect(Collectors.toMap(ConfDictTreeCO::getDictLabel, ConfDictTreeCO::getDictValue));
} }
List<UserE> userEList = userE.parseImportTemplateData(file, List<UserE> userEList = userE.parseImportTemplateData(file,
BeanUtil.copyToList(departmentDOList, DepartmentE.class), BeanUtil.copyToList(departmentDOList, DepartmentE.class),
BeanUtil.copyToList(postDOList, PostE.class), BeanUtil.copyToList(postDOList, PostE.class),
BeanUtil.copyToList(userDOList, UserE.class), BeanUtil.copyToList(userDOList, UserE.class),
roleMap,
dictMap); dictMap);
// 批量插入用户 // 批量插入用户

View File

@ -17,7 +17,6 @@ import javax.validation.constraints.NotNull;
public class UserVerifyQryCmd { public class UserVerifyQryCmd {
@ApiModelProperty(value = "用户id", name = "id", required = true) @ApiModelProperty(value = "用户id", name = "id", required = true)
@NotNull(message = "用户id不能为空")
private Long id; private Long id;
@ApiModelProperty(value = "登录账号", name = "username") @ApiModelProperty(value = "登录账号", name = "username")
private String username; private String username;

View File

@ -149,36 +149,37 @@ public class UserE extends BaseE {
public void initAdd(Long tenantId, UserE userE) { public void initAdd(Long tenantId, UserE userE) {
userE.setUserId(Tools.get32UUID()); userE.setUserId(Tools.get32UUID());
userE.setTenantId(!ObjectUtils.isEmpty(userE.getTenantId())? userE.getTenantId() : tenantId); userE.setTenantId(!ObjectUtils.isEmpty(userE.getTenantId()) ? userE.getTenantId() : tenantId);
userE.setCorpinfoId(!ObjectUtils.isEmpty(userE.getCorpinfoId())? userE.getCorpinfoId() : tenantId); userE.setCorpinfoId(!ObjectUtils.isEmpty(userE.getCorpinfoId()) ? userE.getCorpinfoId() : tenantId);
userE.setEmploymentFlag(1); userE.setEmploymentFlag(1);
userE.setPassword(defaultPassword); userE.setPassword(defaultPassword);
} }
/** /**
* *
*
* @param oldUserE * @param oldUserE
* @param newUserE * @param newUserE
* @return * @return
*/ */
public boolean verifyTransferPosition(UserE oldUserE, UserE newUserE) { public boolean verifyTransferPosition(UserE oldUserE, UserE newUserE) {
if (oldUserE.getCorpinfoId() != newUserE.getCorpinfoId()){ if (oldUserE.getCorpinfoId() != newUserE.getCorpinfoId()) {
return true; return true;
} }
if (oldUserE.getDepartmentId() != newUserE.getDepartmentId()){ if (oldUserE.getDepartmentId() != newUserE.getDepartmentId()) {
return true; return true;
} }
if (oldUserE.getPostId() != newUserE.getPostId()){ if (oldUserE.getPostId() != newUserE.getPostId()) {
return true; return true;
} }
if(oldUserE.getEmploymentFlag() != newUserE.getEmploymentFlag()){ if (oldUserE.getEmploymentFlag() != newUserE.getEmploymentFlag()) {
return true; return true;
} }
return false; return false;
} }
public void checkNull(List<UserE> userList) { public void checkNull(List<UserE> userList) {
if(CollUtil.isNotEmpty(userList)){ if (CollUtil.isNotEmpty(userList)) {
throw new BizException("岗位有相应人员,请解除相关岗位信息后删除。"); throw new BizException("岗位有相应人员,请解除相关岗位信息后删除。");
} }
} }
@ -187,18 +188,19 @@ public class UserE extends BaseE {
// 导出 // 导出
public void exportUserTable(HttpServletResponse httpServletResponse, List<UserExcelExportEntity> userExcelEntities) { public void exportUserTable(HttpServletResponse httpServletResponse, List<UserExcelExportEntity> userExcelEntities) {
try { try {
ExcelUtils.exportExcel(httpServletResponse,UserExcelExportEntity.class,"人员信息", userExcelEntities); ExcelUtils.exportExcel(httpServletResponse, UserExcelExportEntity.class, "人员信息", userExcelEntities);
}catch (Exception e) { } catch (Exception e) {
throw new BizException("导出失败"); throw new BizException("导出失败");
} }
} }
// 导入 // 导入
public List<UserE> parseImportTemplateData(MultipartFile file, public List<UserE> parseImportTemplateData(MultipartFile file,
List<DepartmentE> departmentList, List<DepartmentE> departmentList,
List<PostE> postList, List<PostE> postList,
List<UserE> userList, List<UserE> userList,
Map<String, String> dictMap) { Map<String, Long> roleMap,
Map<String, String> dictMap) {
List<UserExcelImportEntity> list = new ArrayList<>(); List<UserExcelImportEntity> list = new ArrayList<>();
try { try {
ExcelUtils.readBatch(file, UserExcelImportEntity.class, list); ExcelUtils.readBatch(file, UserExcelImportEntity.class, list);
@ -209,7 +211,7 @@ public class UserE extends BaseE {
throw new BizException("文件解析失败"); throw new BizException("文件解析失败");
} }
if(CollUtil.isEmpty(departmentList)){ if (CollUtil.isEmpty(departmentList)) {
throw new BizException("该企业暂未存在部门信息,请添加部门后重新导入"); throw new BizException("该企业暂未存在部门信息,请添加部门后重新导入");
} }
// if(CollUtil.isEmpty(postList)){ // if(CollUtil.isEmpty(postList)){
@ -219,7 +221,7 @@ public class UserE extends BaseE {
Map<String, UserE> userEMap = new HashMap<>(); Map<String, UserE> userEMap = new HashMap<>();
if(CollUtil.isNotEmpty(userList)){ if (CollUtil.isNotEmpty(userList)) {
userEMap = userList.stream().collect(Collectors.toMap(UserE::getUsername, UserE -> UserE)); userEMap = userList.stream().collect(Collectors.toMap(UserE::getUsername, UserE -> UserE));
} }
@ -231,34 +233,37 @@ public class UserE extends BaseE {
// 校验 // 校验
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
UserExcelImportEntity entity = list.get(i); UserExcelImportEntity entity = list.get(i);
if(entity == null){ if (entity == null) {
errList.add("第" + (i+2) + "行数据不能为空。"); errList.add("第" + (i + 2) + "行数据不能为空。");
continue; continue;
} }
if(StringUtils.isEmpty(entity.getRoleName())){ if (StringUtils.isEmpty(entity.getRoleName())) {
errList.add("第" + (i+2) + "行用户角色不能为空。"); errList.add("第" + (i + 2) + "行用户角色不能为空。");
} }
if(StringUtils.isEmpty(entity.getDepartmentName())){ if(!roleMap.containsKey(entity.getRoleName())){
errList.add("第" + (i+2) + "行所属部门不能为空。"); errList.add("第" + (i + 2) + "行用户角色不存在。");
}else { }
if(departmentEMap.get(entity.getDepartmentName()) == null){ if (StringUtils.isEmpty(entity.getDepartmentName())) {
errList.add("第" + (i+2) + "行所属部门不存在。"); errList.add("第" + (i + 2) + "行所属部门不能为空。");
} else {
if (departmentEMap.get(entity.getDepartmentName()) == null) {
errList.add("第" + (i + 2) + "行所属部门不存在。");
} }
} }
// 岗位 // 岗位
if(StringUtils.isNotEmpty(entity.getPostName())){ if (StringUtils.isNotEmpty(entity.getPostName())) {
if(CollUtil.isEmpty(postList)){ if (CollUtil.isEmpty(postList)) {
throw new BizException("该企业暂未存在岗位信息,请添加岗位后重新导入"); throw new BizException("该企业暂未存在岗位信息,请添加岗位后重新导入");
} }
DepartmentE departmentE = departmentEMap.get(entity.getDepartmentName()); DepartmentE departmentE = departmentEMap.get(entity.getDepartmentName());
if(departmentE != null){ if (departmentE != null) {
List<PostE> postListByDepartId = postList.stream().filter(post -> post.getDepartmentId().equals(departmentE.getId())).collect(Collectors.toList()); List<PostE> postListByDepartId = postList.stream().filter(post -> post.getDepartmentId().equals(departmentE.getId())).collect(Collectors.toList());
if(CollUtil.isEmpty(postListByDepartId)){ if (CollUtil.isEmpty(postListByDepartId)) {
errList.add("第" + (i+2) + "行所属部门下不存在岗位信息。"); errList.add("第" + (i + 2) + "行所属部门下不存在岗位信息。");
}else { } else {
Map<String, PostE> postEMap = postListByDepartId.stream().collect(Collectors.toMap(PostE::getPostName, PostE -> PostE)); Map<String, PostE> postEMap = postListByDepartId.stream().collect(Collectors.toMap(PostE::getPostName, PostE -> PostE));
if(postEMap.get(entity.getPostName()) == null){ if (postEMap.get(entity.getPostName()) == null) {
errList.add("第" + (i+2) + "行所属岗位信息与所属部门信息不匹配。"); errList.add("第" + (i + 2) + "行所属岗位信息与所属部门信息不匹配。");
} }
} }
} }
@ -270,39 +275,39 @@ public class UserE extends BaseE {
// //
// } // }
// 用户名 // 用户名
if(StringUtils.isEmpty(entity.getUsername())){ if (StringUtils.isEmpty(entity.getUsername())) {
errList.add("第" + (i+2) + "行用户名不能为空。"); errList.add("第" + (i + 2) + "行用户名不能为空。");
}else { } else {
if(CollUtil.isNotEmpty(userEMap)){ if (CollUtil.isNotEmpty(userEMap)) {
if(userEMap.get(entity.getUsername()) != null){ if (userEMap.get(entity.getUsername()) != null) {
errList.add("第" + (i+2) + "行用户名重复。"); errList.add("第" + (i + 2) + "行用户名重复。");
} }
} }
if(usernameMap.get(entity.getUsername()) != null){ if (usernameMap.get(entity.getUsername()) != null) {
errList.add("第" + (i+2) + "行用户名重复。"); errList.add("第" + (i + 2) + "行用户名重复。");
}else { } else {
usernameMap.put(entity.getUsername(), entity.getUsername()); usernameMap.put(entity.getUsername(), entity.getUsername());
} }
} }
if(StringUtils.isEmpty(entity.getName())){ if (StringUtils.isEmpty(entity.getName())) {
errList.add("第" + (i+2) + "行姓名不能为空。"); errList.add("第" + (i + 2) + "行姓名不能为空。");
} }
if(StringUtils.isEmpty(entity.getPersonnelTypeName())){ if (StringUtils.isEmpty(entity.getPersonnelTypeName())) {
errList.add("第" + (i+2) + "行人员类型不能为空。"); errList.add("第" + (i + 2) + "行人员类型不能为空。");
}else { } else {
if(StringUtils.isEmpty(dictMap.get(entity.getPersonnelTypeName()))){ if (StringUtils.isEmpty(dictMap.get(entity.getPersonnelTypeName()))) {
errList.add("第" + (i+2) + "行人员类型与系统不匹配。"); errList.add("第" + (i + 2) + "行人员类型与系统不匹配。");
} }
} }
} }
if(CollUtil.isNotEmpty(errList)){ if (CollUtil.isNotEmpty(errList)) {
throw new BizException("导入信息有误,请检查后重新导入。错误信息:" + String.join("", errList)); throw new BizException("导入信息有误,请检查后重新导入。错误信息:" + String.join("", errList));
} }
// 映射 // 映射
List<UserE> userEList = new ArrayList<>(); List<UserE> userEList = new ArrayList<>();
for (int i = 0; i < list.size(); i++){ for (int i = 0; i < list.size(); i++) {
UserExcelImportEntity entity = list.get(i); UserExcelImportEntity entity = list.get(i);
UserE userE = new UserE(); UserE userE = new UserE();
@ -312,13 +317,13 @@ public class UserE extends BaseE {
userE.setDepartmentName(entity.getDepartmentName()); userE.setDepartmentName(entity.getDepartmentName());
// 岗位 // 岗位
if(StringUtils.isNotEmpty(entity.getPostName())){ if (StringUtils.isNotEmpty(entity.getPostName())) {
List<PostE> postListByDepartId = postList.stream().filter(post -> post.getDepartmentId().equals(departmentId)).collect(Collectors.toList()); List<PostE> postListByDepartId = postList.stream().filter(post -> post.getDepartmentId().equals(departmentId)).collect(Collectors.toList());
Map<String, PostE> postEMap = postListByDepartId.stream().collect(Collectors.toMap(PostE::getPostName, PostE -> PostE)); Map<String, PostE> postEMap = postListByDepartId.stream().collect(Collectors.toMap(PostE::getPostName, PostE -> PostE));
userE.setPostId(postEMap.get(entity.getPostName()).getId()); userE.setPostId(postEMap.get(entity.getPostName()).getId());
userE.setPostName(entity.getPostName()); userE.setPostName(entity.getPostName());
} }
userE.setRoleId(roleMap.get(entity.getRoleName()));
// 用户名 // 用户名
userE.setUsername(entity.getUsername()); userE.setUsername(entity.getUsername());
// 姓名 // 姓名
@ -346,7 +351,7 @@ public class UserE extends BaseE {
} }
public void dataProcessingRights(Long tenantId, Map<String, Object> params) { public void dataProcessingRights(Long tenantId, Map<String, Object> params) {
if (ObjectUtils.isEmpty(params.get("allFlag")) && ObjectUtils.isEmpty(params.get("eqCorpinfoId"))){ if (ObjectUtils.isEmpty(params.get("allFlag")) && ObjectUtils.isEmpty(params.get("eqCorpinfoId"))) {
params.put("eqCorpinfoId", tenantId); params.put("eqCorpinfoId", tenantId);
} }
} }

View File

@ -5,6 +5,7 @@ import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response; import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById; import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.jjb.saas.framework.repository.repo.BaseRepository; import com.jjb.saas.framework.repository.repo.BaseRepository;
import com.jjb.saas.system.client.role.response.RoleCO;
import com.zcloud.basic.info.domain.model.CorpInfoE; import com.zcloud.basic.info.domain.model.CorpInfoE;
import com.zcloud.basic.info.domain.model.UserE; import com.zcloud.basic.info.domain.model.UserE;
import com.zcloud.basic.info.persistence.dataobject.UserDO; import com.zcloud.basic.info.persistence.dataobject.UserDO;
@ -52,5 +53,8 @@ public interface UserRepository extends BaseRepository<UserDO> {
Map<Long, String> getDepartmentLeaderByDepartmentId(List<Long> departmentIds); Map<Long, String> getDepartmentLeaderByDepartmentId(List<Long> departmentIds);
Long getDefaultRoleId(); Long getDefaultRoleId();
Map<String, Long> getRoleMap();
} }

View File

@ -255,5 +255,16 @@ public class UserRepositoryImpl extends BaseRepositoryImpl<UserMapper, UserDO> i
} }
return null; return null;
} }
@Override
public Map<String, Long> getRoleMap() {
MultiResponse<RoleCO> multiResponse = roleFacade.listRoles();
if (multiResponse.isSuccess()){
return multiResponse.getData().stream()
.collect(Collectors.toMap(RoleCO::getRoleName, RoleCO::getId));
}
return null;
}
} }