用户批量导入

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("新增用户")
@PostMapping("/save")
public SingleResponse<UserCO> add(@Validated @RequestBody UserAddCmd cmd) {
SSOUser ssoUser = AuthContext.getCurrentUser();
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.framework.auth.model.SSOUser;
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.domain.gateway.CorpInfoGateway;
import com.zcloud.basic.info.domain.gateway.UserEmploymentLogGateway;
@ -129,6 +130,9 @@ public class UserAddExe {
UserE userE = new UserE();
Long corpinfoId = AuthContext.getTenantId();
Map<String, Long> roleMap = userRepository.getRoleMap();
CorpInfoDO corpInfoDO = corpInfoRepository.getById(corpinfoId);
// 部门数据
List<DepartmentDO> departmentDOList = departmentRepository.listByCorpInfoId(corpinfoId);
@ -142,11 +146,11 @@ public class UserAddExe {
if(CollectionUtil.isNotEmpty(confDictCOList)){
dictMap = confDictCOList.stream().collect(Collectors.toMap(ConfDictTreeCO::getDictLabel, ConfDictTreeCO::getDictValue));
}
List<UserE> userEList = userE.parseImportTemplateData(file,
BeanUtil.copyToList(departmentDOList, DepartmentE.class),
BeanUtil.copyToList(postDOList, PostE.class),
BeanUtil.copyToList(userDOList, UserE.class),
roleMap,
dictMap);
// 批量插入用户

View File

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

View File

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

View File

@ -5,6 +5,7 @@ import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
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.UserE;
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);
Long getDefaultRoleId();
Map<String, Long> getRoleMap();
}

View File

@ -255,5 +255,16 @@ public class UserRepositoryImpl extends BaseRepositoryImpl<UserMapper, UserDO> i
}
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;
}
}