From 182c50fad138f2155402758a73e0f09eb13e355b Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Fri, 25 Oct 2024 10:30:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9C=80=E6=B1=82=E5=90=8C=E6=AD=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../functional/service/DemandSyncService.java | 70 ++++++++++++------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/DemandSyncService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/DemandSyncService.java index b543662895..a94f8e5cdd 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/DemandSyncService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/DemandSyncService.java @@ -19,7 +19,6 @@ 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; @@ -45,7 +44,8 @@ public class DemandSyncService { /** * 定时任务同步缺陷(存量-默认中文环境通知) - * @param projectId 项目ID + * + * @param projectId 项目ID * @param scheduleUser 任务触发用户 */ public void syncPlatformDemandBySchedule(String projectId, String scheduleUser) { @@ -53,45 +53,63 @@ public class DemandSyncService { // 创建一个 List 来保存合并后的结果 Platform platform = projectApplicationService.getPlatform(projectId, false); Map> updateMap = new HashMap<>(); - ListdeleteIds = new ArrayList<>(); + List deleteIds = new ArrayList<>(); + // 批量处理需求更新 + processDemandUpdates(projectId, platformId, platform, updateMap, deleteIds); + + if (CollectionUtils.isNotEmpty(deleteIds)) { + deleteDemands(deleteIds); + } + batchUpdateDemands(updateMap); + LogUtils.info("End synchronizing demands"); + } + + private void deleteDemands(List deleteIds) { + FunctionalCaseDemandExample functionalCaseDemandExample = new FunctionalCaseDemandExample(); + List deleteIdDistinct = deleteIds.stream().distinct().toList(); + functionalCaseDemandExample.createCriteria().andDemandIdIn(deleteIdDistinct); + demandMapper.deleteByExample(functionalCaseDemandExample); + } + + private void processDemandUpdates(String projectId, String platformId, Platform platform, Map> updateMap, List deleteIds) { int pageNumber = 1; boolean count = true; Page page = PageHelper.startPage(pageNumber, DEFAULT_BATCH_SIZE, count); - Pager> listPager = PageUtils.setPageInfo(page, extFunctionalCaseDemandMapper.selectDemandByProjectId(projectId, platformId)); + Pager> listPager = PageUtils.setPageInfo(page, extFunctionalCaseDemandMapper.selectDemandByProjectId(projectId, platformId)); long total = listPager.getTotal(); List list = listPager.getList(); Map> demandFirstMap = list.stream().collect(Collectors.groupingBy(FunctionalCaseDemand::getDemandId)); Set demandFirstIds = demandFirstMap.keySet(); buildUpdateMap(projectId, demandFirstIds, platform, demandFirstMap, platformId, updateMap, deleteIds); count = false; - for (int i = 1; i < ((int)Math.ceil((double) total/DEFAULT_BATCH_SIZE)); i ++) { - Page pageCycle = PageHelper.startPage(i+1, DEFAULT_BATCH_SIZE, count); - Pager> listPagerCycle = PageUtils.setPageInfo(pageCycle, extFunctionalCaseDemandMapper.selectDemandByProjectId(projectId,platformId)); + for (int i = 1; i < ((int) Math.ceil((double) total / DEFAULT_BATCH_SIZE)); i++) { + Page pageCycle = PageHelper.startPage(i + 1, DEFAULT_BATCH_SIZE, count); + Pager> listPagerCycle = PageUtils.setPageInfo(pageCycle, extFunctionalCaseDemandMapper.selectDemandByProjectId(projectId, platformId)); List pageResults = listPagerCycle.getList(); + if (CollectionUtils.isEmpty(pageResults)) { + break; // 如果列表为空,退出循环 + } Map> demandsCycleMap = pageResults.stream().collect(Collectors.groupingBy(FunctionalCaseDemand::getDemandId)); Set demandCycleIds = demandsCycleMap.keySet(); buildUpdateMap(projectId, demandCycleIds, platform, demandsCycleMap, platformId, updateMap, deleteIds); } - FunctionalCaseDemandExample functionalCaseDemandExample = new FunctionalCaseDemandExample(); - if (CollectionUtils.isNotEmpty(deleteIds)) { - List deleteIdDistinct = deleteIds.stream().distinct().toList(); - functionalCaseDemandExample.createCriteria().andDemandIdIn(deleteIdDistinct); - demandMapper.deleteByExample(functionalCaseDemandExample); - } - updateMap.forEach((k,v)->{ - for (FunctionalCaseDemand functionalCaseDemand : v) { - try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { - FunctionalCaseDemandMapper functionalCaseDemandMapper = sqlSession.getMapper(FunctionalCaseDemandMapper.class); - functionalCaseDemandMapper.updateByPrimaryKeySelective(functionalCaseDemand); - sqlSession.flushStatements(); - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - } - } - }); - LogUtils.info("End synchronizing demands"); } - private void buildUpdateMap(String projectId, Set demandIds, Platform platform, Map> demandMap, String platformId, Map> updateMap, ListdeleteIds) { + private void batchUpdateDemands(Map> updateMap) { + try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { + FunctionalCaseDemandMapper functionalCaseDemandMapper = sqlSession.getMapper(FunctionalCaseDemandMapper.class); + updateMap.forEach((k, v) -> { + for (FunctionalCaseDemand functionalCaseDemand : v) { + functionalCaseDemandMapper.updateByPrimaryKeySelective(functionalCaseDemand); + } + }); + sqlSession.flushStatements(); + } catch (Exception e) { + LogUtils.info("Synchronizing demands error:" + e.getMessage()); + } + } + + private void buildUpdateMap(String projectId, Set demandIds, Platform platform, Map> demandMap, String platformId, Map> updateMap, List deleteIds) { DemandRelateQueryRequest demandRelateQueryRequest = new DemandRelateQueryRequest(); demandRelateQueryRequest.setProjectConfig(projectApplicationService.getProjectDemandThirdPartConfig(projectId)); demandRelateQueryRequest.setRelateDemandIds(new ArrayList<>(demandIds)); @@ -112,7 +130,7 @@ public class DemandSyncService { } for (PlatformDemandDTO.Demand demand : demandList) { List functionalCaseDemands = demandMap.get(demand.getDemandId()); - ListupdateList = new ArrayList<>(); + List updateList = new ArrayList<>(); if (CollectionUtils.isNotEmpty(functionalCaseDemands)) { for (FunctionalCaseDemand functionalCaseDemand : functionalCaseDemands) { FunctionalCaseDemand update = buildFunctionalCaseDemand(functionalCaseDemand.getId(), platformId, demand);