GetPatentFromExcelThread.java 9.9 KB

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