chendayu 2 rokov pred
rodič
commit
f8f6f87d91

+ 31 - 1
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/MessageService.java

@@ -1,6 +1,13 @@
 package cn.cslg.pas.service.upLoadPatent;
 
 
+import cn.cslg.pas.common.core.base.Constants;
+import cn.cslg.pas.common.model.dto.TaskWebSocketDTO;
+import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
+import cn.cslg.pas.common.utils.Response;
+import cn.cslg.pas.common.utils.ResponseEnum;
+import cn.cslg.pas.common.utils.WebSocketServer;
+import cn.cslg.pas.domain.Task;
 import org.springframework.stereotype.Service;
 
 /**
@@ -11,6 +18,29 @@ import org.springframework.stereotype.Service;
 @Service
 public class MessageService {
 
-
+    /**
+     * 通过WebSocket 在每一次循环结束后 向前端发送完成进度
+     *
+     * @param task                  任务Task(这里主要为了拿任务taskId和文件原始名称oldName)
+     * @param projectImportPatentVO 接收前台数据DTO对象(这里主要为了拿专题库projectId)
+     * @param total                 专利总数量
+     * @param i                     当前专利下标
+     * @param percentage            进度(需要计算)
+     */
+    public void sendWebsocketMessage(Task task, ProjectImportPatentVO projectImportPatentVO, Integer total, Integer i, Long percentage) {
+        //通过WebSocket 在每一次循环结束后 向前端发送完成进度
+        //percentage:total == 0 ? 0 : Math.round((total.equals(i) ? (i * 1D) : (i + 1D)) / total * 100D)
+        WebSocketServer.sendInfo(Response.websocket(new TaskWebSocketDTO()
+                .setTaskId(task.getId())
+                .setProjectId(projectImportPatentVO.getProjectId())
+                .setComplete(false)
+                .setIndex(i - 1)
+                .setTaskType(Constants.TASK_IMPORT_PATENT)
+                .setPercentage(percentage)
+                .setFileName("")
+                .setOldName(task.getOldName())
+                .setUrl("")
+                .setTotal(total), ResponseEnum.PATENT_IMPORT_TASK_SUCCESS), task.getCreateBy() + "");
+    }
 
 }

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

@@ -1,5 +1,6 @@
 package cn.cslg.pas.service.upLoadPatent;
 
+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.ReadExcelUtils;
@@ -29,19 +30,20 @@ import java.util.concurrent.locks.ReentrantLock;
 public class PantentQueueService {
     private final ExcuteDataToVOService excuteDataToVOService;
     private final ExcuteUploadSettingService excuteUploadSettingService;
+    private final MessageService messageService;
     private Queue<UploadParamsVO> queue = new LinkedList<>();
     private Boolean flag = false;
     private CountDownLatch latch = new CountDownLatch(10);
 
     //将专利信息存入队列
-    public void addPatnetToQueue(Task task) throws InterruptedException {
+    public void addPatnetToQueue(Task task, ProjectImportPatentVO projectImportPatentVO) throws InterruptedException {
         try {
             //获得文件路径
             String filePath = task.getUrl();
             //检查文件合法性
             Integer totalRow = ReadExcelUtils.textExcel(filePath);
             //根据数据来源id(如1:智慧芽)解析数据源配置文件信息
-            List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting("1");
+            List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(projectImportPatentVO.getSourceId());
             //遍历专利总数量,在循环中保存专利
             for (int i = 1; i <= totalRow; i++) {
                 //解析读取一行专利
@@ -52,7 +54,15 @@ public class PantentQueueService {
                 queue.add(uploadParamsVO);
                 //通知消费者线程
                 latch.countDown();
+
+                //Websocket发送message:通过WebSocket 在每一次循环结束后 向前端发送完成进度
+                Long percentage = totalRow == 0 ? 0 : Math.round((totalRow.equals(i) ? (i * 1D) : (i + 1D)) / totalRow * 100D);
+                messageService.sendWebsocketMessage(task, projectImportPatentVO, totalRow, i, percentage);
             }
+            //全部循环结束后,发送进度
+            Long percentage = 100L;
+            messageService.sendWebsocketMessage(task, projectImportPatentVO, totalRow, totalRow, percentage);
+
         } catch (IOException e) {
             e.printStackTrace();
         } finally {