refactor(项目管理): 优化环境组查询项目列表

This commit is contained in:
wxg0103 2024-02-07 10:41:46 +08:00 committed by wxg0103
parent 1a42390ec5
commit 58d90c3739
5 changed files with 48 additions and 25 deletions

View File

@ -72,11 +72,11 @@ public class EnvironmentGroupController {
return environmentGroupService.get(id);
}
@GetMapping("/get-project")
@GetMapping("/get-project/{organizationId}")
@Operation(summary = "项目管理-环境组-获取项目")
@RequiresPermissions(PermissionConstants.PROJECT_ENVIRONMENT_READ)
public List<OptionDTO> getProject() {
return environmentGroupService.getProject(SessionUtils.getUserId());
public List<OptionDTO> getProject(@PathVariable String organizationId) {
return environmentGroupService.getProject(SessionUtils.getUserId(), organizationId);
}
@PostMapping("/edit/pos")

View File

@ -12,4 +12,9 @@ public interface ExtProjectMapper {
String getModuleSetting(@Param("projectId") String projectId);
List<Project> getProject(@Param("userId") String userId);
List<Project> getAllProjectByOrgId(@Param("organizationId") String organizationId);
List<Project> getProjectByOrgId(@Param("userId") String userId, @Param("organizationId") String organizationId);
}

View File

@ -28,4 +28,28 @@
and p.enable = 1
and user.deleted = 0
</select>
<select id="getAllProjectByOrgId" resultType="io.metersphere.project.domain.Project">
select id, name, organization_id
from project
where
organization_id = #{organizationId}
order by create_time desc
limit 100
</select>
<select id="getProjectByOrgId" resultType="io.metersphere.project.domain.Project">
SELECT DISTINCT p.id, p.name, p.organization_id
FROM user_role u
JOIN user_role_relation urr ON u.id = urr.role_id
JOIN project p ON p.id = urr.source_id
JOIN user on urr.user_id = user.id
where urr.user_id = #{userId}
and u.type = 'PROJECT'
and p.enable = 1
and user.deleted = 0
and p.organization_id = #{organizationId}
order by create_time desc
limit 100
</select>
</mapper>

View File

@ -15,8 +15,6 @@ import io.metersphere.sdk.mapper.EnvironmentGroupRelationMapper;
import io.metersphere.sdk.mapper.EnvironmentMapper;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.Organization;
import io.metersphere.system.domain.OrganizationExample;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.PosRequest;
@ -206,30 +204,20 @@ public class EnvironmentGroupService {
return result;
}
public List<OptionDTO> getProject(String userId) {
public List<OptionDTO> getProject(String userId, String organizationId) {
//判断用户是否是系统管理员
List<OptionDTO> result = new ArrayList<>();
List<Project> projects = new ArrayList<>();
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(userId).andRoleIdEqualTo(InternalUserRole.ADMIN.name());
if (userRoleRelationMapper.countByExample(userRoleRelationExample) > 0) {
projects = projectMapper.selectByExample(new ProjectExample());
projects = extProjectMapper.getAllProjectByOrgId(organizationId);
} else {
projects = extProjectMapper.getProject(userId);
projects = extProjectMapper.getProjectByOrgId(userId, organizationId);
}
if (CollectionUtils.isNotEmpty(projects)) {
result = projects.stream().map(e -> new OptionDTO(e.getId(), e.getName())).collect(Collectors.toList());
}
//提取所有的组织id
List<String> organizationIds = projects.stream().map(Project::getOrganizationId).distinct().toList();
//查询组织名称
OrganizationExample organizationExample = new OrganizationExample();
organizationExample.createCriteria().andIdIn(organizationIds);
List<Organization> organizations = organizationMapper.selectByExample(organizationExample);
Map<String, String> organizationMap = organizations.stream().collect(Collectors.toMap(Organization::getId, Organization::getName));
projects.forEach(e -> {
OptionDTO dto = new OptionDTO();
dto.setId(e.getId());
dto.setName(e.getName() + " (" + organizationMap.get(e.getOrganizationId()) + ")");
result.add(dto);
});
return result;
}

View File

@ -1,9 +1,10 @@
package io.metersphere.project.controller;
import io.metersphere.project.dto.environment.*;
import io.metersphere.project.api.KeyValueEnableParam;
import io.metersphere.project.dto.environment.*;
import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.project.service.EnvironmentGroupService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.domain.Environment;
@ -49,7 +50,7 @@ public class EnvironmentGroupControllerTests extends BaseTest {
private static final String update = prefix + "/update";
private static final String delete = prefix + "/delete/";
private static final String list = prefix + "/list";
private static final String getProject = prefix + "/get-project";
private static final String getProject = prefix + "/get-project/";
private static final String POS_URL = prefix + "/edit/pos";
private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest();
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
@ -61,6 +62,8 @@ public class EnvironmentGroupControllerTests extends BaseTest {
@Resource
private EnvironmentGroupMapper environmentGroupMapper;
private EnvironmentGroup environmentGroup;
@Resource
private EnvironmentGroupService environmentGroupService;
public static <T> T parseObjectFromMvcResult(MvcResult mvcResult, Class<T> parseClass) {
try {
@ -154,6 +157,8 @@ public class EnvironmentGroupControllerTests extends BaseTest {
Assertions.assertEquals(groupResponse.getId(), environmentGroup.getId());
Assertions.assertEquals(groupResponse.getName(), environmentGroup.getName());
checkLog(environmentGroup.getId(), OperationLogType.ADD);
environmentGroupService.getEnvironmentGroupRelations(List.of(environmentGroup.getId()));
environmentGroupService.getEnvironmentGroupRelations(new ArrayList<>());
//校验权限
groupRequest.setName("校验权限");
requestPostPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ_ADD, add, groupRequest);
@ -335,11 +340,12 @@ public class EnvironmentGroupControllerTests extends BaseTest {
@Test
@Order(13)
public void testGetProject() throws Exception {
MvcResult mvcResult = this.responseGet(getProject);
MvcResult mvcResult = this.responseGet(getProject + DEFAULT_ORGANIZATION_ID);
List<OptionDTO> response = parseObjectFromMvcResult(mvcResult, List.class);
Assertions.assertNotNull(response);
//校验权限
requestGetPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ, getProject);
requestGetPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ, getProject + "/" + DEFAULT_ORGANIZATION_ID);
}