GetPatentFromExcelThread.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package cn.cslg.pas.service.importPatent;
  2. import cn.cslg.pas.common.dto.UploadPatentWebDTO;
  3. import cn.cslg.pas.common.model.cronModel.SystemFile;
  4. import cn.cslg.pas.common.utils.ReadExcelUtils;
  5. import cn.cslg.pas.common.vo.ImportTaskAMVO;
  6. import cn.cslg.pas.common.vo.PatentData;
  7. import cn.cslg.pas.common.vo.UploadParamsVO;
  8. import cn.cslg.pas.common.vo.UploadSettingVO;
  9. import cn.cslg.pas.domain.es.Patent;
  10. import cn.cslg.pas.domain.es.Text;
  11. import cn.cslg.pas.service.common.ExcuteDataToVOService;
  12. import cn.cslg.pas.service.common.ExcuteUploadSettingService;
  13. import cn.cslg.pas.service.common.FileManagerService;
  14. import com.alibaba.fastjson.JSONArray;
  15. import org.apache.commons.compress.utils.IOUtils;
  16. import org.apache.poi.ss.usermodel.Sheet;
  17. import org.springframework.beans.factory.annotation.Configurable;
  18. import org.springframework.context.ApplicationContext;
  19. import java.io.*;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. @Configurable
  24. public class GetPatentFromExcelThread extends Thread {
  25. private ImportTaskAMVO importTaskAMVO;
  26. private ApplicationContext applicationContext;
  27. private SavePatentToEsThread savePatentToEsThread;
  28. private GetPatentPictureFromExcelThread getPatentPictureFromExcelThread;
  29. @Override
  30. public void run() {
  31. try {
  32. FileManagerService fileManagerService = applicationContext.getBean(FileManagerService.class);
  33. String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(importTaskAMVO.getFileGuid()));
  34. List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
  35. SystemFile systemFile = systemFiles.get(0);
  36. String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
  37. //调用文件系统取出文件接口,获得文件流
  38. byte[] bytes = fileManagerService.downloadSystemFileFromFMS(importTaskAMVO.getFileGuid());
  39. //创建临时文件tempFile,并将文件读取到tempFile
  40. File tempFile = File.createTempFile(systemFile.getFileName() + "temp", suffix);
  41. try (
  42. InputStream inputStream = new ByteArrayInputStream(bytes);
  43. FileOutputStream outputStream = new FileOutputStream(tempFile)
  44. ) {
  45. IOUtils.copy(inputStream, outputStream); // 将输入流复制到临时文件
  46. }
  47. Integer total = importTaskAMVO.getAllNum();
  48. Integer lastIndex = importTaskAMVO.getDoneNum();
  49. Integer sourceId = importTaskAMVO.getSourceId();
  50. //解析数据源类,通过数据来源id(如1:智慧芽)解析数据源配置文件,获得数据源配置文件对象jsonData
  51. ExcuteDataToVOService excuteDataToVOService = applicationContext.getBean(ExcuteDataToVOService.class);
  52. ExcuteUploadSettingService excuteUploadSettingService = applicationContext.getBean(ExcuteUploadSettingService.class);
  53. List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(sourceId.toString());
  54. //解析Excel文件获得工作簿
  55. Sheet sheet = ReadExcelUtils.readExcel(tempFile);
  56. //遍历专利总数量,在循环中将专利一个一个存入消费者专利队列
  57. for (int i = lastIndex; i < total; i++) {
  58. if(!importTaskAMVO.getState().equals(1)){
  59. System.out.println(importTaskAMVO.getId());
  60. break;
  61. }
  62. PatentData patentData = ReadExcelUtils.readExcelOneRow(tempFile, sheet, i + 1);
  63. //调用装载数据类(根据数据源配置文件对象和专利源数据生成专利数据)
  64. UploadParamsVO uploadParamsVO = excuteDataToVOService.fileToPatentVO(patentData, jsonData);
  65. this.loadPatent(uploadParamsVO);
  66. UploadPatentWebDTO uploadPatentWebDTO =new UploadPatentWebDTO();
  67. uploadPatentWebDTO.setHaveSaveCounter(0);
  68. uploadPatentWebDTO.setTotalCounter(importTaskAMVO.getShouldSaveCounter());
  69. uploadPatentWebDTO.setPictureData(uploadParamsVO.getPictureData());
  70. uploadPatentWebDTO.setPatent(uploadParamsVO.getPatent());
  71. //专利丢入消费者队列,并唤醒消费者线程
  72. savePatentToEsThread.awakeTask(uploadPatentWebDTO.getPatent());
  73. getPatentPictureFromExcelThread.awakeTask(uploadPatentWebDTO);
  74. }
  75. //专利取完通知消费者线程
  76. savePatentToEsThread.setIfProductAll(true);
  77. getPatentPictureFromExcelThread.setIfProductAll(true);
  78. //删除临时文件tempFile
  79. new File(tempFile.getPath()).delete();
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. public GetPatentFromExcelThread(TaskThread taskThread, SavePatentToEsThread savePatentToEsThread, GetPatentPictureFromExcelThread getPatentPictureFromExcelThread) {
  85. this.importTaskAMVO = taskThread.getImportTaskAMVO();
  86. this.savePatentToEsThread = savePatentToEsThread;
  87. this.applicationContext = taskThread.getApplicationContext();
  88. this.getPatentPictureFromExcelThread=getPatentPictureFromExcelThread;
  89. }
  90. public void loadPatent(UploadParamsVO uploadParamsVO) {
  91. Patent patent = uploadParamsVO.getPatent();
  92. String patentNo =patent.getPatentNo();
  93. String contry =patentNo.substring(0,2);
  94. //装载标题
  95. if (uploadParamsVO.getTitle() != null) {
  96. List<Text> texts = new ArrayList<>();
  97. if (uploadParamsVO.getTitle().getName() != null) {
  98. Text text = new Text();
  99. text.setTextContent(uploadParamsVO.getTitle().getName());
  100. text.setIfOrigin(true);
  101. text.setLanguage(contry);
  102. texts.add(text);
  103. }
  104. if (uploadParamsVO.getTitle().getNameOut() != null) {
  105. Text text = new Text();
  106. text.setTextContent(uploadParamsVO.getTitle().getNameOut());
  107. text.setIfOrigin(false);
  108. text.setLanguage("CN");
  109. texts.add(text);
  110. }
  111. if (texts.size() != 0) {
  112. patent.setTitle(texts);
  113. }
  114. }
  115. //装载摘要
  116. if (uploadParamsVO.getAbstractStr() != null) {
  117. List<Text> texts = new ArrayList<>();
  118. if (uploadParamsVO.getAbstractStr().getName() != null) {
  119. Text text = new Text();
  120. text.setTextContent(uploadParamsVO.getAbstractStr().getName());
  121. text.setIfOrigin(true);
  122. text.setLanguage(contry);
  123. texts.add(text);
  124. }
  125. if (uploadParamsVO.getAbstractStr().getNameOut() != null) {
  126. Text text = new Text();
  127. text.setTextContent(uploadParamsVO.getAbstractStr().getNameOut());
  128. text.setIfOrigin(false);
  129. text.setLanguage("CN");
  130. texts.add(text);
  131. }
  132. if (texts.size() != 0) {
  133. patent.setAbstractStr(texts);
  134. }
  135. }
  136. //装载权利要求
  137. if (uploadParamsVO.getPatentRight() != null) {
  138. List<Text> texts = new ArrayList<>();
  139. if (uploadParamsVO.getPatentRight().getName() != null) {
  140. Text text = new Text();
  141. text.setTextContent(uploadParamsVO.getPatentRight().getName());
  142. text.setIfOrigin(true);
  143. text.setLanguage(contry);
  144. texts.add(text);
  145. }
  146. if (uploadParamsVO.getPatentRight().getNameOut() != null) {
  147. Text text = new Text();
  148. text.setTextContent(uploadParamsVO.getPatentRight().getNameOut());
  149. text.setIfOrigin(false);
  150. text.setLanguage("CN");
  151. texts.add(text);
  152. }
  153. if (texts.size() != 0) {
  154. patent.setClaim(texts);
  155. }
  156. }
  157. }
  158. }