diff --git a/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewManege.java b/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewManege.java index f909faba61..aff3756164 100644 --- a/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewManege.java +++ b/core/core-backend/src/main/java/io/dataease/chart/manage/ChartViewManege.java @@ -30,6 +30,7 @@ import io.dataease.license.config.XpackInteract; import io.dataease.utils.BeanUtils; import io.dataease.utils.IDUtils; import io.dataease.utils.JsonUtil; +import io.dataease.utils.LogUtil; import io.dataease.visualization.dao.auto.entity.DataVisualizationInfo; import io.dataease.visualization.dao.auto.mapper.DataVisualizationInfoMapper; import jakarta.annotation.Resource; @@ -41,6 +42,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.*; +import java.util.concurrent.*; import java.util.stream.Collectors; /** @@ -127,23 +129,53 @@ public class ChartViewManege { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("scene_id", sceneId); List chartViewDTOS = transChart(coreChartViewMapper.selectList(wrapper)); - if(!CollectionUtils.isEmpty(chartViewDTOS)){ - List tableIds = chartViewDTOS.stream() - .map(ChartViewDTO::getTableId) // 提取 id + if (!CollectionUtils.isEmpty(chartViewDTOS)) { + List tableIds = chartViewDTOS.stream() + .map(ChartViewDTO::getTableId).distinct() .toList(); - if(!CollectionUtils.isEmpty(tableIds)){ + if (!CollectionUtils.isEmpty(tableIds)) { QueryWrapper wp = new QueryWrapper<>(); wp.in("dataset_group_id", tableIds); List coreDatasetTableFields = coreDatasetTableFieldMapper.selectList(wp); Map> groupedByTableId = coreDatasetTableFields.stream() .collect(Collectors.groupingBy(CoreDatasetTableField::getDatasetGroupId)); - chartViewDTOS.forEach(dto ->{ - if(dto.getTableId() !=null){ - dto.setCalParams(Utils.getParams(datasetTableFieldManage.transDTO(groupedByTableId.get(dto.getTableId())))); - } - }); - } + if(chartViewDTOS.size()<10){ + chartViewDTOS.forEach(dto -> { + if (dto.getTableId() != null) { + dto.setCalParams(Utils.getParams(datasetTableFieldManage.transDTO(groupedByTableId.get(dto.getTableId())))); + } + }); + }else{ + ExecutorService executor = Executors.newFixedThreadPool(10); + try { + // 超过10个图表要处理启用多线程处理 + CountDownLatch latch = new CountDownLatch(chartViewDTOS.size()); + chartViewDTOS.forEach(dto -> { + executor.submit(() -> { + try { + if (dto.getTableId() != null) { + dto.setCalParams(Utils.getParams(datasetTableFieldManage.transDTO(groupedByTableId.get(dto.getTableId())))); + } + } finally { + latch.countDown(); // 减少计数器 + } + }); + }); + // 等待所有线程完成 + boolean completedInTime = latch.await(200, TimeUnit.SECONDS); + if (!completedInTime) { + throw new InterruptedException("Tasks did not complete within 200 seconds"); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LogUtil.error(e); + } finally { + executor.shutdown(); // 确保线程池关闭 + } + } + + } } return chartViewDTOS; } diff --git a/core/core-backend/src/main/java/io/dataease/dataset/manage/DatasetTableFieldManage.java b/core/core-backend/src/main/java/io/dataease/dataset/manage/DatasetTableFieldManage.java index 38a5ff9b91..7831068c0d 100644 --- a/core/core-backend/src/main/java/io/dataease/dataset/manage/DatasetTableFieldManage.java +++ b/core/core-backend/src/main/java/io/dataease/dataset/manage/DatasetTableFieldManage.java @@ -279,18 +279,23 @@ public class DatasetTableFieldManage { } public List transDTO(List list) { - return list.stream().map(ele -> { - DatasetTableFieldDTO dto = new DatasetTableFieldDTO(); - if (ele == null) return null; - BeanUtils.copyBean(dto, ele); - if (StringUtils.isNotEmpty(ele.getParams())) { - TypeReference> tokenType = new TypeReference<>() { - }; - List calParams = JsonUtil.parseList(ele.getParams(), tokenType); - dto.setParams(calParams); - } - return dto; - }).collect(Collectors.toList()); + if(!CollectionUtils.isEmpty(list)){ + return list.stream().map(ele -> { + DatasetTableFieldDTO dto = new DatasetTableFieldDTO(); + if (ele == null) return null; + BeanUtils.copyBean(dto, ele); + if (StringUtils.isNotEmpty(ele.getParams())) { + TypeReference> tokenType = new TypeReference<>() { + }; + List calParams = JsonUtil.parseList(ele.getParams(), tokenType); + dto.setParams(calParams); + } + return dto; + }).collect(Collectors.toList()); + }else{ + return new ArrayList<>(); + } + } private CoreDatasetTableField transDTO2Record(DatasetTableFieldDTO dto) {