Commit 47447ded6b771894ab0e9e8c2ad4e4be248b0547

Authored by 安贞
1 parent 78c132e6

code review 编码规范统一

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;
3 3 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
4 4 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
5 5 import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
  6 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
6 7 import org.apache.ibatis.annotations.Mapper;
7 8  
8 9 import java.util.Collection;
9 10 import java.util.List;
10   -import java.util.stream.Collectors;
11 11  
12 12 @Mapper
13 13 public interface UserPostMapper extends BaseMapperX<UserPostDO> {
14 14  
15   - default List<Long> selectIdList(Long id) {
  15 + default List<UserPostDO> selectIdList(Long id) {
16 16 return selectList(new LambdaQueryWrapperX<UserPostDO>()
17 17 .eq(UserPostDO::getUserId, id)
18   - .select(UserPostDO::getPostId)
19   - )
20   - .stream()
21   - .map(UserPostDO::getPostId)
22   - .collect(Collectors.toList());
  18 + .select(UserPostDO::getPostId));
23 19  
24 20 }
25 21  
26   - default void insertList(Long userId, Collection<Long> createPostIds) {
27   - List<UserPostDO> list = createPostIds
28   - .stream()
29   - .map(postId -> {
30   - UserPostDO entity = new UserPostDO();
31   - entity.setUserId(userId);
32   - entity.setPostId(postId);
33   - return entity;
34   - })
35   - .collect(Collectors.toList());
36   - insertBatch(list);
37   - }
38   -
39   - default void deleteByUserAndPost(Long userId, Collection<Long> deletePostIds) {
  22 + default void deleteByUserIdAndPostId(Long userId, Collection<Long> deletePostIds) {
40 23 delete(new LambdaQueryWrapperX<UserPostDO>()
41 24 .eq(UserPostDO::getUserId, userId)
42 25 .in(UserPostDO::getPostId, deletePostIds));
43 26 }
44 27  
45   - default List<Long> getUserIdByPostIds(Collection<Long> postIds) {
  28 + default List<UserPostDO> selectUserIdByPostIds(Collection<Long> postIds) {
46 29 return selectList(new LambdaQueryWrapperX<UserPostDO>()
47   - .in(UserPostDO::getPostId, postIds))
48   - .stream()
49   - .map(UserPostDO::getUserId)
50   - .distinct()
51   - .collect(Collectors.toList());
  30 + .in(UserPostDO::getPostId, postIds));
  31 + }
  32 +
  33 + default void deleteByUserId(Long userId){
  34 + delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId));
52 35 }
53 36 }
... ...
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&lt;AdminUserDO&gt; {
61 61 return selectList(AdminUserDO::getDeptId, deptIds);
62 62 }
63 63  
64   - default List<AdminUserDO> selectListByIds(List<Long> userIdList) {
65   - return selectList(new LambdaQueryWrapperX<AdminUserDO>().in(AdminUserDO::getId, userIdList));
66   - }
67 64 }
68 65  
... ...
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 {
107 107 user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码
108 108 userMapper.insert(user);
109 109 Set<Long> postIds = user.getPostIds();
110   - if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) {
111   - List<UserPostDO> userPostList = new ArrayList<>();
112   - for (Long postId : postIds) {
  110 + if (CollectionUtil.isNotEmpty(postIds)) {
  111 + List<UserPostDO> insertUserPostList = CollectionUtils.convertList(postIds, postId -> {
113 112 UserPostDO entity = new UserPostDO();
114 113 entity.setUserId(user.getId());
115 114 entity.setPostId(postId);
116   - userPostList.add(entity);
117   - }
118   - userPostMapper.insertBatch(userPostList);
  115 + return entity;
  116 + });
  117 + userPostMapper.insertBatch(insertUserPostList);
119 118 }
120 119 return user.getId();
121 120 }
... ... @@ -130,20 +129,40 @@ public class AdminUserServiceImpl implements AdminUserService {
130 129 AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO);
131 130 userMapper.updateById(updateObj);
132 131 // 更新岗位
  132 + updateUserPost(reqVO, updateObj);
  133 +
  134 + }
  135 +
  136 + private void updateUserPost(UserUpdateReqVO reqVO, AdminUserDO updateObj) {
133 137 Set<Long> postIds = updateObj.getPostIds();
134 138 Long userId = reqVO.getId();
135   - List<Long> dbPostIds = userPostMapper.selectIdList(userId);
  139 + List<Long> dbPostIds = userPostMapper.selectIdList(userId)
  140 + .stream()
  141 + .map(UserPostDO::getPostId)
  142 + .collect(Collectors.toList());
136 143 // 计算新增和删除的岗位编号
137 144 Collection<Long> createPostIds = CollUtil.subtract(postIds, dbPostIds);
138 145 Collection<Long> deletePostIds = CollUtil.subtract(dbPostIds, postIds);
139 146 // 执行新增和删除。对于已经授权的菜单,不用做任何处理
140 147 if (!CollectionUtil.isEmpty(createPostIds)) {
141   - userPostMapper.insertList(userId, createPostIds);
  148 + List<UserPostDO> list = createUserPost(userId, createPostIds);
  149 + userPostMapper.insertBatch(list);
142 150 }
143 151 if (!CollectionUtil.isEmpty(deletePostIds)) {
144   - userPostMapper.deleteByUserAndPost(userId, deletePostIds);
  152 + userPostMapper.deleteByUserIdAndPostId(userId, deletePostIds);
145 153 }
  154 + }
146 155  
  156 + private List<UserPostDO> createUserPost(Long userId, Collection<Long> createPostIds) {
  157 + return createPostIds
  158 + .stream()
  159 + .map(postId -> {
  160 + UserPostDO entity = new UserPostDO();
  161 + entity.setUserId(userId);
  162 + entity.setPostId(postId);
  163 + return entity;
  164 + })
  165 + .collect(Collectors.toList());
147 166 }
148 167  
149 168 @Override
... ... @@ -216,7 +235,7 @@ public class AdminUserServiceImpl implements AdminUserService {
216 235 // 删除用户关联数据
217 236 permissionService.processUserDeleted(id);
218 237 // 删除用户岗位
219   - userPostMapper.delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, id));
  238 + userPostMapper.deleteByUserId(id);
220 239 }
221 240  
222 241 @Override
... ... @@ -247,15 +266,15 @@ public class AdminUserServiceImpl implements AdminUserService {
247 266 if (CollUtil.isEmpty(postIds)) {
248 267 return Collections.emptyList();
249 268 }
250   - List<Long> userIdList = userPostMapper.getUserIdByPostIds(postIds);
  269 + List<Long> userIdList = userPostMapper.selectUserIdByPostIds(postIds)
  270 + .stream()
  271 + .map(UserPostDO::getUserId)
  272 + .distinct()
  273 + .collect(Collectors.toList());;
251 274 if (userIdList.isEmpty()) {
252 275 return Collections.emptyList();
253 276 }
254   - return userMapper
255   - .selectListByIds(userIdList)
256   - .stream()
257   - .peek(user -> user.setPassword(null))
258   - .collect(Collectors.toList());
  277 + return userMapper.selectBatchIds(userIdList);
259 278 }
260 279  
261 280 @Override
... ...