Commit 33c5f88872948bc35631b14180ace5bca1fe83d3
1 parent
3090964b
fix(usr): 修复 review round 1 must-fix REQ-USR-001
- getPermissionGroups 增加 brandId 多租户过滤 - EMPLOYEE_NOT_FOUND = 40401 常量替换魔法数字 40001 - 权限组绑定改为批量 insert(Collection) 消除 N+1
Showing
3 changed files
with
20 additions
and
14 deletions
backend/src/main/java/com/example/erp/common/constants/UsrErrorCode.java
| ... | ... | @@ -5,6 +5,7 @@ public final class UsrErrorCode { |
| 5 | 5 | public static final int PERMISSION_DENIED = 40300; |
| 6 | 6 | public static final int USERNAME_EXISTS = 40901; |
| 7 | 7 | public static final int USER_CODE_EXISTS = 40902; |
| 8 | + public static final int EMPLOYEE_NOT_FOUND = 40401; | |
| 8 | 9 | |
| 9 | 10 | private UsrErrorCode() {} |
| 10 | 11 | } | ... | ... |
backend/src/main/java/com/example/erp/module/usr/service/impl/UserServiceImpl.java
| ... | ... | @@ -60,7 +60,7 @@ public class UserServiceImpl implements UserService { |
| 60 | 60 | .eq(StaffEntity::getSId, req.getEmployeeId()) |
| 61 | 61 | .eq(StaffEntity::getSBrandsId, principal.brandId())); |
| 62 | 62 | if (staff == null) { |
| 63 | - throw new BizException(40001, "员工不存在"); | |
| 63 | + throw new BizException(UsrErrorCode.EMPLOYEE_NOT_FOUND, "员工不存在"); | |
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| ... | ... | @@ -81,15 +81,18 @@ public class UserServiceImpl implements UserService { |
| 81 | 81 | userMapper.insert(user); |
| 82 | 82 | |
| 83 | 83 | if (req.getPermGroupIds() != null && !req.getPermGroupIds().isEmpty()) { |
| 84 | - for (String groupId : req.getPermGroupIds()) { | |
| 85 | - UserPermissionEntity perm = new UserPermissionEntity(); | |
| 86 | - perm.setSId(UUID.randomUUID().toString()); | |
| 87 | - perm.setSBrandsId(principal.brandId()); | |
| 88 | - perm.setTCreateDate(LocalDateTime.now()); | |
| 89 | - perm.setSUserId(user.getSId()); | |
| 90 | - perm.setSPermGroupId(groupId); | |
| 91 | - userPermissionMapper.insert(perm); | |
| 92 | - } | |
| 84 | + List<UserPermissionEntity> perms = req.getPermGroupIds().stream() | |
| 85 | + .map(groupId -> { | |
| 86 | + UserPermissionEntity perm = new UserPermissionEntity(); | |
| 87 | + perm.setSId(UUID.randomUUID().toString()); | |
| 88 | + perm.setSBrandsId(principal.brandId()); | |
| 89 | + perm.setTCreateDate(LocalDateTime.now()); | |
| 90 | + perm.setSUserId(user.getSId()); | |
| 91 | + perm.setSPermGroupId(groupId); | |
| 92 | + return perm; | |
| 93 | + }) | |
| 94 | + .collect(Collectors.toList()); | |
| 95 | + userPermissionMapper.insert(perms); | |
| 93 | 96 | } |
| 94 | 97 | |
| 95 | 98 | UserCreateRespVO vo = new UserCreateRespVO(); |
| ... | ... | @@ -116,7 +119,8 @@ public class UserServiceImpl implements UserService { |
| 116 | 119 | @Override |
| 117 | 120 | @Transactional(readOnly = true) |
| 118 | 121 | public List<PermissionGroupVO> getPermissionGroups(String brandId) { |
| 119 | - List<PermissionGroupEntity> groups = permGroupMapper.selectList(null); | |
| 122 | + List<PermissionGroupEntity> groups = permGroupMapper.selectList(new LambdaQueryWrapper<PermissionGroupEntity>() | |
| 123 | + .eq(PermissionGroupEntity::getSBrandsId, brandId)); | |
| 120 | 124 | return groups.stream().map(g -> { |
| 121 | 125 | PermissionGroupVO vo = new PermissionGroupVO(); |
| 122 | 126 | vo.setSId(g.getSId()); | ... | ... |
backend/src/test/java/com/example/erp/module/usr/UserServiceTest.java
| ... | ... | @@ -8,6 +8,7 @@ import com.example.erp.module.usr.entity.UsrUserEntity; |
| 8 | 8 | import com.example.erp.module.usr.entity.UserPermissionEntity; |
| 9 | 9 | import com.example.erp.module.usr.mapper.PermissionGroupMapper; |
| 10 | 10 | import com.example.erp.module.usr.mapper.StaffMapper; |
| 11 | +import com.example.erp.common.constants.UsrErrorCode; | |
| 11 | 12 | import com.example.erp.module.usr.mapper.UserPermissionMapper; |
| 12 | 13 | import com.example.erp.module.usr.mapper.UsrUserMapper; |
| 13 | 14 | import com.example.erp.module.usr.service.impl.UserServiceImpl; |
| ... | ... | @@ -94,16 +95,16 @@ class UserServiceTest { |
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | @Test |
| 98 | + @SuppressWarnings("unchecked") | |
| 97 | 99 | void createUser_withPermGroups_insertsPermissions() { |
| 98 | 100 | req.setPermGroupIds(List.of("g1", "g2")); |
| 99 | 101 | when(userMapper.selectCount(any())).thenReturn(0L); |
| 100 | 102 | when(passwordEncoder.encode(anyString())).thenReturn("$2a$10$hashed"); |
| 101 | 103 | when(userMapper.insert(any(UsrUserEntity.class))).thenReturn(1); |
| 102 | - when(userPermissionMapper.insert(any(UserPermissionEntity.class))).thenReturn(1); | |
| 103 | 104 | |
| 104 | 105 | userService.createUser(req, superAdmin); |
| 105 | 106 | |
| 106 | - verify(userPermissionMapper, times(2)).insert(any(UserPermissionEntity.class)); | |
| 107 | + verify(userPermissionMapper).insert(argThat((java.util.Collection<UserPermissionEntity> c) -> c.size() == 2)); | |
| 107 | 108 | } |
| 108 | 109 | |
| 109 | 110 | @Test |
| ... | ... | @@ -114,6 +115,6 @@ class UserServiceTest { |
| 114 | 115 | |
| 115 | 116 | BizException ex = assertThrows(BizException.class, |
| 116 | 117 | () -> userService.createUser(req, superAdmin)); |
| 117 | - assertEquals(40001, ex.getCode()); | |
| 118 | + assertEquals(UsrErrorCode.EMPLOYEE_NOT_FOUND, ex.getCode()); | |
| 118 | 119 | } |
| 119 | 120 | } | ... | ... |