refactor(系统设置): 优化组织与项目&组织用户组下拉用户选项查询

This commit is contained in:
song-cc-rock 2023-09-04 15:30:44 +08:00 committed by 刘瑞斌
parent 1e5a94c05f
commit 8e9e01a8b4
7 changed files with 45 additions and 19 deletions

View File

@ -118,7 +118,9 @@ public class SystemOrganizationController {
@Operation(summary = "系统设置-系统-组织与项目-获取成员下拉选项")
@RequiresPermissions(value = {PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ})
@Parameter(name = "sourceId", description = "组织ID或项目ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
public List<UserExtend> getMemberOption(@PathVariable String sourceId) {
return userService.getMemberOption(sourceId);
public List<UserExtend> getMemberOption(@PathVariable String sourceId,
@Schema(description = "查询关键字,根据邮箱和用户名查询")
@RequestParam(value = "keyword", required = false) String keyword) {
return userService.getMemberOption(sourceId, keyword);
}
}

View File

@ -8,7 +8,7 @@ import java.util.List;
public interface ExtUserMapper {
List<UserExtend> getMemberOption(String sourceId);
List<UserExtend> getMemberOption(@Param("sourceId") String sourceId, @Param("keyword") String keyword);
List<User> getUserListByOrgId(@Param("sourceId") String sourceId, @Param("keyword") String keyword);

View File

@ -5,7 +5,11 @@
select distinct u.*, count(urr.id) > 0 as memberFlag
from `user` u left join user_role_relation urr on urr.user_id = u.id and urr.source_id = #{sourceId}
where u.deleted = 0
<if test="keyword != null and keyword != ''">
and (u.name like concat('%', #{keyword}, '%') or u.email like concat('%', #{keyword}, '%'))
</if>
group by u.id
limit 100
</select>
<select id="getUserListByOrgId" resultType="io.metersphere.system.domain.User">

View File

@ -8,6 +8,7 @@ import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.mapper.BaseUserMapper;
import io.metersphere.sdk.service.BaseUserRoleService;
import io.metersphere.sdk.service.BaseUserService;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
@ -42,6 +43,8 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
@Resource
BaseUserMapper baseUserMapper;
@Resource
BaseUserService baseUserService;
@Resource
UserRoleMapper userRoleMapper;
@Resource
ExtUserRoleMapper extUserRoleMapper;
@ -83,9 +86,6 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
public List<UserExtend> getMember(String organizationId, String roleId) {
List<UserExtend> userExtends = new ArrayList<>();
// 查询所有用户
List<User> users = baseUserMapper.findAll();
Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
// 查询组织下所有用户关系
UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria().andSourceIdEqualTo(organizationId);
@ -95,17 +95,32 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
Collectors.mapping(UserRoleRelation::getRoleId, Collectors.toList())));
userRoleMap.forEach((k, v) -> {
UserExtend userExtend = new UserExtend();
User user = userMap.get(k);
if (user != null) {
BeanUtils.copyBean(userExtend, user);
userExtend.setId(k);
v.forEach(roleItem -> {
if (StringUtils.equals(roleItem, roleId)) {
// 该用户已存在用户组关系, 设置为选中状态
userExtend.setCheckRoleFlag(true);
}
});
userExtends.add(userExtend);
}
});
// 设置用户信息, 用户不存在或者已删除, 则不展示
List<String> userIds = userExtends.stream().map(UserExtend::getId).toList();
UserExample userExample = new UserExample();
userExample.createCriteria().andIdIn(userIds).andDeletedEqualTo(false);
List<User> users = userMapper.selectByExample(userExample);
if (CollectionUtils.isNotEmpty(users)) {
Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
userExtends.removeIf(userExtend -> {
if (userMap.containsKey(userExtend.getId())) {
BeanUtils.copyBean(userExtend, userMap.get(userExtend.getId()));
return false;
}
return true;
});
} else {
userExtends.clear();
}
}
return userExtends;
}

View File

@ -307,8 +307,8 @@ public class UserService {
* @param sourceId 组织ID, 项目ID
* @return 系统用户选项
*/
public List<UserExtend> getMemberOption(String sourceId) {
return extUserMapper.getMemberOption(sourceId);
public List<UserExtend> getMemberOption(String sourceId, String keyword) {
return extUserMapper.getMemberOption(sourceId, keyword);
}
public TableBatchProcessResponse resetPassword(TableBatchProcessDTO request, String operator) {

View File

@ -322,7 +322,10 @@ public class OrganizationUserRoleControllerTests extends BaseTest {
@Test
@Order(15)
public void testOrganizationUserRoleGetMemberOption() throws Exception {
// 组织下存在已删除用户
this.responseGet(ORGANIZATION_USER_ROLE_GET_MEMBER_OPTION + "/default-organization-2/default-org-role-id-4");
// 组织下用户都已删除
this.responseGet(ORGANIZATION_USER_ROLE_GET_MEMBER_OPTION + "/default-organization-4/default-org-role-id-3");
// 组织下无用户
this.responseGet(ORGANIZATION_USER_ROLE_GET_MEMBER_OPTION + "/default-organization-3/default-org-role-id-3");
}

View File

@ -17,3 +17,5 @@ INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id
(UUID(), 'default-admin-user', 'default-org-role-id-4', 'default-organization-2', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUE
(UUID(), 'default-admin-user-x', 'default-org-role-id-4', 'default-organization-2', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUE
(UUID(), 'default-admin-user-x', 'default-org-role-id-3', 'default-organization-4', 'default-organization-4', UNIX_TIMESTAMP() * 1000, 'admin');