diff --git a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysUserInfoController.java b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysUserInfoController.java index 6b5dcaf..c87bd39 100644 --- a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysUserInfoController.java +++ b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysUserInfoController.java @@ -31,6 +31,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiOperation; import org.apache.commons.io.FileUtils; +import org.redisson.api.RLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -48,6 +49,7 @@ import java.io.*; import java.net.URLEncoder; import java.nio.file.attribute.PosixFileAttributes; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; @@ -520,83 +522,174 @@ public class SysUserInfoController extends CoreBaseController { @PostMapping("/import") @ApiOperation(value = "导用入户信息", notes = "导入用户信息") public ResultBean importUserInfo(@RequestParam MultipartFile file) { - ExcelTool excelTool = new ExcelTool(entityManager, redisRes); - + String lockKey = "REDISLOCK:IMPP:IMPORT_USER:" + AuthUtil.getSessionUser().getUserId(); + RLock rLock = (RLock) redisRes.getLock(lockKey); try { - //1.读取数据 - List importModelList = excelTool.importDataAndConver(file.getOriginalFilename(), file.getInputStream(), UserInfoImportModel.class, UserInfoImportModel.class.getSimpleName()); - List faildList = new ArrayList<>(); - LOGGER.debug("导入用户信息: 共读取数据{}条", importModelList.size()); - if (ObjectUtils.isEmpty(importModelList)) { - return ResultBean.success(); - } - //2.数据格式校验并去重 - List userInfoList = validateAndDistinc(importModelList, faildList); - - //3.某些数据字段转换 - SysPosition positionBean = new SysPosition(); - String organizationCode = AuthUtil.getOrganizeCode(); - positionBean.setOrganizeCode(organizationCode); - List positionList = positionService.findAllByBaseBean(positionBean); - - Map positionMap = positionList.stream().collect(Collectors.toMap(position -> position.getPositionCode(), position -> String.valueOf(position.getId()), (oldKey, newkey) -> oldKey)); - - SysDepartment departmentBean = new SysDepartment(); - departmentBean.setOrganizeCode(organizationCode); - List departmentList = departmentService.findAllByBaseBean(departmentBean); - //部门树 - List departmentTree = new ArrayList<>(); - //部门id的映射 - Map parentMap = departmentList.stream().collect(Collectors.toMap(el -> el.getId(), Function.identity())); - //构造全部部门的部门树 - departmentList.forEach(el -> { - if (!ObjectUtils.isEmpty(el.getParentId())) { - if (CommonEnumUtil.PARENT.DEFAULT.getValue().longValue() == el.getParentId().longValue()) { - departmentTree.add(el); - } else { - SysDepartment parentSys = parentMap.get(el.getParentId()); - if (!ObjectUtils.isEmpty(parentSys)) { - if (null == parentSys.getChildList()) { - parentSys.setChildList(new ArrayList<>()); + if (rLock.tryLock(0, TimeUnit.MINUTES)) { + ExcelTool excelTool = new ExcelTool(entityManager, redisRes); + //1.读取数据 + List importModelList = excelTool.importDataAndConver(file.getOriginalFilename(), file.getInputStream(), UserInfoImportModel.class, UserInfoImportModel.class.getSimpleName()); + List faildList = new ArrayList<>(); + LOGGER.debug("导入用户信息: 共读取数据{}条", importModelList.size()); + if (ObjectUtils.isEmpty(importModelList)) { + return ResultBean.success(); + } + //2.数据格式校验并去重 + List userInfoList = validateAndDistinc(importModelList, faildList); + + //3.某些数据字段转换 + SysPosition positionBean = new SysPosition(); + String organizationCode = AuthUtil.getOrganizeCode(); + positionBean.setOrganizeCode(organizationCode); + List positionList = positionService.findAllByBaseBean(positionBean); + + Map positionMap = positionList.stream().collect(Collectors.toMap(position -> position.getPositionCode(), position -> String.valueOf(position.getId()), (oldKey, newkey) -> oldKey)); + + SysDepartment departmentBean = new SysDepartment(); + departmentBean.setOrganizeCode(organizationCode); + List departmentList = departmentService.findAllByBaseBean(departmentBean); + //部门树 + List departmentTree = new ArrayList<>(); + //部门id的映射 + Map parentMap = departmentList.stream().collect(Collectors.toMap(el -> el.getId(), Function.identity())); + //构造全部部门的部门树 + departmentList.forEach(el -> { + if (!ObjectUtils.isEmpty(el.getParentId())) { + if (CommonEnumUtil.PARENT.DEFAULT.getValue().longValue() == el.getParentId().longValue()) { + departmentTree.add(el); + } else { + SysDepartment parentSys = parentMap.get(el.getParentId()); + if (!ObjectUtils.isEmpty(parentSys)) { + if (null == parentSys.getChildList()) { + parentSys.setChildList(new ArrayList<>()); + } + parentSys.getChildList().add(el); } - parentSys.getChildList().add(el); } } - } - }); + }); // SysRole roleBean = new SysRole(); // roleBean.setOrganizeCode(organizationCode); - List roleList = sysRoleService.findAll(); - Map roleMap = roleList.stream().collect(Collectors.toMap(sysRole -> sysRole.getName(), sysRole -> String.valueOf(sysRole.getId()), (oldKey, newkey) -> oldKey)); - - List successList = new ArrayList<>(); - //4.插入数据 - for (int i = 0; i < userInfoList.size(); i++) { - UserInfoImportModel el = userInfoList.get(i); - ResultBean resultBean = converImportData(el, departmentTree, positionMap, roleMap); - if (ObjectUtils.isEmpty(resultBean) || !resultBean.success) { - faildList.add(resultBean); - continue; - } - resultBean = personnelController.insertUserDetailModel(resultBean.getResultObject()); - if (ObjectUtils.isEmpty(resultBean) || !resultBean.success) { - faildList.add(resultBean); - continue; + List roleList = sysRoleService.findAll(); + Map roleMap = roleList.stream().collect(Collectors.toMap(sysRole -> sysRole.getName(), sysRole -> String.valueOf(sysRole.getId()), (oldKey, newkey) -> oldKey)); + + List successList = new ArrayList<>(); + //4.插入数据 + for (int i = 0; i < userInfoList.size(); i++) { + UserInfoImportModel el = userInfoList.get(i); + ResultBean resultBean = converImportData(el, departmentTree, positionMap, roleMap); + if (ObjectUtils.isEmpty(resultBean) || !resultBean.success) { + faildList.add(resultBean); + continue; + } + resultBean = personnelController.insertUserDetailModel(resultBean.getResultObject()); + if (ObjectUtils.isEmpty(resultBean) || !resultBean.success) { + faildList.add(resultBean); + continue; + } + successList.add(resultBean); } - successList.add(resultBean); - } - Map resultMap = new HashMap<>(); - resultMap.put("total", importModelList.size()); - resultMap.put("success", successList.size()); - resultMap.put("fail", faildList.size()); + Map resultMap = new HashMap<>(); + resultMap.put("total", importModelList.size()); + resultMap.put("success", successList.size()); + resultMap.put("fail", faildList.size()); // resultMap.put("successList", successList); - resultMap.put("failList", faildList); - return new ResultBean().setSuccess(true).setResultMap(resultMap).build(); + resultMap.put("failList", faildList); + return new ResultBean().setSuccess(true).setResultMap(resultMap).build(); + } + return ResultBean.fail("当前账号有正在上传的任务进行,请等待上传完毕"); + + } catch (InterruptedException e) { + LOGGER.error("导入人员数据出错", e); + return ResultBean.fail(e); } catch (ImppBusiException busExcep) { + LOGGER.error("导入人员数据出错", busExcep); return ResultBean.fail(busExcep); } catch (Exception e) { + LOGGER.error("导入人员数据出错", e); return ImppExceptionBuilder.newInstance().buildExceptionResult(e); + }finally{ + if(rLock.isHeldByCurrentThread()){ + rLock.unlock(); + } } + + +// try { +// //1.读取数据 +// List importModelList = excelTool.importDataAndConver(file.getOriginalFilename(), file.getInputStream(), UserInfoImportModel.class, UserInfoImportModel.class.getSimpleName()); +// List faildList = new ArrayList<>(); +// LOGGER.debug("导入用户信息: 共读取数据{}条", importModelList.size()); +// if (ObjectUtils.isEmpty(importModelList)) { +// return ResultBean.success(); +// } +// //2.数据格式校验并去重 +// List userInfoList = validateAndDistinc(importModelList, faildList); +// +// //3.某些数据字段转换 +// SysPosition positionBean = new SysPosition(); +// String organizationCode = AuthUtil.getOrganizeCode(); +// positionBean.setOrganizeCode(organizationCode); +// List positionList = positionService.findAllByBaseBean(positionBean); +// +// Map positionMap = positionList.stream().collect(Collectors.toMap(position -> position.getPositionCode(), position -> String.valueOf(position.getId()), (oldKey, newkey) -> oldKey)); +// +// SysDepartment departmentBean = new SysDepartment(); +// departmentBean.setOrganizeCode(organizationCode); +// List departmentList = departmentService.findAllByBaseBean(departmentBean); +// //部门树 +// List departmentTree = new ArrayList<>(); +// //部门id的映射 +// Map parentMap = departmentList.stream().collect(Collectors.toMap(el -> el.getId(), Function.identity())); +// //构造全部部门的部门树 +// departmentList.forEach(el -> { +// if (!ObjectUtils.isEmpty(el.getParentId())) { +// if (CommonEnumUtil.PARENT.DEFAULT.getValue().longValue() == el.getParentId().longValue()) { +// departmentTree.add(el); +// } else { +// SysDepartment parentSys = parentMap.get(el.getParentId()); +// if (!ObjectUtils.isEmpty(parentSys)) { +// if (null == parentSys.getChildList()) { +// parentSys.setChildList(new ArrayList<>()); +// } +// parentSys.getChildList().add(el); +// } +// } +// } +// }); +//// SysRole roleBean = new SysRole(); +//// roleBean.setOrganizeCode(organizationCode); +// List roleList = sysRoleService.findAll(); +// Map roleMap = roleList.stream().collect(Collectors.toMap(sysRole -> sysRole.getName(), sysRole -> String.valueOf(sysRole.getId()), (oldKey, newkey) -> oldKey)); +// +// List successList = new ArrayList<>(); +// //4.插入数据 +// for (int i = 0; i < userInfoList.size(); i++) { +// UserInfoImportModel el = userInfoList.get(i); +// ResultBean resultBean = converImportData(el, departmentTree, positionMap, roleMap); +// if (ObjectUtils.isEmpty(resultBean) || !resultBean.success) { +// faildList.add(resultBean); +// continue; +// } +// resultBean = personnelController.insertUserDetailModel(resultBean.getResultObject()); +// if (ObjectUtils.isEmpty(resultBean) || !resultBean.success) { +// faildList.add(resultBean); +// continue; +// } +// successList.add(resultBean); +// } +// Map resultMap = new HashMap<>(); +// resultMap.put("total", importModelList.size()); +// resultMap.put("success", successList.size()); +// resultMap.put("fail", faildList.size()); +//// resultMap.put("successList", successList); +// resultMap.put("failList", faildList); +// return new ResultBean().setSuccess(true).setResultMap(resultMap).build(); +// } catch (ImppBusiException busExcep) { +// return ResultBean.fail(busExcep); +// } catch (Exception e) { +// return ImppExceptionBuilder.newInstance().buildExceptionResult(e); +// } } @GetMapping("/find-position-ref-user") @@ -773,6 +866,7 @@ public class SysUserInfoController extends CoreBaseController { return ResultBean.success().setResultObject(userDetailModel).build(); } catch (Exception e) { + e.printStackTrace(); LOGGER.error("导入人员数据转换出错", e); return ResultBean.fail(e); } diff --git a/modules/logs/2021-09/i3core-2021-09-15-1.log.gz b/modules/logs/2021-09/i3core-2021-09-15-1.log.gz new file mode 100644 index 0000000..2c2e741 Binary files /dev/null and b/modules/logs/2021-09/i3core-2021-09-15-1.log.gz differ