diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 4517f33043..af4293b64b 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -128,6 +128,53 @@ public class ApiDefinitionService { return resList; } + public void initDefaultModuleId(){ + ApiDefinitionExample example = new ApiDefinitionExample(); + example.createCriteria().andModuleIdIsNull(); + List updateApiList = apiDefinitionMapper.selectByExample(example); + Map>> projectIdMap = new HashMap<>(); + for (ApiDefinition api : updateApiList) { + String projectId = api.getProjectId(); + String protocal = api.getProtocol(); + if (projectIdMap.containsKey(projectId)) { + if(projectIdMap.get(projectId).containsKey(protocal)){ + projectIdMap.get(projectId).get(protocal).add(api); + }else { + List list = new ArrayList<>(); + list.add(api); + projectIdMap.get(projectId).put(protocal,list); + } + } else { + List list = new ArrayList<>(); + list.add(api); + Map> map = new HashMap<>(); + map.put(protocal,list); + projectIdMap.put(projectId, map); + } + } + ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); + for (Map.Entry>> entry : projectIdMap.entrySet()) { + String projectId = entry.getKey(); + Map> map = entry.getValue(); + + for (Map.Entry> itemEntry : map.entrySet()) { + String protocal = itemEntry.getKey(); + ApiModule node = apiModuleService.getDefaultNodeUnCreateNew(projectId, protocal); + if(node != null){ + List testCaseList = itemEntry.getValue(); + for (ApiDefinition apiDefinition : testCaseList) { + ApiDefinitionWithBLOBs updateCase = new ApiDefinitionWithBLOBs(); + updateCase.setId(apiDefinition.getId()); + updateCase.setModuleId(node.getId()); + updateCase.setModulePath("/" + node.getName()); + + apiDefinitionMapper.updateByPrimaryKeySelective(updateCase); + } + } + } + } + } + public List listBatch(ApiBatchRequest request) { ServiceUtils.getSelectAllIds(request, request.getCondition(), (query) -> extApiDefinitionMapper.selectIds(query)); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index 6a9eb321c9..325608cede 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -522,4 +522,15 @@ public class ApiModuleService extends NodeTreeService { return list.get(0); } } + + public ApiModule getDefaultNodeUnCreateNew(String projectId,String protocol) { + ApiModuleExample example = new ApiModuleExample(); + example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("未规划接口").andParentIdIsNull();; + List list = apiModuleMapper.selectByExample(example); + if (CollectionUtils.isEmpty(list)) { + return null; + }else { + return list.get(0); + } + } } diff --git a/backend/src/main/java/io/metersphere/listener/AppStartListener.java b/backend/src/main/java/io/metersphere/listener/AppStartListener.java index cc943418ef..8e05737731 100644 --- a/backend/src/main/java/io/metersphere/listener/AppStartListener.java +++ b/backend/src/main/java/io/metersphere/listener/AppStartListener.java @@ -131,6 +131,8 @@ public class AppStartListener implements ApplicationListener