refactor(项目管理): 优化项目成员下拉选择查询

This commit is contained in:
song-cc-rock 2023-09-04 16:48:51 +08:00 committed by 刘瑞斌
parent 4e7e7a67a5
commit 2b0800cf9c
6 changed files with 59 additions and 21 deletions

View File

@ -48,8 +48,10 @@ public class ProjectMemberController {
@GetMapping("/get-member/option/{projectId}")
@Operation(summary = "项目管理-成员-获取成员下拉选项")
@RequiresPermissions(PermissionConstants.PROJECT_MEMBER_ADD)
public List<UserExtend> getMemberOption(@PathVariable String projectId) {
return projectMemberService.getMemberOption(projectId);
public List<UserExtend> getMemberOption(@PathVariable String projectId,
@Schema(description = "查询关键字,根据邮箱和用户名查询")
@RequestParam(value = "keyword", required = false) String keyword) {
return projectMemberService.getMemberOption(projectId, keyword);
}
@GetMapping("/get-role/option/{projectId}")

View File

@ -22,7 +22,8 @@ public interface ExtProjectMemberMapper {
/**
* 获取所有组织成员
* @param organizationId 组织ID
* @param keyword 搜索关键字
* @return 成员
*/
List<UserExtend> getMemberByOrg(String organizationId);
List<UserExtend> getMemberByOrg(@Param("organizationId") String organizationId, @Param("keyword") String keyword);
}

View File

@ -18,13 +18,16 @@
<select id="getMemberByOrg" resultType="io.metersphere.system.dto.UserExtend">
select distinct u.* from user_role_relation urr join `user` u on urr.user_id = u.id
<where>
where
u.deleted = 0
<if test="organizationId != null and organizationId != ''">
and urr.source_id = #{organizationId}
</if>
</where>
<if test="keyword != null and keyword != ''">
and (u.name like concat('%', #{keyword}, '%') or u.email like concat('%', #{keyword}, '%'))
</if>
order by u.create_time desc
limit 100
</select>
<sql id="filter">

View File

@ -74,9 +74,6 @@ public class ProjectMemberService {
List<UserRoleRelation> userRoleRelates = userRoleRelationMapper.selectByExample(relationExample);
Map<String, List<String>> userRoleRelateMap = userRoleRelates.stream().collect(Collectors.groupingBy(UserRoleRelation::getUserId,
Collectors.mapping(UserRoleRelation::getRoleId, Collectors.toList())));
// 查询所有用户
List<User> users = baseUserMapper.findAll();
Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
// 查询所有项目类型用户组
UserRoleExample example = new UserRoleExample();
example.createCriteria().andTypeEqualTo(UserRoleType.PROJECT.name());
@ -85,8 +82,7 @@ public class ProjectMemberService {
List<ProjectUserDTO> projectUsers = new ArrayList<>();
userRoleRelateMap.forEach((k, v) -> {
ProjectUserDTO projectUser = new ProjectUserDTO();
User user = userMap.get(k);
BeanUtils.copyBean(projectUser, user);
projectUser.setId(k);
List<UserRole> userRoles = new ArrayList<>();
v.forEach(roleId -> {
UserRole role = roleMap.get(roleId);
@ -95,6 +91,16 @@ public class ProjectMemberService {
projectUser.setUserRoles(userRoles);
projectUsers.add(projectUser);
});
// 设置用户信息
List<String> uerIds = projectUsers.stream().map(ProjectUserDTO::getId).collect(Collectors.toList());
UserExample userExample = new UserExample();
userExample.createCriteria().andDeletedEqualTo(false).andIdIn(uerIds);
List<User> users = userMapper.selectByExample(userExample);
Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
projectUsers.forEach(projectUser -> {
User user = userMap.get(projectUser.getId());
BeanUtils.copyBean(projectUser, user);
});
return projectUsers;
}
@ -104,12 +110,29 @@ public class ProjectMemberService {
* @param projectId 项目ID
* @return 项目成员下拉选项
*/
public List<UserExtend> getMemberOption(String projectId) {
public List<UserExtend> getMemberOption(String projectId, String keyword) {
Project project = projectMapper.selectByPrimaryKey(projectId);
if (project == null) {
return new ArrayList<>();
}
return extProjectMemberMapper.getMemberByOrg(project.getOrganizationId());
// 组织成员
List<UserExtend> orgMembers = extProjectMemberMapper.getMemberByOrg(project.getOrganizationId(), keyword);
if (CollectionUtils.isEmpty(orgMembers)) {
return new ArrayList<>();
}
// 设置是否是项目成员
List<String> orgMemberIds = orgMembers.stream().map(UserExtend::getId).toList();
UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria().andUserIdIn(orgMemberIds).andSourceIdEqualTo(projectId).andOrganizationIdEqualTo(project.getOrganizationId());
List<UserRoleRelation> projectRelations = userRoleRelationMapper.selectByExample(example);
if (CollectionUtils.isEmpty(projectRelations)) {
orgMembers.forEach(orgMember -> orgMember.setMemberFlag(false));
} else {
List<String> projectUsers = projectRelations.stream().map(UserRoleRelation::getUserId).distinct().toList();
// 已经是项目成员的组织成员, 禁用
orgMembers.forEach(orgMember -> orgMember.setMemberFlag(projectUsers.contains(orgMember.getId())));
}
return orgMembers;
}
/**

View File

@ -104,9 +104,14 @@ public class ProjectMemberControllerTests extends BaseTest {
@Test
@Order(4)
public void testGetMemberOption() throws Exception {
// 正常数据
this.requestGet(GET_MEMBER + "/default-project-member-test", status().isOk());
// 覆盖空数据
// 项目不存在
this.requestGet(GET_MEMBER + "/default-project-member-x", status().isOk());
// 组织成员为空
this.requestGet(GET_MEMBER + "/default-project-member-test-1", status().isOk());
// 项目成员为空
this.requestGet(GET_MEMBER + "/default-project-member-test-2", status().isOk());
// 权限校验
requestGetPermissionTest(PermissionConstants.PROJECT_MEMBER_ADD, GET_MEMBER + "/" + DEFAULT_PROJECT_ID);
}

View File

@ -2,22 +2,26 @@
# 2. 准备组织成员数据
# 3. 准备项目成员数据
INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, deleted, delete_user, delete_time) VALUE
('default-organization-member-test', null, 'efault-organization-member-test', 'efault-organization-member-test', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', 0, null, null);
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUE
('default-project-member-test', null, 'efault-organization-member-test', '默认项目', '系统默认创建的项目', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);
('default-organization-member-test', null, 'default-organization-member-test', 'efault-organization-member-test', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', 0, null, null);
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES
('default-project-member-test', null, 'default-organization-member-test', '默认项目', '系统默认创建的项目', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000),
('default-project-member-test-1', null, 'default-organization-member-test-1', '默认项目', '系统默认创建的项目', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000),
('default-project-member-test-2', null, 'default-organization-member-test-2', '默认项目', '系统默认创建的项目', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);
INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user, deleted) VALUES
('default-project-member-user-1', 'default-project-member-user1', 'project-member1@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('default-project-member-user-2', 'default-project-member-user2', 'project-member2@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('default-project-member-user-del', 'default-project-member-userDel', 'project-member-del@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 1);
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUES
(UUID(), 'default-project-member-user-1', 'org_member', 'default-organization-member-test', 'default_organization', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-2', 'org_member', 'default-organization-member-test', 'default_organization', UNIX_TIMESTAMP() * 1000, 'admin');
(UUID(), 'default-project-member-user-1', 'org_member', 'default-organization-member-test', 'default-organization-member-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-2', 'org_member', 'default-organization-member-test', 'default-organization-member-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-3', 'org_member', 'default-organization-member-test', 'default-organization-member-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-1', 'org_member', 'default-organization-member-test-2', 'default-organization-member-test-2', UNIX_TIMESTAMP() * 1000, 'admin');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUES
(UUID(), 'default-project-member-user-1', 'project_admin', 'default-project-member-test', 'default_organization', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-2', 'project_admin', 'default-project-member-test', 'default_organization', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-del', 'project_admin', 'default-project-member-test', 'default_organization', UNIX_TIMESTAMP() * 1000, 'admin');
(UUID(), 'default-project-member-user-1', 'project_admin', 'default-project-member-test', 'default-organization-member-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-2', 'project_admin', 'default-project-member-test', 'default-organization-member-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-project-member-user-del', 'project_admin', 'default-project-member-test', 'default-organization-member-test', UNIX_TIMESTAMP() * 1000, 'admin');