|
@@ -0,0 +1,272 @@
|
|
|
+package com.shkpr.service.alambizplugin.components;
|
|
|
+
|
|
|
+import com.global.base.log.LogLevelFlag;
|
|
|
+import com.global.base.log.LogPrintMgr;
|
|
|
+import com.shkpr.service.alambizplugin.commproperties.TempFileProperties;
|
|
|
+import com.shkpr.service.alambizplugin.commtools.CompressorUtils;
|
|
|
+import com.shkpr.service.alambizplugin.commtools.ExcelUtils;
|
|
|
+import com.shkpr.service.alambizplugin.commtools.ShapeUtils;
|
|
|
+import com.shkpr.service.alambizplugin.constants.CommAsyncStatusEnum;
|
|
|
+import com.shkpr.service.alambizplugin.constants.ExcelEnum;
|
|
|
+import com.shkpr.service.alambizplugin.constants.GisMetadataDefine;
|
|
|
+import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
|
|
|
+import com.shkpr.service.alambizplugin.dbdao.services.intef.GisMetadataLayerTemplateService;
|
|
|
+import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyLayerApplyService;
|
|
|
+import com.shkpr.service.alambizplugin.dto.CommAsyncResult;
|
|
|
+import com.shkpr.service.alambizplugin.dto.GisMetadataLayerTemplate;
|
|
|
+import com.shkpr.service.alambizplugin.dto.GisMetadataPropertyTemplate;
|
|
|
+import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApply;
|
|
|
+import com.shkpr.service.alambizplugin.dto.GisSurveyPropertyValue;
|
|
|
+import org.apache.commons.compress.archivers.ArchiveException;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.locationtech.jts.geom.Geometry;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.AsyncResult;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.concurrent.ListenableFuture;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 第三方导出执行器
|
|
|
+ *
|
|
|
+ * @author 欧阳劲驰
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class GisSurveyThirdExporter {
|
|
|
+ /**
|
|
|
+ * log
|
|
|
+ */
|
|
|
+ private final String mStrClassName;
|
|
|
+ private final String mBizType;
|
|
|
+ private final TempFileProperties tempFileProperties;
|
|
|
+ private final GisMetadataLayerTemplateService layerTemplateService;
|
|
|
+ private final GisSurveyLayerApplyService gisSurveyLayerApplyService;
|
|
|
+
|
|
|
+
|
|
|
+ public GisSurveyThirdExporter(TempFileProperties tempFileProperties
|
|
|
+ , GisMetadataLayerTemplateService layerTemplateService, GisSurveyLayerApplyService gisSurveyLayerApplyService) {
|
|
|
+ mStrClassName = "GisSurveyThirdExporter";
|
|
|
+ mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
|
|
|
+ this.tempFileProperties = tempFileProperties;
|
|
|
+ this.layerTemplateService = layerTemplateService;
|
|
|
+ this.gisSurveyLayerApplyService = gisSurveyLayerApplyService;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 第三方导出任务
|
|
|
+ *
|
|
|
+ * @param jobId 任务id
|
|
|
+ * @return 导出结果
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ public ListenableFuture<CommAsyncResult<String>> thirdExportTask(String jobId) {
|
|
|
+ //构建返回
|
|
|
+ CommAsyncResult<String> result = CommAsyncResult.fail(jobId);
|
|
|
+ try {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
|
|
|
+ , String.format(
|
|
|
+ "开始执行第三方导出;任务id: %s"
|
|
|
+ , jobId
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ //查询点线数据
|
|
|
+ List<GisSurveyLayerApply> points = gisSurveyLayerApplyService.findAllByJobIdAndKind(jobId, GisMetadataDefine.TYPE_KINE.POINT);
|
|
|
+ List<GisSurveyLayerApply> lines = gisSurveyLayerApplyService.findAllByJobIdAndKind(jobId, GisMetadataDefine.TYPE_KINE.LINE);
|
|
|
+ //获取图层key
|
|
|
+ List<String> pointLayer = getLayerKeys(points);
|
|
|
+ List<String> lineLayer = getLayerKeys(lines);
|
|
|
+ //查询点图层模版
|
|
|
+ List<GisMetadataLayerTemplate> pointLayerTemplates = layerTemplateService.findByKeyIn(pointLayer);
|
|
|
+ //查询线图层模版
|
|
|
+ List<GisMetadataLayerTemplate> lineLayerTemplates = layerTemplateService.findByKeyIn(lineLayer);
|
|
|
+
|
|
|
+ //构建excel头
|
|
|
+ Map<String, Map<String, String>> excelHeader = buildExcelHeader(pointLayerTemplates, lineLayerTemplates);
|
|
|
+
|
|
|
+ //构建excel数据
|
|
|
+ Map<String, List<Map<String, Object>>> excelData = buildExcelData(points, lines);
|
|
|
+ //构建shape数据
|
|
|
+ Map<String, List<Geometry>> shapeData = buildShapeData(points, lines);
|
|
|
+
|
|
|
+ //创建临时文件夹
|
|
|
+ Path directory = Files.createTempDirectory(tempFileProperties.getResourcePath(), "third-export-");
|
|
|
+
|
|
|
+ //导出excel
|
|
|
+ File excelFile = new File(directory + "/" + "output.xlsx");
|
|
|
+ ExcelUtils.writeFile(excelHeader, excelData, Files.newOutputStream(excelFile.toPath()), ExcelEnum.XLSX);
|
|
|
+ //导出shape
|
|
|
+ ShapeUtils.writeShape(shapeData, directory);
|
|
|
+
|
|
|
+ //压缩文件夹
|
|
|
+ Path zipPath = CompressorUtils.archivalZip(directory);
|
|
|
+ if (!Files.exists(zipPath)) {
|
|
|
+ //打印报错信息
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
|
|
|
+ , String.format("第三方导出zip压缩失败 任务id:%s", jobId)
|
|
|
+ );
|
|
|
+ return new AsyncResult<>(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ //导出
|
|
|
+ result.setStatus(CommAsyncStatusEnum.SUCCESS.getCode());
|
|
|
+ result.setCompleteTime(LocalDateTime.now());
|
|
|
+ result.setData(zipPath.getFileName().toString());
|
|
|
+
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
|
|
|
+ , String.format(
|
|
|
+ "结束第三方导出;任务id: %s, 用时(毫秒):%d"
|
|
|
+ , jobId
|
|
|
+ , Duration.between(result.getRequestTime(), result.getCompleteTime()).toMillis()
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ return new AsyncResult<>(result);
|
|
|
+ } catch (InterruptedException | IOException | ArchiveException e) {
|
|
|
+ //打印报错信息
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
|
|
|
+ , String.format("第三方导出异常 任务id:%s error:%s", jobId, e)
|
|
|
+ );
|
|
|
+ return new AsyncResult<>(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图层key
|
|
|
+ *
|
|
|
+ * @param datas 数据
|
|
|
+ * @return 图层key集合
|
|
|
+ */
|
|
|
+ private List<String> getLayerKeys(List<GisSurveyLayerApply> datas) {
|
|
|
+ //去重图层
|
|
|
+ return datas.parallelStream()
|
|
|
+ .map(GisSurveyLayerApply::getLayer)
|
|
|
+ .filter(StringUtils::isNotBlank)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建excel头
|
|
|
+ *
|
|
|
+ * @param pointLayerTemplates 点图层模版
|
|
|
+ * @param lineLayerTemplates 线图层模版
|
|
|
+ * @return excel数据
|
|
|
+ */
|
|
|
+ private Map<String, Map<String, String>> buildExcelHeader(List<GisMetadataLayerTemplate> pointLayerTemplates
|
|
|
+ , List<GisMetadataLayerTemplate> lineLayerTemplates) {
|
|
|
+ //点excel头
|
|
|
+ Map<String, String> pointExcelHeader = pointLayerTemplates.stream()
|
|
|
+ .flatMap(it -> it.getPropertyTemplates().stream())
|
|
|
+ .sorted(Comparator.comparing(GisMetadataPropertyTemplate::getOrdering))
|
|
|
+ .collect(Collectors.toMap(GisMetadataPropertyTemplate::getKey, GisMetadataPropertyTemplate::getName
|
|
|
+ , (it1, it2) -> it1, LinkedHashMap::new));
|
|
|
+ //线excel头
|
|
|
+ Map<String, String> lineExcelHeader = lineLayerTemplates.stream()
|
|
|
+ .flatMap(it -> it.getPropertyTemplates().stream())
|
|
|
+ .collect(Collectors.toMap(GisMetadataPropertyTemplate::getKey, GisMetadataPropertyTemplate::getName
|
|
|
+ , (it1, it2) -> it1, LinkedHashMap::new));
|
|
|
+ //excel头
|
|
|
+ Map<String, Map<String, String>> excelHeader = new HashMap<>();
|
|
|
+ excelHeader.put(GisMetadataDefine.TYPE_KINE.POINT, pointExcelHeader);
|
|
|
+ excelHeader.put(GisMetadataDefine.TYPE_KINE.LINE, lineExcelHeader);
|
|
|
+
|
|
|
+ return excelHeader;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建excel数据
|
|
|
+ *
|
|
|
+ * @param points 点元素
|
|
|
+ * @param lines 线元素
|
|
|
+ * @return excel数据
|
|
|
+ */
|
|
|
+ private Map<String, List<Map<String, Object>>> buildExcelData(List<GisSurveyLayerApply> points, List<GisSurveyLayerApply> lines) {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, "开始构建excel数据 ======>");
|
|
|
+ long begin = System.currentTimeMillis();
|
|
|
+
|
|
|
+ //点excel数据
|
|
|
+ List<Map<String, Object>> pointExcelData = points.parallelStream().map(it ->
|
|
|
+ it.getPropertyValues().stream()
|
|
|
+ .filter(it1 -> !StringUtils.isAnyBlank(it1.getProperty(), it1.getValue()))
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ GisSurveyPropertyValue::getProperty,
|
|
|
+ gisSurveyPropertyValue -> (Object) gisSurveyPropertyValue.getValue()
|
|
|
+ ))
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+ //线excel数据
|
|
|
+ List<Map<String, Object>> lineExcelData = lines.parallelStream().map(it ->
|
|
|
+ it.getPropertyValues().stream()
|
|
|
+ .filter(it1 -> !StringUtils.isAnyBlank(it1.getProperty(), it1.getValue()))
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ GisSurveyPropertyValue::getProperty,
|
|
|
+ gisSurveyPropertyValue -> (Object) gisSurveyPropertyValue.getValue()
|
|
|
+ ))
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+
|
|
|
+ //excel数据
|
|
|
+ Map<String, List<Map<String, Object>>> excelData = new HashMap<>();
|
|
|
+ excelData.put(GisMetadataDefine.TYPE_KINE.POINT, pointExcelData);
|
|
|
+ excelData.put(GisMetadataDefine.TYPE_KINE.LINE, lineExcelData);
|
|
|
+
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
|
|
|
+ , String.format(
|
|
|
+ "结束构建excel数据,用时(毫秒):%d"
|
|
|
+ , (end - begin)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return excelData;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建shape数据
|
|
|
+ *
|
|
|
+ * @param points 点元素
|
|
|
+ * @param lines 线元素
|
|
|
+ * @return excel数据
|
|
|
+ */
|
|
|
+ private Map<String, List<Geometry>> buildShapeData(List<GisSurveyLayerApply> points, List<GisSurveyLayerApply> lines) {
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, "开始构建shape数据 ======>");
|
|
|
+ long begin = System.currentTimeMillis();
|
|
|
+
|
|
|
+ //点excel数据
|
|
|
+ List<Geometry> pointExcelData = points.parallelStream()
|
|
|
+ .map(GisSurveyLayerApply::getGis)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ //线excel数据
|
|
|
+ List<Geometry> lineExcelData = lines.parallelStream()
|
|
|
+ .map(GisSurveyLayerApply::getGis)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ //excel数据
|
|
|
+ Map<String, List<Geometry>> shapeData = new HashMap<>();
|
|
|
+ shapeData.put(GisMetadataDefine.TYPE_KINE.POINT, pointExcelData);
|
|
|
+ shapeData.put(GisMetadataDefine.TYPE_KINE.LINE, lineExcelData);
|
|
|
+
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
|
|
|
+ , String.format(
|
|
|
+ "结束构建shape数据,用时(毫秒):%d"
|
|
|
+ , (end - begin)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return shapeData;
|
|
|
+ }
|
|
|
+}
|