Browse Source

5.30 将原保存专利入库整体进行拆分完成

chendayu 2 years ago
parent
commit
9b26571e8d

+ 2 - 0
PAS/src/main/java/cn/cslg/pas/common/model/vo/ProjectImportPatentVO.java

@@ -1,9 +1,11 @@
 package cn.cslg.pas.common.model.vo;
 
 import lombok.Data;
+import lombok.experimental.Accessors;
 
 import java.util.List;
 
+@Accessors(chain = true)
 @Data
 public class ProjectImportPatentVO {
     /**

+ 130 - 58
PAS/src/main/java/cn/cslg/pas/common/utils/ReadExcelUtils.java

@@ -1,16 +1,19 @@
 package cn.cslg.pas.common.utils;
 
-import cn.hutool.core.collection.IterUtil;
-import cn.hutool.poi.excel.ExcelUtil;
-import io.swagger.v3.oas.models.security.SecurityScheme;
+import cn.cslg.pas.domain.PatentData;
+import org.apache.poi.hssf.usermodel.HSSFPicture;
+import org.apache.poi.hssf.usermodel.HSSFShape;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ooxml.POIXMLDocumentPart;
+import org.apache.poi.ss.usermodel.PictureData;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.xssf.usermodel.*;
+import org.springframework.stereotype.Service;
 
 import java.io.*;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -19,83 +22,152 @@ import java.util.Map;
  * @Author xiexiang
  * @Date 2023/5/30
  */
+@Service
 public class ReadExcelUtils {
-    //每次读取Excel一行
-    public static Map ReadExcelOneRow(String filePath, Integer row) {
+    /**
+     * 检测Excel文件合法性
+     *
+     * @param filePath 文件路径
+     * @return 返回文件总行数
+     */
+    public static Integer textExcel(String filePath) throws IOException {
+        //判断文件是否存在
+        if (filePath == null || filePath.equals("")) {
+            return -1;
+        }
+        File file = new File(filePath);
+        if (!file.exists()) {
+            return -1;
+        }
+
+        // 检测是否为excel文件
+        if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
+            return -1;
+        }
+
+        InputStream inputStream = new FileInputStream(file);
+        //XSSFWorkbook可以处理xlsx文件
+        Workbook workbook = null;
+        //当文件以.xls结尾时
+        if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
+            workbook = new HSSFWorkbook(inputStream);
+        } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
+            workbook = new XSSFWorkbook(inputStream);
+        }
+        //读取第几个sheet
+        Sheet sheet = workbook.getSheetAt(0);
+        //读取总行数
+        int rows = sheet.getPhysicalNumberOfRows();
+        if (rows <= 0) {
+            return -2;
+        }
+        Row firstRow = sheet.getRow(0);
+        if (!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")) {
+            return -2;
+        }
+        return rows - 1;
+    }
+
+    /**
+     * 获取一行专利的全部数据(专利内容数据 + 摘要附图)
+     *
+     * @param filePath Excel文件路径
+     * @param row      行数
+     * @return 返回装载专利数据(专利内容数据 + 摘要附图)的对象
+     */
+    public static PatentData readExcelOneRow(String filePath, Integer row) {
+        //返回最终结果的对象
+        PatentData patentData = new PatentData();
+        //装载专利数据(除了摘要附图)的map:(key:表头如 "公开(公告)号"  value:表头对应内容如 "CN1307082B")
         Map<Object, Object> map = new HashMap<>();
+        //装载摘要附图的对象
+        PictureData pictureData = null;
+
         File file = new File(filePath);
         try {
             InputStream inputStream = new FileInputStream(file);
-            //XSSFWorkbook可以处理xlsx文件
-            Workbook workbook =null;
+            //POI可以处理Excel文件
+            Workbook workbook = null;
             //当文件以.xls结尾时
-            if(filePath.endsWith(".xls")||filePath.endsWith(".XLS")) {
-                workbook   = new HSSFWorkbook(inputStream);
-            }
-            else if(filePath.endsWith(".xlsx")||filePath.endsWith(".XLSX"))
-            {
-                workbook   = new XSSFWorkbook(inputStream);
+            if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
+                workbook = new HSSFWorkbook(inputStream);
+            } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
+                workbook = new XSSFWorkbook(inputStream);
             }
+
             //读取第几个sheet
             Sheet sheet = workbook.getSheetAt(0);
             //读取总行数
             int rows = sheet.getPhysicalNumberOfRows();
-            if(rows - 1 <= row){
+            if (rows - 1 <= row) {
                 ThrowException.throwXiaoShiException("row超出Excel文档中数量");
             }
+
+            //开始装载专利数据
             Row firstRow = sheet.getRow(0);
             Row needRow = sheet.getRow(row);
             //读取第一行的时候会多读一列
-            Integer firstColumns = firstRow.getLastCellNum() - 0;
-            for(int i = 0; i < firstColumns; i++){
-                map.put(firstRow.getCell(i),needRow.getCell(i));
+            int firstColumns = firstRow.getLastCellNum() - 0;
+            for (int i = 0; i < firstColumns; i++) {
+                map.put(firstRow.getCell(i) + "", needRow.getCell(i) + "");
+            }
+
+            //开始装载专利摘要附图(判断用07还是03的方法获取图片)
+            if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
+                pictureData = getPictures1((HSSFSheet) sheet, row - 1);
+            } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
+                pictureData = getPictures2((XSSFSheet) sheet, row - 1);
             }
-        }  catch (IOException e) {
+            workbook.close();
+
+            //返回结果对象装载结果
+            patentData.setMap(map);
+            patentData.setPictureData(pictureData);
+
+        } catch (IOException e) {
             e.printStackTrace();
         }
-        return map;
+
+        return patentData;
     }
 
-    //检测Excel文件合法性
-    public static Integer textExcel(String filePath) throws IOException {
-        //判断文件是否存在
-        if(filePath == null && filePath == ""){
-        return -1;
-        }
-        File file = new File(filePath);
-        if(!file.exists())
-        {
-            return  -1;
-        }
-        // 检测是否是excel文件
-        if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
-            return -1;
+    /**
+     * 03版本Excel取附图
+     *
+     * @param sheet Excel工作簿
+     * @return 返回附图map
+     */
+    public static PictureData getPictures1(HSSFSheet sheet, Integer row) throws IOException {
+        if (sheet.getDrawingPatriarch() != null) {
+            List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
+            HSSFShape shape = list.get(row);
+            if (shape instanceof HSSFPicture) {
+                HSSFPicture picture = (HSSFPicture) shape;
+                return picture.getPictureData();
+            }
         }
+        return null;
+    }
 
-            InputStream inputStream = new FileInputStream(file);
-            //XSSFWorkbook可以处理xlsx文件
-        Workbook workbook =null;
-        //当文件以.xls结尾时
-        if(filePath.endsWith(".xls")||filePath.endsWith(".XLS")) {
-            workbook   = new HSSFWorkbook(inputStream);
-        }
-        else if(filePath.endsWith(".xlsx")||filePath.endsWith(".XLSX"))
-        {
-            workbook   = new XSSFWorkbook(inputStream);
-        }
-            //读取第几个sheet
-            Sheet sheet = workbook.getSheetAt(0);
-            //读取总行数
-            int rows = sheet.getPhysicalNumberOfRows();
-         if(rows<=0)
-            {
-                return -2;
+    /**
+     * 07版本Excel取附图
+     *
+     * @param sheet Excel工作簿
+     * @return 返回附图map
+     */
+    public static PictureData getPictures2(XSSFSheet sheet, Integer row) throws IOException {
+        List<POIXMLDocumentPart> list = sheet.getRelations();
+        for (POIXMLDocumentPart part : list) {
+            if (part instanceof XSSFDrawing) {
+                XSSFDrawing drawing = (XSSFDrawing) part;
+                List<XSSFShape> shapes = drawing.getShapes();
+                //获取指定行row的图片形状
+                XSSFShape shape = shapes.get(row);
+                XSSFPicture picture = (XSSFPicture) shape;
+                return picture.getPictureData();
             }
-            Row firstRow = sheet.getRow(0);
-         if(!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")){
-             return -2;
-         }
-        return  rows-1;
+        }
+        return null;
     }
 
 }

+ 25 - 0
PAS/src/main/java/cn/cslg/pas/domain/PatentData.java

@@ -0,0 +1,25 @@
+package cn.cslg.pas.domain;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+import org.apache.poi.ss.usermodel.PictureData;
+
+import java.util.Map;
+
+/**
+ * @Author chenyu
+ * @Date 2023/5/31
+ */
+@Accessors(chain = true)
+@Data
+public class PatentData {
+    /**
+     * 专利内容数据(除了摘要附图, key:表头如 "公开(公告)号"  value:表头对应内容如 "CN1307082B")
+     */
+    private Map<Object, Object> map;
+    /**
+     * 专利摘要附图
+     */
+    private PictureData pictureData;
+
+}

+ 1 - 1
PAS/src/main/java/cn/cslg/pas/domain/Project.java

@@ -24,7 +24,7 @@ import java.util.List;
 @TableName("os_thematic")
 public class Project extends BaseEntity<Project> {
     /**
-     * 标题
+     * 专题库名称
      */
     private String name;
 

+ 1 - 0
PAS/src/main/java/cn/cslg/pas/service/UploadPatentBatchService.java

@@ -94,6 +94,7 @@ public class UploadPatentBatchService {
 
                 //将装配对象中的数据保存到数据库
                 dataToDB(params, uploadParamsVO, projectImportPatentVO);
+
                 //通过WebSocket 在每一次循环结束后 向前端发送完成进度
                 WebSocketServer.sendInfo(Response.websocket(new TaskWebSocketDTO()
                         .setTaskId(params.getTaskId())

+ 6 - 19
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/ThreadSafeQueue.java

@@ -1,17 +1,7 @@
 package cn.cslg.pas.service.upLoadPatent;
 
-import cn.cslg.pas.common.model.dto.UploadFileDTO;
-import cn.cslg.pas.common.model.vo.TaskParams;
-import cn.cslg.pas.common.model.vo.UploadParamsVO;
-import cn.cslg.pas.common.utils.FileUtils;
-import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
-import cn.cslg.pas.service.TaskService;
-import cn.hutool.core.collection.IterUtil;
-import cn.hutool.poi.excel.ExcelUtil;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.*;
-import java.util.concurrent.TimeUnit;
+import java.util.LinkedList;
+import java.util.Queue;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -39,11 +29,9 @@ public class ThreadSafeQueue<T> {
             while (queue.size() == maxSize) {
                 notFull.await();
             }
-
-            UploadParamsVO up =(UploadParamsVO)item;
-System.out.println("进入队列"+up.getFirstAddress());
+            System.out.println("一个数据准备进入队列");
             queue.add(item);
-            TimeUnit.MILLISECONDS.sleep(1000);
+            System.out.println(queue.size());
             notEmpty.signalAll();
         } finally {
             lock.unlock();
@@ -56,10 +44,9 @@ System.out.println("进入队列"+up.getFirstAddress());
             while (queue.isEmpty()) {
                 notEmpty.await();
             }
-            UploadParamsVO up =(UploadParamsVO)queue.element();
-            System.out.println("出队列"+up.getFirstAddress());
+            System.out.println("一个数据准备出队列");
             T item = queue.remove();
-            TimeUnit.MILLISECONDS.sleep(2000);
+            System.out.println(queue.size());
             notFull.signalAll();
             return item;
         } finally {

+ 2 - 2
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/UploadAssoProPatService.java

@@ -7,7 +7,7 @@ import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 
 /**
- * 保存与专利关联信息(如自定义字段、文件夹、标签等关联信息)并入库
+ * 保存专利关联数据(如自定义字段、文件夹、标签等)入库
  *
  * @Author chenyu
  * @Date 2023/5/30
@@ -18,7 +18,7 @@ public class UploadAssoProPatService {
     private final UploadPatentBatchService uploadPatentBatchService;
 
     /**
-     * 保存与专利关联信息(如自定义字段、文件夹、标签等关联信息)入库
+     * 专利相关联数据(如自定义字段、文件夹、标签等关联信息)入库
      *
      * @param uploadParamsVO        专利内容实体类对象
      * @param projectImportPatentVO 专题库关联专利信息(专题库id、自定义字段、文件夹等数据)

+ 76 - 2
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/UploadPatentService.java

@@ -1,19 +1,34 @@
 package cn.cslg.pas.service.upLoadPatent;
 
 import cn.cslg.pas.common.core.base.Constants;
+import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
 import cn.cslg.pas.common.model.vo.UploadParamsVO;
+import cn.cslg.pas.common.model.vo.UploadSettingVO;
+import cn.cslg.pas.common.utils.DateUtils;
+import cn.cslg.pas.common.utils.FileUtils;
+import cn.cslg.pas.common.utils.ReadExcelUtils;
+import cn.cslg.pas.common.utils.UploadPatentBatchUtil;
+import cn.cslg.pas.domain.PatentData;
 import cn.cslg.pas.domain.SystemDict;
 import cn.cslg.pas.service.PatentAgencyService;
+import cn.cslg.pas.service.PatentImageService;
 import cn.cslg.pas.service.SystemDictService;
 import cn.cslg.pas.service.UploadPatentBatchService;
+import cn.hutool.core.util.IdUtil;
 import lombok.RequiredArgsConstructor;
+import org.apache.poi.ss.usermodel.PictureData;
 import org.springframework.stereotype.Service;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
- * 保存专利入库
+ * 保存专利入库
  *
  * @Author chenyu
  * @Date 2023/5/30
@@ -24,15 +39,59 @@ public class UploadPatentService {
     private final SystemDictService systemDictService;
     private final PatentAgencyService patentAgencyService;
     private final UploadPatentBatchService uploadPatentBatchService;
+    private final PatentImageService patentImageService;
+
+    public static void main(String[] args) throws IOException {
+        //获得uploadSetting.json配置文件的JSON串
+        String getSettingJson = new FileUtils().analysisJsonFile();
+        //查找并装载本次导入的专利需要的数据源(如智慧芽)的对应配置(如前台传来的数据来源id是1,即“智慧芽”,那么jsonData装载的就是智慧芽的数据源配置)
+        ProjectImportPatentVO projectImportPatentVO = new ProjectImportPatentVO()
+                .setSourceId("1");
+        List<UploadSettingVO.Column> jsonData = UploadPatentBatchUtil.parsingConfigurationFiles(projectImportPatentVO, getSettingJson);
+
+        String path = "C:\\Users\\Administrator\\Desktop\\Leisure\\专利文件\\CN多件专利(回车换行).XLSX";
+        int row = 1;
+        //根据路径和行数获得专利数据
+        PatentData patentData = ReadExcelUtils.readExcelOneRow(path, row);
+        //取出专利内容数据(除了摘要附图),(key:表头如 "公开(公告)号"  value:表头对应内容如 "CN1307082B")
+        Map<Object, Object> patentMap = patentData.getMap();
+        //取出专利摘要附图
+        PictureData pictureData = patentData.getPictureData();
+
+        //专利基础数据装配(此步与数据源配置文件对象相关)
+        UploadParamsVO uploadParamsVO = UploadPatentBatchUtil.processData(patentMap, jsonData);
+
+        Map<String, String> result = new HashMap<>();
+        String ext = pictureData.suggestFileExtension();
+        byte[] data = pictureData.getData();
+        String picName = IdUtil.simpleUUID() + "." + ext;
+        String date = DateUtils.getNowTimeFormat("yyyyMMdd");
+        String folderPath = new FileUtils().getSavePath(date);
+        String filePath = FileUtils.FILE_SEPARATOR + date + FileUtils.FILE_SEPARATOR + picName;
+        File directory = new File(folderPath);
+        if (!directory.exists()) {
+            directory.mkdir();
+        }
+        FileOutputStream out = new FileOutputStream(folderPath + picName);
+        out.write(data);
+        out.close();
+        result.put("path", filePath);
+        result.put("name", picName);
+        System.out.println(result);
+
+    }
+
 
     /**
      * 专利入库
      *
      * @param uploadParamsVO 专利内容实体类对象
      */
-    public void uploadPatent(UploadParamsVO uploadParamsVO) {
+    public void uploadPatent(UploadParamsVO uploadParamsVO, PictureData pictureData) throws IOException {
         //基础数据入库(取专利id)
         uploadPatentBase(uploadParamsVO);
+        //摘要附图入库
+        uploadPatentImage(uploadParamsVO, pictureData);
         //著录项目入库
         uploadPatentZhulu(uploadParamsVO);
         //说明书文本入库
@@ -53,6 +112,21 @@ public class UploadPatentService {
     }
 
     /**
+     * 摘要附图入库
+     *
+     * @param uploadParamsVO 专利内容实体类对象
+     * @param pictureData    摘要附图
+     */
+    public void uploadPatentImage(UploadParamsVO uploadParamsVO, PictureData pictureData) throws IOException {
+        //摘要附图数据装配及入库(摘要附图表"os_patent_img")
+        if (uploadParamsVO.getPatent() != null) {
+            if (uploadParamsVO.getPatent().getId() != null && pictureData != null) {
+                uploadParamsVO.getPatent().setAbstractPath(patentImageService.updatePatentImage(uploadParamsVO.getPatent().getId(), pictureData));
+            }
+        }
+    }
+
+    /**
      * 著录项目入库
      *
      * @param uploadParamsVO 专利内容实体类对象

+ 29 - 28
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/UploadTaskService.java

@@ -20,44 +20,45 @@ public class UploadTaskService {
     private final FileUtils fileUtils;
     private final TaskService taskService;
     private final ReadExcelUtils readExcelUtils;
-    public   Integer upLoadPatent(MultipartFile file) throws InterruptedException, IOException {
+
+    public Integer upLoadPatent(MultipartFile file) throws InterruptedException, IOException {
         //1.检查文档合法性
         UploadFileDTO fileDTO = fileUtils.uploadFile(file);
         //获得文件路径
         String filePath = fileUtils.getPath(fileDTO.getPath());
-      Integer total=  ReadExcelUtils.textExcel(filePath);
-      if(total<=0){
-          return  -1;
-      }
+        Integer total = ReadExcelUtils.textExcel(filePath);
+        if (total <= 0) {
+            return -1;
+        }
 
         //2.解析文档
         //4.建立任务
-        Integer projectId =null;
+        Integer projectId = null;
 //        Integer taskId = taskService.add(fileDTO, projectId, null, total- 1, 1, 0, file.getOriginalFilename());
         ThreadSafeQueue<UploadParamsVO> threadSafeQueue = new ThreadSafeQueue<>(8);
-       for(int i=0;i<total;i++) {
+        for (int i = 0; i < total; i++) {
 
-           //3.装载实体类
-           UploadParamsVO uploadParamsVO = new UploadParamsVO();
-           uploadParamsVO.setFirstAddress(i+"");
-           //5.上传到库
-           Thread thread1 = new Thread(() -> {
-               try {
-                   threadSafeQueue.enqueue(uploadParamsVO);
-               } catch (InterruptedException e) {
-                   e.printStackTrace();
-               }
-           });
-           thread1.start();
-           Thread thread2 = new Thread(() -> {
-               try {
-                   threadSafeQueue.dequeue();
-               } catch (InterruptedException e) {
-                   e.printStackTrace();
-               }
-           });
-           thread2.start();
-       }
+            //3.装载实体类
+            UploadParamsVO uploadParamsVO = new UploadParamsVO();
+            uploadParamsVO.setFirstAddress(i + "");
+            //5.上传到库
+            Thread thread1 = new Thread(() -> {
+                try {
+                    threadSafeQueue.enqueue(uploadParamsVO);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            });
+            thread1.start();
+            Thread thread2 = new Thread(() -> {
+                try {
+                    threadSafeQueue.dequeue();
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            });
+            thread2.start();
+        }
         return 1;
     }