Browse Source

4/13 patentExport

xiexiang 1 year ago
parent
commit
946fcf72e8

+ 5 - 10
src/main/java/com/example/xiaoshiweixinback/controller/PatentController.java

@@ -144,16 +144,11 @@ public class PatentController {
     //--------------------------导出专利-------------------------------
     @PostMapping(value = "/exportPatent")
     @Operation(summary = "导出专利数据")
-    public ResponseEntity<InputStreamResource> exportPatent(@RequestBody ExportPatentDTO exportPatentDTO) throws IOException {
-        byte[] fileData = patentExportService.exportPatent(exportPatentDTO.getPatentNos());
-        //保存生成excel的地址
-        String fileName = "导出专利.xlsx";
-        //文件原始名中的中文字符可能会乱码,对文件名进行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)));
+    public Response exportPatent(@RequestBody ExportPatentDTO exportPatentDTO) throws IOException {
+        String fileGuid = patentExportService.exportPatent(exportPatentDTO.getPatentNos());
+        Records records = new Records();
+        records.setData(fileGuid);
+        return Response.success(records);
     }
 
 }

+ 34 - 29
src/main/java/com/example/xiaoshiweixinback/service/exportPatent/PatentExportService.java

@@ -51,7 +51,7 @@ public class PatentExportService {
      * @throws IOException
      */
     @Async
-    public byte[] exportPatent(List<String> patentNos) throws IOException {
+    public String exportPatent(List<String> patentNos) throws IOException {
         try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
             String patentExportJson = ExcuteConfigUtils.excuteConfigJson();
             //selected字符串转PatentExportVO集合
@@ -93,39 +93,38 @@ public class PatentExportService {
             //遍历专利号数量
             for (int i = 0; i < patentNos.size(); i++) {
                 Map<String, Object> map = new LinkedHashMap<>();
-
                 //获取专利信息
                 PatentColumnDTO patent = esPatentService.selectPatentByAppNo(patentNos.get(i));
-                if (patent == null) {
-                    defaultNum++;
-                    continue;
-                }
-
-                //遍历字段 配置map集合
-                for (int j = 0; j < columns.size(); j++) {
-                    String column = columns.get(j);
-                    //解析出的patentExportVOS根据value匹配column字段,拿出对应字段的配置
-                    PatentExportVO patentExportVO = patentExportVOS.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(new PatentExportVO());
-                    //使用反射获取属性值  (例如:title->patent.getTitile())
-                    Object value = GenerateObjectUtil.getPropertyValue(patent, patentExportVO.getValue());
-                    if (value != null) {
-                        //解析专利配置文件
-                        String json = CommonService.readJsonFile("patent.json");
-                        List<PatentConfigVO> patentConfigVOS = JSON.parseArray(json, PatentConfigVO.class);
-                        PatentConfigVO patentConfigVO = patentConfigVOS.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(null);
-                        String exportClass = patentConfigVO.getExportClass();
-                        GetValueImp getValueImp = patentExportFactory.getClass(exportClass);
-                        if (getValueImp != null) {
-                            String reValue = getValueImp.getValue(value);
-                            map.put(patentExportVO.getName(), reValue);
+                if (patent != null) {
+                    //遍历字段 配置map集合
+                    for (int j = 0; j < columns.size(); j++) {
+                        String column = columns.get(j);
+                        //解析出的patentExportVOS根据value匹配column字段,拿出对应字段的配置
+                        PatentExportVO patentExportVO = patentExportVOS.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(new PatentExportVO());
+                        //使用反射获取属性值  (例如:title->patent.getTitile())
+                        Object value = GenerateObjectUtil.getPropertyValue(patent, patentExportVO.getValue());
+                        if (value != null) {
+                            //解析专利配置文件
+                            String json = CommonService.readJsonFile("patent.json");
+                            List<PatentConfigVO> patentConfigVOS = JSON.parseArray(json, PatentConfigVO.class);
+                            PatentConfigVO patentConfigVO = patentConfigVOS.stream().filter(item -> item.getValue().equals(column)).findFirst().orElse(null);
+                            if (patentConfigVO != null) {
+                                String exportClass = patentConfigVO.getExportClass();
+                                if (exportClass != null) {
+                                    GetValueImp getValueImp = patentExportFactory.getClass(exportClass);
+                                    if (getValueImp != null) {
+                                        String reValue = getValueImp.getValue(value);
+                                        map.put(patentExportVO.getName(), reValue);
+                                    }
+                                }
+                            }
                         }
                     }
+                } else {
+                    map.put("专利号", patentNos.get(i));
                 }
-
                 HSSFRow row = sheet.createRow(i + 1);//新建一普通行
                 row.setHeight((short) 800);
-
-
                 //填入数据
                 for (String key : map.keySet()) {
                     int index = headers.indexOf(key);
@@ -137,14 +136,20 @@ public class PatentExportService {
                         cell.setCellStyle(commonCellStyle);
                         if (StringUtils.isNotNull(map.get(key))) {
                             cell.setCellValue(String.valueOf(map.get(key)));
+                        } else {
+                            cell.setCellValue("");
                         }
                     }
                 }
             }
             hssfWorkbook.write(out);
-            return out.toByteArray();
+            String fileGuid = "";
+            if (out.toByteArray() != null && out.toByteArray().length != 0) {
+                fileGuid = parseByteToFileUtils.uploadFile(out.toByteArray(), 1);
+            }
+            return fileGuid;
         } catch (FileNotFoundException e) {
             throw new FileNotFoundException();
         }
     }
-}
+}