xiexiang 1 سال پیش
والد
کامیت
44f04cbb18

+ 5 - 18
src/main/java/cn/cslg/pas/controller/PatentController.java

@@ -30,12 +30,14 @@ import cn.cslg.pas.service.business.MergePersonService;
 import cn.cslg.pas.service.business.es.EsCountService;
 import cn.cslg.pas.service.business.es.EsPatentService;
 import cn.cslg.pas.service.business.es.EsService;
+import cn.cslg.pas.service.common.FileManagerService;
 import cn.cslg.pas.service.common.PatentStarApiService;
 import cn.hutool.core.util.IdUtil;
 import io.swagger.v3.oas.annotations.Operation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.InputStreamResource;
 import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
@@ -74,6 +76,8 @@ public class PatentController {
     private PatentExportService patentExportService;
     @Autowired
     private PDFExportFirstPageService pdfExportFirstPageService;
+    @Autowired
+    private FileManagerService fileManagerService;
 
     @Operation(summary = "查询专利")
     @PostMapping("/queryPatent")
@@ -175,25 +179,8 @@ public class PatentController {
         return Response.success(id);
     }
 
-//    @GetMapping("/pdf")
-//    @Operation(summary = "导出专利")
-//    public ResponseEntity<InputStreamResource> pdf(Integer projectId) throws IOException {
-//        byte[] fileData = pdfExportFirstPageService.mergeAndExportPDFFirstPage(projectId);
-//        //保存生成excel的地址
-//        String fileName = "PDF.pdf";
-////        String fileName = IdUtil.simpleUUID() + ".xls";
-////        String directoryName = fileUtils.createDirectory();
-////        String savePath = fileUtils.getSavePath(directoryName);
-//        //文件原始名中的中文字符可能会乱码,对文件名进行URL编码
-//        String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
-//        return ResponseEntity.ok().contentLength(fileData.length)
-//                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFileName + "\"")
-//                .contentType(MediaType.parseMediaType("application/octet-stream"))
-//                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
-//    }
-
     @GetMapping("/exportPDFFirstPage")
-    @Operation(summary = "导出专利")
+    @Operation(summary = "导出专利PDF")
     public ResponseEntity<InputStreamResource> exportPDFFirstPage(Integer projectId) throws IOException {
         byte[] fileData = pdfExportFirstPageService.mergeAndExportPDFFirstPage(projectId);
         //保存生成excel的地址

+ 24 - 4
src/main/java/cn/cslg/pas/service/business/PDFExportFirstPageService.java

@@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -49,15 +50,28 @@ public class PDFExportFirstPageService {
         try {
             patentDTO = esService.esSearch(queryRequest);
         } catch (Exception e) {
-            throw new XiaoShiException("错误");
+            throw new XiaoShiException("检索专利错误");
         }
         List<PatentColumnDTO> patents = patentDTO.getPatents();
         byte[][] pdfDocuments = this.getPDFByteByAppNo(patents);
-        byte[] pdfData = this.mergeFirstPage(pdfDocuments);
+        byte[] pdfData = new byte[0];
+        // 判断 byte[][] 数组是否为空
+        if (!this.isByteEmpty(pdfDocuments)) {
+            pdfData = this.mergeFirstPage(pdfDocuments);
+        }
         return pdfData;
     }
 
     /**
+     * 判断文件数组是否为空
+     * @param pdfDocuments
+     * @return
+     */
+    private boolean isByteEmpty(byte[][] pdfDocuments) {
+        return pdfDocuments == null || pdfDocuments.length == 0 || Arrays.stream(pdfDocuments).allMatch(array -> array == null || array.length == 0);
+    }
+
+    /**
      * 3
      * @param pdfDatas
      * @return
@@ -113,17 +127,23 @@ public class PDFExportFirstPageService {
                 byte[] pdfData = new byte[0];
                 try {
                     pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid);
-                    if (pdfData == null) {
+                    if (pdfData == null  || pdfData.length == 0) {
                         //获取公开专利pdf文档guid
                         Integer type2 = 0;
                         String pdfGuid2 = FormatUtil.getPDFFormat(appNo, type2);
                         pdfData = fileManagerService.downloadSystemFileFromFMS(pdfGuid2);
+                        if (pdfData == null  || pdfData.length == 0) {
+                            continue;
+                        } else {
+                            pdfDatas.add(pdfData);
+                        }
+                    } else {
+                        pdfDatas.add(pdfData);
                     }
                 } catch (IOException e) {
                     e.printStackTrace();
                     continue;//继续处理下一件专利
                 }
-                pdfDatas.add(pdfData);
             }
         }
         byte[][] pdfDataArray = new byte[pdfDatas.size()][];