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 1fab38daca..b84ec857af 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -630,6 +630,9 @@ public class ApiDefinitionService { reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); apiDefinition.setRequest(requestStr); importApiCase(apiDefinition, apiTestImportRequest); + }else { + //不覆盖的接口,如果没有sameRequest则不导入。此时清空mock信息 + mocks.clear(); } } else { _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases, mocks); @@ -696,6 +699,7 @@ public class ApiDefinitionService { apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); apiDefinition.setRequest(request); reSetImportCasesApiId(cases, originId, apiDefinition.getId()); + reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); importApiCase(apiDefinition, apiTestImportRequest); } else { apiDefinition.setId(sameRequest.get(0).getId()); diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index 75a83cb91b..e5c274a048 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -1381,7 +1381,7 @@ public class MockConfigService { if (CollectionUtils.isNotEmpty(apiImport.getMocks())) { Map> saveMap = new HashMap<>(); for (MockConfigImportDTO dto : apiImport.getMocks()) { - String apiId = dto.getApiId();//de33108c-26e2-4d4f-826a-a5f8e017d2f4 + String apiId = dto.getApiId(); if (saveMap.containsKey(apiId)) { saveMap.get(apiId).add(dto); } else { @@ -1393,37 +1393,91 @@ public class MockConfigService { for (Map.Entry> entry : saveMap.entrySet()) { String apiId = entry.getKey(); - this.deleteMockConfigByApiId(apiId); - List list = entry.getValue(); - String mockId = UUID.randomUUID().toString(); - MockConfig config = new MockConfig(); - config.setProjectId(request.getProjectId()); - config.setId(mockId); - config.setCreateUserId(SessionUtils.getUserId()); - config.setCreateTime(System.currentTimeMillis()); - config.setUpdateTime(System.currentTimeMillis()); - config.setApiId(apiId); - mockConfigMapper.insert(config); - - int batchCount = 0; - for (MockExpectConfigWithBLOBs mockExpect : list) { - mockExpect.setId(UUID.randomUUID().toString()); - mockExpect.setMockConfigId(mockId); - mockExpect.setCreateTime(System.currentTimeMillis()); - mockExpect.setUpdateTime(System.currentTimeMillis()); - mockExpect.setCreateUserId(SessionUtils.getUserId()); - mockExpectConfigMapper.insert(mockExpect); - } - if (batchCount % 300 == 0) { - sqlSession.flushStatements(); + MockConfig mockConfig = this.selectMockConfigByApiId(apiId); + if (mockConfig == null) { + this.insertMockExpectConfigs(apiId, request.getProjectId(), list, sqlSession); + } else { + this.updateMockExpectConfigs(mockConfig, list ,sqlSession); } } } } + private void updateMockExpectConfigs(MockConfig mockConfig, List list, SqlSession sqlSession) { + int batchCount = 0; + for (MockExpectConfigWithBLOBs mockExpect : list) { + MockExpectConfig expectInDb = this.findMockExpectConfigByMockConfigIdAndExpectNum(mockConfig.getId(),mockExpect.getExpectNum()); + if(expectInDb == null){ + mockExpect.setId(UUID.randomUUID().toString()); + mockExpect.setMockConfigId(mockConfig.getId()); + mockExpect.setCreateTime(System.currentTimeMillis()); + mockExpect.setUpdateTime(System.currentTimeMillis()); + mockExpect.setCreateUserId(SessionUtils.getUserId()); + mockExpectConfigMapper.insert(mockExpect); + }else { + mockExpect.setMockConfigId(mockConfig.getId()); + mockExpect.setId(expectInDb.getId()); + mockExpect.setUpdateTime(System.currentTimeMillis()); + mockExpectConfigMapper.updateByPrimaryKey(mockExpect); + } + + } + if (batchCount % 300 == 0) { + sqlSession.flushStatements(); + } + } + + private MockExpectConfig findMockExpectConfigByMockConfigIdAndExpectNum(String mockConfigId, String expectNum) { + MockExpectConfigExample example = new MockExpectConfigExample(); + example.createCriteria().andMockConfigIdEqualTo(mockConfigId).andExpectNumEqualTo(expectNum); + List bloBs = this.mockExpectConfigMapper.selectByExample(example); + if(CollectionUtils.isNotEmpty(bloBs)){ + return bloBs.get(0); + }else { + return null; + } + } + + private void insertMockExpectConfigs(String apiId, String projectId, List list, SqlSession sqlSession) { + String mockId = UUID.randomUUID().toString(); + MockConfig config = new MockConfig(); + config.setProjectId(projectId); + config.setId(mockId); + config.setCreateUserId(SessionUtils.getUserId()); + config.setCreateTime(System.currentTimeMillis()); + config.setUpdateTime(System.currentTimeMillis()); + config.setApiId(apiId); + mockConfigMapper.insert(config); + + int batchCount = 0; + for (MockExpectConfigWithBLOBs mockExpect : list) { + mockExpect.setId(UUID.randomUUID().toString()); + mockExpect.setMockConfigId(mockId); + mockExpect.setCreateTime(System.currentTimeMillis()); + mockExpect.setUpdateTime(System.currentTimeMillis()); + mockExpect.setCreateUserId(SessionUtils.getUserId()); + mockExpectConfigMapper.insert(mockExpect); + } + if (batchCount % 300 == 0) { + sqlSession.flushStatements(); + } + } + + + private MockConfig selectMockConfigByApiId(String apiId) { + MockConfigExample example = new MockConfigExample(); + example.createCriteria().andApiIdEqualTo(apiId); + List mockConfigList = this.mockConfigMapper.selectByExample(example); + if (CollectionUtils.isNotEmpty(mockConfigList)) { + return mockConfigList.get(0); + } else { + return null; + } + } + public void updateMockReturnMsgByApi(ApiDefinitionWithBLOBs apiDefinitionWithBLOBs) { if (apiDefinitionWithBLOBs == null) { return;