code review 编码规范统一

This commit is contained in:
安贞 2022-04-06 13:58:08 +08:00
parent 78c132e68d
commit 47447ded6b
3 changed files with 45 additions and 46 deletions

View File

@ -3,51 +3,34 @@ package cn.iocoder.yudao.module.system.dal.mysql.dept;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Mapper @Mapper
public interface UserPostMapper extends BaseMapperX<UserPostDO> { public interface UserPostMapper extends BaseMapperX<UserPostDO> {
default List<Long> selectIdList(Long id) { default List<UserPostDO> selectIdList(Long id) {
return selectList(new LambdaQueryWrapperX<UserPostDO>() return selectList(new LambdaQueryWrapperX<UserPostDO>()
.eq(UserPostDO::getUserId, id) .eq(UserPostDO::getUserId, id)
.select(UserPostDO::getPostId) .select(UserPostDO::getPostId));
)
.stream()
.map(UserPostDO::getPostId)
.collect(Collectors.toList());
} }
default void insertList(Long userId, Collection<Long> createPostIds) { default void deleteByUserIdAndPostId(Long userId, Collection<Long> deletePostIds) {
List<UserPostDO> list = createPostIds
.stream()
.map(postId -> {
UserPostDO entity = new UserPostDO();
entity.setUserId(userId);
entity.setPostId(postId);
return entity;
})
.collect(Collectors.toList());
insertBatch(list);
}
default void deleteByUserAndPost(Long userId, Collection<Long> deletePostIds) {
delete(new LambdaQueryWrapperX<UserPostDO>() delete(new LambdaQueryWrapperX<UserPostDO>()
.eq(UserPostDO::getUserId, userId) .eq(UserPostDO::getUserId, userId)
.in(UserPostDO::getPostId, deletePostIds)); .in(UserPostDO::getPostId, deletePostIds));
} }
default List<Long> getUserIdByPostIds(Collection<Long> postIds) { default List<UserPostDO> selectUserIdByPostIds(Collection<Long> postIds) {
return selectList(new LambdaQueryWrapperX<UserPostDO>() return selectList(new LambdaQueryWrapperX<UserPostDO>()
.in(UserPostDO::getPostId, postIds)) .in(UserPostDO::getPostId, postIds));
.stream() }
.map(UserPostDO::getUserId)
.distinct() default void deleteByUserId(Long userId){
.collect(Collectors.toList()); delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId));
} }
} }

View File

@ -61,8 +61,5 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {
return selectList(AdminUserDO::getDeptId, deptIds); return selectList(AdminUserDO::getDeptId, deptIds);
} }
default List<AdminUserDO> selectListByIds(List<Long> userIdList) {
return selectList(new LambdaQueryWrapperX<AdminUserDO>().in(AdminUserDO::getId, userIdList));
}
} }

View File

@ -107,15 +107,14 @@ public class AdminUserServiceImpl implements AdminUserService {
user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码 user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码
userMapper.insert(user); userMapper.insert(user);
Set<Long> postIds = user.getPostIds(); Set<Long> postIds = user.getPostIds();
if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { if (CollectionUtil.isNotEmpty(postIds)) {
List<UserPostDO> userPostList = new ArrayList<>(); List<UserPostDO> insertUserPostList = CollectionUtils.convertList(postIds, postId -> {
for (Long postId : postIds) {
UserPostDO entity = new UserPostDO(); UserPostDO entity = new UserPostDO();
entity.setUserId(user.getId()); entity.setUserId(user.getId());
entity.setPostId(postId); entity.setPostId(postId);
userPostList.add(entity); return entity;
} });
userPostMapper.insertBatch(userPostList); userPostMapper.insertBatch(insertUserPostList);
} }
return user.getId(); return user.getId();
} }
@ -130,20 +129,40 @@ public class AdminUserServiceImpl implements AdminUserService {
AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO);
userMapper.updateById(updateObj); userMapper.updateById(updateObj);
// 更新岗位 // 更新岗位
updateUserPost(reqVO, updateObj);
}
private void updateUserPost(UserUpdateReqVO reqVO, AdminUserDO updateObj) {
Set<Long> postIds = updateObj.getPostIds(); Set<Long> postIds = updateObj.getPostIds();
Long userId = reqVO.getId(); Long userId = reqVO.getId();
List<Long> dbPostIds = userPostMapper.selectIdList(userId); List<Long> dbPostIds = userPostMapper.selectIdList(userId)
.stream()
.map(UserPostDO::getPostId)
.collect(Collectors.toList());
// 计算新增和删除的岗位编号 // 计算新增和删除的岗位编号
Collection<Long> createPostIds = CollUtil.subtract(postIds, dbPostIds); Collection<Long> createPostIds = CollUtil.subtract(postIds, dbPostIds);
Collection<Long> deletePostIds = CollUtil.subtract(dbPostIds, postIds); Collection<Long> deletePostIds = CollUtil.subtract(dbPostIds, postIds);
// 执行新增和删除对于已经授权的菜单不用做任何处理 // 执行新增和删除对于已经授权的菜单不用做任何处理
if (!CollectionUtil.isEmpty(createPostIds)) { if (!CollectionUtil.isEmpty(createPostIds)) {
userPostMapper.insertList(userId, createPostIds); List<UserPostDO> list = createUserPost(userId, createPostIds);
userPostMapper.insertBatch(list);
} }
if (!CollectionUtil.isEmpty(deletePostIds)) { if (!CollectionUtil.isEmpty(deletePostIds)) {
userPostMapper.deleteByUserAndPost(userId, deletePostIds); userPostMapper.deleteByUserIdAndPostId(userId, deletePostIds);
} }
}
private List<UserPostDO> createUserPost(Long userId, Collection<Long> createPostIds) {
return createPostIds
.stream()
.map(postId -> {
UserPostDO entity = new UserPostDO();
entity.setUserId(userId);
entity.setPostId(postId);
return entity;
})
.collect(Collectors.toList());
} }
@Override @Override
@ -216,7 +235,7 @@ public class AdminUserServiceImpl implements AdminUserService {
// 删除用户关联数据 // 删除用户关联数据
permissionService.processUserDeleted(id); permissionService.processUserDeleted(id);
// 删除用户岗位 // 删除用户岗位
userPostMapper.delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, id)); userPostMapper.deleteByUserId(id);
} }
@Override @Override
@ -247,15 +266,15 @@ public class AdminUserServiceImpl implements AdminUserService {
if (CollUtil.isEmpty(postIds)) { if (CollUtil.isEmpty(postIds)) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<Long> userIdList = userPostMapper.getUserIdByPostIds(postIds); List<Long> userIdList = userPostMapper.selectUserIdByPostIds(postIds)
.stream()
.map(UserPostDO::getUserId)
.distinct()
.collect(Collectors.toList());;
if (userIdList.isEmpty()) { if (userIdList.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
return userMapper return userMapper.selectBatchIds(userIdList);
.selectListByIds(userIdList)
.stream()
.peek(user -> user.setPassword(null))
.collect(Collectors.toList());
} }
@Override @Override