diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index 7496257938..25df856dd9 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -10,20 +10,16 @@ import io.metersphere.api.service.ApiAutomationService; import io.metersphere.base.domain.ApiScenario; import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.domain.Schedule; -import io.metersphere.base.domain.UserGroup; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.OperLogConstants; import io.metersphere.commons.constants.PermissionConstants; import io.metersphere.commons.constants.TriggerMode; import io.metersphere.commons.utils.PageUtils; import io.metersphere.commons.utils.Pager; -import io.metersphere.commons.utils.SessionUtils; import io.metersphere.controller.request.ScheduleRequest; import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest; import io.metersphere.track.request.testplan.FileOperationRequest; -import org.apache.commons.lang3.StringUtils; -import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -32,9 +28,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.util.HashMap; import java.util.List; -import java.util.stream.Collectors; @RestController @RequestMapping(value = "/api/automation") @@ -251,6 +245,11 @@ public class ApiAutomationController { public List batchGenPerformanceTestJmx(@RequestBody ApiScenarioBatchRequest request) { return apiAutomationService.batchGenPerformanceTestJmx(request); } + @PostMapping("/batchCopy") + public void batchCopy(@RequestBody ApiScenarioBatchRequest request) { + apiAutomationService.batchCopy(request); + } + @PostMapping("/file/download") public ResponseEntity download(@RequestBody FileOperationRequest fileOperationRequest) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 4cb2129ef9..6f45ec6cf1 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -315,6 +315,20 @@ public class ApiAutomationService { } } + private boolean isCustomNumExist(ApiScenarioWithBLOBs blobs) { + ApiScenarioExample example = new ApiScenarioExample(); + example.createCriteria() + .andCustomNumEqualTo(blobs.getCustomNum()) + .andProjectIdEqualTo(blobs.getProjectId()) + .andIdNotEqualTo(blobs.getId()); + List list = apiScenarioMapper.selectByExample(example); + if (CollectionUtils.isNotEmpty(list)) { + return true; + }else { + return false; + } + } + private int getNextNum(String projectId) { ApiScenario apiScenario = extApiScenarioMapper.getNextNum(projectId); if (apiScenario == null) { @@ -2149,4 +2163,51 @@ public class ApiAutomationService { return returnList; } } + + public void batchCopy(ApiScenarioBatchRequest batchRequest) { + + ServiceUtils.getSelectAllIds(batchRequest, batchRequest.getCondition(), + (query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query)); + List apiScenarioList = extApiScenarioMapper.selectIds(batchRequest.getIds()); + for (ApiScenarioWithBLOBs apiModel:apiScenarioList) { + ApiScenarioWithBLOBs newModel = apiModel; + newModel.setId(UUID.randomUUID().toString()); + newModel.setName("copy_"+apiModel.getName()); + newModel.setCreateTime(System.currentTimeMillis()); + newModel.setNum(getNextNum(newModel.getProjectId())); + + ApiScenarioExample example = new ApiScenarioExample(); + example.createCriteria().andNameEqualTo(newModel.getName()). + andProjectIdEqualTo(newModel.getProjectId()).andStatusNotEqualTo("Trash").andIdNotEqualTo(newModel.getId()); + if (apiScenarioMapper.countByExample(example) > 0) { + continue; + }else { + boolean insertFlag = true; + if (StringUtils.isNotBlank(newModel.getCustomNum())) { + insertFlag = false; + String projectId = newModel.getProjectId(); + Project project = projectMapper.selectByPrimaryKey(projectId); + if (project != null) { + Boolean customNum = project.getScenarioCustomNum(); + // 未开启自定义ID + if (!customNum) { + insertFlag = true; + newModel.setCustomNum(null); + } else { + boolean isCustomNumExist = true; + try { + isCustomNumExist = this.isCustomNumExist(newModel); + }catch (Exception e){} + insertFlag = !isCustomNumExist; + } + } + } + + if(insertFlag){ + apiScenarioMapper.insert(newModel); + } + } + } +// uploadFiles(request, bodyFiles, scenarioFiles); + } } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml index 349e74fcfc..e4508263e0 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioMapper.xml @@ -374,7 +374,6 @@ from api_scenario left join project on api_scenario.project_id = project.id -