diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java index 5f7818fa66..fea1fe939d 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java @@ -2,6 +2,7 @@ package io.metersphere.project.controller; import io.metersphere.sdk.constants.PermissionConstants; 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.taskhub.ResourcePoolOptionsDTO; import io.metersphere.system.dto.taskhub.TaskHubDTO; @@ -156,4 +157,20 @@ public class ProjectTaskHubController { public void enable(@PathVariable String id) { 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())); + } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java index a11fb99dda..31d036318c 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java @@ -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_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_BATCH_ENABLE = "/project/task-center/schedule/batch-enable"; + public static final String PROJECT_SCHEDULE_TASK_BATCH_DISABLE = "/project/task-center/schedule/batch-disable"; @Test @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 { 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); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java index 1f6dd958f5..cef5af1f6d 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java @@ -167,4 +167,24 @@ public class OrganizationTaskHubController { public void enable(@PathVariable String id) { 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 projectList = baseProjectMapper.getProjectOptionsByOrgId(SessionUtils.getCurrentOrganizationId()); + List 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 projectList = baseProjectMapper.getProjectOptionsByOrgId(SessionUtils.getCurrentOrganizationId()); + List 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); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java index 258e341887..7b18ef3f66 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java @@ -169,5 +169,21 @@ public class SystemTaskHubController { public void enable(@PathVariable String id) { 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 系统&组织&项目 后台任务操作:删除 批量开启 批量关闭 } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java index e09ff7d7ec..e6c9c766c7 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java @@ -5,6 +5,7 @@ import io.metersphere.api.domain.ApiTestCase; import io.metersphere.system.domain.Schedule; import io.metersphere.system.dto.ProjectDTO; 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.request.TaskCenterScheduleBatchRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; @@ -45,4 +46,6 @@ public interface ExtScheduleMapper { int countByProjectIds(@Param("ids") List ids); List selectScheduleList(@Param("request") BasePageRequest request, @Param("projectIds") List projectIds); + + List getSchedules(@Param("request") TableBatchProcessDTO request, @Param("projectIds") List projectIds); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml index f509c2f500..c7247c8ea5 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml @@ -412,4 +412,23 @@ + + + \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java index ae81bcf096..73cd6a4def 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java @@ -35,6 +35,7 @@ import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.mapper.*; +import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.schedule.ApiScheduleNoticeService; import io.metersphere.system.schedule.ScheduleService; import io.metersphere.system.utils.PageUtils; @@ -782,4 +783,45 @@ public class BaseTaskHubService { apiScheduleNoticeService.sendScheduleNotice(schedule, userId); 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 projectIds) { + List 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 getSchedule(TableBatchProcessDTO request, List projectIds, boolean enable) { + List 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; + } + } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java index f8c2ff7393..3a16c6eaad 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java @@ -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_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 @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_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_BATCH_ENABLE = "/organization/task-center/schedule/batch-enable"; + public static final String ORG_SCHEDULE_TASK_BATCH_DISABLE = "/organization/task-center/schedule/batch-disable"; @Test @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 @Order(21) public void testInsert() throws Exception {