Browse Source

第三方导出修改为按文件类型导出

欧阳劲驰 1 month ago
parent
commit
19c69e785b

+ 9 - 6
src/main/java/com/shkpr/service/alambizplugin/bizservice/GisSurveyThirdExportBizService.java

@@ -4,6 +4,7 @@ import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
 import com.shkpr.service.alambizplugin.components.GisSurveyThirdExporter;
 import com.shkpr.service.alambizplugin.constants.CommAsyncStatusEnum;
+import com.shkpr.service.alambizplugin.constants.FileTypeEnum;
 import com.shkpr.service.alambizplugin.constants.GisSurveConvertStatusEnum;
 import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
 import com.shkpr.service.alambizplugin.dto.CommAsyncResult;
@@ -50,16 +51,17 @@ public class GisSurveyThirdExportBizService {
     /**
      * 第三方导出
      *
-     * @param jobId 任务id
+     * @param jobId    任务id
+     * @param fileType 导出文件类型
      * @return 转换结果
      */
-    public CommAsyncResult<String> thirdExport(String jobId) {
+    public CommAsyncResult<String> thirdExport(String jobId, FileTypeEnum fileType) {
         //如已有结果,则直接返回
         CommAsyncResult<String> result = getResult(jobId);
         if (result != null) return result;
 
         //启动任务
-        startTask(jobId);
+        startTask(jobId, fileType);
         //返回进行中
         return CommAsyncResult.inProgress(jobId, LocalDateTime.now());
 
@@ -104,15 +106,16 @@ public class GisSurveyThirdExportBizService {
     /**
      * 启动任务
      *
-     * @param jobId 任务id
+     * @param jobId    任务id
+     * @param fileType 导出文件类型
      */
-    private void startTask(String jobId) {
+    private void startTask(String jobId, FileTypeEnum fileType) {
         //获取已存在的任务
         ListenableFuture<CommAsyncResult<String>> previousFuture = TASK_CACHE.get(jobId);
         //已结束判断,删除缓存
         if (previousFuture != null && (previousFuture.isDone() || previousFuture.isCancelled())) removeCache(jobId);
         //执行异步任务
-        ListenableFuture<CommAsyncResult<String>> checkFuture = thirdExporter.thirdExportTask(jobId);
+        ListenableFuture<CommAsyncResult<String>> checkFuture = thirdExporter.thirdExportTask(jobId, fileType);
         //缓存任务句柄
         TASK_CACHE.put(jobId, checkFuture);
         //缓存时间

+ 69 - 34
src/main/java/com/shkpr/service/alambizplugin/components/GisSurveyThirdExporter.java

@@ -8,6 +8,7 @@ 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;
@@ -26,7 +27,6 @@ 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;
@@ -70,11 +70,12 @@ public class GisSurveyThirdExporter {
     /**
      * 第三方导出任务
      *
-     * @param jobId 任务id
+     * @param jobId    任务id
+     * @param fileType 导出文件类型
      * @return 导出结果
      */
     @Async
-    public ListenableFuture<CommAsyncResult<String>> thirdExportTask(String jobId) {
+    public ListenableFuture<CommAsyncResult<String>> thirdExportTask(String jobId, FileTypeEnum fileType) {
         //构建返回
         CommAsyncResult<String> result = CommAsyncResult.fail(jobId);
         try {
@@ -88,45 +89,27 @@ public class GisSurveyThirdExporter {
             //查询点线数据
             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, pointLayerTemplates, lineLayerTemplates);
-            //构建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)) {
+
+            //根据文件类型导出
+            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("第三方导出zip压缩失败 任务id:%s", jobId)
+                        , String.format("第三方导出文件写入失败, 任务id:%s", jobId)
                 );
                 return new AsyncResult<>(result);
             }
 
-            //导出
+            //导出完成
             result.setStatus(CommAsyncStatusEnum.SUCCESS.getCode());
             result.setCompleteTime(LocalDateTime.now());
-            result.setData(zipPath.getFileName().toString());
+            result.setData(resultPath.getFileName().toString());
 
             LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
                     , String.format(
@@ -147,6 +130,58 @@ public class GisSurveyThirdExporter {
     }
 
     /**
+     * 导出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 数据

+ 38 - 0
src/main/java/com/shkpr/service/alambizplugin/constants/FileTypeEnum.java

@@ -0,0 +1,38 @@
+package com.shkpr.service.alambizplugin.constants;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * 文件类型枚举
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@AllArgsConstructor
+@Getter
+public enum FileTypeEnum {
+    EXCEL("excel"),
+    SHAPE_FILE("shapefile"),
+    ;
+
+    /**
+     * 文件类型命
+     */
+    private final String name;
+
+    /**
+     * 根据名称获取枚举
+     *
+     * @param name 名称
+     * @return 文件类型枚举
+     */
+    public static FileTypeEnum getFileType(String name) {
+        if (StringUtils.isBlank(name)) return null;
+        //匹配名称
+        for (FileTypeEnum type : FileTypeEnum.values())
+            if (type.getName().equalsIgnoreCase(name)) return type;
+        return null;
+    }
+}

+ 10 - 3
src/main/java/com/shkpr/service/alambizplugin/controller/ApiGisSurveyController.java

@@ -12,6 +12,7 @@ import com.shkpr.service.alambizplugin.commtools.CommTool;
 import com.shkpr.service.alambizplugin.constants.ApiURI;
 import com.shkpr.service.alambizplugin.constants.CadEnum;
 import com.shkpr.service.alambizplugin.constants.CommAsyncStatusEnum;
+import com.shkpr.service.alambizplugin.constants.FileTypeEnum;
 import com.shkpr.service.alambizplugin.constants.GisSurveConvertStatusEnum;
 import com.shkpr.service.alambizplugin.constants.GisSurveyCheckStatusEnum;
 import com.shkpr.service.alambizplugin.constants.GisSurveyImportStatusEnum;
@@ -597,18 +598,20 @@ public class ApiGisSurveyController {
      * @param strClientType 客户端类型
      * @param strUserAgent  用户信息
      * @param jobId         任务id
+     * @param fileType      导出文件类型
      * @return cad转换结果
      */
     @GetMapping(value = ApiURI.URI_XXX_THIRD_EXPORT)
     public ResponseRes<CommAsyncResult<String>> thirdExport(HttpServletRequest request
             , @RequestHeader(value = ApiURI.HEADER_CLIENT_TYPE, required = false) String strClientType
             , @RequestHeader(value = ApiURI.HEADER_USER_AGENT, required = false) String strUserAgent
-            , @RequestParam(value = "jobId", required = false) String jobId) throws SelfException {
+            , @RequestParam(value = "jobId", required = false) String jobId
+            , @RequestParam(value = "fileType", required = false, defaultValue = "excel") String fileType) throws SelfException {
         //入参校验
         final String URI_PATH = request.getRequestURI();
         final String strPlatform = CommTool.getPlatformByAgent(strClientType, strUserAgent);
         final String strUserId = (String) request.getAttribute(TokenAuthenticationService.HEADER_USERID);
-        if (StringUtils.isBlank(jobId) || StringUtils.length(jobId) > 64) {
+        if (StringUtils.isAnyBlank(jobId) || StringUtils.length(jobId) > 64 || StringUtils.length(fileType) > 64) {
             throw new SelfException(ResponseCode.STATUS_ERROR_PARAM_FORMAT.toStrCode()
                     , String.format(ApiURI.EXCEPTION_FORMAT
                     , strPlatform
@@ -631,8 +634,12 @@ public class ApiGisSurveyController {
         resResult.setRescode(ResponseCode.RESULT_ASYNC_TASK_FAILED.toStrCode());
         resResult.setResmsg(ResponseCode.RESULT_ASYNC_TASK_FAILED.toStrMsg());
 
+        //导出类型枚举
+        FileTypeEnum fileTypeEnum = FileTypeEnum.getFileType(fileType);
+        if (fileTypeEnum == null) fileTypeEnum = FileTypeEnum.EXCEL;
+
         //执行第三方导出
-        CommAsyncResult<String> result = thirdExportBizService.thirdExport(jobId);
+        CommAsyncResult<String> result = thirdExportBizService.thirdExport(jobId, fileTypeEnum);
 
         //执行成功
         if (result != null && !Objects.equals(result.getStatus(), CommAsyncStatusEnum.FAIL.getCode())) {