Commit 6c83ceab authored by hkl's avatar hkl

feat:1.用户管理修改

parent 16ccb5e5
...@@ -63,105 +63,105 @@ import java.util.stream.Collectors; ...@@ -63,105 +63,105 @@ import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService { public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
@Autowired @Autowired
private SysUserMapper userMapper; private SysUserMapper userMapper;
@Autowired @Autowired
private SysPermissionMapper sysPermissionMapper; private SysPermissionMapper sysPermissionMapper;
@Autowired @Autowired
private SysUserRoleMapper sysUserRoleMapper; private SysUserRoleMapper sysUserRoleMapper;
@Autowired @Autowired
private SysUserDepartMapper sysUserDepartMapper; private SysUserDepartMapper sysUserDepartMapper;
@Autowired @Autowired
private SysDepartMapper sysDepartMapper; private SysDepartMapper sysDepartMapper;
@Autowired @Autowired
private SysRoleMapper sysRoleMapper; private SysRoleMapper sysRoleMapper;
@Autowired @Autowired
private SysDepartRoleUserMapper departRoleUserMapper; private SysDepartRoleUserMapper departRoleUserMapper;
@Autowired @Autowired
private SysDepartRoleMapper sysDepartRoleMapper; private SysDepartRoleMapper sysDepartRoleMapper;
@Resource @Resource
private BaseCommonService baseCommonService; private BaseCommonService baseCommonService;
@Autowired @Autowired
private SysThirdAccountMapper sysThirdAccountMapper; private SysThirdAccountMapper sysThirdAccountMapper;
@Autowired @Autowired
ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService; ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService;
@Autowired @Autowired
ThirdAppDingtalkServiceImpl dingtalkService; ThirdAppDingtalkServiceImpl dingtalkService;
@Autowired @Autowired
SysRoleIndexMapper sysRoleIndexMapper; SysRoleIndexMapper sysRoleIndexMapper;
@Autowired @Autowired
SysTenantMapper sysTenantMapper; SysTenantMapper sysTenantMapper;
@Autowired @Autowired
private SysUserTenantMapper relationMapper; private SysUserTenantMapper relationMapper;
@Autowired @Autowired
private SysUserTenantMapper userTenantMapper; private SysUserTenantMapper userTenantMapper;
@Override @Override
public Result<IPage<SysUser>> queryPageList(HttpServletRequest req, QueryWrapper<SysUser> queryWrapper, Integer pageSize, Integer pageNo) { public Result<IPage<SysUser>> queryPageList(HttpServletRequest req, QueryWrapper<SysUser> queryWrapper, Integer pageSize, Integer pageNo) {
Result<IPage<SysUser>> result = new Result<IPage<SysUser>>(); Result<IPage<SysUser>> result = new Result<IPage<SysUser>>();
String realname = req.getParameter("realname"); String realname = req.getParameter("realname");
//update-begin-Author:wangshuai--Date:20211119--for:【vue3】通过部门id查询用户,通过code查询id //update-begin-Author:wangshuai--Date:20211119--for:【vue3】通过部门id查询用户,通过code查询id
//部门ID //部门ID
String departId = req.getParameter("departId"); String departId = req.getParameter("departId");
if (oConvertUtils.isNotEmpty(departId)) { if (oConvertUtils.isNotEmpty(departId)) {
LambdaQueryWrapper<SysUserDepart> query = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUserDepart> query = new LambdaQueryWrapper<>();
query.eq(SysUserDepart::getDepId, departId); query.eq(SysUserDepart::getDepId, departId);
List<SysUserDepart> list = sysUserDepartMapper.selectList(query); List<SysUserDepart> list = sysUserDepartMapper.selectList(query);
List<String> userIds = list.stream().map(SysUserDepart::getUserId).collect(Collectors.toList()); List<String> userIds = list.stream().map(SysUserDepart::getUserId).collect(Collectors.toList());
//update-begin---author:wangshuai ---date:20220322 for:[issues/I4XTYB]查询用户时,当部门id 下没有分配用户时接口报错------------ //update-begin---author:wangshuai ---date:20220322 for:[issues/I4XTYB]查询用户时,当部门id 下没有分配用户时接口报错------------
if (oConvertUtils.listIsNotEmpty(userIds)) { if (oConvertUtils.listIsNotEmpty(userIds)) {
queryWrapper.in("id", userIds); queryWrapper.in("id", userIds);
} else { } else {
return Result.OK(); return Result.OK();
} }
//update-end---author:wangshuai ---date:20220322 for:[issues/I4XTYB]查询用户时,当部门id 下没有分配用户时接口报错------------ //update-end---author:wangshuai ---date:20220322 for:[issues/I4XTYB]查询用户时,当部门id 下没有分配用户时接口报错------------
} }
//用户ID //用户ID
String code = req.getParameter("code"); String code = req.getParameter("code");
if (oConvertUtils.isNotEmpty(code)) { if (oConvertUtils.isNotEmpty(code)) {
queryWrapper.in("id", Arrays.asList(code.split(","))); queryWrapper.in("id", Arrays.asList(code.split(",")));
pageSize = code.split(",").length; pageSize = code.split(",").length;
} }
//update-end-Author:wangshuai--Date:20211119--for:【vue3】通过部门id查询用户,通过code查询id //update-end-Author:wangshuai--Date:20211119--for:【vue3】通过部门id查询用户,通过code查询id
//update-begin-author:taoyan--date:20220104--for: JTC-372 【用户冻结问题】 online授权、用户组件,选择用户都能看到被冻结的用户 //update-begin-author:taoyan--date:20220104--for: JTC-372 【用户冻结问题】 online授权、用户组件,选择用户都能看到被冻结的用户
String status = req.getParameter("status"); String status = req.getParameter("status");
if (oConvertUtils.isNotEmpty(status)) { if (oConvertUtils.isNotEmpty(status)) {
queryWrapper.eq("status", Integer.parseInt(status)); queryWrapper.eq("status", Integer.parseInt(status));
} }
//update-end-author:taoyan--date:20220104--for: JTC-372 【用户冻结问题】 online授权、用户组件,选择用户都能看到被冻结的用户 //update-end-author:taoyan--date:20220104--for: JTC-372 【用户冻结问题】 online授权、用户组件,选择用户都能看到被冻结的用户
//TODO 外部模拟登陆临时账号,列表不显示 //TODO 外部模拟登陆临时账号,列表不显示
queryWrapper.ne("username", "_reserve_user_external"); queryWrapper.ne("username", "_reserve_user_external");
// queryWrapper.eq("del_flag",0); // queryWrapper.eq("del_flag",0);
Page<SysUser> page = new Page<SysUser>(pageNo, pageSize); Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
IPage<SysUser> pageList = this.page(page, queryWrapper); IPage<SysUser> pageList = this.page(page, queryWrapper);
//批量查询用户的所属部门 //批量查询用户的所属部门
//step.1 先拿到全部的 useids //step.1 先拿到全部的 useids
//step.2 通过 useids,一次性查询用户的所属部门名字 //step.2 通过 useids,一次性查询用户的所属部门名字
List<String> userIds = pageList.getRecords().stream().map(SysUser::getId).collect(Collectors.toList()); List<String> userIds = pageList.getRecords().stream().map(SysUser::getId).collect(Collectors.toList());
if (userIds != null && userIds.size() > 0) { if (userIds != null && userIds.size() > 0) {
Map<String, String> useDepNames = this.getDepNamesByUserIds(userIds); Map<String, String> useDepNames = this.getDepNamesByUserIds(userIds);
pageList.getRecords().forEach(item -> { pageList.getRecords().forEach(item -> {
item.setOrgCodeTxt(useDepNames.get(item.getId())); item.setOrgCodeTxt(useDepNames.get(item.getId()));
//查询用户的租户ids //查询用户的租户ids
List<Integer> list = userTenantMapper.getTenantIdsByUserId(item.getId()); List<Integer> list = userTenantMapper.getTenantIdsByUserId(item.getId());
if (oConvertUtils.isNotEmpty(list)) { if (oConvertUtils.isNotEmpty(list)) {
item.setRelTenantIds(StringUtils.join(list.toArray(), SymbolConstant.COMMA)); item.setRelTenantIds(StringUtils.join(list.toArray(), SymbolConstant.COMMA));
} else { } else {
item.setRelTenantIds(""); item.setRelTenantIds("");
} }
}); });
} }
result.setSuccess(true); result.setSuccess(true);
result.setResult(pageList); result.setResult(pageList);
result.setCode(200); result.setCode(200);
//log.info(pageList.toString()); //log.info(pageList.toString());
return result; return result;
} }
@Override @Override
...@@ -196,219 +196,223 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -196,219 +196,223 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
} }
@Override @Override
@CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean deleteUser(String userId) { public boolean deleteUser(String userId) {
//1.删除用户 //1.删除用户
this.removeById(userId); this.removeById(userId);
return false; return false;
} }
@Override @Override
@CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean deleteBatchUsers(String userIds) { public boolean deleteBatchUsers(String userIds) {
//1.删除用户 //1.删除用户
this.removeByIds(Arrays.asList(userIds.split(","))); this.removeByIds(Arrays.asList(userIds.split(",")));
return false; return false;
} }
@Override @Override
public SysUser getUserByName(String username) { public SysUser getUserByName(String username) {
SysUser sysUser = userMapper.getUserByName(username); SysUser sysUser = userMapper.getUserByName(username);
//查询用户的租户ids //查询用户的租户ids
if(sysUser!=null){ if (sysUser != null) {
List<Integer> list = userTenantMapper.getTenantIdsByUserId(sysUser.getId()); List<Integer> list = userTenantMapper.getTenantIdsByUserId(sysUser.getId());
if (oConvertUtils.isNotEmpty(list)) { if (oConvertUtils.isNotEmpty(list)) {
sysUser.setRelTenantIds(StringUtils.join(list.toArray(), SymbolConstant.COMMA)); sysUser.setRelTenantIds(StringUtils.join(list.toArray(), SymbolConstant.COMMA));
} else { } else {
sysUser.setRelTenantIds(""); sysUser.setRelTenantIds("");
} }
} }
return sysUser; return sysUser;
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void addUserWithRole(SysUser user, String roles) { public void addUserWithRole(SysUser user, String roles) {
this.save(user); this.save(user);
if(oConvertUtils.isNotEmpty(roles)) { if (oConvertUtils.isNotEmpty(roles)) {
String[] arr = roles.split(","); String[] arr = roles.split(",");
for (String roleId : arr) { for (String roleId : arr) {
SysUserRole userRole = new SysUserRole(user.getId(), roleId); SysUserRole userRole = new SysUserRole(user.getId(), roleId);
sysUserRoleMapper.insert(userRole); sysUserRoleMapper.insert(userRole);
} }
} }
} }
@Override @Override
@CacheEvict(value= {CacheConstant.SYS_USERS_CACHE}, allEntries=true) @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void editUserWithRole(SysUser user, String roles) { public void editUserWithRole(SysUser user, String roles) {
this.updateById(user); this.updateById(user);
//先删后加 //先删后加
sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, user.getId())); sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, user.getId()));
if(oConvertUtils.isNotEmpty(roles)) { if (oConvertUtils.isNotEmpty(roles)) {
String[] arr = roles.split(","); String[] arr = roles.split(",");
for (String roleId : arr) { for (String roleId : arr) {
SysUserRole userRole = new SysUserRole(user.getId(), roleId); SysUserRole userRole = new SysUserRole(user.getId(), roleId);
sysUserRoleMapper.insert(userRole); sysUserRoleMapper.insert(userRole);
} }
} }
} }
@Override @Override
public List<String> getRole(String username) { public List<String> getRole(String username) {
return sysUserRoleMapper.getRoleByUserName(username); return sysUserRoleMapper.getRoleByUserName(username);
} }
/** /**
* 获取动态首页路由配置 * 获取动态首页路由配置
* @param username *
* @param version * @param username
* @return * @param version
*/ * @return
@Override */
public SysRoleIndex getDynamicIndexByUserRole(String username,String version) { @Override
List<String> roles = sysUserRoleMapper.getRoleByUserName(username); public SysRoleIndex getDynamicIndexByUserRole(String username, String version) {
String componentUrl = RoleIndexConfigEnum.getIndexByRoles(roles); List<String> roles = sysUserRoleMapper.getRoleByUserName(username);
SysRoleIndex roleIndex = new SysRoleIndex(componentUrl); String componentUrl = RoleIndexConfigEnum.getIndexByRoles(roles);
//只有 X-Version=v3 的时候,才读取sys_role_index表获取角色首页配置 SysRoleIndex roleIndex = new SysRoleIndex(componentUrl);
if (oConvertUtils.isNotEmpty(version) && roles!=null && roles.size()>0) { //只有 X-Version=v3 的时候,才读取sys_role_index表获取角色首页配置
LambdaQueryWrapper<SysRoleIndex> routeIndexQuery = new LambdaQueryWrapper(); if (oConvertUtils.isNotEmpty(version) && roles != null && roles.size() > 0) {
//用户所有角色 LambdaQueryWrapper<SysRoleIndex> routeIndexQuery = new LambdaQueryWrapper();
routeIndexQuery.in(SysRoleIndex::getRoleCode, roles); //用户所有角色
//角色首页状态0:未开启 1:开启 routeIndexQuery.in(SysRoleIndex::getRoleCode, roles);
routeIndexQuery.eq(SysRoleIndex::getStatus, CommonConstant.STATUS_1); //角色首页状态0:未开启 1:开启
//优先级正序排序 routeIndexQuery.eq(SysRoleIndex::getStatus, CommonConstant.STATUS_1);
routeIndexQuery.orderByAsc(SysRoleIndex::getPriority); //优先级正序排序
List<SysRoleIndex> list = sysRoleIndexMapper.selectList(routeIndexQuery); routeIndexQuery.orderByAsc(SysRoleIndex::getPriority);
if (null != list && list.size() > 0) { List<SysRoleIndex> list = sysRoleIndexMapper.selectList(routeIndexQuery);
roleIndex = list.get(0); if (null != list && list.size() > 0) {
} roleIndex = list.get(0);
} }
}
//如果componentUrl为空,则返回空
if(oConvertUtils.isEmpty(roleIndex.getComponent())){ //如果componentUrl为空,则返回空
return null; if (oConvertUtils.isEmpty(roleIndex.getComponent())) {
} return null;
return roleIndex; }
} return roleIndex;
}
/**
* 通过用户名获取用户角色集合 /**
* @param username 用户名 * 通过用户名获取用户角色集合
*
* @param username 用户名
* @return 角色集合 * @return 角色集合
*/ */
@Override @Override
public Set<String> getUserRolesSet(String username) { public Set<String> getUserRolesSet(String username) {
// 查询用户拥有的角色集合 // 查询用户拥有的角色集合
List<String> roles = sysUserRoleMapper.getRoleByUserName(username); List<String> roles = sysUserRoleMapper.getRoleByUserName(username);
log.info("-------通过数据库读取用户拥有的角色Rules------username: " + username + ",Roles size: " + (roles == null ? 0 : roles.size())); log.info("-------通过数据库读取用户拥有的角色Rules------username: " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
return new HashSet<>(roles); return new HashSet<>(roles);
} }
/** /**
* 通过用户名获取用户权限集合 * 通过用户名获取用户权限集合
* *
* @param username 用户名 * @param username 用户名
* @return 权限集合 * @return 权限集合
*/ */
@Override @Override
public Set<String> getUserPermissionsSet(String username) { public Set<String> getUserPermissionsSet(String username) {
Set<String> permissionSet = new HashSet<>(); Set<String> permissionSet = new HashSet<>();
List<SysPermission> permissionList = sysPermissionMapper.queryByUser(username); List<SysPermission> permissionList = sysPermissionMapper.queryByUser(username);
for (SysPermission po : permissionList) { for (SysPermission po : permissionList) {
// // TODO URL规则有问题? // // TODO URL规则有问题?
// if (oConvertUtils.isNotEmpty(po.getUrl())) { // if (oConvertUtils.isNotEmpty(po.getUrl())) {
// permissionSet.add(po.getUrl()); // permissionSet.add(po.getUrl());
// } // }
if (oConvertUtils.isNotEmpty(po.getPerms())) { if (oConvertUtils.isNotEmpty(po.getPerms())) {
permissionSet.add(po.getPerms()); permissionSet.add(po.getPerms());
} }
} }
log.info("-------通过数据库读取用户拥有的权限Perms------username: "+ username+",Perms size: "+ (permissionSet==null?0:permissionSet.size()) ); log.info("-------通过数据库读取用户拥有的权限Perms------username: " + username + ",Perms size: " + (permissionSet == null ? 0 : permissionSet.size()));
return permissionSet; return permissionSet;
} }
/** /**
* 升级SpringBoot2.6.6,不允许循环依赖 * 升级SpringBoot2.6.6,不允许循环依赖
* @author:qinfeng *
* @update: 2022-04-07 * @param username
* @param username * @return
* @return * @author:qinfeng
*/ * @update: 2022-04-07
@Override */
public SysUserCacheInfo getCacheUser(String username) { @Override
SysUserCacheInfo info = new SysUserCacheInfo(); public SysUserCacheInfo getCacheUser(String username) {
info.setOneDepart(true); SysUserCacheInfo info = new SysUserCacheInfo();
if(oConvertUtils.isEmpty(username)) { info.setOneDepart(true);
return null; if (oConvertUtils.isEmpty(username)) {
} return null;
}
//查询用户信息
SysUser sysUser = userMapper.getUserByName(username); //查询用户信息
if(sysUser!=null) { SysUser sysUser = userMapper.getUserByName(username);
info.setSysUserCode(sysUser.getUsername()); if (sysUser != null) {
info.setSysUserName(sysUser.getRealname()); info.setSysUserCode(sysUser.getUsername());
info.setSysOrgCode(sysUser.getOrgCode()); info.setSysUserName(sysUser.getRealname());
} info.setSysOrgCode(sysUser.getOrgCode());
}
//多部门支持in查询
List<SysDepart> list = sysDepartMapper.queryUserDeparts(sysUser.getId()); //多部门支持in查询
List<String> sysMultiOrgCode = new ArrayList<String>(); List<SysDepart> list = sysDepartMapper.queryUserDeparts(sysUser.getId());
if(list==null || list.size()==0) { List<String> sysMultiOrgCode = new ArrayList<String>();
//当前用户无部门 if (list == null || list.size() == 0) {
//sysMultiOrgCode.add("0"); //当前用户无部门
}else if(list.size()==1) { //sysMultiOrgCode.add("0");
sysMultiOrgCode.add(list.get(0).getOrgCode()); } else if (list.size() == 1) {
}else { sysMultiOrgCode.add(list.get(0).getOrgCode());
info.setOneDepart(false); } else {
for (SysDepart dpt : list) { info.setOneDepart(false);
sysMultiOrgCode.add(dpt.getOrgCode()); for (SysDepart dpt : list) {
} sysMultiOrgCode.add(dpt.getOrgCode());
} }
info.setSysMultiOrgCode(sysMultiOrgCode); }
info.setSysMultiOrgCode(sysMultiOrgCode);
return info;
} return info;
}
/** /**
* 根据部门Id查询 * 根据部门Id查询
*
* @param page * @param page
* @param departId 部门id * @param departId 部门id
* @param username 用户账户名称 * @param username 用户账户名称
* @return * @return
*/ */
@Override @Override
public IPage<SysUser> getUserByDepId(Page<SysUser> page, String departId,String username) { public IPage<SysUser> getUserByDepId(Page<SysUser> page, String departId, String username) {
return userMapper.getUserByDepId(page, departId,username); return userMapper.getUserByDepId(page, departId, username);
} }
@Override @Override
public IPage<SysUser> getUserByDepIds(Page<SysUser> page, List<String> departIds, String username) { public IPage<SysUser> getUserByDepIds(Page<SysUser> page, List<String> departIds, String username) {
return userMapper.getUserByDepIds(page, departIds,username); return userMapper.getUserByDepIds(page, departIds, username);
} }
@Override @Override
public Map<String, String> getDepNamesByUserIds(List<String> userIds) { public Map<String, String> getDepNamesByUserIds(List<String> userIds) {
List<SysUserDepVo> list = this.baseMapper.getDepNamesByUserIds(userIds); List<SysUserDepVo> list = this.baseMapper.getDepNamesByUserIds(userIds);
Map<String, String> res = new HashMap(5); Map<String, String> res = new HashMap(5);
list.forEach(item -> { list.forEach(item -> {
if (res.get(item.getUserId()) == null) { if (res.get(item.getUserId()) == null) {
res.put(item.getUserId(), item.getDepartName()); res.put(item.getUserId(), item.getDepartName());
} else { } else {
res.put(item.getUserId(), res.get(item.getUserId()) + "," + item.getDepartName()); res.put(item.getUserId(), res.get(item.getUserId()) + "," + item.getDepartName());
} }
} }
); );
return res; return res;
} }
//update-begin-author:taoyan date:2022-9-13 for: VUEN-2245【漏洞】发现新漏洞待处理20220906 ----sql注入 方法没有使用,注掉 //update-begin-author:taoyan date:2022-9-13 for: VUEN-2245【漏洞】发现新漏洞待处理20220906 ----sql注入 方法没有使用,注掉
/* @Override /* @Override
public IPage<SysUser> getUserByDepartIdAndQueryWrapper(Page<SysUser> page, String departId, QueryWrapper<SysUser> queryWrapper) { public IPage<SysUser> getUserByDepartIdAndQueryWrapper(Page<SysUser> page, String departId, QueryWrapper<SysUser> queryWrapper) {
LambdaQueryWrapper<SysUser> lambdaQueryWrapper = queryWrapper.lambda(); LambdaQueryWrapper<SysUser> lambdaQueryWrapper = queryWrapper.lambda();
...@@ -418,134 +422,136 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -418,134 +422,136 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return userMapper.selectPage(page, lambdaQueryWrapper); return userMapper.selectPage(page, lambdaQueryWrapper);
}*/ }*/
//update-end-author:taoyan date:2022-9-13 for: VUEN-2245【漏洞】发现新漏洞待处理20220906 ----sql注入 方法没有使用,注掉 //update-end-author:taoyan date:2022-9-13 for: VUEN-2245【漏洞】发现新漏洞待处理20220906 ----sql注入 方法没有使用,注掉
@Override @Override
public IPage<SysUserSysDepartModel> queryUserByOrgCode(String orgCode, SysUser userParams, IPage page) { public IPage<SysUserSysDepartModel> queryUserByOrgCode(String orgCode, SysUser userParams, IPage page) {
List<SysUserSysDepartModel> list = baseMapper.getUserByOrgCode(page, orgCode, userParams); List<SysUserSysDepartModel> list = baseMapper.getUserByOrgCode(page, orgCode, userParams);
Integer total = baseMapper.getUserByOrgCodeTotal(orgCode, userParams); Integer total = baseMapper.getUserByOrgCodeTotal(orgCode, userParams);
IPage<SysUserSysDepartModel> result = new Page<>(page.getCurrent(), page.getSize(), total); IPage<SysUserSysDepartModel> result = new Page<>(page.getCurrent(), page.getSize(), total);
result.setRecords(list); result.setRecords(list);
return result; return result;
} }
/** /**
* 根据角色Id查询 * 根据角色Id查询
*
* @param page * @param page
* @param roleId 角色id * @param roleId 角色id
* @param username 用户账户名称 * @param username 用户账户名称
* @return * @return
*/ */
@Override @Override
public IPage<SysUser> getUserByRoleId(Page<SysUser> page, String roleId, String username) { public IPage<SysUser> getUserByRoleId(Page<SysUser> page, String roleId, String username) {
return userMapper.getUserByRoleId(page,roleId,username); return userMapper.getUserByRoleId(page, roleId, username);
} }
@Override @Override
@CacheEvict(value= {CacheConstant.SYS_USERS_CACHE}, key="#username") @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, key = "#username")
public void updateUserDepart(String username,String orgCode,Integer loginTenantId) { public void updateUserDepart(String username, String orgCode, Integer loginTenantId) {
baseMapper.updateUserDepart(username, orgCode,loginTenantId); baseMapper.updateUserDepart(username, orgCode, loginTenantId);
} }
@Override @Override
public SysUser getUserByPhone(String phone) { public SysUser getUserByPhone(String phone) {
return userMapper.getUserByPhone(phone); return userMapper.getUserByPhone(phone);
} }
@Override @Override
public SysUser getUserByEmail(String email) { public SysUser getUserByEmail(String email) {
return userMapper.getUserByEmail(email); return userMapper.getUserByEmail(email);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void addUserWithDepart(SysUser user, String selectedParts) { public void addUserWithDepart(SysUser user, String selectedParts) {
// this.save(user); //保存角色的时候已经添加过一次了 // this.save(user); //保存角色的时候已经添加过一次了
if(oConvertUtils.isNotEmpty(selectedParts)) { if (oConvertUtils.isNotEmpty(selectedParts)) {
String[] arr = selectedParts.split(","); String[] arr = selectedParts.split(",");
for (String deaprtId : arr) { for (String deaprtId : arr) {
SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId); SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId);
sysUserDepartMapper.insert(userDeaprt); sysUserDepartMapper.insert(userDeaprt);
} }
} }
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
public void editUserWithDepart(SysUser user, String departs) { public void editUserWithDepart(SysUser user, String departs) {
//更新角色的时候已经更新了一次了,可以再跟新一次 //更新角色的时候已经更新了一次了,可以再跟新一次
this.updateById(user); this.updateById(user);
String[] arr = {}; String[] arr = {};
if(oConvertUtils.isNotEmpty(departs)){ if (oConvertUtils.isNotEmpty(departs)) {
arr = departs.split(","); arr = departs.split(",");
} }
//查询已关联部门 //查询已关联部门
List<SysUserDepart> userDepartList = sysUserDepartMapper.selectList(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId())); List<SysUserDepart> userDepartList = sysUserDepartMapper.selectList(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId()));
if(userDepartList != null && userDepartList.size()>0){ if (userDepartList != null && userDepartList.size() > 0) {
for(SysUserDepart depart : userDepartList ){ for (SysUserDepart depart : userDepartList) {
//修改已关联部门删除部门用户角色关系 //修改已关联部门删除部门用户角色关系
if(!Arrays.asList(arr).contains(depart.getDepId())){ if (!Arrays.asList(arr).contains(depart.getDepId())) {
List<SysDepartRole> sysDepartRoleList = sysDepartRoleMapper.selectList( List<SysDepartRole> sysDepartRoleList = sysDepartRoleMapper.selectList(
new QueryWrapper<SysDepartRole>().lambda().eq(SysDepartRole::getDepartId,depart.getDepId())); new QueryWrapper<SysDepartRole>().lambda().eq(SysDepartRole::getDepartId, depart.getDepId()));
List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList()); List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList());
if(roleIds != null && roleIds.size()>0){ if (roleIds != null && roleIds.size() > 0) {
departRoleUserMapper.delete(new QueryWrapper<SysDepartRoleUser>().lambda().eq(SysDepartRoleUser::getUserId, user.getId()) departRoleUserMapper.delete(new QueryWrapper<SysDepartRoleUser>().lambda().eq(SysDepartRoleUser::getUserId, user.getId())
.in(SysDepartRoleUser::getDroleId,roleIds)); .in(SysDepartRoleUser::getDroleId, roleIds));
} }
} }
} }
} }
//先删后加 //先删后加
sysUserDepartMapper.delete(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId())); sysUserDepartMapper.delete(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId()));
if(oConvertUtils.isNotEmpty(departs)) { if (oConvertUtils.isNotEmpty(departs)) {
for (String departId : arr) { for (String departId : arr) {
SysUserDepart userDepart = new SysUserDepart(user.getId(), departId); SysUserDepart userDepart = new SysUserDepart(user.getId(), departId);
sysUserDepartMapper.insert(userDepart); sysUserDepartMapper.insert(userDepart);
} }
} }
} }
/** /**
* 校验用户是否有效 * 校验用户是否有效
* @param sysUser *
* @return * @param sysUser
*/ * @return
@Override */
public Result<?> checkUserIsEffective(SysUser sysUser) { @Override
Result<?> result = new Result<Object>(); public Result<?> checkUserIsEffective(SysUser sysUser) {
//情况1:根据用户信息查询,该用户不存在 Result<?> result = new Result<Object>();
if (sysUser == null) { //情况1:根据用户信息查询,该用户不存在
result.error500("该用户不存在,请注册"); if (sysUser == null) {
baseCommonService.addLog("用户登录失败,用户不存在!", CommonConstant.LOG_TYPE_1, null); result.error500("该用户不存在");
return result; baseCommonService.addLog("用户登录失败,用户不存在!", CommonConstant.LOG_TYPE_1, null);
} return result;
//情况2:根据用户信息查询,该用户已注销 }
//update-begin---author:王帅 Date:20200601 for:if条件永远为falsebug------------ //情况2:根据用户信息查询,该用户已注销
if (CommonConstant.DEL_FLAG_1.equals(sysUser.getDelFlag())) { //update-begin---author:王帅 Date:20200601 for:if条件永远为falsebug------------
//update-end---author:王帅 Date:20200601 for:if条件永远为falsebug------------ if (CommonConstant.DEL_FLAG_1.equals(sysUser.getDelFlag())) {
baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已注销!", CommonConstant.LOG_TYPE_1, null); //update-end---author:王帅 Date:20200601 for:if条件永远为falsebug------------
result.error500("该用户已注销"); baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已注销!", CommonConstant.LOG_TYPE_1, null);
return result; result.error500("该用户已注销");
} return result;
//情况3:根据用户信息查询,该用户已冻结 }
if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) { //情况3:根据用户信息查询,该用户已冻结
baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已冻结!", CommonConstant.LOG_TYPE_1, null); if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) {
result.error500("该用户已冻结"); baseCommonService.addLog("用户登录失败,用户名:" + sysUser.getUsername() + "已冻结!", CommonConstant.LOG_TYPE_1, null);
return result; result.error500("该用户已冻结");
} return result;
return result; }
} return result;
}
@Override
public List<SysUser> queryLogicDeleted() { @Override
public List<SysUser> queryLogicDeleted() {
//update-begin---author:wangshuai ---date:20221116 for:回收站查询未离职的------------ //update-begin---author:wangshuai ---date:20221116 for:回收站查询未离职的------------
LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
wrapper.ne(SysUser::getStatus, CommonConstant.USER_QUIT); wrapper.ne(SysUser::getStatus, CommonConstant.USER_QUIT);
...@@ -553,45 +559,45 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -553,45 +559,45 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
//update-end---author:wangshuai ---date:20221116 for:回收站查询未离职的-------------- //update-end---author:wangshuai ---date:20221116 for:回收站查询未离职的--------------
} }
@Override @Override
public List<SysUser> queryLogicDeleted(LambdaQueryWrapper<SysUser> wrapper) { public List<SysUser> queryLogicDeleted(LambdaQueryWrapper<SysUser> wrapper) {
if (wrapper == null) { if (wrapper == null) {
wrapper = new LambdaQueryWrapper<>(); wrapper = new LambdaQueryWrapper<>();
} }
wrapper.eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_1); wrapper.eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_1);
return userMapper.selectLogicDeleted(wrapper); return userMapper.selectLogicDeleted(wrapper);
} }
@Override @Override
@CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
public boolean revertLogicDeleted(List<String> userIds, SysUser updateEntity) { public boolean revertLogicDeleted(List<String> userIds, SysUser updateEntity) {
return userMapper.revertLogicDeleted(userIds, updateEntity) > 0; return userMapper.revertLogicDeleted(userIds, updateEntity) > 0;
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean removeLogicDeleted(List<String> userIds) { public boolean removeLogicDeleted(List<String> userIds) {
// 1. 删除用户 // 1. 删除用户
int line = userMapper.deleteLogicDeleted(userIds); int line = userMapper.deleteLogicDeleted(userIds);
// 2. 删除用户部门关系 // 2. 删除用户部门关系
line += sysUserDepartMapper.delete(new LambdaQueryWrapper<SysUserDepart>().in(SysUserDepart::getUserId, userIds)); line += sysUserDepartMapper.delete(new LambdaQueryWrapper<SysUserDepart>().in(SysUserDepart::getUserId, userIds));
//3. 删除用户角色关系 //3. 删除用户角色关系
line += sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, userIds)); line += sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, userIds));
//4.同步删除第三方App的用户 //4.同步删除第三方App的用户
try { try {
dingtalkService.removeThirdAppUser(userIds); dingtalkService.removeThirdAppUser(userIds);
wechatEnterpriseService.removeThirdAppUser(userIds); wechatEnterpriseService.removeThirdAppUser(userIds);
} catch (Exception e) { } catch (Exception e) {
log.error("同步删除第三方App的用户失败:", e); log.error("同步删除第三方App的用户失败:", e);
} }
//5. 删除第三方用户表(因为第4步需要用到第三方用户表,所以在他之后删) //5. 删除第三方用户表(因为第4步需要用到第三方用户表,所以在他之后删)
line += sysThirdAccountMapper.delete(new LambdaQueryWrapper<SysThirdAccount>().in(SysThirdAccount::getSysUserId, userIds)); line += sysThirdAccountMapper.delete(new LambdaQueryWrapper<SysThirdAccount>().in(SysThirdAccount::getSysUserId, userIds));
//6. 删除租户用户中间表的数据 //6. 删除租户用户中间表的数据
line += userTenantMapper.delete(new LambdaQueryWrapper<SysUserTenant>().in(SysUserTenant::getUserId,userIds)); line += userTenantMapper.delete(new LambdaQueryWrapper<SysUserTenant>().in(SysUserTenant::getUserId, userIds));
return line != 0; return line != 0;
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
...@@ -601,163 +607,163 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -601,163 +607,163 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return true; return true;
} }
@Override @Override
public void saveThirdUser(SysUser sysUser) { public void saveThirdUser(SysUser sysUser) {
//保存用户 //保存用户
String userid = UUIDGenerator.generate(); String userid = UUIDGenerator.generate();
sysUser.setId(userid); sysUser.setId(userid);
baseMapper.insert(sysUser); baseMapper.insert(sysUser);
//获取第三方角色 //获取第三方角色
SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleCode, "third_role")); SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleCode, "third_role"));
//保存用户角色 //保存用户角色
SysUserRole userRole = new SysUserRole(); SysUserRole userRole = new SysUserRole();
userRole.setRoleId(sysRole.getId()); userRole.setRoleId(sysRole.getId());
userRole.setUserId(userid); userRole.setUserId(userid);
sysUserRoleMapper.insert(userRole); sysUserRoleMapper.insert(userRole);
} }
@Override @Override
public List<SysUser> queryByDepIds(List<String> departIds, String username) { public List<SysUser> queryByDepIds(List<String> departIds, String username) {
return userMapper.queryByDepIds(departIds,username); return userMapper.queryByDepIds(departIds, username);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void saveUser(SysUser user, String selectedRoles, String selectedDeparts, String relTenantIds) { public void saveUser(SysUser user, String selectedRoles, String selectedDeparts, String relTenantIds) {
//step.1 保存用户 //step.1 保存用户
this.save(user); this.save(user);
//获取用户保存前台传过来的租户id并添加到租户 //获取用户保存前台传过来的租户id并添加到租户
this.saveUserTenant(user.getId(),relTenantIds); this.saveUserTenant(user.getId(), relTenantIds);
//step.2 保存角色 //step.2 保存角色
if(oConvertUtils.isNotEmpty(selectedRoles)) { if (oConvertUtils.isNotEmpty(selectedRoles)) {
String[] arr = selectedRoles.split(","); String[] arr = selectedRoles.split(",");
for (String roleId : arr) { for (String roleId : arr) {
SysUserRole userRole = new SysUserRole(user.getId(), roleId); SysUserRole userRole = new SysUserRole(user.getId(), roleId);
sysUserRoleMapper.insert(userRole); sysUserRoleMapper.insert(userRole);
} }
} }
//step.3 保存所属部门 //step.3 保存所属部门
if(oConvertUtils.isNotEmpty(selectedDeparts)) { if (oConvertUtils.isNotEmpty(selectedDeparts)) {
String[] arr = selectedDeparts.split(","); String[] arr = selectedDeparts.split(",");
for (String deaprtId : arr) { for (String deaprtId : arr) {
SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId); SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId);
sysUserDepartMapper.insert(userDeaprt); sysUserDepartMapper.insert(userDeaprt);
} }
} }
//触发入职流程 //触发入职流程
LoginUser userInfo=new LoginUser(); LoginUser userInfo = new LoginUser();
BeanUtils.copyProperties(user,userInfo); BeanUtils.copyProperties(user, userInfo);
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
public void editUser(SysUser user, String roles, String departs, String relTenantIds) { public void editUser(SysUser user, String roles, String departs, String relTenantIds) {
//获取用户编辑前台传过来的租户id //获取用户编辑前台传过来的租户id
this.editUserTenants(user.getId(),relTenantIds); this.editUserTenants(user.getId(), relTenantIds);
//step.1 修改用户基础信息 //step.1 修改用户基础信息
this.updateById(user); this.updateById(user);
//step.2 修改角色 //step.2 修改角色
//处理用户角色 先删后加 //处理用户角色 先删后加
sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, user.getId())); sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, user.getId()));
if(oConvertUtils.isNotEmpty(roles)) { if (oConvertUtils.isNotEmpty(roles)) {
String[] arr = roles.split(","); String[] arr = roles.split(",");
for (String roleId : arr) { for (String roleId : arr) {
SysUserRole userRole = new SysUserRole(user.getId(), roleId); SysUserRole userRole = new SysUserRole(user.getId(), roleId);
sysUserRoleMapper.insert(userRole); sysUserRoleMapper.insert(userRole);
} }
} }
//step.3 修改部门 //step.3 修改部门
String[] arr = {}; String[] arr = {};
if(oConvertUtils.isNotEmpty(departs)){ if (oConvertUtils.isNotEmpty(departs)) {
arr = departs.split(","); arr = departs.split(",");
} }
//查询已关联部门 //查询已关联部门
List<SysUserDepart> userDepartList = sysUserDepartMapper.selectList(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId())); List<SysUserDepart> userDepartList = sysUserDepartMapper.selectList(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId()));
if(userDepartList != null && userDepartList.size()>0){ if (userDepartList != null && userDepartList.size() > 0) {
for(SysUserDepart depart : userDepartList ){ for (SysUserDepart depart : userDepartList) {
//修改已关联部门删除部门用户角色关系 //修改已关联部门删除部门用户角色关系
if(!Arrays.asList(arr).contains(depart.getDepId())){ if (!Arrays.asList(arr).contains(depart.getDepId())) {
List<SysDepartRole> sysDepartRoleList = sysDepartRoleMapper.selectList( List<SysDepartRole> sysDepartRoleList = sysDepartRoleMapper.selectList(
new QueryWrapper<SysDepartRole>().lambda().eq(SysDepartRole::getDepartId,depart.getDepId())); new QueryWrapper<SysDepartRole>().lambda().eq(SysDepartRole::getDepartId, depart.getDepId()));
List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList()); List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList());
if(roleIds != null && roleIds.size()>0){ if (roleIds != null && roleIds.size() > 0) {
departRoleUserMapper.delete(new QueryWrapper<SysDepartRoleUser>().lambda().eq(SysDepartRoleUser::getUserId, user.getId()) departRoleUserMapper.delete(new QueryWrapper<SysDepartRoleUser>().lambda().eq(SysDepartRoleUser::getUserId, user.getId())
.in(SysDepartRoleUser::getDroleId,roleIds)); .in(SysDepartRoleUser::getDroleId, roleIds));
} }
} }
} }
} }
//先删后加 //先删后加
sysUserDepartMapper.delete(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId())); sysUserDepartMapper.delete(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId()));
if(oConvertUtils.isNotEmpty(departs)) { if (oConvertUtils.isNotEmpty(departs)) {
for (String departId : arr) { for (String departId : arr) {
SysUserDepart userDepart = new SysUserDepart(user.getId(), departId); SysUserDepart userDepart = new SysUserDepart(user.getId(), departId);
sysUserDepartMapper.insert(userDepart); sysUserDepartMapper.insert(userDepart);
} }
} }
//step.4 修改手机号和邮箱 //step.4 修改手机号和邮箱
// 更新手机号、邮箱空字符串为 null // 更新手机号、邮箱空字符串为 null
userMapper.updateNullByEmptyString("email"); userMapper.updateNullByEmptyString("email");
userMapper.updateNullByEmptyString("phone"); userMapper.updateNullByEmptyString("phone");
} }
@Override @Override
public List<String> userIdToUsername(Collection<String> userIdList) { public List<String> userIdToUsername(Collection<String> userIdList) {
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysUser::getId, userIdList); queryWrapper.in(SysUser::getId, userIdList);
List<SysUser> userList = super.list(queryWrapper); List<SysUser> userList = super.list(queryWrapper);
return userList.stream().map(SysUser::getUsername).collect(Collectors.toList()); return userList.stream().map(SysUser::getUsername).collect(Collectors.toList());
} }
@Override @Override
@Cacheable(cacheNames=CacheConstant.SYS_USERS_CACHE, key="#username") @Cacheable(cacheNames = CacheConstant.SYS_USERS_CACHE, key = "#username")
@SensitiveEncode @SensitiveEncode
public LoginUser getEncodeUserInfo(String username){ public LoginUser getEncodeUserInfo(String username) {
if(oConvertUtils.isEmpty(username)) { if (oConvertUtils.isEmpty(username)) {
return null; return null;
} }
LoginUser loginUser = new LoginUser(); LoginUser loginUser = new LoginUser();
SysUser sysUser = userMapper.getUserByName(username); SysUser sysUser = userMapper.getUserByName(username);
//查询用户的租户ids //查询用户的租户ids
this.setUserTenantIds(sysUser); this.setUserTenantIds(sysUser);
if(sysUser==null) { if (sysUser == null) {
return null; return null;
} }
BeanUtils.copyProperties(sysUser, loginUser); BeanUtils.copyProperties(sysUser, loginUser);
return loginUser; return loginUser;
} }
@Override @Override
@CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true) @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void userQuit(String username) { public void userQuit(String username) {
SysUser sysUser = userMapper.getUserByName(username); SysUser sysUser = userMapper.getUserByName(username);
if(null == sysUser){ if (null == sysUser) {
throw new JeecgBootException("离职失败,该用户已不存在"); throw new JeecgBootException("离职失败,该用户已不存在");
} }
//update-begin---author:wangshuai ---date:20230111 for:[QQYUN-3951]租户用户离职重构------------ //update-begin---author:wangshuai ---date:20230111 for:[QQYUN-3951]租户用户离职重构------------
int tenantId = oConvertUtils.getInt(TenantContext.getTenant(), 0); int tenantId = oConvertUtils.getInt(TenantContext.getTenant(), 0);
//更新用户租户表的状态为离职状态 //更新用户租户表的状态为离职状态
if(tenantId==0){ if (tenantId == 0) {
throw new JeecgBootException("离职失败,租户不存在"); throw new JeecgBootException("离职失败,租户不存在");
} }
LambdaQueryWrapper<SysUserTenant> query = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUserTenant> query = new LambdaQueryWrapper<>();
query.eq(SysUserTenant::getUserId,sysUser.getId()); query.eq(SysUserTenant::getUserId, sysUser.getId());
query.eq(SysUserTenant::getTenantId,tenantId); query.eq(SysUserTenant::getTenantId, tenantId);
SysUserTenant userTenant = new SysUserTenant(); SysUserTenant userTenant = new SysUserTenant();
userTenant.setStatus(CommonConstant.USER_TENANT_QUIT); userTenant.setStatus(CommonConstant.USER_TENANT_QUIT);
userTenantMapper.update(userTenant,query); userTenantMapper.update(userTenant, query);
//update-end---author:wangshuai ---date:20230111 for:[QQYUN-3951]租户用户离职重构------------ //update-end---author:wangshuai ---date:20230111 for:[QQYUN-3951]租户用户离职重构------------
//触发离职流程 //触发离职流程
LoginUser userInfo=new LoginUser(); LoginUser userInfo = new LoginUser();
BeanUtils.copyProperties(sysUser,userInfo); BeanUtils.copyProperties(sysUser, userInfo);
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
} }
@Override @Override
...@@ -767,83 +773,86 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -767,83 +773,86 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override @Override
public void updateStatusAndFlag(List<String> userIds, SysUser sysUser) { public void updateStatusAndFlag(List<String> userIds, SysUser sysUser) {
userMapper.updateStatusAndFlag(userIds,sysUser); userMapper.updateStatusAndFlag(userIds, sysUser);
} }
/** /**
* 设置登录租户 * 设置登录租户
* @param sysUser *
* @return * @param sysUser
*/ * @return
@Override */
public Result<JSONObject> setLoginTenant(SysUser sysUser, JSONObject obj, String username, Result<JSONObject> result){ @Override
// update-begin--Author:sunjianlei Date:20210802 for:获取用户租户信息 public Result<JSONObject> setLoginTenant(SysUser sysUser, JSONObject obj, String username, Result<JSONObject> result) {
//用户有哪些租户 // update-begin--Author:sunjianlei Date:20210802 for:获取用户租户信息
List<SysTenant> tenantList = null; //用户有哪些租户
List<SysTenant> tenantList = null;
//update-begin---author:wangshuai ---date:20221223 for:[QQYUN-3371]租户逻辑改造,改成关系表------------ //update-begin---author:wangshuai ---date:20221223 for:[QQYUN-3371]租户逻辑改造,改成关系表------------
List<Integer> tenantIdList = relationMapper.getTenantIdsNoStatus(sysUser.getId()); List<Integer> tenantIdList = relationMapper.getTenantIdsNoStatus(sysUser.getId());
if (null!=tenantIdList && tenantIdList.size()>0) { if (null != tenantIdList && tenantIdList.size() > 0) {
//update-end---author:wangshuai ---date:20221223 for:[QQYUN-3371]租户逻辑改造,改成关系表-------------- //update-end---author:wangshuai ---date:20221223 for:[QQYUN-3371]租户逻辑改造,改成关系表--------------
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
//查询有效的租户集合 //查询有效的租户集合
LambdaQueryWrapper<SysTenant> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysTenant> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysTenant::getId, tenantIdList); queryWrapper.in(SysTenant::getId, tenantIdList);
queryWrapper.eq(SysTenant::getStatus, Integer.valueOf(CommonConstant.STATUS_1)); queryWrapper.eq(SysTenant::getStatus, Integer.valueOf(CommonConstant.STATUS_1));
tenantList = sysTenantMapper.selectList(queryWrapper); tenantList = sysTenantMapper.selectList(queryWrapper);
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
if (tenantList.size() == 0) { if (tenantList.size() == 0) {
return result.error500("与该用户关联的租户均已被冻结,无法登录!"); return result.error500("与该用户关联的租户均已被冻结,无法登录!");
} else { } else {
obj.put("tenantList", tenantList); obj.put("tenantList", tenantList);
} }
} }
// update-end--Author:sunjianlei Date:20210802 for:获取用户租户信息 // update-end--Author:sunjianlei Date:20210802 for:获取用户租户信息
//登录会话租户ID,有效性重置 //登录会话租户ID,有效性重置
if (tenantList != null && tenantList.size() > 0) { if (tenantList != null && tenantList.size() > 0) {
if (tenantList.size() == 1) { if (tenantList.size() == 1) {
sysUser.setLoginTenantId(tenantList.get(0).getId()); sysUser.setLoginTenantId(tenantList.get(0).getId());
} else { } else {
List<SysTenant> listAfterFilter = tenantList.stream().filter(s -> s.getId().equals(sysUser.getLoginTenantId())).collect(Collectors.toList()); List<SysTenant> listAfterFilter = tenantList.stream().filter(s -> s.getId().equals(sysUser.getLoginTenantId())).collect(Collectors.toList());
if (listAfterFilter == null || listAfterFilter.size() == 0) { if (listAfterFilter == null || listAfterFilter.size() == 0) {
//如果上次登录租户ID,在用户拥有的租户集合里面没有了,则随机取用户拥有的第一个租户ID //如果上次登录租户ID,在用户拥有的租户集合里面没有了,则随机取用户拥有的第一个租户ID
sysUser.setLoginTenantId(tenantList.get(0).getId()); sysUser.setLoginTenantId(tenantList.get(0).getId());
} }
} }
} else { } else {
//无租户的时候,设置为 0 //无租户的时候,设置为 0
sysUser.setLoginTenantId(0); sysUser.setLoginTenantId(0);
} }
//设置用户登录缓存租户 //设置用户登录缓存租户
this.updateUserDepart(username, null,sysUser.getLoginTenantId()); this.updateUserDepart(username, null, sysUser.getLoginTenantId());
log.info(" 登录接口用户的租户ID = {}", sysUser.getLoginTenantId()); log.info(" 登录接口用户的租户ID = {}", sysUser.getLoginTenantId());
if(sysUser.getLoginTenantId()!=null){ if (sysUser.getLoginTenantId() != null) {
//登录的时候需要手工设置下会话中的租户ID,不然登录接口无法通过租户隔离查询到数据 //登录的时候需要手工设置下会话中的租户ID,不然登录接口无法通过租户隔离查询到数据
TenantContext.setTenant(sysUser.getLoginTenantId()+""); TenantContext.setTenant(sysUser.getLoginTenantId() + "");
} }
return null; return null;
} }
/** /**
* 获取租户id * 获取租户id
*
* @param sysUser * @param sysUser
*/ */
private void setUserTenantIds(SysUser sysUser) { private void setUserTenantIds(SysUser sysUser) {
if(ObjectUtils.isNotEmpty(sysUser)) { if (ObjectUtils.isNotEmpty(sysUser)) {
List<Integer> list = relationMapper.getTenantIdsNoStatus(sysUser.getId()); List<Integer> list = relationMapper.getTenantIdsNoStatus(sysUser.getId());
if(null!=list && list.size()>0){ if (null != list && list.size() > 0) {
sysUser.setRelTenantIds(StringUtils.join(list.toArray(), ",")); sysUser.setRelTenantIds(StringUtils.join(list.toArray(), ","));
}else{ } else {
sysUser.setRelTenantIds(""); sysUser.setRelTenantIds("");
} }
} }
} }
/** /**
* 保存租户 * 保存租户
*
* @param userId * @param userId
* @param relTenantIds * @param relTenantIds
*/ */
...@@ -857,28 +866,29 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -857,28 +866,29 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
relation.setStatus(CommonConstant.STATUS_1); relation.setStatus(CommonConstant.STATUS_1);
relationMapper.insert(relation); relationMapper.insert(relation);
} }
}else{ } else {
//是否开启系统管理模块的多租户数据隔离【SAAS多租户模式】 //是否开启系统管理模块的多租户数据隔离【SAAS多租户模式】
if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) { if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) {
//update-begin---author:wangshuai ---date:20230220 for:判断当前用户是否在当前租户里面,如果不存在在新增------------ //update-begin---author:wangshuai ---date:20230220 for:判断当前用户是否在当前租户里面,如果不存在在新增------------
String tenantId = TenantContext.getTenant(); String tenantId = TenantContext.getTenant();
if(oConvertUtils.isNotEmpty(tenantId)){ if (oConvertUtils.isNotEmpty(tenantId)) {
Integer count = relationMapper.userTenantIzExist(userId, Integer.parseInt(tenantId)); Integer count = relationMapper.userTenantIzExist(userId, Integer.parseInt(tenantId));
if(count == 0){ if (count == 0) {
SysUserTenant relation = new SysUserTenant(); SysUserTenant relation = new SysUserTenant();
relation.setUserId(userId); relation.setUserId(userId);
relation.setTenantId(Integer.parseInt(tenantId)); relation.setTenantId(Integer.parseInt(tenantId));
relation.setStatus(CommonConstant.STATUS_1); relation.setStatus(CommonConstant.STATUS_1);
relationMapper.insert(relation); relationMapper.insert(relation);
} }
} }
//update-end---author:wangshuai ---date:20230220 for:判断当前用户是否在当前租户里面,如果不存在在新增------------ //update-end---author:wangshuai ---date:20230220 for:判断当前用户是否在当前租户里面,如果不存在在新增------------
} }
} }
} }
/** /**
* 编辑租户 * 编辑租户
*
* @param userId * @param userId
* @param relTenantIds * @param relTenantIds
*/ */
...@@ -886,7 +896,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -886,7 +896,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
LambdaQueryWrapper<SysUserTenant> query = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUserTenant> query = new LambdaQueryWrapper<>();
query.eq(SysUserTenant::getUserId, userId); query.eq(SysUserTenant::getUserId, userId);
//数据库的租户id //数据库的租户id
List<Integer> oldTenantIds = relationMapper.getTenantIdsNoStatus(userId); List<Integer> oldTenantIds = relationMapper.getTenantIdsNoStatus(userId);
//如果传过来的租户id为空,那么就删除租户 //如果传过来的租户id为空,那么就删除租户
if (oConvertUtils.isEmpty(relTenantIds) && CollectionUtils.isNotEmpty(oldTenantIds)) { if (oConvertUtils.isEmpty(relTenantIds) && CollectionUtils.isNotEmpty(oldTenantIds)) {
this.deleteTenantByUserId(userId, null); this.deleteTenantByUserId(userId, null);
...@@ -894,371 +904,374 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -894,371 +904,374 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
//如果传过来的租户id不为空但是数据库的租户id为空,那么就新增 //如果传过来的租户id不为空但是数据库的租户id为空,那么就新增
this.saveUserTenant(userId, relTenantIds); this.saveUserTenant(userId, relTenantIds);
} else { } else {
//都不为空,需要比较,进行添加或删除 //都不为空,需要比较,进行添加或删除
if(oConvertUtils.isNotEmpty(relTenantIds) && CollectionUtils.isNotEmpty(oldTenantIds)){ if (oConvertUtils.isNotEmpty(relTenantIds) && CollectionUtils.isNotEmpty(oldTenantIds)) {
//找到新的租户id与原来的租户id不同之处,进行删除 //找到新的租户id与原来的租户id不同之处,进行删除
String[] relTenantIdArray = relTenantIds.split(SymbolConstant.COMMA); String[] relTenantIdArray = relTenantIds.split(SymbolConstant.COMMA);
List<String> relTenantIdList = Arrays.asList(relTenantIdArray); List<String> relTenantIdList = Arrays.asList(relTenantIdArray);
List<Integer> deleteTenantIdList = oldTenantIds.stream().filter(item -> !relTenantIdList.contains(item.toString())).collect(Collectors.toList()); List<Integer> deleteTenantIdList = oldTenantIds.stream().filter(item -> !relTenantIdList.contains(item.toString())).collect(Collectors.toList());
for (Integer tenantId : deleteTenantIdList) { for (Integer tenantId : deleteTenantIdList) {
this.deleteTenantByUserId(userId, tenantId); this.deleteTenantByUserId(userId, tenantId);
} }
//找到原来租户的用户id与新的租户id不同之处,进行新增 //找到原来租户的用户id与新的租户id不同之处,进行新增
String tenantIds = relTenantIdList.stream().filter(item -> !oldTenantIds.contains(Integer.valueOf(item))).collect(Collectors.joining(",")); String tenantIds = relTenantIdList.stream().filter(item -> !oldTenantIds.contains(Integer.valueOf(item))).collect(Collectors.joining(","));
this.saveUserTenant(userId, tenantIds); this.saveUserTenant(userId, tenantIds);
} }
} }
} }
/** /**
* 删除租户通过用户id * 删除租户通过用户id
*
* @param tenantId * @param tenantId
* @param userId * @param userId
*/ */
private void deleteTenantByUserId(String userId,Integer tenantId){ private void deleteTenantByUserId(String userId, Integer tenantId) {
LambdaQueryWrapper<SysUserTenant> query = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUserTenant> query = new LambdaQueryWrapper<>();
query.eq(SysUserTenant::getUserId, userId); query.eq(SysUserTenant::getUserId, userId);
if(oConvertUtils.isNotEmpty(tenantId)){ if (oConvertUtils.isNotEmpty(tenantId)) {
query.eq(SysUserTenant::getTenantId, tenantId); query.eq(SysUserTenant::getTenantId, tenantId);
} }
relationMapper.delete(query); relationMapper.delete(query);
} }
@Override
public void batchEditUsers(JSONObject json) {
String userIds = json.getString("userIds");
List<String> idList = JSONArray.parseArray(userIds, String.class);
//部门
String selecteddeparts = json.getString("selecteddeparts");
//职位
String post = json.getString("post");
//工作地点? 没有这个字段
String workAddress = json.getString("workAddress");
if (oConvertUtils.isNotEmpty(post)) {
LambdaUpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<SysUser>().lambda()
.in(SysUser::getId, idList)
.set(SysUser::getPost, post);
this.update(updateWrapper);
}
if (oConvertUtils.isNotEmpty(selecteddeparts)) {
//查询当前租户的部门列表
Integer currentTenantId = oConvertUtils.getInt(TenantContext.getTenant(), 0);
LambdaQueryWrapper<SysDepart> departQuery = new LambdaQueryWrapper<SysDepart>()
.eq(SysDepart::getTenantId, currentTenantId);
List<SysDepart> departList = sysDepartMapper.selectList(departQuery);
if (departList == null || departList.size() == 0) {
log.error("batchEditUsers 根据租户ID没有找到部门>" + currentTenantId);
return;
}
List<String> departIdList = new ArrayList<String>();
for (SysDepart depart : departList) {
if (depart != null) {
String id = depart.getId();
if (oConvertUtils.isNotEmpty(id)) {
departIdList.add(id);
}
}
}
//删除人员的部门关联
LambdaQueryWrapper<SysUserDepart> query = new LambdaQueryWrapper<SysUserDepart>()
.in(SysUserDepart::getUserId, idList)
.in(SysUserDepart::getDepId, departIdList);
sysUserDepartMapper.delete(query);
String[] arr = selecteddeparts.split(",");
//再新增
for (String deaprtId : arr) {
for (String userId : idList) {
SysUserDepart userDepart = new SysUserDepart(userId, deaprtId);
sysUserDepartMapper.insert(userDepart);
}
}
}
}
@Override
public DepartAndUserInfo searchByKeyword(String keyword) {
DepartAndUserInfo departAndUserInfo = new DepartAndUserInfo();
if (oConvertUtils.isNotEmpty(keyword)) {
LambdaQueryWrapper<SysUser> query1 = new LambdaQueryWrapper<SysUser>()
.like(SysUser::getRealname, keyword);
String str = oConvertUtils.getString(TenantContext.getTenant(), "0");
Integer tenantId = Integer.valueOf(str);
if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) {
List<String> userIds = userTenantMapper.getUserIdsByTenantId(tenantId);
if (oConvertUtils.listIsNotEmpty(userIds)) {
query1.in(SysUser::getId, userIds);
} else {
query1.eq(SysUser::getId, "");
}
}
List<SysUser> list1 = this.baseMapper.selectList(query1);
if (list1 != null && list1.size() > 0) {
List<UserAvatar> userList = list1.stream().map(v -> new UserAvatar(v)).collect(Collectors.toList());
departAndUserInfo.setUserList(userList);
}
LambdaQueryWrapper<SysDepart> query2 = new LambdaQueryWrapper<SysDepart>()
.like(SysDepart::getDepartName, keyword);
if (MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL) {
query2.eq(SysDepart::getTenantId, tenantId);
}
List<SysDepart> list2 = sysDepartMapper.selectList(query2);
if (list2 != null && list2.size() > 0) {
List<DepartInfo> departList = new ArrayList<>();
for (SysDepart depart : list2) {
List<String> orgName = new ArrayList<>();
List<String> orgId = new ArrayList<>();
getParentDepart(depart, orgName, orgId);
DepartInfo departInfo = new DepartInfo();
departInfo.setId(depart.getId());
departInfo.setOrgId(orgId);
departInfo.setOrgName(orgName);
departList.add(departInfo);
}
departAndUserInfo.setDepartList(departList);
}
}
return departAndUserInfo;
}
@Override
public UpdateDepartInfo getUpdateDepartInfo(String departId) {
SysDepart depart = sysDepartMapper.selectById(departId);
if (depart != null) {
UpdateDepartInfo info = new UpdateDepartInfo(depart);
List<SysDepart> subList = sysDepartMapper.queryDeptByPid(departId);
if (subList != null && subList.size() > 0) {
info.setHasSub(true);
}
//获取部门负责人信息
LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getUserIdentity, 2)
.like(SysUser::getDepartIds, depart.getId());
List<SysUser> userList = this.baseMapper.selectList(query);
if (userList != null && userList.size() > 0) {
List<String> idList = userList.stream().map(i -> i.getId()).collect(Collectors.toList());
info.setChargePersonList(idList);
}
return info;
}
return null;
}
@Override
public void doUpdateDepartInfo(UpdateDepartInfo info) {
String departId = info.getDepartId();
SysDepart depart = sysDepartMapper.selectById(departId);
if (depart != null) {
//修改部门信息-上级和部门名称
if (!depart.getParentId().equals(info.getParentId())) {
String pid = info.getParentId();
SysDepart parentDepart = sysDepartMapper.selectById(pid);
if (parentDepart != null) {
String orgCode = getNextOrgCode(pid);
depart.setOrgCode(orgCode);
depart.setParentId(pid);
}
}
depart.setDepartName(info.getDepartName());
sysDepartMapper.updateById(depart);
//先查询这个部门的负责人
List<SysUser> departChargeUsers = queryDepartChargePersons(departId);
List<String> departChargeUserIdList = departChargeUsers.stream().map(i -> i.getId()).collect(Collectors.toList());
//修改部门负责人
List<String> userIdList = info.getChargePersonList();
if (userIdList != null && userIdList.size() > 0) {
for (String userId : userIdList) {
SysUser user = this.baseMapper.selectById(userId);
if (user != null) {
departChargeUserIdList.remove(user.getId());
user.setUserIdentity(2);
String departIds = user.getDepartIds();
if (oConvertUtils.isEmpty(departIds)) {
user.setDepartIds(departId);
} else {
List<String> list = new ArrayList<String>(Arrays.asList(departIds.split(",")));
if (list.indexOf(departId) >= 0) {
continue;
} else {
list.add(departId);
String newDepartIds = String.join(",", list);
user.setDepartIds(newDepartIds);
}
}
this.baseMapper.updateById(user);
}
}
//删除
for (String chargeUserId : departChargeUserIdList) {
for (SysUser chargeUser : departChargeUsers) {
if (chargeUser.getId().equals(chargeUserId)) {
String departIds = chargeUser.getDepartIds();
List<String> list = new ArrayList<String>(Arrays.asList(departIds.split(",")));
list.remove(departId);
String newDepartIds = String.join(",", list);
chargeUser.setDepartIds(newDepartIds);
this.baseMapper.updateById(chargeUser);
break;
}
}
}
}
}
}
private List<SysUser> queryDepartChargePersons(String departId) {
List<SysUser> result = new ArrayList<>();
LambdaQueryWrapper<SysUserDepart> query1 = new LambdaQueryWrapper<SysUserDepart>()
.eq(SysUserDepart::getDepId, departId);
List<SysUserDepart> list1 = sysUserDepartMapper.selectList(query1);
if (list1 != null && list1.size() > 0) {
List<String> userIdList = list1.stream().map(item -> item.getUserId()).collect(Collectors.toList());
LambdaQueryWrapper<SysUser> query2 = new LambdaQueryWrapper<SysUser>()
.in(SysUser::getId, userIdList);
List<SysUser> userList = this.baseMapper.selectList(query2);
if (userList != null && userList.size() > 0) {
for (SysUser user : userList) {
Integer identity = user.getUserIdentity();
String deps = user.getDepartIds();
if (identity != null && identity == 2) {
if (oConvertUtils.isNotEmpty(deps)) {
if (deps.indexOf(departId) >= 0) {
result.add(user);
}
}
}
}
}
}
return result;
}
/**
* 变更父级部门 修改编码
*
* @param parentId
* @return
*/
private String getNextOrgCode(String parentId) {
JSONObject formData = new JSONObject();
formData.put("parentId", parentId);
String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.DEPART, formData);
return codeArray[0];
}
@Override
public void changeDepartChargePerson(JSONObject json) {
String userId = json.getString("userId");
String departId = json.getString("departId");
boolean status = json.getBoolean("status");
SysUser user = this.getById(userId);
if (user != null) {
String ids = user.getDepartIds();
if (status == true) {
//设置部门负责人
if (oConvertUtils.isEmpty(ids)) {
user.setDepartIds(departId);
} else {
List<String> list = new ArrayList<String>(Arrays.asList(ids.split(",")));
if (list.indexOf(departId) >= 0) {
//啥也不干
} else {
list.add(departId);
String newIds = String.join(",", list);
user.setDepartIds(newIds);
}
}
} else {
// 取消负责人
if (oConvertUtils.isNotEmpty(ids)) {
List<String> list = new ArrayList<String>();
for (String temp : ids.split(",")) {
if (oConvertUtils.isEmpty(temp)) {
continue;
}
if (!temp.equals(departId)) {
list.add(temp);
}
}
String newIds = "";
if (list.size() > 0) {
newIds = String.join(",", list);
}
user.setDepartIds(newIds);
}
}
this.updateById(user);
}
}
@Override /**
public void batchEditUsers(JSONObject json) { * 找上级部门
String userIds = json.getString("userIds"); *
List<String> idList = JSONArray.parseArray(userIds, String.class); * @param depart
//部门 * @param orgName
String selecteddeparts = json.getString("selecteddeparts"); * @param orgId
//职位 */
String post = json.getString("post"); private void getParentDepart(SysDepart depart, List<String> orgName, List<String> orgId) {
//工作地点? 没有这个字段 String pid = depart.getParentId();
String workAddress = json.getString("workAddress"); orgName.add(0, depart.getDepartName());
if(oConvertUtils.isNotEmpty(post)) { orgId.add(0, depart.getId());
LambdaUpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<SysUser>().lambda() if (oConvertUtils.isNotEmpty(pid)) {
.in(SysUser::getId, idList) SysDepart temp = sysDepartMapper.selectById(pid);
.set(SysUser::getPost, post); getParentDepart(temp, orgName, orgId);
this.update(updateWrapper); }
} }
if(oConvertUtils.isNotEmpty(selecteddeparts)) {
//查询当前租户的部门列表 @Override
Integer currentTenantId = oConvertUtils.getInt(TenantContext.getTenant(), 0); @Transactional(rollbackFor = Exception.class)
LambdaQueryWrapper<SysDepart> departQuery = new LambdaQueryWrapper<SysDepart>() @CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
.eq(SysDepart::getTenantId, currentTenantId); public void editTenantUser(SysUser sysUser, String tenantId, String departs, String roles) {
List<SysDepart> departList = sysDepartMapper.selectList(departQuery); SysUser user = new SysUser();
if(departList==null || departList.size()==0){ user.setWorkNo(sysUser.getWorkNo());
log.error("batchEditUsers 根据租户ID没有找到部门>"+currentTenantId); user.setPost(sysUser.getPost());
return; user.setId(sysUser.getId());
} this.updateById(user);
List<String> departIdList = new ArrayList<String>(); //修改租户用户下的部门
for(SysDepart depart: departList){ this.updateTenantDepart(user, tenantId, departs);
if(depart!=null){ }
String id = depart.getId();
if(oConvertUtils.isNotEmpty(id)){ /**
departIdList.add(id); * 修改租户下的部门
} *
} * @param departs
} */
//删除人员的部门关联 public void updateTenantDepart(SysUser user, String tenantId, String departs) {
LambdaQueryWrapper<SysUserDepart> query = new LambdaQueryWrapper<SysUserDepart>() List<String> departList = new ArrayList<>();
.in(SysUserDepart::getUserId, idList) if (oConvertUtils.isNotEmpty(departs)) {
.in(SysUserDepart::getDepId, departIdList); //获取当前租户下的部门id,根据前台
sysUserDepartMapper.delete(query); departList = sysUserDepartMapper.getTenantDepart(Arrays.asList(departs.split(SymbolConstant.COMMA)), tenantId);
}
String[] arr = selecteddeparts.split(","); //查询当前租户下部门和用户已关联的部门
List<SysUserDepart> userDepartList = sysUserDepartMapper.getTenantUserDepart(user.getId(), tenantId);
//再新增 if (userDepartList != null && userDepartList.size() > 0 && departList.size() > 0) {
for (String deaprtId : arr) { for (SysUserDepart depart : userDepartList) {
for(String userId: idList){ //修改已关联部门删除部门用户角色关系
SysUserDepart userDepart = new SysUserDepart(userId, deaprtId); if (!departList.contains(depart.getDepId())) {
sysUserDepartMapper.insert(userDepart); List<SysDepartRole> sysDepartRoleList = sysDepartRoleMapper.selectList(
} new QueryWrapper<SysDepartRole>().lambda().eq(SysDepartRole::getDepartId, depart.getDepId()));
} List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList());
} if (roleIds.size() > 0) {
} departRoleUserMapper.delete(new QueryWrapper<SysDepartRoleUser>().lambda().eq(SysDepartRoleUser::getUserId, user.getId())
.in(SysDepartRoleUser::getDroleId, roleIds));
@Override }
public DepartAndUserInfo searchByKeyword(String keyword) { }
DepartAndUserInfo departAndUserInfo = new DepartAndUserInfo(); }
if(oConvertUtils.isNotEmpty(keyword)){ }
LambdaQueryWrapper<SysUser> query1 = new LambdaQueryWrapper<SysUser>() if (departList.size() > 0) {
.like(SysUser::getRealname, keyword); //删除用户下的部门
String str = oConvertUtils.getString(TenantContext.getTenant(), "0"); sysUserDepartMapper.deleteUserDepart(user.getId(), tenantId);
Integer tenantId = Integer.valueOf(str); for (String departId : departList) {
if(MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL){ //添加部门
List<String> userIds = userTenantMapper.getUserIdsByTenantId(tenantId); SysUserDepart userDepart = new SysUserDepart(user.getId(), departId);
if (oConvertUtils.listIsNotEmpty(userIds)) { sysUserDepartMapper.insert(userDepart);
query1.in(SysUser::getId, userIds); }
}else{ }
query1.eq(SysUser::getId, ""); }
}
}
List<SysUser> list1 = this.baseMapper.selectList(query1);
if(list1!=null && list1.size()>0){
List<UserAvatar> userList = list1.stream().map(v -> new UserAvatar(v)).collect(Collectors.toList());
departAndUserInfo.setUserList(userList);
}
LambdaQueryWrapper<SysDepart> query2 = new LambdaQueryWrapper<SysDepart>()
.like(SysDepart::getDepartName, keyword);
if(MybatisPlusSaasConfig.OPEN_SYSTEM_TENANT_CONTROL){
query2.eq(SysDepart::getTenantId, tenantId);
}
List<SysDepart> list2 = sysDepartMapper.selectList(query2);
if(list2!=null && list2.size()>0){
List<DepartInfo> departList = new ArrayList<>();
for(SysDepart depart: list2){
List<String> orgName = new ArrayList<>();
List<String> orgId = new ArrayList<>();
getParentDepart(depart, orgName, orgId);
DepartInfo departInfo = new DepartInfo();
departInfo.setId(depart.getId());
departInfo.setOrgId(orgId);
departInfo.setOrgName(orgName);
departList.add(departInfo);
}
departAndUserInfo.setDepartList(departList);
}
}
return departAndUserInfo;
}
@Override
public UpdateDepartInfo getUpdateDepartInfo(String departId) {
SysDepart depart = sysDepartMapper.selectById(departId);
if(depart!=null){
UpdateDepartInfo info = new UpdateDepartInfo(depart);
List<SysDepart> subList = sysDepartMapper.queryDeptByPid(departId);
if(subList!=null && subList.size()>0){
info.setHasSub(true);
}
//获取部门负责人信息
LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getUserIdentity, 2)
.like(SysUser::getDepartIds, depart.getId());
List<SysUser> userList = this.baseMapper.selectList(query);
if(userList!=null && userList.size()>0){
List<String> idList = userList.stream().map(i -> i.getId()).collect(Collectors.toList());
info.setChargePersonList(idList);
}
return info;
}
return null;
}
@Override
public void doUpdateDepartInfo(UpdateDepartInfo info) {
String departId = info.getDepartId();
SysDepart depart = sysDepartMapper.selectById(departId);
if(depart!=null){
//修改部门信息-上级和部门名称
if(!depart.getParentId().equals(info.getParentId())){
String pid = info.getParentId();
SysDepart parentDepart = sysDepartMapper.selectById(pid);
if(parentDepart!=null){
String orgCode = getNextOrgCode(pid);
depart.setOrgCode(orgCode);
depart.setParentId(pid);
}
}
depart.setDepartName(info.getDepartName());
sysDepartMapper.updateById(depart);
//先查询这个部门的负责人
List<SysUser> departChargeUsers = queryDepartChargePersons(departId);
List<String> departChargeUserIdList = departChargeUsers.stream().map(i -> i.getId()).collect(Collectors.toList());
//修改部门负责人
List<String> userIdList = info.getChargePersonList();
if(userIdList!=null && userIdList.size()>0){
for(String userId: userIdList){
SysUser user = this.baseMapper.selectById(userId);
if(user!=null){
departChargeUserIdList.remove(user.getId());
user.setUserIdentity(2);
String departIds = user.getDepartIds();
if(oConvertUtils.isEmpty(departIds)){
user.setDepartIds(departId);
}else{
List<String> list = new ArrayList<String>(Arrays.asList(departIds.split(",")));
if(list.indexOf(departId)>=0){
continue;
}else{
list.add(departId);
String newDepartIds = String.join(",", list);
user.setDepartIds(newDepartIds);
}
}
this.baseMapper.updateById(user);
}
}
//删除
for(String chargeUserId: departChargeUserIdList){
for(SysUser chargeUser: departChargeUsers){
if(chargeUser.getId().equals(chargeUserId)){
String departIds = chargeUser.getDepartIds();
List<String> list = new ArrayList<String>(Arrays.asList(departIds.split(",")));
list.remove(departId);
String newDepartIds = String.join(",", list);
chargeUser.setDepartIds(newDepartIds);
this.baseMapper.updateById(chargeUser);
break;
}
}
}
}
}
}
private List<SysUser> queryDepartChargePersons(String departId){
List<SysUser> result = new ArrayList<>();
LambdaQueryWrapper<SysUserDepart> query1 = new LambdaQueryWrapper<SysUserDepart>()
.eq(SysUserDepart::getDepId, departId);
List<SysUserDepart> list1 = sysUserDepartMapper.selectList(query1);
if(list1!=null && list1.size()>0){
List<String> userIdList = list1.stream().map(item -> item.getUserId()).collect(Collectors.toList());
LambdaQueryWrapper<SysUser> query2 = new LambdaQueryWrapper<SysUser>()
.in(SysUser::getId, userIdList);
List<SysUser> userList = this.baseMapper.selectList(query2);
if(userList!=null && userList.size()>0){
for(SysUser user: userList){
Integer identity = user.getUserIdentity();
String deps = user.getDepartIds();
if(identity!=null && identity==2){
if(oConvertUtils.isNotEmpty(deps)){
if(deps.indexOf(departId)>=0){
result.add(user);
}
}
}
}
}
}
return result;
}
/**
* 变更父级部门 修改编码
* @param parentId
* @return
*/
private String getNextOrgCode(String parentId){
JSONObject formData = new JSONObject();
formData.put("parentId",parentId);
String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.DEPART, formData);
return codeArray[0];
}
@Override
public void changeDepartChargePerson(JSONObject json) {
String userId = json.getString("userId");
String departId = json.getString("departId");
boolean status = json.getBoolean("status");
SysUser user = this.getById(userId);
if(user!=null){
String ids = user.getDepartIds();
if(status==true){
//设置部门负责人
if(oConvertUtils.isEmpty(ids)){
user.setDepartIds(departId);
}else{
List<String> list = new ArrayList<String>(Arrays.asList(ids.split(",")));
if(list.indexOf(departId)>=0){
//啥也不干
}else{
list.add(departId);
String newIds = String.join(",", list);
user.setDepartIds(newIds);
}
}
}else{
// 取消负责人
if(oConvertUtils.isNotEmpty(ids)){
List<String> list = new ArrayList<String>();
for(String temp: ids.split(",")){
if(oConvertUtils.isEmpty(temp)){
continue;
}
if(!temp.equals(departId)){
list.add(temp);
}
}
String newIds = "";
if(list.size()>0){
newIds = String.join(",", list);
}
user.setDepartIds(newIds);
}
}
this.updateById(user);
}
}
/**
* 找上级部门
* @param depart
* @param orgName
* @param orgId
*/
private void getParentDepart(SysDepart depart,List<String> orgName,List<String> orgId){
String pid = depart.getParentId();
orgName.add(0, depart.getDepartName());
orgId.add(0, depart.getId());
if(oConvertUtils.isNotEmpty(pid)){
SysDepart temp = sysDepartMapper.selectById(pid);
getParentDepart(temp, orgName, orgId);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
@CacheEvict(value={CacheConstant.SYS_USERS_CACHE}, allEntries=true)
public void editTenantUser(SysUser sysUser, String tenantId, String departs, String roles) {
SysUser user = new SysUser();
user.setWorkNo(sysUser.getWorkNo());
user.setPost(sysUser.getPost());
user.setId(sysUser.getId());
this.updateById(user);
//修改租户用户下的部门
this.updateTenantDepart(user, tenantId, departs);
}
/**
* 修改租户下的部门
* @param departs
*/
public void updateTenantDepart(SysUser user, String tenantId, String departs) {
List<String> departList = new ArrayList<>();
if (oConvertUtils.isNotEmpty(departs)) {
//获取当前租户下的部门id,根据前台
departList = sysUserDepartMapper.getTenantDepart(Arrays.asList(departs.split(SymbolConstant.COMMA)), tenantId);
}
//查询当前租户下部门和用户已关联的部门
List<SysUserDepart> userDepartList = sysUserDepartMapper.getTenantUserDepart(user.getId(), tenantId);
if (userDepartList != null && userDepartList.size() > 0 && departList.size() > 0) {
for (SysUserDepart depart : userDepartList) {
//修改已关联部门删除部门用户角色关系
if (!departList.contains(depart.getDepId())) {
List<SysDepartRole> sysDepartRoleList = sysDepartRoleMapper.selectList(
new QueryWrapper<SysDepartRole>().lambda().eq(SysDepartRole::getDepartId, depart.getDepId()));
List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList());
if (roleIds.size() > 0) {
departRoleUserMapper.delete(new QueryWrapper<SysDepartRoleUser>().lambda().eq(SysDepartRoleUser::getUserId, user.getId())
.in(SysDepartRoleUser::getDroleId, roleIds));
}
}
}
}
if (departList.size() > 0) {
//删除用户下的部门
sysUserDepartMapper.deleteUserDepart(user.getId(), tenantId);
for (String departId : departList) {
//添加部门
SysUserDepart userDepart = new SysUserDepart(user.getId(), departId);
sysUserDepartMapper.insert(userDepart);
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment