refactor(系统设置): 优化任务中心查询逻辑

This commit is contained in:
wxg0103 2024-03-23 15:40:58 +08:00 committed by Craftsman
parent 82752b5bf7
commit de37367c62
2 changed files with 29 additions and 37 deletions

View File

@ -2,30 +2,53 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.system.mapper.ExtScheduleMapper"> <mapper namespace="io.metersphere.system.mapper.ExtScheduleMapper">
<select id="taskCenterSchedulelist" resultType="io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO"> <select id="taskCenterSchedulelist" resultType="io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO">
SELECT task.* from (
SELECT SELECT
schedule.id, schedule.id,
schedule.name as taskname, schedule.name as taskname,
schedule.project_id, schedule.project_id,
'API_SCENARIO' as resource_type,
api_scenario.num as resource_num,
api_scenario.name as resource_name,
api_scenario.id as resource_id,
schedule.value, schedule.value,
schedule.enable, schedule.enable,
schedule.resource_id,
schedule.resource_type,
schedule.create_user AS createUserName, schedule.create_user AS createUserName,
schedule.create_time schedule.create_time
FROM FROM
schedule schedule
inner join api_scenario on schedule.resource_id = api_scenario.id
union all
SELECT
schedule.id,
schedule.name as taskname,
schedule.project_id,
'API_IMPORT' as resource_type,
ads.num as resource_num,
ads.name as resource_name,
ads.id as resource_id,
schedule.value,
schedule.enable,
schedule.create_user AS createUserName,
schedule.create_time
FROM
schedule
inner join api_definition_swagger ads on schedule.resource_id = ads.id
) task
where where
schedule.resource_type in task.resource_type in
<foreach collection="resourceTypes" item="resourceType" separator="," open="(" close=")"> <foreach collection="resourceTypes" item="resourceType" separator="," open="(" close=")">
#{resourceType} #{resourceType}
</foreach> </foreach>
and schedule.project_id IN and task.project_id IN
<foreach collection="projectIds" item="projectId" separator="," open="(" close=")"> <foreach collection="projectIds" item="projectId" separator="," open="(" close=")">
#{projectId} #{projectId}
</foreach> </foreach>
<if test="request.keyword != null and request.keyword != ''"> <if test="request.keyword != null and request.keyword != ''">
and ( and (
schedule.name like concat('%', #{request.keyword},'%') task.resource_name like concat('%', #{request.keyword},'%')
) )
</if> </if>
<include refid="taskCenterScheduleFilters"> <include refid="taskCenterScheduleFilters">
@ -39,7 +62,7 @@
<if test="values != null and values.size() > 0"> <if test="values != null and values.size() > 0">
<choose> <choose>
<when test="key=='resourceType'"> <when test="key=='resourceType'">
and schedule.resource_type in and task.resource_type in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/> <include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when> </when>
</choose> </choose>

View File

@ -2,10 +2,8 @@ package io.metersphere.system.service;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod; import com.github.pagehelper.page.PageMethod;
import io.metersphere.api.domain.ApiScenario;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.ScheduleResourceType;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Organization;
@ -28,7 +26,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -124,10 +121,6 @@ public class TaskCenterService {
list.forEach(item -> { list.forEach(item -> {
String resourceId = item.getResourceId();
if (ScheduleTagType.TEST_RESOURCE.toString().equals(scheduleTagType)) {
processTaskCenterScheduleData(list, resourceId, item);
}
item.setCreateUserName(userMap.getOrDefault(item.getCreateUserName(), StringUtils.EMPTY)); item.setCreateUserName(userMap.getOrDefault(item.getCreateUserName(), StringUtils.EMPTY));
item.setProjectName(projectMap.getOrDefault(item.getProjectId(), StringUtils.EMPTY)); item.setProjectName(projectMap.getOrDefault(item.getProjectId(), StringUtils.EMPTY));
item.setOrganizationName(orgMap.getOrDefault(item.getProjectId(), StringUtils.EMPTY)); item.setOrganizationName(orgMap.getOrDefault(item.getProjectId(), StringUtils.EMPTY));
@ -136,30 +129,6 @@ public class TaskCenterService {
} }
} }
private void processTaskCenterScheduleData (List<TaskCenterScheduleDTO> list, String resourceId, TaskCenterScheduleDTO taskCenterScheduleDTO) {
// 业务数据
// 根据 resourceType 分组并获取每个类型对应的 resourceId 数组
Map<String, List<String>> resultMap = list.stream()
.collect(Collectors.groupingBy(TaskCenterScheduleDTO::getResourceType,
Collectors.mapping(TaskCenterScheduleDTO::getResourceId, Collectors.toList())));
Map<String, ApiScenario> apiScenarioMap = new HashMap<>();
resultMap.forEach((type, resourceIds) ->{
if (type.equals(ScheduleResourceType.API_SCENARIO.toString())) {
List<ApiScenario> apiScenarios = extScheduleMapper.getApiScenarioListByIds(resourceIds);
apiScenarioMap.putAll(apiScenarios.stream().collect(Collectors.toMap(ApiScenario::getId, Function.identity())));
}
});
// TODO ui test load test ...
if (apiScenarioMap.containsKey(resourceId)) {
ApiScenario apiScenario = apiScenarioMap.get(resourceId);
taskCenterScheduleDTO.setResourceName(apiScenario.getName());
taskCenterScheduleDTO.setResourceNum(apiScenario.getNum());
} else {
taskCenterScheduleDTO.setResourceName(StringUtils.EMPTY);
}
}
private List<OptionDTO> getProjectOption(String id){ private List<OptionDTO> getProjectOption(String id){
return baseProjectMapper.getProjectOptionsById(id); return baseProjectMapper.getProjectOptionsById(id);
} }