dev:导入接口
parent
3299a56368
commit
6384f0cc5c
|
|
@ -118,8 +118,15 @@ public class UserController {
|
||||||
|
|
||||||
@ApiOperation("导入用户列表")
|
@ApiOperation("导入用户列表")
|
||||||
@PostMapping("/importUserTable")
|
@PostMapping("/importUserTable")
|
||||||
public Response importUserTable(@RequestPart(value = "file") MultipartFile file, @RequestPart(value = "corpFlag") Integer corpFlag) {
|
public Response importUserTable(@RequestPart(value = "file") MultipartFile file) {
|
||||||
userService.importUserTable(file, corpFlag);
|
userService.importUserTable(file, 1);
|
||||||
|
return SingleResponse.buildSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation("导入用户列表")
|
||||||
|
@PostMapping("/importUserTableByCorp2")
|
||||||
|
public Response importUserTableByCorp2(@RequestPart(value = "file") MultipartFile file) {
|
||||||
|
userService.importUserTable(file, 2);
|
||||||
return SingleResponse.buildSuccess();
|
return SingleResponse.buildSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,10 @@ import com.alibaba.cloud.commons.lang.StringUtils;
|
||||||
import com.alibaba.cola.dto.MultiResponse;
|
import com.alibaba.cola.dto.MultiResponse;
|
||||||
import com.alibaba.cola.exception.BizException;
|
import com.alibaba.cola.exception.BizException;
|
||||||
import com.jjb.saas.framework.auth.utils.AuthContext;
|
import com.jjb.saas.framework.auth.utils.AuthContext;
|
||||||
|
import com.jjb.saas.system.client.dept.facade.DeptFacade;
|
||||||
import com.jjb.saas.system.client.role.facade.RoleFacade;
|
import com.jjb.saas.system.client.role.facade.RoleFacade;
|
||||||
import com.jjb.saas.system.client.role.response.RoleCO;
|
import com.jjb.saas.system.client.role.response.RoleCO;
|
||||||
|
import com.jjb.saas.system.client.user.facade.UserFacade;
|
||||||
import com.zcloud.basic.info.domain.gateway.DepartmentGateway;
|
import com.zcloud.basic.info.domain.gateway.DepartmentGateway;
|
||||||
import com.zcloud.basic.info.domain.gateway.PostGateway;
|
import com.zcloud.basic.info.domain.gateway.PostGateway;
|
||||||
import com.zcloud.basic.info.domain.gateway.UserEmploymentLogGateway;
|
import com.zcloud.basic.info.domain.gateway.UserEmploymentLogGateway;
|
||||||
|
|
@ -60,6 +62,10 @@ public class UserImportExe {
|
||||||
private final DepartmentGateway departmentGateway;
|
private final DepartmentGateway departmentGateway;
|
||||||
@DubboReference
|
@DubboReference
|
||||||
private RoleFacade roleFacade;
|
private RoleFacade roleFacade;
|
||||||
|
@DubboReference
|
||||||
|
private DeptFacade deptFacade;
|
||||||
|
@DubboReference
|
||||||
|
private UserFacade userFacade;
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void importUserTable(MultipartFile file, Integer corpFlag) {
|
public void importUserTable(MultipartFile file, Integer corpFlag) {
|
||||||
|
|
@ -76,10 +82,12 @@ public class UserImportExe {
|
||||||
}
|
}
|
||||||
// 部门数据
|
// 部门数据
|
||||||
List<DepartmentDO> departmentDOList = departmentRepository.listByCorpInfoId(corpinfoId);
|
List<DepartmentDO> departmentDOList = departmentRepository.listByCorpInfoId(corpinfoId);
|
||||||
|
List<DepartmentDO> departmentDOTempList = new ArrayList<>();
|
||||||
// 岗位数据
|
// 岗位数据
|
||||||
List<PostDO> postDOList = postRepository.listByCorpInfoId(corpinfoId);
|
List<PostDO> postDOList = postRepository.listByCorpInfoId(corpinfoId);
|
||||||
// 用户名数据
|
// 用户名数据
|
||||||
List<UserDO> userDOList = userRepository.listByCorpInfoId(corpinfoId);
|
List<UserDO> userDOList = userRepository.listByCorpInfoId(corpinfoId);
|
||||||
|
List<UserDO> userDOTempList = new ArrayList<>();
|
||||||
|
|
||||||
List<UserExcelImportEntity> list = new ArrayList<>();
|
List<UserExcelImportEntity> list = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
|
|
@ -94,8 +102,19 @@ public class UserImportExe {
|
||||||
// 开始处理导入逻辑
|
// 开始处理导入逻辑
|
||||||
for (UserExcelImportEntity importEntity : list) {
|
for (UserExcelImportEntity importEntity : list) {
|
||||||
try {
|
try {
|
||||||
processSingleUser(importEntity, corpFlag, corpinfoId, corpInfoDO, departmentDOList, postDOList, userDOList, roleCOMultiResponse.getData());
|
processSingleUser(importEntity, corpFlag, corpinfoId, corpInfoDO, departmentDOList, departmentDOTempList, postDOList, userDOList, userDOTempList, roleCOMultiResponse.getData());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
if(CollUtil.isNotEmpty(departmentDOTempList)){
|
||||||
|
// 删除底座创建的部门
|
||||||
|
for (DepartmentDO departmentDO : departmentDOTempList) {
|
||||||
|
deptFacade.deleteById(departmentDO.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(CollUtil.isNotEmpty(userDOTempList)){
|
||||||
|
for (UserDO userDO : userDOTempList) {
|
||||||
|
userFacade.delete(userDO.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
throw new BizException("导入该用户失败: " + importEntity.getUsername() + ", 原因: " + e.getMessage());
|
throw new BizException("导入该用户失败: " + importEntity.getUsername() + ", 原因: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -107,11 +126,15 @@ public class UserImportExe {
|
||||||
private void processSingleUser(UserExcelImportEntity importEntity, Integer corpFlag,
|
private void processSingleUser(UserExcelImportEntity importEntity, Integer corpFlag,
|
||||||
Long corpinfoId,
|
Long corpinfoId,
|
||||||
CorpInfoDO corpInfoDO,
|
CorpInfoDO corpInfoDO,
|
||||||
List<DepartmentDO> departmentDOList, List<PostDO> postDOList,
|
List<DepartmentDO> departmentDOList,
|
||||||
List<UserDO> userDOList, List<RoleCO> roleList) {
|
List<DepartmentDO> departmentDOTempList,
|
||||||
|
List<PostDO> postDOList,
|
||||||
|
List<UserDO> userDOList,
|
||||||
|
List<UserDO> userDOTempList,
|
||||||
|
List<RoleCO> roleList) {
|
||||||
|
|
||||||
// 1. 处理部门层级,获取最终部门ID
|
// 1. 处理部门层级,获取最终部门ID
|
||||||
Long departmentId = processDepartmentHierarchy(importEntity, corpinfoId, departmentDOList);
|
Long departmentId = processDepartmentHierarchy(importEntity, corpinfoId, departmentDOList, departmentDOTempList);
|
||||||
|
|
||||||
// 2. 处理岗位,获取岗位ID
|
// 2. 处理岗位,获取岗位ID
|
||||||
Long postId = processPost(importEntity, corpinfoId, corpInfoDO, departmentId, postDOList);
|
Long postId = processPost(importEntity, corpinfoId, corpInfoDO, departmentId, postDOList);
|
||||||
|
|
@ -120,14 +143,15 @@ public class UserImportExe {
|
||||||
Long roleId = processRoleInfo(importEntity, roleList);
|
Long roleId = processRoleInfo(importEntity, roleList);
|
||||||
|
|
||||||
// 4. 处理用户信息
|
// 4. 处理用户信息
|
||||||
processUserInfo(importEntity, corpFlag, corpinfoId, corpInfoDO, departmentId, postId, userDOList, roleId);
|
processUserInfo(importEntity, corpFlag, corpinfoId, corpInfoDO, departmentId, postId, userDOList, userDOTempList, roleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理部门层级结构
|
* 处理部门层级结构
|
||||||
*/
|
*/
|
||||||
private Long processDepartmentHierarchy(UserExcelImportEntity importEntity, Long corpinfoId,
|
private Long processDepartmentHierarchy(UserExcelImportEntity importEntity, Long corpinfoId,
|
||||||
List<DepartmentDO> allDepartments) {
|
List<DepartmentDO> allDepartments,
|
||||||
|
List<DepartmentDO> departmentDOTempList) {
|
||||||
|
|
||||||
if(StringUtils.isEmpty(importEntity.getDepartmentName1())
|
if(StringUtils.isEmpty(importEntity.getDepartmentName1())
|
||||||
&& StringUtils.isEmpty(importEntity.getDepartmentName2())
|
&& StringUtils.isEmpty(importEntity.getDepartmentName2())
|
||||||
|
|
@ -175,7 +199,7 @@ public class UserImportExe {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找或创建部门(从二级部门开始)
|
// 查找或创建部门(从二级部门开始)
|
||||||
currentDept = findOrCreateDepartment(deptName, currentParentId, level + 1, corpinfoId, allDepartments);
|
currentDept = findOrCreateDepartment(deptName, currentParentId, level + 1, corpinfoId, allDepartments, departmentDOTempList);
|
||||||
|
|
||||||
if (currentDept == null) {
|
if (currentDept == null) {
|
||||||
throw new BizException("创建部门失败: " + deptName + " (层级: " + (level + 1) + ")");
|
throw new BizException("创建部门失败: " + deptName + " (层级: " + (level + 1) + ")");
|
||||||
|
|
@ -213,7 +237,7 @@ public class UserImportExe {
|
||||||
* 查找或创建部门(从二级部门开始使用)
|
* 查找或创建部门(从二级部门开始使用)
|
||||||
*/
|
*/
|
||||||
private DepartmentDO findOrCreateDepartment(String deptName, Long parentId, int level,
|
private DepartmentDO findOrCreateDepartment(String deptName, Long parentId, int level,
|
||||||
Long corpinfoId, List<DepartmentDO> allDepartments) {
|
Long corpinfoId, List<DepartmentDO> allDepartments,List<DepartmentDO> departmentDOTempList) {
|
||||||
// 在现有部门中查找
|
// 在现有部门中查找
|
||||||
DepartmentDO existingDept = findDepartmentInList(allDepartments, parentId, deptName);
|
DepartmentDO existingDept = findDepartmentInList(allDepartments, parentId, deptName);
|
||||||
if (existingDept != null) {
|
if (existingDept != null) {
|
||||||
|
|
@ -225,6 +249,7 @@ public class UserImportExe {
|
||||||
|
|
||||||
// 将新部门添加到列表中
|
// 将新部门添加到列表中
|
||||||
allDepartments.add(newDept);
|
allDepartments.add(newDept);
|
||||||
|
departmentDOTempList.add(newDept);
|
||||||
|
|
||||||
return newDept;
|
return newDept;
|
||||||
}
|
}
|
||||||
|
|
@ -338,7 +363,9 @@ public class UserImportExe {
|
||||||
private void processUserInfo(UserExcelImportEntity importEntity, Integer corpFlag,
|
private void processUserInfo(UserExcelImportEntity importEntity, Integer corpFlag,
|
||||||
Long corpinfoId,
|
Long corpinfoId,
|
||||||
CorpInfoDO corpInfoDO,
|
CorpInfoDO corpInfoDO,
|
||||||
Long departmentId, Long postId, List<UserDO> allUsers,
|
Long departmentId, Long postId,
|
||||||
|
List<UserDO> allUsers,
|
||||||
|
List<UserDO> userDOTempList,
|
||||||
Long roleId) {
|
Long roleId) {
|
||||||
// 检查用户名是否已存在
|
// 检查用户名是否已存在
|
||||||
boolean usernameExists = allUsers.stream()
|
boolean usernameExists = allUsers.stream()
|
||||||
|
|
@ -374,6 +401,7 @@ public class UserImportExe {
|
||||||
UserDO userDO = new UserDO();
|
UserDO userDO = new UserDO();
|
||||||
BeanUtils.copyProperties(user, userDO);
|
BeanUtils.copyProperties(user, userDO);
|
||||||
allUsers.add(userDO);
|
allUsers.add(userDO);
|
||||||
|
userDOTempList.add(userDO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue