refactor(组织管理): 优化获取组织成员下拉框筛选

This commit is contained in:
guoyuqi 2023-09-04 16:31:19 +08:00 committed by 刘瑞斌
parent c40f7e08ca
commit abdb3ae220
9 changed files with 116 additions and 46 deletions

View File

@ -8,6 +8,7 @@ import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.dto.IdNameDisabledDTO;
import io.metersphere.system.dto.IdNameStructureDTO;
import io.metersphere.system.dto.OrgUserExtend;
import io.metersphere.system.request.*;
@ -89,8 +90,8 @@ public class OrganizationController {
@GetMapping("/project/list/{organizationId}")
@Operation(summary = "系统设置-组织-成员-获取当前组织下的所有项目")
@RequiresPermissions(PermissionConstants.ORGANIZATION_PROJECT_READ)
public List<IdNameStructureDTO> getProjectList(@PathVariable(value = "organizationId") String organizationId) {
return organizationService.getProjectList(organizationId);
public List<IdNameStructureDTO> getProjectList(@PathVariable(value = "organizationId") String organizationId, @Schema(description = "查询关键字,根据项目名查询", requiredMode = Schema.RequiredMode.REQUIRED) @RequestParam(value = "keyword", required = false) String keyword) {
return organizationService.getProjectList(organizationId, keyword);
}
@GetMapping("/user/role/list/{organizationId}")
@ -103,8 +104,9 @@ public class OrganizationController {
@GetMapping("/not-exist/user/list/{organizationId}")
@Operation(summary = "系统设置-组织-成员-获取不在当前组织的所有用户")
@RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_READ)
public List<IdNameStructureDTO> getUserList(@PathVariable(value = "organizationId") String organizationId) {
return organizationService.getUserList(organizationId);
public List<IdNameDisabledDTO> getUserList(@PathVariable(value = "organizationId") String organizationId, @Schema(description = "查询关键字,根据用户名查询", requiredMode = Schema.RequiredMode.REQUIRED)
@RequestParam(value = "keyword", required = false) String keyword) {
return organizationService.getUserList(organizationId, keyword);
}
}

View File

@ -0,0 +1,14 @@
package io.metersphere.system.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IdNameDisabledDTO extends IdNameStructureDTO{
@Schema(description = "是否已经关联过")
private Boolean disabled = false;
}

View File

@ -1,10 +1,17 @@
package io.metersphere.system.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IdNameStructureDTO {
@Schema(description = "选项ID")
private String id;
@Schema(description = "选项名称")
private String name;
}

View File

@ -2,10 +2,7 @@ package io.metersphere.system.mapper;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.OrgUserExtend;
import io.metersphere.system.dto.OrganizationDTO;
import io.metersphere.system.dto.OrganizationProjectOptionsDTO;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.system.dto.*;
import io.metersphere.system.request.OrganizationDeleteRequest;
import io.metersphere.system.request.OrganizationRequest;
import org.apache.ibatis.annotations.Param;
@ -64,6 +61,13 @@ public interface ExtOrganizationMapper {
*/
List<OrgUserExtend> listMemberByOrg(@Param("request") OrganizationRequest request);
/**
* 获取组织成员下拉列表(角色)
* @param keyword 组织成员列表请求参数
* @return 组织成员列表数据
*/
List<IdNameDisabledDTO> selectListMemberByOrg(@Param("keyword") String keyword);
/**
* 获取组织管理员
* @param orgId 组织ID

View File

@ -76,6 +76,17 @@
group by temp.id
</select>
<select id="selectListMemberByOrg" resultType="io.metersphere.system.dto.IdNameDisabledDTO">
select id, name
from user
where deleted = 0
<if test="keyword != null and keyword != ''">
and (name LIKE CONCAT('%', #{keyword}, '%')
</if>
order by update_time desc
limit 100
</select>
<select id="getOrgAdminList" resultType="io.metersphere.system.domain.User">
select u.*
from user_role_relation urr

View File

@ -0,0 +1,18 @@
package io.metersphere.system.mapper;
import io.metersphere.system.dto.IdNameStructureDTO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author guoyuqi
*/
public interface ExtSystemOrgProjectMapper {
/**
* 获取组织项目下拉列表(角色)
* @param keyword 组织项目列表请求参数
* @return 组织项目列表数据
*/
List<IdNameStructureDTO> selectListProjectByOrg(@Param("organizationId") String organizationId, @Param("keyword") String keyword);
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.system.mapper.ExtSystemOrgProjectMapper">
<select id="selectListProjectByOrg" resultType="io.metersphere.system.dto.IdNameStructureDTO">
select id, name
from project
where deleted = 0
and organization_id = #{organizationId}
<if test="keyword != null and keyword != ''">
and (name LIKE CONCAT('%', #{keyword}, '%')
</if>
order by update_time desc
limit 100
</select>
</mapper>

View File

@ -46,6 +46,8 @@ public class OrganizationService {
@Resource
private ExtOrganizationMapper extOrganizationMapper;
@Resource
private ExtSystemOrgProjectMapper extSystemOrgProjectMapper;
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
@Resource
private UserMapper userMapper;
@ -284,21 +286,25 @@ public class OrganizationService {
projects = projectMapper.selectByExample(projectExample);
}
for (OrgUserExtend orgUserExtend : orgUserExtends) {
if (projects.size()>0) {
Set<String> projectIds = userIdProjectIdMap.get(orgUserExtend.getId());
List<Project> projectFilters = projects.stream().filter(t -> projectIds.contains(t.getId())).toList();
List<IdNameStructureDTO> projectList = new ArrayList<>();
setProjectList(projectList, projectFilters);
orgUserExtend.setProjectIdNameMap(projectList);
if (CollectionUtils.isNotEmpty(projectIds)) {
List<Project> projectFilters = projects.stream().filter(t -> projectIds.contains(t.getId())).toList();
List<IdNameStructureDTO> projectList = new ArrayList<>();
setProjectList(projectList, projectFilters);
orgUserExtend.setProjectIdNameMap(projectList);
}
}
Set<String> userRoleIds = userIdRoleIdMap.get(orgUserExtend.getId());
List<UserRole> userRoleFilters = userRoles.stream().filter(t -> userRoleIds.contains(t.getId())).toList();
List<IdNameStructureDTO> userRoleList = new ArrayList<>();
setUserRoleList(userRoleList, userRoleFilters);
orgUserExtend.setUserRoleIdNameMap(userRoleList);
if (CollectionUtils.isNotEmpty(userRoleIds)){
List<UserRole> userRoleFilters = userRoles.stream().filter(t -> userRoleIds.contains(t.getId())).toList();
List<IdNameStructureDTO> userRoleList = new ArrayList<>();
setUserRoleList(userRoleList, userRoleFilters);
orgUserExtend.setUserRoleIdNameMap(userRoleList);
}
}
return orgUserExtends;
}
@ -642,20 +648,10 @@ public class OrganizationService {
* @param organizationId 组织ID
* @return 项目列表
*/
public List<IdNameStructureDTO> getProjectList(String organizationId) {
public List<IdNameStructureDTO> getProjectList(String organizationId, String keyword) {
//校验组织是否存在
checkOrgExistById(organizationId);
List<IdNameStructureDTO> projectList = new ArrayList<>();
ProjectExample projectExample = new ProjectExample();
projectExample.createCriteria().andOrganizationIdEqualTo(organizationId);
List<Project> projects = projectMapper.selectByExample(projectExample);
for (Project project : projects) {
IdNameStructureDTO idNameStructureDTO = new IdNameStructureDTO();
idNameStructureDTO.setId(project.getId());
idNameStructureDTO.setName(project.getName());
projectList.add(idNameStructureDTO);
}
return projectList;
return extSystemOrgProjectMapper.selectListProjectByOrg(organizationId, keyword);
}
/**
@ -686,33 +682,29 @@ public class OrganizationService {
}
/**
* 获取不在当前组织的所有用户
* 获取所有未被删除用户
*
* @param organizationId 组织ID
* @return 用户列表
*/
public List<IdNameStructureDTO> getUserList(String organizationId) {
public List<IdNameDisabledDTO> getUserList(String organizationId, String keyword) {
//校验组织是否存在
checkOrgExistById(organizationId);
List<IdNameDisabledDTO> idNameDisabledDTOS = extOrganizationMapper.selectListMemberByOrg(keyword);
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo(organizationId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
List<String> userIds = userRoleRelations.stream().map(UserRoleRelation::getUserId).distinct().toList();
UserExample userExample = new UserExample();
UserExample.Criteria criteria = userExample.createCriteria();
if (CollectionUtils.isNotEmpty(userIds)) {
criteria.andIdNotIn(userIds);
for (IdNameDisabledDTO idNameDisabledDTO : idNameDisabledDTOS) {
if (CollectionUtils.isNotEmpty(userIds) && userIds.contains(idNameDisabledDTO.getId())) {
idNameDisabledDTO.setDisabled(true);
}
}
criteria.andDeletedEqualTo(false);
List<User> users = userMapper.selectByExample(userExample);
List<IdNameStructureDTO> userList = new ArrayList<>();
for (User user : users) {
IdNameStructureDTO idNameStructureDTO = new IdNameStructureDTO();
idNameStructureDTO.setId(user.getId());
idNameStructureDTO.setName(user.getName());
userList.add(idNameStructureDTO);
}
return userList;
return idNameDisabledDTOS;
}
/**

View File

@ -244,7 +244,12 @@ public class OrganizationControllerTests extends BaseTest {
@Test
@Order(11)
public void getOrgMemberListSuccess() throws Exception {
listByKeyWord("testUserOne", "sys_default_organization_3", false, null, null, false, null, null);
OrganizationMemberExtendRequest organizationMemberRequest = new OrganizationMemberExtendRequest();
organizationMemberRequest.setOrganizationId("sys_default_organization_3");
organizationMemberRequest.setMemberIds(Arrays.asList("sys_default_user3"));
organizationMemberRequest.setUserRoleIds(Arrays.asList("sys_default_org_role_id_3"));
this.requestPost(ORGANIZATION_LIST_ADD_MEMBER, organizationMemberRequest, status().isOk());
listByKeyWord("testUserThree", "sys_default_organization_3", false, null, null, false, null, null);
}
@Test