From 47447ded6b771894ab0e9e8c2ad4e4be248b0547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E8=B4=9E?= Date: Wed, 6 Apr 2022 13:58:08 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=20=E7=BC=96=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/dal/mysql/dept/UserPostMapper.java | 37 ++++---------- .../dal/mysql/user/AdminUserMapper.java | 3 -- .../service/user/AdminUserServiceImpl.java | 51 +++++++++++++------ 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java index a5f1fce84..f91ce2ef6 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java @@ -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.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; import org.apache.ibatis.annotations.Mapper; import java.util.Collection; import java.util.List; -import java.util.stream.Collectors; @Mapper public interface UserPostMapper extends BaseMapperX { - default List selectIdList(Long id) { + default List selectIdList(Long id) { return selectList(new LambdaQueryWrapperX() .eq(UserPostDO::getUserId, id) - .select(UserPostDO::getPostId) - ) - .stream() - .map(UserPostDO::getPostId) - .collect(Collectors.toList()); + .select(UserPostDO::getPostId)); } - default void insertList(Long userId, Collection createPostIds) { - List 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 deletePostIds) { + default void deleteByUserIdAndPostId(Long userId, Collection deletePostIds) { delete(new LambdaQueryWrapperX() .eq(UserPostDO::getUserId, userId) .in(UserPostDO::getPostId, deletePostIds)); } - default List getUserIdByPostIds(Collection postIds) { + default List selectUserIdByPostIds(Collection postIds) { return selectList(new LambdaQueryWrapperX() - .in(UserPostDO::getPostId, postIds)) - .stream() - .map(UserPostDO::getUserId) - .distinct() - .collect(Collectors.toList()); + .in(UserPostDO::getPostId, postIds)); + } + + default void deleteByUserId(Long userId){ + delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId)); } } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/user/AdminUserMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/user/AdminUserMapper.java index 68acb7dcd..be4b5ea21 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/user/AdminUserMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/user/AdminUserMapper.java @@ -61,8 +61,5 @@ public interface AdminUserMapper extends BaseMapperX { return selectList(AdminUserDO::getDeptId, deptIds); } - default List selectListByIds(List userIdList) { - return selectList(new LambdaQueryWrapperX().in(AdminUserDO::getId, userIdList)); - } } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java index e651b1513..5be5ff16f 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java @@ -107,15 +107,14 @@ public class AdminUserServiceImpl implements AdminUserService { user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码 userMapper.insert(user); Set postIds = user.getPostIds(); - if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { - List userPostList = new ArrayList<>(); - for (Long postId : postIds) { + if (CollectionUtil.isNotEmpty(postIds)) { + List insertUserPostList = CollectionUtils.convertList(postIds, postId -> { UserPostDO entity = new UserPostDO(); entity.setUserId(user.getId()); entity.setPostId(postId); - userPostList.add(entity); - } - userPostMapper.insertBatch(userPostList); + return entity; + }); + userPostMapper.insertBatch(insertUserPostList); } return user.getId(); } @@ -130,20 +129,40 @@ public class AdminUserServiceImpl implements AdminUserService { AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); userMapper.updateById(updateObj); // 更新岗位 + updateUserPost(reqVO, updateObj); + + } + + private void updateUserPost(UserUpdateReqVO reqVO, AdminUserDO updateObj) { Set postIds = updateObj.getPostIds(); Long userId = reqVO.getId(); - List dbPostIds = userPostMapper.selectIdList(userId); + List dbPostIds = userPostMapper.selectIdList(userId) + .stream() + .map(UserPostDO::getPostId) + .collect(Collectors.toList()); // 计算新增和删除的岗位编号 Collection createPostIds = CollUtil.subtract(postIds, dbPostIds); Collection deletePostIds = CollUtil.subtract(dbPostIds, postIds); // 执行新增和删除。对于已经授权的菜单,不用做任何处理 if (!CollectionUtil.isEmpty(createPostIds)) { - userPostMapper.insertList(userId, createPostIds); + List list = createUserPost(userId, createPostIds); + userPostMapper.insertBatch(list); } if (!CollectionUtil.isEmpty(deletePostIds)) { - userPostMapper.deleteByUserAndPost(userId, deletePostIds); + userPostMapper.deleteByUserIdAndPostId(userId, deletePostIds); } + } + private List createUserPost(Long userId, Collection createPostIds) { + return createPostIds + .stream() + .map(postId -> { + UserPostDO entity = new UserPostDO(); + entity.setUserId(userId); + entity.setPostId(postId); + return entity; + }) + .collect(Collectors.toList()); } @Override @@ -216,7 +235,7 @@ public class AdminUserServiceImpl implements AdminUserService { // 删除用户关联数据 permissionService.processUserDeleted(id); // 删除用户岗位 - userPostMapper.delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, id)); + userPostMapper.deleteByUserId(id); } @Override @@ -247,15 +266,15 @@ public class AdminUserServiceImpl implements AdminUserService { if (CollUtil.isEmpty(postIds)) { return Collections.emptyList(); } - List userIdList = userPostMapper.getUserIdByPostIds(postIds); + List userIdList = userPostMapper.selectUserIdByPostIds(postIds) + .stream() + .map(UserPostDO::getUserId) + .distinct() + .collect(Collectors.toList());; if (userIdList.isEmpty()) { return Collections.emptyList(); } - return userMapper - .selectListByIds(userIdList) - .stream() - .peek(user -> user.setPassword(null)) - .collect(Collectors.toList()); + return userMapper.selectBatchIds(userIdList); } @Override