升级用户导入:加入部门名称

This commit is contained in:
dataprince 2023-10-26 17:28:16 +08:00
parent 29d47ab642
commit 81ed861819
6 changed files with 110 additions and 69 deletions

View File

@ -36,6 +36,9 @@ public class SysUserBo extends BaseEntity {
*/ */
private Long deptId; private Long deptId;
/** 部门名称,导入时使用 */
private String deptName;
/** /**
* 用户账号 * 用户账号
*/ */

View File

@ -17,7 +17,6 @@ import java.io.Serializable;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
// @Accessors(chain = true) // 导入不允许使用 会找不到set方法
public class SysUserImportVo implements Serializable { public class SysUserImportVo implements Serializable {
@Serial @Serial
@ -29,11 +28,13 @@ public class SysUserImportVo implements Serializable {
@ExcelProperty(value = "用户序号") @ExcelProperty(value = "用户序号")
private Long userId; private Long userId;
/** /** 部门ID */
* 部门ID // @ExcelProperty(value = "部门编号")
*/ // private Long deptId;
@ExcelProperty(value = "部门编号")
private Long deptId; /** 部门名称 */
@ExcelProperty(value = "部门名称")
private String deptName;
/** /**
* 用户账号 * 用户账号

View File

@ -7,14 +7,17 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.ValidatorUtils; import com.ruoyi.common.core.utils.ValidatorUtils;
import com.ruoyi.common.excel.core.ExcelListener; import com.ruoyi.common.excel.core.ExcelListener;
import com.ruoyi.common.excel.core.ExcelResult; import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.security.utils.LoginHelper; import com.ruoyi.common.security.utils.LoginHelper;
import com.ruoyi.system.domain.bo.SysUserBo; import com.ruoyi.system.domain.bo.SysUserBo;
import com.ruoyi.system.domain.vo.SysDeptVo;
import com.ruoyi.system.domain.vo.SysUserImportVo; import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.system.domain.vo.SysUserVo; import com.ruoyi.system.domain.vo.SysUserVo;
import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService; import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -30,11 +33,13 @@ public class SysUserImportListener extends AnalysisEventListener<SysUserImportVo
private final ISysUserService userService; private final ISysUserService userService;
private final ISysDeptService deptService;
private final String password; private final String password;
private final Boolean isUpdateSupport; private final Boolean isUpdateSupport;
private final Long operUserId; private final Long operateUserId;
private int successNum = 0; private int successNum = 0;
private int failureNum = 0; private int failureNum = 0;
@ -44,21 +49,30 @@ public class SysUserImportListener extends AnalysisEventListener<SysUserImportVo
public SysUserImportListener(Boolean isUpdateSupport) { public SysUserImportListener(Boolean isUpdateSupport) {
String initPassword = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("sys.user.initPassword"); String initPassword = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("sys.user.initPassword");
this.userService = SpringUtils.getBean(ISysUserService.class); this.userService = SpringUtils.getBean(ISysUserService.class);
this.deptService = SpringUtils.getBean(ISysDeptService.class);
this.password = BCrypt.hashpw(initPassword); this.password = BCrypt.hashpw(initPassword);
this.isUpdateSupport = isUpdateSupport; this.isUpdateSupport = isUpdateSupport;
this.operUserId = LoginHelper.getUserId(); this.operateUserId = LoginHelper.getUserId();
} }
@Override @Override
public void invoke(SysUserImportVo userVo, AnalysisContext context) { public void invoke(SysUserImportVo userVo, AnalysisContext context) {
SysUserVo sysUser = this.userService.selectUserByUserName(userVo.getUserName()); SysUserVo sysUser = this.userService.selectUserByUserName(userVo.getUserName());
try { try {
if (StringUtils.isNotEmpty(userVo.getDeptName())) {
long deptCount = deptService.selectDeptCountByName(userVo.getDeptName());
if (deptCount == 1) {
SysDeptVo dept = deptService.selectDeptByName(userVo.getDeptName());
if (ObjectUtil.isNotNull(dept)) {
// 验证是否存在这个用户 // 验证是否存在这个用户
if (ObjectUtil.isNull(sysUser)) { if (ObjectUtil.isNull(sysUser)) {
//用户不存在插入
userVo.setUserId(null);//屏蔽掉前台传过来的用户ID使用系统的雪花算法值
SysUserBo user = BeanUtil.toBean(userVo, SysUserBo.class); SysUserBo user = BeanUtil.toBean(userVo, SysUserBo.class);
user.setDeptId(dept.getDeptId());//获取用户部门ID
ValidatorUtils.validate(user); ValidatorUtils.validate(user);
user.setPassword(password); user.setPassword(password);
user.setCreateBy(operUserId); user.setCreateBy(operateUserId);
userService.insertUser(user); userService.insertUser(user);
successNum++; successNum++;
successMsg.append("<br/>").append(successNum).append("、账号 ").append(user.getUserName()).append(" 导入成功"); successMsg.append("<br/>").append(successNum).append("、账号 ").append(user.getUserName()).append(" 导入成功");
@ -66,10 +80,11 @@ public class SysUserImportListener extends AnalysisEventListener<SysUserImportVo
Long userId = sysUser.getUserId(); Long userId = sysUser.getUserId();
SysUserBo user = BeanUtil.toBean(userVo, SysUserBo.class); SysUserBo user = BeanUtil.toBean(userVo, SysUserBo.class);
user.setUserId(userId); user.setUserId(userId);
user.setDeptId(dept.getDeptId());//获取用户部门ID
ValidatorUtils.validate(user); ValidatorUtils.validate(user);
userService.checkUserAllowed(user.getUserId()); userService.checkUserAllowed(user.getUserId());
userService.checkUserDataScope(user.getUserId()); userService.checkUserDataScope(user.getUserId());
user.setUpdateBy(operUserId); user.setUpdateBy(operateUserId);
userService.updateUser(user); userService.updateUser(user);
successNum++; successNum++;
successMsg.append("<br/>").append(successNum).append("、账号 ").append(user.getUserName()).append(" 更新成功"); successMsg.append("<br/>").append(successNum).append("、账号 ").append(user.getUserName()).append(" 更新成功");
@ -77,6 +92,26 @@ public class SysUserImportListener extends AnalysisEventListener<SysUserImportVo
failureNum++; failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(sysUser.getUserName()).append(" 已存在"); failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(sysUser.getUserName()).append(" 已存在");
} }
} else {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(sysUser.getUserName()).append(" 的部门名称 ").append(userVo.getDeptName()).append(" 在部门表中不存在,无法导入");
}
}
if (deptCount == 0) {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(userVo.getUserName()).append(" 的部门名称 ").append(userVo.getDeptName()).append(" 在部门表中不存在,无法导入");
}
if (deptCount > 1) {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(userVo.getUserName()).append(" 的部门名称 ").append(userVo.getDeptName()).append(" 存在两条以上部门记录,无法导入");
}
} else {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(userVo.getUserName()).append(" 的部门名称为空,无法导入");
}
} catch (Exception e) { } catch (Exception e) {
failureNum++; failureNum++;
String msg = "<br/>" + failureNum + "、账号 " + userVo.getUserName() + " 导入失败:"; String msg = "<br/>" + failureNum + "、账号 " + userVo.getUserName() + " 导入失败:";
@ -97,7 +132,7 @@ public class SysUserImportListener extends AnalysisEventListener<SysUserImportVo
@Override @Override
public String getAnalysis() { public String getAnalysis() {
if (failureNum > 0) { if (failureNum > 0) {
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据没有成功导入,错误如下:");
throw new ServiceException(failureMsg.toString()); throw new ServiceException(failureMsg.toString());
} else { } else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");

View File

@ -3,6 +3,7 @@ package com.ruoyi.system.service;
import java.util.List; import java.util.List;
import cn.hutool.core.lang.tree.Tree; import cn.hutool.core.lang.tree.Tree;
import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener;
import com.ruoyi.common.orm.core.service.IBaseService; import com.ruoyi.common.orm.core.service.IBaseService;
import com.ruoyi.system.domain.SysDept; import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.bo.SysDeptBo; import com.ruoyi.system.domain.bo.SysDeptBo;
@ -55,6 +56,20 @@ public interface ISysDeptService extends IBaseService<SysDept>
*/ */
SysDeptVo selectDeptById(Long deptId); SysDeptVo selectDeptById(Long deptId);
/**
* 根据 部门名称 查询记录数量
* @param deptName 部门名称
* @return 记录数量
*/
long selectDeptCountByName(String deptName);
/**
* 根据 部门名称 查询部门信息
* @param deptName 部门名称
* @return 部门信息
*/
SysDeptVo selectDeptByName(String deptName);
/** /**
* 根据ID查询所有子部门正常状态 * 根据ID查询所有子部门正常状态
* *

View File

@ -159,6 +159,28 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept>
return this.getOneAs(queryWrapper, SysDeptVo.class);//TODO:缺少parent_name return this.getOneAs(queryWrapper, SysDeptVo.class);//TODO:缺少parent_name
} }
/**
* 根据 部门名称 查询记录数量
* @param deptName 部门名称
* @return 记录数量
*/
@Override
public long selectDeptCountByName(String deptName) {
QueryWrapper queryWrapper = query().where(SYS_DEPT.DEL_FLAG.eq("0")).and(SYS_DEPT.DEPT_NAME.eq(deptName)).and(SYS_DEPT.STATUS.eq("0"));
return this.count(queryWrapper);
}
/**
* 根据 部门名称 查询部门信息
* @param deptName 部门名称
* @return 部门信息
*/
@Override
public SysDeptVo selectDeptByName(String deptName) {
QueryWrapper queryWrapper = query().where(SYS_DEPT.DEL_FLAG.eq("0")).and(SYS_DEPT.DEPT_NAME.eq(deptName)).and(SYS_DEPT.STATUS.eq("0"));
return this.getOneAs(queryWrapper,SysDeptVo.class);
}
/** /**
* 根据ID查询所有子部门正常状态 * 根据ID查询所有子部门正常状态
* *
@ -343,42 +365,6 @@ public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept>
return this.updateById(sysDept); return this.updateById(sysDept);
} }
// /**
// * 递归列表
// */
// private void recursionFn(List<SysDept> list, SysDept t) {
// // 得到子节点列表
// List<SysDept> childList = getChildList(list, t);
// t.setChildren(childList);
// for (SysDept tChild : childList) {
// if (hasChild(list, tChild)) {
// recursionFn(list, tChild);
// }
// }
// }
//
// /**
// * 得到子节点列表
// */
// private List<SysDept> getChildList(List<SysDept> list, SysDept t) {
// List<SysDept> tlist = new ArrayList<>();
// Iterator<SysDept> it = list.iterator();
// while (it.hasNext()) {
// SysDept n = (SysDept) it.next();
// if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) {
// tlist.add(n);
// }
// }
// return tlist;
// }
//
// /**
// * 判断是否有子节点
// */
// private boolean hasChild(List<SysDept> list, SysDept t) {
// return getChildList(list, t).size() > 0;
// }
/** /**
* 通过部门ID查询部门名称 * 通过部门ID查询部门名称
* *

View File

@ -21,7 +21,6 @@ import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.security.utils.LoginHelper; import com.ruoyi.common.security.utils.LoginHelper;
import com.ruoyi.system.domain.*; import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.bo.SysUserBo; import com.ruoyi.system.domain.bo.SysUserBo;
import com.ruoyi.system.domain.vo.SysDeptVo;
import com.ruoyi.system.domain.vo.SysPostVo; import com.ruoyi.system.domain.vo.SysPostVo;
import com.ruoyi.system.domain.vo.SysRoleVo; import com.ruoyi.system.domain.vo.SysRoleVo;
import com.ruoyi.system.domain.vo.SysUserVo; import com.ruoyi.system.domain.vo.SysUserVo;
@ -595,8 +594,10 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
* @param user 用户对象 * @param user 用户对象
*/ */
public void insertUserRole(SysUser user) { public void insertUserRole(SysUser user) {
if (ObjectUtil.isNotEmpty(user.getUserId()) && ObjectUtil.isNotEmpty(user.getRoleIds())) {
userRoleService.insertUserRoles(user.getUserId(),user.getRoleIds()); userRoleService.insertUserRoles(user.getUserId(),user.getRoleIds());
} }
}
/** /**
* 新增用户岗位信息 * 新增用户岗位信息
@ -606,7 +607,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
public boolean insertUserPost(SysUser user) { public boolean insertUserPost(SysUser user) {
boolean inserted = true; boolean inserted = true;
Long[] posts = user.getPostIds(); Long[] posts = user.getPostIds();
if (StringUtils.isNotEmpty(posts)) { if (ObjectUtil.isNotEmpty(posts)) {
// 新增用户与岗位管理 // 新增用户与岗位管理
List<SysUserPost> list = new ArrayList<>(posts.length); List<SysUserPost> list = new ArrayList<>(posts.length);
for (Long postId : posts) { for (Long postId : posts) {