|
@@ -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();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|