chendayu 2 years ago
parent
commit
39ff1d3da5

+ 9 - 14
PAS/src/main/java/cn/cslg/pas/common/utils/ReadExcelUtils.java

@@ -32,19 +32,16 @@ public class ReadExcelUtils {
     public static Integer textExcel(String filePath) throws IOException {
         //判断文件是否存在
         if (filePath == null || filePath.equals("")) {
-            return -1;
-//            ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
+            ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
         }
         File file = new File(filePath);
         if (!file.exists()) {
-            return -1;
-//            ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
+            ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
         }
 
         // 检测是否为excel文件
         if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
-            return -1;
-//            ThrowException.throwXiaoShiException("请上传Excel文件!");
+            ThrowException.throwXiaoShiException("文件格式错误,请上传Excel文件!");
         }
 
         InputStream fis = new FileInputStream(file);
@@ -60,15 +57,13 @@ public class ReadExcelUtils {
         Sheet sheet = workbook.getSheetAt(0);
         //读取总行数
         int rows = sheet.getPhysicalNumberOfRows();
-        if (rows <= 0) {
-            return -2;
-//            ThrowException.throwXiaoShiException("文件内容格式不正确!");
+        if (rows <= 1) {
+            ThrowException.throwXiaoShiException("文件内容格式不正确,请检查总行数是否有专利内容");
         }
 
         Row firstRow = sheet.getRow(0);
         if (!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")) {
-            return -2;
-//            ThrowException.throwXiaoShiException("文件内容格式不正确!");
+            ThrowException.throwXiaoShiException("文件内容格式不正确,第一列应为【公开(公告)号】");
         }
 
         //返回文件总行数-1(即专利总数量)
@@ -93,9 +88,9 @@ public class ReadExcelUtils {
         //开始装载专利数据
         Row firstRow = sheet.getRow(0);
         Row needRow = sheet.getRow(row);
-        //读取第一行的时候会多读一列
-        int firstColumns = firstRow.getLastCellNum() - 0;
-        for (int i = 0; i < firstColumns; i++) {
+        //获得总列数
+        int columns = firstRow.getLastCellNum();
+        for (int i = 0; i < columns; i++) {
             map.put(firstRow.getCell(i) + "", needRow.getCell(i) + "");
         }
 

+ 13 - 4
PAS/src/main/java/cn/cslg/pas/controller/ProjectImportController.java

@@ -9,10 +9,12 @@ import cn.cslg.pas.common.model.vo.TaskParams;
 import cn.cslg.pas.common.utils.*;
 import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
 import cn.cslg.pas.common.utils.auth.checkAuth;
+import cn.cslg.pas.exception.XiaoShiException;
 import cn.cslg.pas.service.ProjectImportService;
 import cn.cslg.pas.service.ProjectImportStatusService;
 import cn.cslg.pas.service.ProjectService;
 import cn.cslg.pas.service.UploadPatentBatchService;
+import cn.cslg.pas.service.upLoadPatent.UploadTaskService;
 import cn.dev33.satoken.stp.StpUtil;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
@@ -23,6 +25,7 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -44,18 +47,24 @@ public class ProjectImportController {
     private final ProjectImportStatusService projectImportStatusService;
     private final LoginUtils loginUtils;
     private final UploadPatentBatchService uploadPatentBatchService;
+    private final UploadTaskService uploadTaskService;
 
     @Permission(roles = {2})
     @PostMapping("patent")
     @Operation(summary = "为专题库导入专利")
-    public String importPatent(MultipartFile file, String json) {
+    public String importPatent(MultipartFile file, String json) throws IOException {
         ProjectImportPatentVO params = JsonUtils.jsonToPojo(json, ProjectImportPatentVO.class);
         if (params == null) {
             return Response.error("参数错误");
         }
-        TaskParams taskParams = projectService.getImportPatentTaskParams(file, params.getProjectId());
+//        TaskParams taskParams = projectService.getImportPatentTaskParams(file, params.getProjectId());
         //projectService.importPatent(taskParams, params);
-        uploadPatentBatchService.uploadPatentBatch(taskParams, params);
+//        uploadPatentBatchService.uploadPatentBatch(taskParams, params);
+        try {
+            uploadTaskService.addTask(file, json);
+        } catch (XiaoShiException e) {
+            return Response.error(e.getMessage());
+        }
         return Response.success(true);
     }
 
@@ -96,7 +105,7 @@ public class ProjectImportController {
         if (StringUtils.isNotEmpty(tempId)) {
             return Response.error("有导入任务在进行中");
         }
-        Integer importId = projectImportService.add(userId, url,personnelVO.getTenantId());
+        Integer importId = projectImportService.add(userId, url, personnelVO.getTenantId());
         projectService.importData(url, params, userId, importId);
         cacheUtils.setUserImportId(userId, importId);
         return Response.success(importId);

+ 15 - 15
PAS/src/main/java/cn/cslg/pas/controller/TaskController.java

@@ -1,11 +1,9 @@
 package cn.cslg.pas.controller;
 
 import cn.cslg.pas.common.core.base.Constants;
-import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
-import cn.cslg.pas.common.utils.JsonUtils;
+import cn.cslg.pas.common.model.vo.TaskVO;
 import cn.cslg.pas.common.utils.Response;
 import cn.cslg.pas.domain.Task;
-import cn.cslg.pas.common.model.vo.TaskVO;
 import cn.cslg.pas.service.TaskService;
 import cn.cslg.pas.service.upLoadPatent.UploadTaskService;
 import io.swagger.v3.oas.annotations.Operation;
@@ -13,7 +11,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.RequiredArgsConstructor;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 
@@ -30,13 +27,13 @@ import java.io.IOException;
 @RequestMapping(Constants.API_VERSION_V2 + "/task")
 @RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class TaskController {
-
     private final TaskService taskService;
     private final UploadTaskService uploadTaskService;
+
     @GetMapping("list")
     @Operation(summary = "任务列表")
     public String getPageList(TaskVO params) throws IOException {
-      return Response.success(taskService.getPageList(params));
+        return Response.success(taskService.getPageList(params));
     }
 
     @PostMapping("delete")
@@ -47,8 +44,8 @@ public class TaskController {
 
     @GetMapping("queue")
     @Operation(summary = "获取进行中的任务")
-    public String getQueueList(Integer type, Integer projectId,Integer productId) {
-        return Response.success(taskService.getQueueList(type, projectId,productId));
+    public String getQueueList(Integer type, Integer projectId, Integer productId) {
+        return Response.success(taskService.getQueueList(type, projectId, productId));
     }
 
     @PostMapping("update")
@@ -58,13 +55,16 @@ public class TaskController {
         return Response.success(true);
     }
 
-    @PostMapping("add")
-    @Operation(summary = "添加任务")
-    public String add(MultipartFile file, String json) throws IOException {
-        ProjectImportPatentVO params = JsonUtils.jsonToPojo(json, ProjectImportPatentVO.class);
-        uploadTaskService.addTask(file,params);
-        return Response.success(true);
-    }
+//    @PostMapping("add")
+//    @Operation(summary = "新增专利导入任务")
+//    public String add(MultipartFile file, String json) throws IOException {
+//        ProjectImportPatentVO params = JsonUtils.jsonToPojo(json, ProjectImportPatentVO.class);
+//        if (params == null) {
+//            return Response.error("参数错误");
+//        }
+//        uploadTaskService.addTask(file, json);
+//        return Response.success(true);
+//    }
 
 }
 

+ 87 - 81
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/PantentQueueService.java

@@ -1,10 +1,8 @@
 package cn.cslg.pas.service.upLoadPatent;
 
-import cn.cslg.pas.common.model.dto.UploadFileDTO;
 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.ExcelUtils;
 import cn.cslg.pas.common.utils.FileUtils;
 import cn.cslg.pas.common.utils.JsonUtils;
 import cn.cslg.pas.common.utils.ReadExcelUtils;
@@ -13,29 +11,20 @@ import cn.cslg.pas.domain.QueueData;
 import cn.cslg.pas.domain.Task;
 import cn.cslg.pas.service.TaskService;
 import cn.cslg.pas.service.UploadPatentBatchService;
-import cn.hutool.core.collection.IterUtil;
-import cn.hutool.poi.excel.ExcelUtil;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import lombok.RequiredArgsConstructor;
-import org.apache.poi.ss.usermodel.PictureData;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.springframework.context.annotation.Lazy;
-import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Service;
 
-import javax.annotation.PostConstruct;
 import java.io.IOException;
 import java.util.*;
-import java.util.concurrent.CountDownLatch;
-
-import static org.apache.poi.ss.usermodel.TableStyleType.totalRow;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * 将专利信息存入队列或从队列取出
- *
- * @author 李仁杰
  */
-
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class PantentQueueService {
@@ -51,65 +40,50 @@ public class PantentQueueService {
     private Queue<QueueData> patentAssoQueue = new LinkedList<>();
     private List<Integer> taskQueueList = new ArrayList<>();
     private Boolean flag = false;
-    private CountDownLatch taskLatch = new CountDownLatch(1);
-    private CountDownLatch patentImageZhuluLatch = new CountDownLatch(1);
-    private CountDownLatch patentRightInstructionTextLatch = new CountDownLatch(1);
-    private CountDownLatch patentAssoLatch = new CountDownLatch(1);
+    private Lock taskLock = new ReentrantLock();
+    private Lock patentImageZhuluLock = new ReentrantLock();
+    private Lock patentRightInstructionTextLock = new ReentrantLock();
+    private Lock patentAssoLock = new ReentrantLock();
+    Condition taskCondition = taskLock.newCondition();
+    private Condition patentImageZhuluCondition = patentImageZhuluLock.newCondition();
+    private Condition patentRightInstructionTextCondition = patentRightInstructionTextLock.newCondition();
+    private Condition patentAssoCondition = patentAssoLock.newCondition();
+    private HashMap<Integer, Integer> patentIdMap = new HashMap<>();
+    private Task task = null;
+    private Integer patentFinishNum = 0;
 
     //将专利信息存入队列
     public void addPatnetToQueue() {
-        Task task = null;
         try {
             while (true) {
                 //检查任务队列
                 if (taskQueueList.size() == 0) {
-                    taskLatch.await();
+                    taskLock.lock();
+                    taskCondition.await();
                 }
-
-                //1.从taskQueueList中取出第一个taskId,并将其从taskQueueList中删除
+                //1.从taskQueueList中取出第一个task,并将其从taskQueueList中删除
                 task = taskService.getById(taskQueueList.get(0));
                 taskQueueList.remove(0);
-                //获得Excel文件路径
+                //从任务数据中取出文件路径、总条数、成功条数、前台参数json
                 String filePath = task.getUrl();  //相对路径
                 filePath = fileUtils.getPath(filePath);  //绝对路径
+                Integer total = task.getTotal();
+                patentIdMap.put(0, total);
 
-                //List<List<Object>> rowList = new ArrayList<>();
-                //List<Map<Object, Object>> mapList = new ArrayList<>();
-                //解析Excel获取所有行数据 rowList
-                //ExcelUtil.readBySax(filePath, -1, (sheetIndex, rowIndex, row) -> rowList.add(row));
-                //遍历 rowList,将第一行即表头作为key,其余行即专利内容作为value添加进map集合 mapList
-                //for (int i = 1; i < rowList.size(); i++) {
-                //    mapList.add(IterUtil.toMap(rowList.get(0), rowList.get(i)));
-                //}
-                //解析Excel获取所有摘要附图
-                //Map<String, PictureData> pictureDataMap = ExcelUtils.getDataFromExcel(filePath);
-
-                //Integer total = task.getTotal();
                 int lastIndex = task.getSuccessNum();
                 String json = task.getPramJson();
                 ProjectImportPatentVO projectImportPatentVO = JsonUtils.jsonToPojo(json, ProjectImportPatentVO.class);
                 //解析数据源类,通过数据来源id(如1:智慧芽)解析数据源配置文件,获得数据源配置文件对象jsonData
                 List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(projectImportPatentVO.getSourceId());
-
-                //解析Excel文件获得工作簿,并取出工作簿总行数作为专利数量
+                //解析Excel文件获得工作簿
                 Sheet sheet = ReadExcelUtils.readExcel(filePath);
-                int total = sheet.getPhysicalNumberOfRows() - 1;
-
                 //遍历专利总数量,在循环中保存专利
                 for (int i = lastIndex; i < total; i++) {
-                    //解析读取一行专利
-                    //PatentData patentData = ReadExcelUtils.readExcelOneRow(filePath, i);
-                    //PatentData patentData = new PatentData()
-                    //        .setMap(mapList.get(i))
-                    //        .setPictureData(pictureDataMap.get(String.valueOf(i + 1)));
-                    PatentData patentData = ReadExcelUtils.readExcelOneRow(filePath, sheet, i);
-                    //patentData.setPictureData(pictureDataMap.get(String.valueOf(i + 1)));
+                    PatentData patentData = ReadExcelUtils.readExcelOneRow(filePath, sheet, i + 1);
                     //调用装载数据类,专利数据转换为VO对象
                     UploadParamsVO uploadParamsVO = excuteDataToVOService.fileToPatentVO(patentData, jsonData);
-
                     //保存专利基础数据(专利表"os_patent")
                     uploadPatentBatchService.getOneOrInsertOne(uploadParamsVO);
-
                     //专利分别加入三个消费者队列
                     QueueData queueData = new QueueData()
                             .setUploadParamsVO(uploadParamsVO)
@@ -117,31 +91,25 @@ public class PantentQueueService {
                     patentImageZhuluQueue.add(queueData);
                     patentRightInstructionTextQueue.add(queueData);
                     patentAssoQueue.add(queueData);
-
                     //通知消费者线程(三个消费者:保存摘要附图和著录、保存说明书和权要、保存专题库关联专利数据)
                     //保存摘要附图和著录消费者
-                    patentImageZhuluLatch.countDown();
+                    //patentImageZhuluLatch.countDown();
+                    patentImageZhuluLock.lock();
+                    patentImageZhuluCondition.signalAll();
+                    patentImageZhuluLock.unlock();
                     //保存说明书和权要消费者
-                    patentRightInstructionTextLatch.countDown();
+                    //patentRightInstructionTextLatch.countDown();
+                    patentRightInstructionTextLock.lock();
+                    patentRightInstructionTextCondition.signalAll();
+                    patentRightInstructionTextLock.unlock();
                     //保存专题库关联专利数据消费者
-                    patentAssoLatch.countDown();
+                    //patentAssoLatch.countDown();
+                    patentAssoLock.lock();
+                    patentAssoCondition.signalAll();
+                    patentAssoLock.unlock();
 
-                    //任务表更新数据
-                    //上一次的位置(保存到第几个专利)
-                    task.setLastIndex(i);
-                    //成功条数
-                    task.setSuccessNum(i);
-                    //失败条数
-                    task.setDefaultNum(total - i);
-                    taskService.updateById(task);
 
-                    //Websocket发送message:通过WebSocket 在每一次循环结束后 向前端发送完成进度
-                    Long percentage = total == 0 ? 0 : Math.round((total == i ? (i * 1D) : (i + 1D)) / total * 100D);
-                    messageService.sendWebsocketMessage(task, total, i, percentage);
                 }
-                //全部循环结束后,发送进度
-                Long percentage = 100L;
-                messageService.sendWebsocketMessage(task, total, total, percentage);
 
                 //任务表更新数据
                 task.setStatus(2);
@@ -166,20 +134,23 @@ public class PantentQueueService {
             while (true) {
                 if (patentImageZhuluQueue.isEmpty()) {
                     if (flag) {
-                        System.out.println("退出循环");
+                        System.out.println("所有专利的摘要附图和著录项目全部完成,退出循环");
                         return;
                     } else {
-                        patentImageZhuluLatch.await();
+                        patentImageZhuluLock.lock();
+                        patentImageZhuluCondition.await();
+                        patentImageZhuluLock.unlock();
                     }
                 } else {
-                    System.out.println("开始保存摘要附图和著录项目");
                     QueueData queueData = patentImageZhuluQueue.remove();
                     //摘要附图入库
                     uploadPatentToDBService.uploadPatentImage(queueData.getUploadParamsVO());
                     //著录项目入库
                     uploadPatentToDBService.uploadPatentZhulu(queueData.getUploadParamsVO());
-                    System.out.println("出队列");
+                    //Websocket发送message:通过WebSocket 在每一次循环结束后 向前端发送完成进度
+                    sendMessage(queueData, patentIdMap.get(0), task);
                 }
+
             }
         } finally {
 
@@ -194,20 +165,21 @@ public class PantentQueueService {
             while (true) {
                 if (patentRightInstructionTextQueue.isEmpty()) {
                     if (flag) {
-                        System.out.println("退出循环");
+                        System.out.println("所有专利的说明书和权要全部完成,退出循环");
                         return;
                     } else {
-                        patentRightInstructionTextLatch.await();
+                        patentRightInstructionTextLock.lock();
+                        patentRightInstructionTextCondition.await();
+                        patentRightInstructionTextLock.unlock();
                     }
                 } else {
-                    System.out.println("开始保存说明书和权要");
                     QueueData queueData = patentRightInstructionTextQueue.remove();
                     //说明书入库
                     uploadPatentToDBService.uploadPatentInstructionText(queueData.getUploadParamsVO());
                     //权要入库
                     uploadPatentToDBService.uploadPatentRight(queueData.getUploadParamsVO());
-
-                    System.out.println("出队列");
+                    //Websocket发送message:通过WebSocket 在每一次循环结束后 向前端发送完成进度
+                    sendMessage(queueData, patentIdMap.get(0), task);
                 }
             }
         } finally {
@@ -223,13 +195,14 @@ public class PantentQueueService {
             while (true) {
                 if (patentAssoQueue.isEmpty()) {
                     if (flag) {
-                        System.out.println("退出循环");
+                        System.out.println("所有专利的与专利关联的数据全部完成,退出循环");
                         return;
                     } else {
-                        patentAssoLatch.await();
+                        patentAssoLock.lock();
+                        patentAssoCondition.await();
+                        patentAssoLock.unlock();
                     }
                 } else {
-                    System.out.println("开始保存与专利关联的数据");
                     QueueData queueData = patentAssoQueue.remove();
                     //专题库与专利关联入库
                     uploadPatentToDBService.uploadAssoThemaPat(queueData.getUploadParamsVO(), queueData.getProjectImportPatentVO());
@@ -237,8 +210,8 @@ public class PantentQueueService {
                     uploadPatentToDBService.uploadAssoFieldPat(queueData.getUploadParamsVO(), queueData.getProjectImportPatentVO());
                     //文件夹与专利关联入库
                     uploadPatentToDBService.uploadAssoPorPat(queueData.getUploadParamsVO(), queueData.getProjectImportPatentVO());
-
-                    System.out.println("出队列");
+                    //Websocket发送message:通过WebSocket 在每一次循环结束后 向前端发送完成进度
+                    sendMessage(queueData, patentIdMap.get(0), task);
                 }
             }
         } finally {
@@ -246,12 +219,45 @@ public class PantentQueueService {
         }
     }
 
+    public void sendMessage(QueueData queueData, Integer total, Task task) {
+        //每完成一个专利,通过计算,发送进度
+        synchronized ("啊啊啊") {
+            Integer currentPatentId = queueData.getUploadParamsVO().getPatent().getId();
+            Integer num = patentIdMap.get(currentPatentId);
+            if (num == null) {
+                patentIdMap.put(currentPatentId, 1);
+            } else if (num == 1) {
+                patentIdMap.put(currentPatentId, 2);
+            } else {
+                //num达到3了就从patentIdMap中删除
+                patentIdMap.remove(currentPatentId);
+                long percentage = Math.round((patentFinishNum + 1D) / total * 100D);
+                if (patentFinishNum.equals(total)) {
+                    percentage = 100L;
+                }
+                patentFinishNum++;
+                messageService.sendWebsocketMessage(task, total, patentFinishNum, percentage);
+
+//任务表更新数据 ↓
+                //成功条数
+                task.setSuccessNum(patentFinishNum);
+                //失败条数
+                task.setDefaultNum(total - task.getSuccessNum());
+                taskService.updateById(task);
+            }
+        }
+    }
+
+
     public void queueAddTask(List<Integer> taskQueueList) {
         this.taskQueueList.addAll(taskQueueList);
     }
 
     public void awakeTasktch() {
-        this.taskLatch.countDown();
+        taskLock.lock();
+        taskCondition.signalAll();
+        taskLock.unlock();
 
     }
+
 }

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

@@ -5,6 +5,7 @@ import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
 import cn.cslg.pas.common.utils.FileUtils;
 import cn.cslg.pas.common.utils.ReadExcelUtils;
 import cn.cslg.pas.common.utils.ThrowException;
+import cn.cslg.pas.exception.XiaoShiException;
 import cn.cslg.pas.service.TaskService;
 import cn.hutool.core.collection.IterUtil;
 import cn.hutool.poi.excel.ExcelUtil;
@@ -13,6 +14,7 @@ import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -38,9 +40,9 @@ public class UploadTaskService {
     public void addTask(MultipartFile file, String json) throws IOException {
         //将包含多件专利的Excel文件上传至本地,并返回文件对象fileDTO
         UploadFileDTO fileDTO = fileUtils.uploadFile(file);
-        //获得文件路径
+        //获得文件路径(绝对路径)
         String filePath = fileUtils.getPath(fileDTO.getPath());
-        //获取专利总数量
+        //检查文件合法性并获取专利总数量
         Integer total = ReadExcelUtils.textExcel(filePath);
         //新增任务(专利导入导出任务表)
         //Integer taskId = taskService.add(fileDTO, projectImportPatentVO.getProjectId(), null, total, 1, 0, file.getOriginalFilename());