123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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.FileTypeEnum;
- import com.shkpr.service.alambizplugin.constants.GisMetadataDefine;
- import com.shkpr.service.alambizplugin.constants.GisSurveyExcelDefine;
- 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.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
- * @param fileType 导出文件类型
- * @return 导出结果
- */
- @Async
- public ListenableFuture<CommAsyncResult<String>> thirdExportTask(String jobId, FileTypeEnum fileType) {
- //构建返回
- 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);
- //根据文件类型导出
- Path resultPath = null;
- if (fileType == FileTypeEnum.EXCEL)
- resultPath = exportExcel(points, lines);
- if (fileType == FileTypeEnum.SHAPE_FILE)
- resultPath = exportShape(points, lines);
- //导出文件判断
- if (resultPath == null || !Files.exists(resultPath)) {
- //打印报错信息
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
- , String.format("第三方导出文件写入失败, 任务id:%s", jobId)
- );
- return new AsyncResult<>(result);
- }
- //导出完成
- result.setStatus(CommAsyncStatusEnum.SUCCESS.getCode());
- result.setCompleteTime(LocalDateTime.now());
- result.setData(resultPath.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);
- }
- }
- /**
- * 导出excel
- *
- * @param points 点
- * @param lines 线
- * @return excel路径
- * @throws IOException io异常
- */
- private Path exportExcel(List<GisSurveyLayerApply> points, List<GisSurveyLayerApply> lines) throws IOException {
- //获取图层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, pointLayerTemplates, lineLayerTemplates);
- //导出excel
- Path excelPath = Files.createTempFile(tempFileProperties.getResourcePath(), "third-export-", ".xlsx");
- ExcelUtils.writeFile(excelHeader, excelData, Files.newOutputStream(excelPath), ExcelEnum.XLSX);
- return excelPath;
- }
- /**
- * 导出shape
- *
- * @param points 点
- * @param lines 线
- * @return shape路径
- * @throws IOException io异常
- * @throws ArchiveException 压缩异常
- */
- private Path exportShape(List<GisSurveyLayerApply> points, List<GisSurveyLayerApply> lines) throws IOException, ArchiveException {
- //构建shape数据
- Map<String, List<Geometry>> shapeData = buildShapeData(points, lines);
- //创建临时文件夹
- Path directory = Files.createTempDirectory(tempFileProperties.getResourcePath(), "third-export-");
- //导出shape
- ShapeUtils.writeShape(shapeData, directory);
- //压缩文件夹
- return CompressorUtils.archivalZip(directory);
- }
- /**
- * 获取图层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 = new LinkedHashMap<>();
- //线excel表头
- Map<String, String> lineExcelHeader = new LinkedHashMap<>();
- //点模版表头
- Map<String, String> pointTemplateHeader = pointLayerTemplates.stream()
- .flatMap(it -> it.getPropertyTemplates().stream())
- .sorted(Comparator.comparing(GisMetadataPropertyTemplate::getOrdering))
- .collect(Collectors.toMap(GisMetadataPropertyTemplate::getKey, GisMetadataPropertyTemplate::getName
- , (it1, it2) -> it1, LinkedHashMap::new));
- //线模版表头
- Map<String, String> lineTemplateHeader = lineLayerTemplates.stream()
- .flatMap(it -> it.getPropertyTemplates().stream())
- .collect(Collectors.toMap(GisMetadataPropertyTemplate::getKey, GisMetadataPropertyTemplate::getName
- , (it1, it2) -> it1, LinkedHashMap::new));
- //加入图层表头
- pointExcelHeader.put(GisSurveyExcelDefine.TEMPLATE.LAYER, GisSurveyExcelDefine.FILE.POINT_LAYER);
- lineExcelHeader.put(GisSurveyExcelDefine.TEMPLATE.LAYER, GisSurveyExcelDefine.FILE.LINE_LAYER);
- //加入模版表头
- pointExcelHeader.putAll(pointTemplateHeader);
- lineExcelHeader.putAll(lineTemplateHeader);
- //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 线元素
- * @param pointLayerTemplates 点图层模版
- * @param lineLayerTemplates 线图层模版
- * @return excel数据
- */
- private Map<String, List<Map<String, Object>>> buildExcelData(List<GisSurveyLayerApply> points, List<GisSurveyLayerApply> lines
- , List<GisMetadataLayerTemplate> pointLayerTemplates, List<GisMetadataLayerTemplate> lineLayerTemplates) {
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, "开始构建excel数据 ======>");
- long begin = System.currentTimeMillis();
- //excel数据
- Map<String, List<Map<String, Object>>> excelData = new HashMap<>();
- excelData.put(GisMetadataDefine.TYPE_KINE.POINT, buildExcelSheetData(points, pointLayerTemplates));
- excelData.put(GisMetadataDefine.TYPE_KINE.LINE, buildExcelSheetData(lines, lineLayerTemplates));
- long end = System.currentTimeMillis();
- LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
- , String.format(
- "结束构建excel数据,用时(毫秒):%d"
- , (end - begin)
- )
- );
- return excelData;
- }
- /**
- * 构建excel页数据
- *
- * @param layerApplies 采集元素集合
- * @param layerTemplates 图层模版
- * @return excel数据
- */
- private List<Map<String, Object>> buildExcelSheetData(List<GisSurveyLayerApply> layerApplies, List<GisMetadataLayerTemplate> layerTemplates) {
- return layerApplies.parallelStream()
- .map(it -> {
- //处理模版数据
- Map<String, Object> rowData = it.getPropertyValues().stream()
- .filter(it1 -> !StringUtils.isAnyBlank(it1.getProperty(), it1.getValue()))
- .collect(Collectors.toMap(GisSurveyPropertyValue::getProperty, GisSurveyPropertyValue::getValue));
- //处理图层数据
- if (StringUtils.isNotBlank(it.getLayer())) {
- //获取模版
- GisMetadataLayerTemplate gisMetadataLayerTemplate = layerTemplates.stream()
- .filter(it1 -> Objects.equals(it1.getKey(), it.getLayer()))
- .findFirst().orElse(null);
- //存入图层
- if (gisMetadataLayerTemplate != null)
- rowData.put(GisSurveyExcelDefine.TEMPLATE.LAYER, gisMetadataLayerTemplate.getName());
- }
- return rowData;
- }).collect(Collectors.toList());
- }
- /**
- * 构建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;
- }
- }
|