Commit 33c5f88872948bc35631b14180ace5bca1fe83d3

Authored by zichun
1 parent 3090964b

fix(usr): 修复 review round 1 must-fix REQ-USR-001

- getPermissionGroups 增加 brandId 多租户过滤
- EMPLOYEE_NOT_FOUND = 40401 常量替换魔法数字 40001
- 权限组绑定改为批量 insert(Collection) 消除 N+1
backend/src/main/java/com/example/erp/common/constants/UsrErrorCode.java
@@ -5,6 +5,7 @@ public final class UsrErrorCode { @@ -5,6 +5,7 @@ public final class UsrErrorCode {
5 public static final int PERMISSION_DENIED = 40300; 5 public static final int PERMISSION_DENIED = 40300;
6 public static final int USERNAME_EXISTS = 40901; 6 public static final int USERNAME_EXISTS = 40901;
7 public static final int USER_CODE_EXISTS = 40902; 7 public static final int USER_CODE_EXISTS = 40902;
  8 + public static final int EMPLOYEE_NOT_FOUND = 40401;
8 9
9 private UsrErrorCode() {} 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,7 +60,7 @@ public class UserServiceImpl implements UserService {
60 .eq(StaffEntity::getSId, req.getEmployeeId()) 60 .eq(StaffEntity::getSId, req.getEmployeeId())
61 .eq(StaffEntity::getSBrandsId, principal.brandId())); 61 .eq(StaffEntity::getSBrandsId, principal.brandId()));
62 if (staff == null) { 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,15 +81,18 @@ public class UserServiceImpl implements UserService {
81 userMapper.insert(user); 81 userMapper.insert(user);
82 82
83 if (req.getPermGroupIds() != null && !req.getPermGroupIds().isEmpty()) { 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 UserCreateRespVO vo = new UserCreateRespVO(); 98 UserCreateRespVO vo = new UserCreateRespVO();
@@ -116,7 +119,8 @@ public class UserServiceImpl implements UserService { @@ -116,7 +119,8 @@ public class UserServiceImpl implements UserService {
116 @Override 119 @Override
117 @Transactional(readOnly = true) 120 @Transactional(readOnly = true)
118 public List<PermissionGroupVO> getPermissionGroups(String brandId) { 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 return groups.stream().map(g -> { 124 return groups.stream().map(g -> {
121 PermissionGroupVO vo = new PermissionGroupVO(); 125 PermissionGroupVO vo = new PermissionGroupVO();
122 vo.setSId(g.getSId()); 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,6 +8,7 @@ import com.example.erp.module.usr.entity.UsrUserEntity;
8 import com.example.erp.module.usr.entity.UserPermissionEntity; 8 import com.example.erp.module.usr.entity.UserPermissionEntity;
9 import com.example.erp.module.usr.mapper.PermissionGroupMapper; 9 import com.example.erp.module.usr.mapper.PermissionGroupMapper;
10 import com.example.erp.module.usr.mapper.StaffMapper; 10 import com.example.erp.module.usr.mapper.StaffMapper;
  11 +import com.example.erp.common.constants.UsrErrorCode;
11 import com.example.erp.module.usr.mapper.UserPermissionMapper; 12 import com.example.erp.module.usr.mapper.UserPermissionMapper;
12 import com.example.erp.module.usr.mapper.UsrUserMapper; 13 import com.example.erp.module.usr.mapper.UsrUserMapper;
13 import com.example.erp.module.usr.service.impl.UserServiceImpl; 14 import com.example.erp.module.usr.service.impl.UserServiceImpl;
@@ -94,16 +95,16 @@ class UserServiceTest { @@ -94,16 +95,16 @@ class UserServiceTest {
94 } 95 }
95 96
96 @Test 97 @Test
  98 + @SuppressWarnings("unchecked")
97 void createUser_withPermGroups_insertsPermissions() { 99 void createUser_withPermGroups_insertsPermissions() {
98 req.setPermGroupIds(List.of("g1", "g2")); 100 req.setPermGroupIds(List.of("g1", "g2"));
99 when(userMapper.selectCount(any())).thenReturn(0L); 101 when(userMapper.selectCount(any())).thenReturn(0L);
100 when(passwordEncoder.encode(anyString())).thenReturn("$2a$10$hashed"); 102 when(passwordEncoder.encode(anyString())).thenReturn("$2a$10$hashed");
101 when(userMapper.insert(any(UsrUserEntity.class))).thenReturn(1); 103 when(userMapper.insert(any(UsrUserEntity.class))).thenReturn(1);
102 - when(userPermissionMapper.insert(any(UserPermissionEntity.class))).thenReturn(1);  
103 104
104 userService.createUser(req, superAdmin); 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 @Test 110 @Test
@@ -114,6 +115,6 @@ class UserServiceTest { @@ -114,6 +115,6 @@ class UserServiceTest {
114 115
115 BizException ex = assertThrows(BizException.class, 116 BizException ex = assertThrows(BizException.class,
116 () -> userService.createUser(req, superAdmin)); 117 () -> userService.createUser(req, superAdmin));
117 - assertEquals(40001, ex.getCode()); 118 + assertEquals(UsrErrorCode.EMPLOYEE_NOT_FOUND, ex.getCode());
118 } 119 }
119 } 120 }