debug,用户导入

This commit is contained in:
磷叶 2025-03-12 14:32:43 +08:00
parent 431a718d2e
commit 30a9545e0e

View File

@ -1,5 +1,25 @@
package com.ruoyi.system.service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.auth.wx.WxAuthService;
@ -38,22 +58,6 @@ import com.ruoyi.yh.logImport.domain.enums.LogImportType;
import com.ruoyi.yh.logImport.service.LogImportService;
import com.ruoyi.yh.logImportDetail.domain.enums.LogImportDetailType;
import com.ruoyi.yh.logImportDetail.service.ILogImportDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* 用户 业务层处理
@ -562,73 +566,91 @@ public class SysUserServiceImpl implements ISysUserService
// 获取Security上下文
Authentication auth = SecurityUtils.getAuthentication();
// 保存当前请求上下文如果存在
ServletRequestAttributes requestAttributes = null;
try {
requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
} catch (Exception e) {
log.debug("无法获取当前请求上下文", e);
}
final ServletRequestAttributes finalRequestAttributes = requestAttributes;
// 开始异步导入
scheduledExecutorService.schedule(() -> {
// 设置当前Security上下文
SecurityUtils.setAuthentication(auth);
// 查询部门列表
List<SysDept> deptList = deptService.selectDeptListByNames(CollectionUtils.map(userList, SysUserVO::getDeptName));
SysDept defaultDept = new SysDept();
// 查询角色列表
List<SysRoleVO> roleList = roleService.selectListByNames(CollectionUtils.map(userList, SysUserVO::getRoleName));
SysRoleVO defaultRole = new SysRoleVO();
for (SysUserVO user : userList) {
try {
// 通过手机号验证是否存在这个用户
SysUser old = this.selectUserByUserNo(user.getUserNo());
transactionTemplate.execute(status -> {
// 关联部门
user.setDeptId(deptList.stream().filter(d -> Objects.equals(d.getDeptName(), user.getDeptName())).findFirst().orElse(defaultDept).getDeptId());
// 关联角色
Long roleId = roleList.stream().filter(r -> Objects.equals(r.getRoleName(), user.getRoleName())).findFirst().orElse(defaultRole).getRoleId();
if (roleId != null) {
user.setRoleIds(new Long[]{roleId});
}
if (StringUtils.isNull(old)) {
BeanValidators.validateWithException(validator, ValidGroup.Create.class);
deptService.checkDeptDataScope(user.getDeptId());
String password = configService.selectConfigByKey("sys.user.initPassword");
user.setPassword(SecurityUtils.encryptPassword(password));
user.setCreateBy(operator.getNickName());
int insert = this.insertUser(user);
ServiceUtil.assertion(insert != 1, "新增用户失败");
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.ADD, user, null, user.getUserId());
return insert;
}
else if (isUpdateSupport) {
BeanValidators.validateWithException(validator, ValidGroup.Update.class);
checkUserAllowed(old);
user.setUserId(old.getUserId());
user.setUpdateBy(operator.getNickName());
int update = this.updateUser(user);
ServiceUtil.assertion(update != 1, "修改用户失败");
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.UPDATE, user, null, user.getUserId());
return update;
}
else {
String msg = "用户工号为 " + user.getUserNo() + " 的数据已存在";
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.FAIL, user, msg, old.getUserId());
return 0;
}
});
} catch (Exception e) {
String msg = "用户工号为 " + user.getUserNo() + " 的数据导入失败:" + e.getMessage();
log.error(msg, e);
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.FAIL, user, msg, null);
try {
// 设置当前Security上下文
SecurityUtils.setAuthentication(auth);
// 恢复请求上下文如果之前保存了
if (finalRequestAttributes != null) {
RequestContextHolder.setRequestAttributes(finalRequestAttributes, true);
}
// 查询部门列表
List<SysDept> deptList = deptService.selectDeptListByNames(CollectionUtils.map(userList, SysUserVO::getDeptName));
SysDept defaultDept = new SysDept();
// 查询角色列表
List<SysRoleVO> roleList = roleService.selectListByNames(CollectionUtils.map(userList, SysUserVO::getRoleName));
SysRoleVO defaultRole = new SysRoleVO();
for (SysUserVO user : userList) {
try {
// 通过手机号验证是否存在这个用户
SysUser old = this.selectUserByUserNo(user.getUserNo());
transactionTemplate.execute(status -> {
// 关联部门
user.setDeptId(deptList.stream().filter(d -> Objects.equals(d.getDeptName(), user.getDeptName())).findFirst().orElse(defaultDept).getDeptId());
// 关联角色
Long roleId = roleList.stream().filter(r -> Objects.equals(r.getRoleName(), user.getRoleName())).findFirst().orElse(defaultRole).getRoleId();
if (roleId != null) {
user.setRoleIds(new Long[]{roleId});
}
if (StringUtils.isNull(old)) {
BeanValidators.validateWithException(validator, ValidGroup.Create.class);
deptService.checkDeptDataScope(user.getDeptId());
String password = configService.selectConfigByKey("sys.user.initPassword");
user.setPassword(SecurityUtils.encryptPassword(password));
user.setCreateBy(operator.getNickName());
int insert = this.insertUser(user);
ServiceUtil.assertion(insert != 1, "新增用户失败");
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.ADD, user, null, user.getUserId());
return insert;
}
else if (isUpdateSupport) {
BeanValidators.validateWithException(validator, ValidGroup.Update.class);
checkUserAllowed(old);
user.setUserId(old.getUserId());
user.setUpdateBy(operator.getNickName());
int update = this.updateUser(user);
ServiceUtil.assertion(update != 1, "修改用户失败");
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.UPDATE, user, null, user.getUserId());
return update;
}
else {
String msg = "用户工号为 " + user.getUserNo() + " 的数据已存在";
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.FAIL, user, msg, old.getUserId());
return 0;
}
});
} catch (Exception e) {
String msg = "用户工号为 " + user.getUserNo() + " 的数据导入失败:" + e.getMessage();
log.error(msg, e);
logImportDetailService.addResult(importLog.getLogId(), LogImportDetailType.FAIL, user, msg, null);
}
}
// 修改导入状态为处理完成
logImportService.updateLogToFinished(importLog.getLogId());
} finally {
// 清理请求上下文避免内存泄漏
RequestContextHolder.resetRequestAttributes();
}
// 修改导入状态为处理完成
logImportService.updateLogToFinished(importLog.getLogId());
}, 0, TimeUnit.SECONDS);
return userList.size();
}