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,51 +3,34 @@ package cn.iocoder.yudao.module.system.dal.mysql.dept;
3 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; 3 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
4 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; 4 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
5 import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; 5 import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
  6 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
6 import org.apache.ibatis.annotations.Mapper; 7 import org.apache.ibatis.annotations.Mapper;
7 8
8 import java.util.Collection; 9 import java.util.Collection;
9 import java.util.List; 10 import java.util.List;
10 -import java.util.stream.Collectors;  
11 11
12 @Mapper 12 @Mapper
13 public interface UserPostMapper extends BaseMapperX<UserPostDO> { 13 public interface UserPostMapper extends BaseMapperX<UserPostDO> {
14 14
15 - default List<Long> selectIdList(Long id) { 15 + default List<UserPostDO> selectIdList(Long id) {
16 return selectList(new LambdaQueryWrapperX<UserPostDO>() 16 return selectList(new LambdaQueryWrapperX<UserPostDO>()
17 .eq(UserPostDO::getUserId, id) 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 delete(new LambdaQueryWrapperX<UserPostDO>() 23 delete(new LambdaQueryWrapperX<UserPostDO>()
41 .eq(UserPostDO::getUserId, userId) 24 .eq(UserPostDO::getUserId, userId)
42 .in(UserPostDO::getPostId, deletePostIds)); 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 return selectList(new LambdaQueryWrapperX<UserPostDO>() 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,8 +61,5 @@ public interface AdminUserMapper extends BaseMapperX&lt;AdminUserDO&gt; {
61 return selectList(AdminUserDO::getDeptId, deptIds); 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,15 +107,14 @@ public class AdminUserServiceImpl implements AdminUserService {
107 user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码 107 user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码
108 userMapper.insert(user); 108 userMapper.insert(user);
109 Set<Long> postIds = user.getPostIds(); 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 UserPostDO entity = new UserPostDO(); 112 UserPostDO entity = new UserPostDO();
114 entity.setUserId(user.getId()); 113 entity.setUserId(user.getId());
115 entity.setPostId(postId); 114 entity.setPostId(postId);
116 - userPostList.add(entity);  
117 - }  
118 - userPostMapper.insertBatch(userPostList); 115 + return entity;
  116 + });
  117 + userPostMapper.insertBatch(insertUserPostList);
119 } 118 }
120 return user.getId(); 119 return user.getId();
121 } 120 }
@@ -130,20 +129,40 @@ public class AdminUserServiceImpl implements AdminUserService { @@ -130,20 +129,40 @@ public class AdminUserServiceImpl implements AdminUserService {
130 AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); 129 AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO);
131 userMapper.updateById(updateObj); 130 userMapper.updateById(updateObj);
132 // 更新岗位 131 // 更新岗位
  132 + updateUserPost(reqVO, updateObj);
  133 +
  134 + }
  135 +
  136 + private void updateUserPost(UserUpdateReqVO reqVO, AdminUserDO updateObj) {
133 Set<Long> postIds = updateObj.getPostIds(); 137 Set<Long> postIds = updateObj.getPostIds();
134 Long userId = reqVO.getId(); 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 Collection<Long> createPostIds = CollUtil.subtract(postIds, dbPostIds); 144 Collection<Long> createPostIds = CollUtil.subtract(postIds, dbPostIds);
138 Collection<Long> deletePostIds = CollUtil.subtract(dbPostIds, postIds); 145 Collection<Long> deletePostIds = CollUtil.subtract(dbPostIds, postIds);
139 // 执行新增和删除。对于已经授权的菜单,不用做任何处理 146 // 执行新增和删除。对于已经授权的菜单,不用做任何处理
140 if (!CollectionUtil.isEmpty(createPostIds)) { 147 if (!CollectionUtil.isEmpty(createPostIds)) {
141 - userPostMapper.insertList(userId, createPostIds); 148 + List<UserPostDO> list = createUserPost(userId, createPostIds);
  149 + userPostMapper.insertBatch(list);
142 } 150 }
143 if (!CollectionUtil.isEmpty(deletePostIds)) { 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 @Override 168 @Override
@@ -216,7 +235,7 @@ public class AdminUserServiceImpl implements AdminUserService { @@ -216,7 +235,7 @@ public class AdminUserServiceImpl implements AdminUserService {
216 // 删除用户关联数据 235 // 删除用户关联数据
217 permissionService.processUserDeleted(id); 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 @Override 241 @Override
@@ -247,15 +266,15 @@ public class AdminUserServiceImpl implements AdminUserService { @@ -247,15 +266,15 @@ public class AdminUserServiceImpl implements AdminUserService {
247 if (CollUtil.isEmpty(postIds)) { 266 if (CollUtil.isEmpty(postIds)) {
248 return Collections.emptyList(); 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 if (userIdList.isEmpty()) { 274 if (userIdList.isEmpty()) {
252 return Collections.emptyList(); 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 @Override 280 @Override