feat(任务中心): 系统&组织&项目后台任务列表批量操作

This commit is contained in:
WangXu10 2024-10-21 10:19:57 +08:00 committed by Craftsman
parent 1c4c0a3245
commit 4ba7ee7dfc
8 changed files with 185 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.project.controller;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.metersphere.system.dto.taskhub.ResourcePoolOptionsDTO; import io.metersphere.system.dto.taskhub.ResourcePoolOptionsDTO;
import io.metersphere.system.dto.taskhub.TaskHubDTO; import io.metersphere.system.dto.taskhub.TaskHubDTO;
@ -156,4 +157,20 @@ public class ProjectTaskHubController {
public void enable(@PathVariable String id) { public void enable(@PathVariable String id) {
baseTaskHubService.enable(id, SessionUtils.getUserId(), "/project/task-center/schedule/switch/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER); baseTaskHubService.enable(id, SessionUtils.getUserId(), "/project/task-center/schedule/switch/", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER);
} }
@PostMapping("/schedule/batch-enable")
@Operation(summary = "项目-任务中心-后台任务-批量开启")
@RequiresPermissions(PermissionConstants.PROJECT_SCHEDULE_TASK_CENTER_READ_UPDATE)
public void batchEnable(@Validated @RequestBody TableBatchProcessDTO request) {
baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), SessionUtils.getCurrentProjectId(), "/project/task-center/schedule/batch-enable", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, true, List.of(SessionUtils.getCurrentProjectId()));
}
@PostMapping("/schedule/batch-disable")
@Operation(summary = "项目-任务中心-后台任务-批量关闭")
@RequiresPermissions(PermissionConstants.PROJECT_SCHEDULE_TASK_CENTER_READ_UPDATE)
public void batchDisable(@Validated @RequestBody TableBatchProcessDTO request) {
baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), null, "/project/task-center/schedule/batch-disable", OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, false, List.of(SessionUtils.getCurrentProjectId()));
}
} }

View File

@ -36,6 +36,8 @@ public class ProjectTaskHubControllerTests extends BaseTest {
public static final String PROJECT_TASK_ITEM_BATCH_STOP = "/project/task-center/exec-task/item/batch-stop"; public static final String PROJECT_TASK_ITEM_BATCH_STOP = "/project/task-center/exec-task/item/batch-stop";
public static final String PROJECT_SCHEDULE_TASK_DELETE = "/project/task-center/schedule/delete/"; public static final String PROJECT_SCHEDULE_TASK_DELETE = "/project/task-center/schedule/delete/";
public static final String PROJECT_SCHEDULE_TASK_SWITCH = "/project/task-center/schedule/switch/"; public static final String PROJECT_SCHEDULE_TASK_SWITCH = "/project/task-center/schedule/switch/";
public static final String PROJECT_SCHEDULE_TASK_BATCH_ENABLE = "/project/task-center/schedule/batch-enable";
public static final String PROJECT_SCHEDULE_TASK_BATCH_DISABLE = "/project/task-center/schedule/batch-disable";
@Test @Test
@Order(1) @Order(1)
@ -184,7 +186,6 @@ public class ProjectTaskHubControllerTests extends BaseTest {
} }
/** /**
* 项目执行任务项停止 * 项目执行任务项停止
*/ */
@ -230,4 +231,26 @@ public class ProjectTaskHubControllerTests extends BaseTest {
public void projectScheduleTaskSwitch() throws Exception { public void projectScheduleTaskSwitch() throws Exception {
this.requestGet(PROJECT_SCHEDULE_TASK_SWITCH + "pro_wx_1"); this.requestGet(PROJECT_SCHEDULE_TASK_SWITCH + "pro_wx_1");
} }
/**
* 系统后台任务删除
*/
@Test
@Order(7)
public void projectScheduleBatchEnable() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(PROJECT_SCHEDULE_TASK_BATCH_ENABLE, request);
request.setSelectAll(false);
request.setSelectIds(List.of("pro_wx_1", "pro_wx_1"));
this.requestPost(PROJECT_SCHEDULE_TASK_BATCH_ENABLE, request);
}
@Test
@Order(8)
public void projectScheduleBatchDisable() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(PROJECT_SCHEDULE_TASK_BATCH_DISABLE, request);
}
} }

View File

@ -167,4 +167,24 @@ public class OrganizationTaskHubController {
public void enable(@PathVariable String id) { public void enable(@PathVariable String id) {
baseTaskHubService.enable(id, SessionUtils.getUserId(), "/organization/task-center/schedule/switch/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER); baseTaskHubService.enable(id, SessionUtils.getUserId(), "/organization/task-center/schedule/switch/", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER);
} }
@PostMapping("/schedule/batch-enable")
@Operation(summary = "组织-任务中心-后台任务-批量开启")
@RequiresPermissions(PermissionConstants.ORGANIZATION_SCHEDULE_TASK_CENTER_READ_UPDATE)
public void batchEnable(@Validated @RequestBody TableBatchProcessDTO request) {
List<OptionDTO> projectList = baseProjectMapper.getProjectOptionsByOrgId(SessionUtils.getCurrentOrganizationId());
List<String> projectIds = projectList.stream().map(OptionDTO::getId).toList();
baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), SessionUtils.getCurrentProjectId(), "/organization/task-center/schedule/batch-enable", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, true,projectIds);
}
@PostMapping("/schedule/batch-disable")
@Operation(summary = "组织-任务中心-后台任务-批量关闭")
@RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ_UPDATE)
public void batchDisable(@Validated @RequestBody TableBatchProcessDTO request) {
List<OptionDTO> projectList = baseProjectMapper.getProjectOptionsByOrgId(SessionUtils.getCurrentOrganizationId());
List<String> projectIds = projectList.stream().map(OptionDTO::getId).toList();
baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), null, "/organization/task-center/schedule/batch-disable", OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, false, projectIds);
}
} }

View File

@ -169,5 +169,21 @@ public class SystemTaskHubController {
public void enable(@PathVariable String id) { public void enable(@PathVariable String id) {
baseTaskHubService.enable(id, SessionUtils.getUserId(), "/system/task-center/schedule/switch/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER); baseTaskHubService.enable(id, SessionUtils.getUserId(), "/system/task-center/schedule/switch/", OperationLogModule.SETTING_SYSTEM_TASK_CENTER);
} }
@PostMapping("/schedule/batch-enable")
@Operation(summary = "系统-任务中心-后台任务-批量开启")
@RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ_UPDATE)
public void batchEnable(@Validated @RequestBody TableBatchProcessDTO request) {
baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), null, "/system/task-center/schedule/batch-enable", OperationLogModule.SETTING_SYSTEM_TASK_CENTER, true, null);
}
@PostMapping("/schedule/batch-disable")
@Operation(summary = "系统-任务中心-后台任务-批量关闭")
@RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ_UPDATE)
public void batchDisable(@Validated @RequestBody TableBatchProcessDTO request) {
baseTaskHubService.scheduleBatchOperation(request, SessionUtils.getUserId(), null, "/system/task-center/schedule/batch-disable", OperationLogModule.SETTING_SYSTEM_TASK_CENTER, false, null);
}
//TODO 系统&组织&项目 后台任务操作删除 批量开启 批量关闭 //TODO 系统&组织&项目 后台任务操作删除 批量开启 批量关闭
} }

View File

@ -5,6 +5,7 @@ import io.metersphere.api.domain.ApiTestCase;
import io.metersphere.system.domain.Schedule; import io.metersphere.system.domain.Schedule;
import io.metersphere.system.dto.ProjectDTO; import io.metersphere.system.dto.ProjectDTO;
import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO; import io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO;
import io.metersphere.system.dto.taskcenter.request.TaskCenterScheduleBatchRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterScheduleBatchRequest;
import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest;
@ -45,4 +46,6 @@ public interface ExtScheduleMapper {
int countByProjectIds(@Param("ids") List<String> ids); int countByProjectIds(@Param("ids") List<String> ids);
List<TaskHubScheduleDTO> selectScheduleList(@Param("request") BasePageRequest request, @Param("projectIds") List<String> projectIds); List<TaskHubScheduleDTO> selectScheduleList(@Param("request") BasePageRequest request, @Param("projectIds") List<String> projectIds);
List<Schedule> getSchedules(@Param("request") TableBatchProcessDTO request, @Param("projectIds") List<String> projectIds);
} }

View File

@ -412,4 +412,23 @@
</foreach> </foreach>
</if> </if>
</sql> </sql>
<select id="getSchedules" resultType="io.metersphere.system.domain.Schedule">
select id, enable from schedule
<where>
<if test="projectIds != null and projectIds.size() > 0">
and task.project_id IN
<foreach collection="projectIds" item="projectId" separator="," open="(" close=")">
#{projectId}
</foreach>
</if>
<if test="request.condition.keyword != null and request.condition.keyword != ''">
and (
schedule.num like concat('%', #{request.keyword},'%')
or schedule.name like concat('%', #{request.keyword},'%')
)
</if>
</where>
</select>
</mapper> </mapper>

View File

@ -35,6 +35,7 @@ import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.mapper.*; import io.metersphere.system.mapper.*;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.schedule.ApiScheduleNoticeService; import io.metersphere.system.schedule.ApiScheduleNoticeService;
import io.metersphere.system.schedule.ScheduleService; import io.metersphere.system.schedule.ScheduleService;
import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.PageUtils;
@ -782,4 +783,45 @@ public class BaseTaskHubService {
apiScheduleNoticeService.sendScheduleNotice(schedule, userId); apiScheduleNoticeService.sendScheduleNotice(schedule, userId);
saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), module, OperationLogType.UPDATE.name()); saveLog(List.of(schedule), userId, path, HttpMethodConstants.GET.name(), module, OperationLogType.UPDATE.name());
} }
public void scheduleBatchOperation(TableBatchProcessDTO request, String userId, String projectId, String path, String module, boolean enable, List<String> projectIds) {
List<Schedule> scheduleList = getSchedule(request, projectIds, enable);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ScheduleMapper batchMapper = sqlSession.getMapper(ScheduleMapper.class);
SubListUtils.dealForSubList(scheduleList, 100, list -> {
list.forEach(s -> {
s.setEnable(enable);
batchMapper.updateByPrimaryKeySelective(s);
try {
scheduleService.addOrUpdateCronJob(s, new JobKey(s.getKey(), s.getJob()),
new TriggerKey(s.getKey(), s.getJob()), Class.forName(s.getJob()));
} catch (ClassNotFoundException e) {
LogUtils.error(e);
throw new RuntimeException(e);
}
});
sqlSession.flushStatements();
});
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
apiScheduleNoticeService.batchSendNotice(projectId, scheduleList, userMapper.selectByPrimaryKey(userId), enable ? NoticeConstants.Event.OPEN : NoticeConstants.Event.CLOSE);
saveLog(scheduleList, userId, path, HttpMethodConstants.POST.name(), module, OperationLogType.UPDATE.name());
}
private List<Schedule> getSchedule(TableBatchProcessDTO request, List<String> projectIds, boolean enable) {
List<Schedule> list;
if (request.isSelectAll()) {
list = extScheduleMapper.getSchedules(request, projectIds);
} else {
ScheduleExample example = new ScheduleExample();
example.createCriteria().andIdIn(request.getSelectIds());
list = scheduleMapper.selectByExample(example);
}
list = list.stream().filter(s -> s.getEnable() != enable).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(request.getExcludeIds())) {
list.removeIf(schedule -> request.getExcludeIds().contains(schedule.getId()));
}
return list;
}
} }

View File

@ -53,6 +53,8 @@ public class BaseTaskHubControllerTests extends BaseTest {
public static final String SYSTEM_SCHEDULE_TASK_DELETE = "/system/task-center/schedule/delete/"; public static final String SYSTEM_SCHEDULE_TASK_DELETE = "/system/task-center/schedule/delete/";
public static final String SYSTEM_SCHEDULE_TASK_SWITCH = "/system/task-center/schedule/switch/"; public static final String SYSTEM_SCHEDULE_TASK_SWITCH = "/system/task-center/schedule/switch/";
public static final String SYSTEM_SCHEDULE_TASK_BATCH_ENABLE = "/system/task-center/schedule/batch-enable";
public static final String SYSTEM_SCHEDULE_TASK_BATCH_DISABLE = "/system/task-center/schedule/batch-disable";
@Test @Test
@Order(1) @Order(1)
@ -267,6 +269,28 @@ public class BaseTaskHubControllerTests extends BaseTest {
} }
@Test
@Order(7)
public void systemScheduleBatchEnable() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(SYSTEM_SCHEDULE_TASK_BATCH_ENABLE, request);
request.setSelectAll(false);
request.setSelectIds(List.of("wx_1", "wx_2"));
this.requestPost(SYSTEM_SCHEDULE_TASK_BATCH_ENABLE, request);
}
@Test
@Order(8)
public void systemScheduleBatchDisable() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(SYSTEM_SCHEDULE_TASK_BATCH_DISABLE, request);
request.setSelectAll(false);
request.setSelectIds(List.of("wx_1", "wx_2"));
this.requestPost(SYSTEM_SCHEDULE_TASK_BATCH_DISABLE, request);
}
/** /**
* 组织任务中心测试用例 * 组织任务中心测试用例
*/ */
@ -284,6 +308,8 @@ public class BaseTaskHubControllerTests extends BaseTest {
public static final String ORG_TASK_ITEM_BATCH_STOP = "/organization/task-center/exec-task/item/batch-stop"; public static final String ORG_TASK_ITEM_BATCH_STOP = "/organization/task-center/exec-task/item/batch-stop";
public static final String ORG_SCHEDULE_TASK_DELETE = "/organization/task-center/schedule/delete/"; public static final String ORG_SCHEDULE_TASK_DELETE = "/organization/task-center/schedule/delete/";
public static final String ORG_SCHEDULE_TASK_SWITCH = "/organization/task-center/schedule/switch/"; public static final String ORG_SCHEDULE_TASK_SWITCH = "/organization/task-center/schedule/switch/";
public static final String ORG_SCHEDULE_TASK_BATCH_ENABLE = "/organization/task-center/schedule/batch-enable";
public static final String ORG_SCHEDULE_TASK_BATCH_DISABLE = "/organization/task-center/schedule/batch-disable";
@Test @Test
@Order(20) @Order(20)
@ -480,6 +506,24 @@ public class BaseTaskHubControllerTests extends BaseTest {
} }
@Test
@Order(28)
public void orgScheduleBatchEnable() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(ORG_SCHEDULE_TASK_BATCH_ENABLE, request);
}
@Test
@Order(28)
public void orgScheduleBatchDisable() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(ORG_SCHEDULE_TASK_BATCH_DISABLE, request);
}
@Test @Test
@Order(21) @Order(21)
public void testInsert() throws Exception { public void testInsert() throws Exception {