fix(项目管理): 修复需求同步问题

This commit is contained in:
guoyuqi 2024-10-25 10:30:59 +08:00 committed by Craftsman
parent 776c7edebc
commit 182c50fad1
1 changed files with 44 additions and 26 deletions

View File

@ -19,7 +19,6 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -45,6 +44,7 @@ public class DemandSyncService {
/** /**
* 定时任务同步缺陷(存量-默认中文环境通知) * 定时任务同步缺陷(存量-默认中文环境通知)
*
* @param projectId 项目ID * @param projectId 项目ID
* @param scheduleUser 任务触发用户 * @param scheduleUser 任务触发用户
*/ */
@ -54,6 +54,24 @@ public class DemandSyncService {
Platform platform = projectApplicationService.getPlatform(projectId, false); Platform platform = projectApplicationService.getPlatform(projectId, false);
Map<String, List<FunctionalCaseDemand>> updateMap = new HashMap<>(); Map<String, List<FunctionalCaseDemand>> updateMap = new HashMap<>();
List<String> deleteIds = new ArrayList<>(); List<String> 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<String> deleteIds) {
FunctionalCaseDemandExample functionalCaseDemandExample = new FunctionalCaseDemandExample();
List<String> deleteIdDistinct = deleteIds.stream().distinct().toList();
functionalCaseDemandExample.createCriteria().andDemandIdIn(deleteIdDistinct);
demandMapper.deleteByExample(functionalCaseDemandExample);
}
private void processDemandUpdates(String projectId, String platformId, Platform platform, Map<String, List<FunctionalCaseDemand>> updateMap, List<String> deleteIds) {
int pageNumber = 1; int pageNumber = 1;
boolean count = true; boolean count = true;
Page<Object> page = PageHelper.startPage(pageNumber, DEFAULT_BATCH_SIZE, count); Page<Object> page = PageHelper.startPage(pageNumber, DEFAULT_BATCH_SIZE, count);
@ -68,27 +86,27 @@ public class DemandSyncService {
Page<Object> pageCycle = PageHelper.startPage(i + 1, DEFAULT_BATCH_SIZE, count); Page<Object> pageCycle = PageHelper.startPage(i + 1, DEFAULT_BATCH_SIZE, count);
Pager<List<FunctionalCaseDemand>> listPagerCycle = PageUtils.setPageInfo(pageCycle, extFunctionalCaseDemandMapper.selectDemandByProjectId(projectId, platformId)); Pager<List<FunctionalCaseDemand>> listPagerCycle = PageUtils.setPageInfo(pageCycle, extFunctionalCaseDemandMapper.selectDemandByProjectId(projectId, platformId));
List<FunctionalCaseDemand> pageResults = listPagerCycle.getList(); List<FunctionalCaseDemand> pageResults = listPagerCycle.getList();
if (CollectionUtils.isEmpty(pageResults)) {
break; // 如果列表为空退出循环
}
Map<String, List<FunctionalCaseDemand>> demandsCycleMap = pageResults.stream().collect(Collectors.groupingBy(FunctionalCaseDemand::getDemandId)); Map<String, List<FunctionalCaseDemand>> demandsCycleMap = pageResults.stream().collect(Collectors.groupingBy(FunctionalCaseDemand::getDemandId));
Set<String> demandCycleIds = demandsCycleMap.keySet(); Set<String> demandCycleIds = demandsCycleMap.keySet();
buildUpdateMap(projectId, demandCycleIds, platform, demandsCycleMap, platformId, updateMap, deleteIds); buildUpdateMap(projectId, demandCycleIds, platform, demandsCycleMap, platformId, updateMap, deleteIds);
} }
FunctionalCaseDemandExample functionalCaseDemandExample = new FunctionalCaseDemandExample();
if (CollectionUtils.isNotEmpty(deleteIds)) {
List<String> deleteIdDistinct = deleteIds.stream().distinct().toList();
functionalCaseDemandExample.createCriteria().andDemandIdIn(deleteIdDistinct);
demandMapper.deleteByExample(functionalCaseDemandExample);
} }
updateMap.forEach((k,v)->{
for (FunctionalCaseDemand functionalCaseDemand : v) { private void batchUpdateDemands(Map<String, List<FunctionalCaseDemand>> updateMap) {
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
FunctionalCaseDemandMapper functionalCaseDemandMapper = sqlSession.getMapper(FunctionalCaseDemandMapper.class); FunctionalCaseDemandMapper functionalCaseDemandMapper = sqlSession.getMapper(FunctionalCaseDemandMapper.class);
updateMap.forEach((k, v) -> {
for (FunctionalCaseDemand functionalCaseDemand : v) {
functionalCaseDemandMapper.updateByPrimaryKeySelective(functionalCaseDemand); functionalCaseDemandMapper.updateByPrimaryKeySelective(functionalCaseDemand);
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
} }
}); });
LogUtils.info("End synchronizing demands"); sqlSession.flushStatements();
} catch (Exception e) {
LogUtils.info("Synchronizing demands error:" + e.getMessage());
}
} }
private void buildUpdateMap(String projectId, Set<String> demandIds, Platform platform, Map<String, List<FunctionalCaseDemand>> demandMap, String platformId, Map<String, List<FunctionalCaseDemand>> updateMap, List<String> deleteIds) { private void buildUpdateMap(String projectId, Set<String> demandIds, Platform platform, Map<String, List<FunctionalCaseDemand>> demandMap, String platformId, Map<String, List<FunctionalCaseDemand>> updateMap, List<String> deleteIds) {