refactor(项目管理): 优化项目列表排序

--bug=1036520 --user=王孝刚 切换项目列表,当前项目默认排第一,其它项目按首字母排序
https://www.tapd.cn/55049933/s/1467013
This commit is contained in:
wxg0103 2024-02-29 15:36:18 +08:00 committed by 刘瑞斌
parent e098c0383d
commit b891d57a6d
4 changed files with 47 additions and 8 deletions

View File

@ -23,4 +23,6 @@ public interface ExtProjectMapper {
int resourcePoolIsExistByOrg(@Param("poolId") String string, @Param("projectId") String projectId, @Param("type") String type);
List<ProjectTestResourcePool> getResourcePool(@Param("projectId") String projectId, @Param("type") String type);
List<Project> getAllProject(@Param("organizationId") String organizationId);
}

View File

@ -11,6 +11,8 @@
JOIN user on urr.user_id = user.id
where urr.user_id = #{userId} and u.type = 'PROJECT'
AND p.organization_id = #{organizationId} and p.enable = 1 and user.deleted = 0
order by
CONVERT( p.name USING GBK) ASC
</select>
@ -74,4 +76,12 @@
and t.deleted = 0
and t.${type} = 1
</select>
<select id="getAllProject" resultType="io.metersphere.project.domain.Project">
select *
from project
where enable = 1 and
organization_id = #{organizationId}
order by
CONVERT( name USING GBK) ASC
</select>
</mapper>

View File

@ -22,10 +22,7 @@ import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.system.dto.user.UserExtendDTO;
import io.metersphere.system.mapper.ExtSystemProjectMapper;
import io.metersphere.system.mapper.OrganizationMapper;
import io.metersphere.system.mapper.TestResourcePoolMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.mapper.*;
import io.metersphere.system.service.CommonProjectService;
import io.metersphere.system.service.UserLoginService;
import io.metersphere.system.utils.ServiceUtils;
@ -64,6 +61,8 @@ public class ProjectService {
private ProjectTestResourcePoolMapper projectTestResourcePoolMapper;
@Resource
private ExtSystemProjectMapper extSystemProjectMapper;
@Resource
private BaseUserMapper baseUserMapper;
public static final Long ORDER_STEP = 5000L;
@ -72,15 +71,33 @@ public class ProjectService {
if (organizationMapper.selectByPrimaryKey(organizationId) == null) {
throw new MSException(Translator.get("organization_not_exist"));
}
//查询用户当前的项目 如果存在默认排在第一个
User user = baseUserMapper.selectById(userId);
String projectId;
if (user != null && StringUtils.isNotBlank(user.getLastProjectId())) {
projectId = user.getLastProjectId();
} else {
projectId = null;
}
//判断用户是否是系统管理员
List<Project> allProject = new ArrayList<>();
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(userId).andRoleIdEqualTo(InternalUserRole.ADMIN.name());
if (userRoleRelationMapper.countByExample(userRoleRelationExample) > 0) {
ProjectExample example = new ProjectExample();
example.createCriteria().andOrganizationIdEqualTo(organizationId);
return projectMapper.selectByExample(example);
allProject = extProjectMapper.getAllProject(organizationId);
} else {
allProject = extProjectMapper.getUserProject(organizationId, userId);
}
return extProjectMapper.getUserProject(organizationId, userId);
List<Project> temp = allProject;
return allProject.stream()
.filter(project -> StringUtils.equals(project.getId(), projectId))
.findFirst()
.map(project -> {
temp.remove(project);
temp.add(0, project);
return temp;
})
.orElse(allProject);
}
public UserDTO switchProject(ProjectSwitchRequest request, String currentUserId) {

View File

@ -19,6 +19,8 @@ import io.metersphere.system.dto.ProjectDTO;
import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.system.invoker.ProjectServiceInvoker;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.mapper.BaseUserMapper;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
@ -65,6 +67,11 @@ public class ProjectControllerTests extends BaseTest {
private ProjectService projectService;
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
@Resource
private BaseUserMapper baseUserMapper;
@Resource
private UserMapper userMapper;
@Autowired
public ProjectControllerTests(ProjectServiceInvoker serviceInvoker) {
@ -215,6 +222,9 @@ public class ProjectControllerTests extends BaseTest {
.andReturn();
String sessionId = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.sessionId");
String csrfToken = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.csrfToken");
UserDTO userDTO = baseUserMapper.selectById("admin1");
userDTO.setLastProjectId(null);
userMapper.updateByPrimaryKey(userDTO);
mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(getOptions + DEFAULT_ORGANIZATION_ID)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)