fix: 保存用户添加用户名重复判断,保存用户角色代码优化

This commit is contained in:
郝先瑞 2022-08-16 22:17:30 +08:00
parent 177e3c29b0
commit 9cc6b340a6
3 changed files with 91 additions and 40 deletions

View File

@ -4,5 +4,16 @@ package com.youlai.admin.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.admin.pojo.entity.SysUserRole;
import java.util.List;
public interface SysUserRoleService extends IService<SysUserRole> {
/**
* 保存用户角色
*
* @param userId
* @param roleIds
* @return
*/
boolean saveUserRoles(Long userId, List<Long> roleIds);
}

View File

@ -1,12 +1,68 @@
package com.youlai.admin.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.admin.pojo.entity.SysUserRole;
import com.youlai.admin.mapper.SysUserRoleMapper;
import com.youlai.admin.pojo.entity.SysUserRole;
import com.youlai.admin.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
/**
* 保存用户角色
*
* @param userId
* @param roleIds
* @return
*/
@Override
public boolean saveUserRoles(Long userId, List<Long> roleIds) {
if (userId == null || CollectionUtil.isEmpty(roleIds)) {
return false;
}
// 用户原角色ID集合
List<Long> userRoleIds = this.list(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getUserId, userId))
.stream()
.map(item -> item.getRoleId())
.collect(Collectors.toList());
// 新增用户角色
List<Long> saveRoleIds;
if (CollectionUtil.isEmpty(userRoleIds)) {
saveRoleIds = roleIds;
} else {
saveRoleIds = roleIds.stream()
.filter(roleId -> !userRoleIds.contains(roleId))
.collect(Collectors.toList());
}
List<SysUserRole> saveUserRoles = saveRoleIds
.stream()
.map(roleId -> new SysUserRole(userId, roleId))
.collect(Collectors.toList());
this.saveBatch(saveUserRoles);
// 删除用户角色
if (CollectionUtil.isNotEmpty(userRoleIds)) {
List<Long> removeRoleIds = userRoleIds.stream()
.filter(roleId -> !roleIds.contains(roleId))
.collect(Collectors.toList());
this.remove(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getUserId, userId)
.in(SysUserRole::getRoleId, removeRoleIds)
);
}
return true;
}
}

View File

@ -13,12 +13,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.youlai.admin.pojo.vo.user.UserVO;
import com.youlai.common.constant.SystemConstants;
import com.youlai.common.enums.GenderEnum;
import com.youlai.admin.listener.excel.UserImportListener;
import com.youlai.admin.converter.UserConverter;
import com.youlai.admin.dto.UserAuthDTO;
import com.youlai.admin.listener.excel.UserImportListener;
import com.youlai.admin.mapper.SysUserMapper;
import com.youlai.admin.pojo.dto.UserImportDTO;
import com.youlai.admin.pojo.entity.SysUser;
@ -29,10 +26,13 @@ import com.youlai.admin.pojo.po.UserPO;
import com.youlai.admin.pojo.query.UserPageQuery;
import com.youlai.admin.pojo.vo.user.LoginUserVO;
import com.youlai.admin.pojo.vo.user.UserExportVO;
import com.youlai.admin.pojo.vo.user.UserVO;
import com.youlai.admin.service.SysPermissionService;
import com.youlai.admin.service.SysUserRoleService;
import com.youlai.admin.service.SysUserService;
import com.youlai.common.base.IBaseEnum;
import com.youlai.common.constant.SystemConstants;
import com.youlai.common.enums.GenderEnum;
import com.youlai.common.web.util.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
@ -44,7 +44,6 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@ -109,6 +108,11 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override
public boolean saveUser(UserForm userForm) {
String username = userForm.getUsername();
int count = this.count(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
Assert.isTrue(count == 0, "用户名已存在");
// 实体转换 form->entity
SysUser entity = userConverter.form2Entity(userForm);
@ -120,16 +124,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
boolean result = this.save(entity);
if (result) {
// 保存用户角色关联信息
Long userId = entity.getId();
List<Long> roleIds = userForm.getRoleIds();
if (CollectionUtil.isNotEmpty(roleIds)) {
List<SysUserRole> userRoles = roleIds
.stream()
.map(roleId -> new SysUserRole(userId, roleId))
.collect(Collectors.toList());
userRoleService.saveBatch(userRoles);
}
// 保存用户角色
userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
}
return result;
}
@ -144,38 +140,25 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override
@Transactional
public boolean updateUser(Long userId, UserForm userForm) {
// 用户的旧角色ID集合
List<Long> oldRoleIds = userRoleService.list(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getUserId, userId))
.stream()
.map(item -> item.getRoleId())
.collect(Collectors.toList());
// 用户的新角色ID集合
List<Long> newRoleIds = userForm.getRoleIds();
// 新增用户的角色
List<Long> addRoleIds = newRoleIds.stream().filter(roleId -> !oldRoleIds.contains(roleId)).collect(Collectors.toList());
List<SysUserRole> addUserRoles = Optional.ofNullable(addRoleIds).orElse(new ArrayList<>())
.stream().map(roleId -> new SysUserRole(userId, roleId))
.collect(Collectors.toList());
userRoleService.saveBatch(addUserRoles);
// 删除用户的角色
List<Long> removeRoleIds = oldRoleIds.stream().filter(roleId -> !newRoleIds.contains(roleId)).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(removeRoleIds)) {
userRoleService.remove(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getUserId, userId)
.in(SysUserRole::getRoleId, removeRoleIds)
);
}
String username = userForm.getUsername();
int count = this.count(new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getUsername, username)
.ne(SysUser::getId,userId)
);
Assert.isTrue(count == 0, "用户名已存在");
// form -> entity
SysUser entity = userConverter.form2Entity(userForm);
// 修改用户
boolean result = this.updateById(entity);
if (result) {
// 保存用户角色
userRoleService.saveUserRoles(entity.getId(), userForm.getRoleIds());
}
return result;
}
@ -358,4 +341,5 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return loginUserVO;
}
}