Selaa lähdekoodia

code review 编码规范统一

安贞 3 vuotta sitten
vanhempi
commit
47447ded6b

+ 10 - 27
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<UserPostDO> {
 
-    default List<Long> selectIdList(Long id) {
+    default List<UserPostDO> selectIdList(Long id) {
         return selectList(new LambdaQueryWrapperX<UserPostDO>()
                 .eq(UserPostDO::getUserId, id)
-                .select(UserPostDO::getPostId)
-        )
-                .stream()
-                .map(UserPostDO::getPostId)
-                .collect(Collectors.toList());
+                .select(UserPostDO::getPostId));
 
     }
 
-    default void insertList(Long userId, Collection<Long> createPostIds) {
-        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) {
+    default void deleteByUserIdAndPostId(Long userId, Collection<Long> deletePostIds) {
         delete(new LambdaQueryWrapperX<UserPostDO>()
                 .eq(UserPostDO::getUserId, userId)
                 .in(UserPostDO::getPostId, deletePostIds));
     }
 
-    default List<Long> getUserIdByPostIds(Collection<Long> postIds) {
+    default List<UserPostDO> selectUserIdByPostIds(Collection<Long> postIds) {
         return selectList(new LambdaQueryWrapperX<UserPostDO>()
-                .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));
     }
 }

+ 0 - 3
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<AdminUserDO> {
         return selectList(AdminUserDO::getDeptId, deptIds);
     }
 
-    default List<AdminUserDO> selectListByIds(List<Long> userIdList) {
-        return selectList(new LambdaQueryWrapperX<AdminUserDO>().in(AdminUserDO::getId, userIdList));
-    }
 }
 

+ 35 - 16
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<Long> postIds = user.getPostIds();
-        if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) {
-            List<UserPostDO> userPostList = new ArrayList<>();
-            for (Long postId : postIds) {
+        if (CollectionUtil.isNotEmpty(postIds)) {
+            List<UserPostDO> 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<Long> postIds = updateObj.getPostIds();
         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> deletePostIds = CollUtil.subtract(dbPostIds, postIds);
         // 执行新增和删除。对于已经授权的菜单,不用做任何处理
         if (!CollectionUtil.isEmpty(createPostIds)) {
-            userPostMapper.insertList(userId, createPostIds);
+            List<UserPostDO> list = createUserPost(userId, createPostIds);
+            userPostMapper.insertBatch(list);
         }
         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
@@ -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<Long> userIdList = userPostMapper.getUserIdByPostIds(postIds);
+        List<Long> 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