人员导入增加redis锁

yun-zuoyi
nies 4 years ago
parent faf60dd3e5
commit 082bcf994a

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

Loading…
Cancel
Save