chendayu 2 年之前
父节点
当前提交
5ce480c85e

+ 53 - 0
PAS/src/main/java/cn/cslg/pas/common/core/CreateTaskThread.java

@@ -0,0 +1,53 @@
+package cn.cslg.pas.common.core;
+
+import cn.cslg.pas.domain.Task;
+import cn.cslg.pas.service.TaskService;
+import cn.cslg.pas.service.upLoadPatent.PantentQueueService;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 项目启动时初始化任务列表队列
+ *
+ * @Author chenyu
+ * @Date 2023/6/2
+ */
+@Component
+@RequiredArgsConstructor
+public class CreateTaskThread implements InitializingBean {
+
+    private final TaskService taskService;
+
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        //初始化任务列表数据(taskQueueList)
+        //查找任务状态为0(队列中)和1(进行中)的任务
+        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().in(Task::getStatus, new ArrayList<>(Arrays.asList(0, 1))));
+        List<Integer> taskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
+        //将任务ids存入 taskQueueList
+        PantentQueueService pantentQueueService =PantentQueueService.getInstance();
+        pantentQueueService.setTaskQueueList(taskIds);
+        //一个线程执行解析专利并入队列
+        Thread thread1 = new Thread(() -> {
+            pantentQueueService.addPatnetToQueue();
+        });
+
+        //一个线程执行保存专利并出队列
+        Thread thread2 = new Thread(() -> {
+            try {
+                pantentQueueService.pushPantentToDb();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        });
+    }
+
+}

+ 1 - 1
PAS/src/main/java/cn/cslg/pas/common/utils/PatentRightUtils.java

@@ -173,7 +173,7 @@ public class PatentRightUtils {
                 }
 
                 String regex;
-                if (content.contains("@2")) {
+                if (content.contains("@")) {
                     regex = "@[0-9]+";
                 } else if (content.contains("[001]")) {
                     regex = "\\[[0-9]+]";

+ 18 - 10
PAS/src/main/java/cn/cslg/pas/common/utils/ReadExcelUtils.java

@@ -32,38 +32,46 @@ public class ReadExcelUtils {
     public static Integer textExcel(String filePath) throws IOException {
         //判断文件是否存在
         if (filePath == null || filePath.equals("")) {
-            return -1;
+            //return -1;
+            ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
         }
         File file = new File(filePath);
         if (!file.exists()) {
-            return -1;
+            //return -1;
+            ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
         }
 
         // 检测是否为excel文件
         if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
-            return -1;
+            //return -1;
+            ThrowException.throwXiaoShiException("请上传Excel文件!");
         }
 
-        InputStream inputStream = new FileInputStream(file);
-        //XSSFWorkbook可以处理xlsx文件
+        InputStream fis = new FileInputStream(file);
+        //使用poi框架解析处理Excel文件
         Workbook workbook = null;
-        //当文件以.xls结尾时
+        //区分不同版本Excel,使用各自对应的工具类
         if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
-            workbook = new HSSFWorkbook(inputStream);
+            workbook = new HSSFWorkbook(fis);
         } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
-            workbook = new XSSFWorkbook(inputStream);
+            workbook = new XSSFWorkbook(fis);
         }
         //读取第几个sheet
         Sheet sheet = workbook.getSheetAt(0);
         //读取总行数
         int rows = sheet.getPhysicalNumberOfRows();
         if (rows <= 0) {
-            return -2;
+            //return -2;
+            ThrowException.throwXiaoShiException("文件内容格式不正确!");
         }
+
         Row firstRow = sheet.getRow(0);
         if (!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")) {
-            return -2;
+            //return -2;
+            ThrowException.throwXiaoShiException("文件内容格式不正确!");
         }
+
+        //返回文件总行数-1(即专利总数量)
         return rows - 1;
     }
 

+ 1 - 1
PAS/src/main/java/cn/cslg/pas/controller/UserController.java

@@ -62,7 +62,7 @@ public class UserController {
     @PostMapping("test")
     @Operation(summary = "删除用户")
     public String test(Integer id) {
-        uploadTaskService.test();
+        //uploadTaskService.test();
         return "test";
     }
 

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

@@ -59,7 +59,7 @@ public class Task extends BaseEntity<Task> {
     private Integer endTime;
 
     /**
-     * 0.队列中 1.进行中 2.成功 3.失败
+     * 0.队列中 1.进行中 2.成功 3.失败 4.暂停 5.取消
      */
     @TableField("status")
     private Integer status;

+ 13 - 8
PAS/src/main/java/cn/cslg/pas/service/PatentRightService.java

@@ -436,16 +436,21 @@ public class PatentRightService extends ServiceImpl<PatentRightMapper, PatentRig
      * @param params 权利要求相关数据
      */
     public void updatePatentRight(PatentRightParams params) {
-        //调用拆分权要工具类
-        List<PatentRight> patentRights = PatentRightUtils.formatPatentRight(params);
+        try {
+            //调用拆分权要工具类
+            List<PatentRight> patentRights = PatentRightUtils.formatPatentRight(params);
 
-        if (patentRights.size() > 0) {
-            //TODO 先根据专利id查询库中原权要,与当前权要比对,若不同则更新,若相同则不更新
+            if (patentRights.size() > 0) {
+                //TODO 先根据专利id查询库中原权要,与当前权要比对,若不同则更新,若相同则不更新
 
-            //删除库表中原有该权要
-            this.deleteByPatentId(params.getPatentId());
-            //权要数据入表"os_patent_right"
-            this.saveOrUpdateBatch(patentRights);
+                //删除库表中原有该权要
+                this.deleteByPatentId(params.getPatentId());
+                //权要数据入表"os_patent_right"
+                this.saveOrUpdateBatch(patentRights);
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
         }
 
     }

+ 4 - 3
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/ExcuteTaskService.java

@@ -10,7 +10,7 @@ import org.springframework.stereotype.Service;
 import java.io.IOException;
 
 /**
- * 执行人物
+ * 执行任务
  *
  * @Author 李仁杰
  */
@@ -20,10 +20,11 @@ public class ExcuteTaskService {
     private final PantentQueueService pantentQueueService;
 
     //执行Excel导入任务
-    public void executeExcelTask(Task task, ProjectImportPatentVO projectImportPatentVO) throws IOException {
+    public void executeExcelTask(Task task, ProjectImportPatentVO projectImportPatentVO) {
         //一个线程执行入队列
         Thread thread1 = new Thread(() -> {
-            pantentQueueService.addPatnetToQueue(task, projectImportPatentVO);
+            pantentQueueService.addPatnetToQueue();
+
         });
 
 

+ 6 - 7
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/MessageService.java

@@ -21,18 +21,17 @@ public class MessageService {
     /**
      * 通过WebSocket 在每一次循环结束后 向前端发送完成进度
      *
-     * @param task                  任务Task(这里主要为了拿任务taskId和文件原始名称oldName)
-     * @param projectImportPatentVO 接收前台数据DTO对象(这里主要为了拿专题库projectId)
-     * @param total                 专利总数量
-     * @param i                     当前专利下标
-     * @param percentage            进度(需要计算)
+     * @param task       任务Task(这里主要为了拿任务taskId和文件原始名称oldName)
+     * @param total      专利总数量
+     * @param i          当前专利下标
+     * @param percentage 进度(需要计算)
      */
-    public void sendWebsocketMessage(Task task, ProjectImportPatentVO projectImportPatentVO, Integer total, Integer i, Long percentage) {
+    public void sendWebsocketMessage(Task task, 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())
+                .setProjectId(task.getProjectId())
                 .setComplete(false)
                 .setIndex(i - 1)
                 .setTaskType(Constants.TASK_IMPORT_PATENT)

+ 60 - 15
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/PantentQueueService.java

@@ -6,12 +6,17 @@ import cn.cslg.pas.common.model.vo.UploadSettingVO;
 import cn.cslg.pas.common.utils.ReadExcelUtils;
 import cn.cslg.pas.domain.PatentData;
 import cn.cslg.pas.domain.Task;
+import cn.cslg.pas.service.TaskService;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import lombok.Data;
 import lombok.RequiredArgsConstructor;
 import org.apache.poi.ss.formula.functions.T;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Queue;
@@ -26,44 +31,75 @@ import java.util.concurrent.locks.ReentrantLock;
  * @author 李仁杰
  */
 @Service
-@RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class PantentQueueService {
-    private final ExcuteDataToVOService excuteDataToVOService;
-    private final ExcuteUploadSettingService excuteUploadSettingService;
-    private final MessageService messageService;
+    @Autowired
+    private  ExcuteDataToVOService excuteDataToVOService;
+    @Autowired
+    private  ExcuteUploadSettingService excuteUploadSettingService;
+    @Autowired
+    private  MessageService messageService;
+    @Autowired
+    private  TaskService taskService;
+
     private Queue<UploadParamsVO> queue = new LinkedList<>();
+    private  List<Integer> taskQueueList = new ArrayList<>();
     private Boolean flag = false;
-    private CountDownLatch latch = new CountDownLatch(10);
+    private CountDownLatch patentLatch = new CountDownLatch(10);
+    private CountDownLatch taskLatch = new CountDownLatch(10);
+    private static PantentQueueService pantentQueueService =new PantentQueueService();
+    //构造函数
+    private PantentQueueService(){
 
+    }
+    public static PantentQueueService getInstance(){
+        return pantentQueueService;
+    }
     //将专利信息存入队列
-    public void addPatnetToQueue(Task task, ProjectImportPatentVO projectImportPatentVO) throws InterruptedException {
+    public void addPatnetToQueue() {
         try {
+            //检查任务队列
+            if (taskQueueList.size() == 0) {
+                taskLatch.await();
+            }
+
+            //查找 taskQueueList 中有没有进行中的任务
+            long count = taskService.count(new LambdaQueryWrapper<Task>().in(Task::getId, taskQueueList).eq(Task::getStatus, 1));
+            //若没有,则取出第一个队列中的任务开始执行
+            Task task = null;
+            if (count == 0) {
+                task = taskService.getById(taskQueueList.get(0));
+                //任务队列去除该任务
+                taskQueueList.remove(0);
+                //修改该任务状态,改为进行中
+                Task currentTask = new Task();
+                currentTask.setId(task.getId());
+                currentTask.setStatus(1);
+                taskService.updateById(currentTask);
+            }
             //获得文件路径
             String filePath = task.getUrl();
             //检查文件合法性
             Integer totalRow = ReadExcelUtils.textExcel(filePath);
-            //根据数据来源id(如1:智慧芽)解析数据源配置文件信息
+            //调用解析数据类,根据数据来源id(如1:智慧芽)解析数据源配置文件信息
             List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(projectImportPatentVO.getSourceId());
             //遍历专利总数量,在循环中保存专利
             for (int i = 1; i <= totalRow; i++) {
                 //解析读取一行专利
                 PatentData patentData = ReadExcelUtils.readExcelOneRow(filePath, i);
-                //专利数据转换为VO对象
+                //调用装载数据类,专利数据转换为VO对象
                 UploadParamsVO uploadParamsVO = excuteDataToVOService.fileToPatentVO(patentData, jsonData);
                 //一个专利加入队列
                 queue.add(uploadParamsVO);
                 //通知消费者线程
-                latch.countDown();
-
+                patentLatch.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);
+                messageService.sendWebsocketMessage(task, totalRow, i, percentage);
             }
             //全部循环结束后,发送进度
             Long percentage = 100L;
-            messageService.sendWebsocketMessage(task, projectImportPatentVO, totalRow, totalRow, percentage);
-
-        } catch (IOException e) {
+            messageService.sendWebsocketMessage(task, totalRow, totalRow, percentage);
+        } catch (Exception e) {
             e.printStackTrace();
         } finally {
         }
@@ -80,7 +116,7 @@ public class PantentQueueService {
                         System.out.println("退出循环");
                         return;
                     } else {
-                        latch.await();
+                        patentLatch.await();
                     }
                 } else {
                     UploadParamsVO uploadParamsVO = queue.remove();
@@ -93,4 +129,13 @@ public class PantentQueueService {
         }
     }
 
+    public void setTaskQueueList(List<Integer> taskQueueList) {
+        this.taskQueueList = taskQueueList;
+    }
+
+    public void awakeTasktch(){
+        taskLatch.countDown();
+
+    }
+
 }

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

@@ -18,12 +18,15 @@ public class PatentToDbTaskJob extends QuartzJobBean {
 
     @Override
     public void executeInternal(JobExecutionContext context) throws JobExecutionException {
+        //0.检查全部任务情况
+
+
+        //1.获得任务信息(检查进行中的任务,执行等待中的任务)
 
-        //1.获得任务信息
 
         //2.根据不同的任务类型,选择不同方法上传任务
         //2.1 Excel导入专利任务
-        excuteTaskService.executeExcelTask();
+        //excuteTaskService.executeExcelTask();
 
         //2.2 网站导入专利任务
 

+ 23 - 10
PAS/src/main/java/cn/cslg/pas/service/upLoadPatent/UploadTaskService.java

@@ -1,46 +1,59 @@
 package cn.cslg.pas.service.upLoadPatent;
 
 import cn.cslg.pas.common.model.dto.UploadFileDTO;
-import cn.cslg.pas.common.model.vo.UploadParamsVO;
+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.service.TaskService;
+import cn.hutool.core.collection.IterUtil;
+import cn.hutool.poi.excel.ExcelUtil;
 import lombok.RequiredArgsConstructor;
-import org.apache.poi.ss.formula.functions.T;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 
 /**
- * @Author  李仁杰
+ * 导入专利任务的业务层
+ *
+ * @Author 李仁杰
  * 导入任务类
  */
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class UploadTaskService {
-
     private final FileUtils fileUtils;
     private final TaskService taskService;
-    private final ReadExcelUtils readExcelUtils;
 
     /**
      * 添加导入任务
      */
-    public void addTask(){
+    public void addTask(MultipartFile file, ProjectImportPatentVO projectImportPatentVO) throws IOException {
+        //将包含多件专利的Excel文件上传至本地,并返回文件对象fileDTO
+        UploadFileDTO fileDTO = fileUtils.uploadFile(file);
 
+        //获得文件路径
+        String filePath = fileUtils.getPath(fileDTO.getPath());
 
+        //获取专利总数量
+        Integer total = ReadExcelUtils.textExcel(filePath);
 
+        //新增任务(专利导入导出任务表)
+        taskService.add(fileDTO, projectImportPatentVO.getProjectId(), null, total, 1, 0, file.getOriginalFilename());
 
+    }
 
+    public void pauseTask() {
 
     }
 
-    public void pauseTask(){}
-
-    public void deleteTask(){}
+    public void deleteTask() {
+    }
 
 
 }