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 63a17e8393..7d71a91f7d 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 @@ -1,13 +1,17 @@ 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.taskhub.TaskHubDTO; +import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO; import io.metersphere.system.service.BaseTaskHubService; import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.SessionUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -29,4 +33,11 @@ public class ProjectTaskHubController { public Pager> projectList(@Validated @RequestBody BasePageRequest request) { return baseTaskHubService.getTaskList(request, null, SessionUtils.getCurrentProjectId()); } + + @PostMapping("/schedule/page") + @Operation(summary = "项目-任务中心-后台执行任务列表") + @RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ) + public Pager> scheduleList(@Validated @RequestBody BasePageRequest request) { + return baseTaskHubService.getScheduleTaskList(request, 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 16b69607a7..380b8183f6 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 @@ -18,6 +18,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { * 项目任务中心测试用例 */ public static final String PROJECT_TASK_PAGE = "/project/task-center/exec-task/page"; + public static final String PROJECT_SCHEDULE_TASK_PAGE = "/project/task-center/schedule/page"; @Test @Order(1) @@ -34,4 +35,23 @@ public class ProjectTaskHubControllerTests extends BaseTest { // 返回请求正常 Assertions.assertNotNull(resultHolder); } + + + /** + * 项目后台任务 + */ + @Test + @Order(2) + public void getProjectSchedulePage() throws Exception { + BasePageRequest request = new BasePageRequest(); + this.requestPost(PROJECT_SCHEDULE_TASK_PAGE, request); + request.setCurrent(1); + request.setPageSize(10); + MvcResult mvcResult = this.requestPostWithOkAndReturn(PROJECT_SCHEDULE_TASK_PAGE, request); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } } 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 b48a4430bb..f6f353c6cc 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 @@ -2,18 +2,23 @@ package io.metersphere.system.service; import com.github.pagehelper.Page; import com.github.pagehelper.page.PageMethod; +import io.metersphere.sdk.util.SubListUtils; +import io.metersphere.system.domain.ExecTask; +import io.metersphere.system.domain.ExecTaskItem; import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.taskhub.TaskHubDTO; import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO; -import io.metersphere.system.mapper.ExtExecTaskMapper; -import io.metersphere.system.mapper.ExtOrganizationMapper; -import io.metersphere.system.mapper.ExtScheduleMapper; +import io.metersphere.system.mapper.*; import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -39,6 +44,8 @@ public class BaseTaskHubService { ExtOrganizationMapper extOrganizationMapper; @Resource UserLoginService userLoginService; + @Resource + private SqlSessionFactory sqlSessionFactory; /** * 系统-获取执行任务列表 @@ -103,4 +110,47 @@ public class BaseTaskHubService { return extOrganizationMapper.getOrgListByProjectIds(projectIds); } + + /** + * 单任务详情数据入库接口 + * + * @param items + */ + public void insertExecTaskAndDetail(List items) { + if (CollectionUtils.isNotEmpty(items)) { + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + ExecTaskItemMapper itemMapper = sqlSession.getMapper(ExecTaskItemMapper.class); + SubListUtils.dealForSubList(items, 1000, subList -> { + subList.forEach(itemMapper::insertSelective); + }); + sqlSession.flushStatements(); + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } + } + + + /** + * 批量任务&任务详情入库接口 + * + * @param tasks + * @param items + */ + public void insertExecTaskAndDetail(List tasks, List items) { + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + if (CollectionUtils.isNotEmpty(tasks)) { + ExecTaskMapper execTaskMapper = sqlSession.getMapper(ExecTaskMapper.class); + SubListUtils.dealForSubList(tasks, 1000, subList -> { + subList.forEach(execTaskMapper::insertSelective); + }); + } + + if (CollectionUtils.isNotEmpty(items)) { + ExecTaskItemMapper itemMapper = sqlSession.getMapper(ExecTaskItemMapper.class); + SubListUtils.dealForSubList(items, 1000, subList -> { + subList.forEach(itemMapper::insertSelective); + }); + } + sqlSession.flushStatements(); + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } 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 e372e12558..3ca30ff11f 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 @@ -3,7 +3,11 @@ package io.metersphere.system.controller; import io.metersphere.sdk.util.JSON; import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.handler.ResultHolder; +import io.metersphere.system.domain.ExecTask; +import io.metersphere.system.domain.ExecTaskItem; import io.metersphere.system.dto.sdk.BasePageRequest; +import io.metersphere.system.service.BaseTaskHubService; +import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; @@ -12,12 +16,17 @@ import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MvcResult; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class BaseTaskHubControllerTests extends BaseTest { + @Resource + private BaseTaskHubService baseTaskHubService; + /** * 系统任务中心测试用例 */ @@ -86,7 +95,7 @@ public class BaseTaskHubControllerTests extends BaseTest { * 组织后台任务 */ @Test - @Order(2) + @Order(21) public void getOrgSchedulePage() throws Exception { BasePageRequest request = new BasePageRequest(); this.requestPost(ORG_SCHEDULE_TASK_PAGE, request); @@ -99,4 +108,40 @@ public class BaseTaskHubControllerTests extends BaseTest { // 返回请求正常 Assertions.assertNotNull(resultHolder); } + + + @Test + @Order(21) + public void testInsert() throws Exception { + baseTaskHubService.insertExecTaskAndDetail(new ArrayList<>()); + ExecTaskItem execTaskItem = new ExecTaskItem(); + execTaskItem.setId("1111"); + execTaskItem.setTaskId("1"); + execTaskItem.setResourceId("1"); + execTaskItem.setStatus("SUCCESS"); + execTaskItem.setResourcePoolId("1"); + execTaskItem.setResourceType("FUNCTIONAL"); + execTaskItem.setProjectId("1234"); + execTaskItem.setOrganizationId("1234123"); + execTaskItem.setExecutor("admin"); + baseTaskHubService.insertExecTaskAndDetail(List.of(execTaskItem)); + + + baseTaskHubService.insertExecTaskAndDetail(new ArrayList<>(), new ArrayList<>()); + execTaskItem.setId("2333"); + ExecTask execTask = new ExecTask(); + execTask.setId("121321"); + execTask.setNum(123L); + execTask.setTaskName("名称"); + execTask.setStatus("SUCCESS"); + execTask.setCaseCount(123L); + execTask.setTaskType("API_CASE"); + execTask.setTriggerMode("API"); + execTask.setProjectId("1234"); + execTask.setOrganizationId("123432"); + execTask.setCreateTime(System.currentTimeMillis()); + execTask.setCreateUser("admin"); + baseTaskHubService.insertExecTaskAndDetail(List.of(execTask), List.of(execTaskItem)); + + } }