GetPatentFromExcelThread.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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.dto.es.EsCustomFieldDTO;
  6. import cn.cslg.pas.common.model.cronModel.SystemFile;
  7. import cn.cslg.pas.common.model.importTaskModel.PatentAffair;
  8. import cn.cslg.pas.common.model.importTaskModel.PatentInventor;
  9. import cn.cslg.pas.common.utils.ReadExcelUtils;
  10. import cn.cslg.pas.common.utils.esDataForm.PatentClassifySplitter;
  11. import cn.cslg.pas.common.vo.ImportTaskAMVO;
  12. import cn.cslg.pas.common.vo.PatentData;
  13. import cn.cslg.pas.common.vo.UploadParamsVO;
  14. import cn.cslg.pas.common.vo.UploadSettingVO;
  15. import cn.cslg.pas.domain.es.*;
  16. import cn.cslg.pas.service.business.CustomFieldService;
  17. import cn.cslg.pas.service.business.es.EsService;
  18. import cn.cslg.pas.service.common.ExcuteDataToVOService;
  19. import cn.cslg.pas.service.common.ExcuteUploadSettingService;
  20. import cn.cslg.pas.service.common.FileManagerService;
  21. import com.alibaba.fastjson.JSONArray;
  22. import org.apache.commons.compress.utils.IOUtils;
  23. import org.apache.poi.ss.usermodel.Sheet;
  24. import org.springframework.beans.factory.annotation.Configurable;
  25. import org.springframework.context.ApplicationContext;
  26. import java.io.*;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. @Configurable
  31. public class GetPatentFromExcelThread extends Thread {
  32. private ImportTaskAMVO importTaskAMVO;
  33. private ApplicationContext applicationContext;
  34. private SavePatentToEsThread savePatentToEsThread;
  35. private GetPatentPictureFromExcelThread getPatentPictureFromExcelThread;
  36. @Override
  37. public void run() {
  38. try {
  39. FileManagerService fileManagerService = applicationContext.getBean(FileManagerService.class);
  40. String res = fileManagerService.getSystemFileFromFMS(Arrays.asList(importTaskAMVO.getFileGuid()));
  41. List<SystemFile> systemFiles = JSONArray.parseArray(res, SystemFile.class);
  42. SystemFile systemFile = systemFiles.get(0);
  43. String suffix = systemFile.getFileName().substring(systemFile.getFileName().lastIndexOf("."));
  44. //调用文件系统取出文件接口,获得文件流
  45. byte[] bytes = fileManagerService.downloadSystemFileFromFMS(importTaskAMVO.getFileGuid());
  46. //创建临时文件tempFile,并将文件读取到tempFile
  47. File tempFile = File.createTempFile(systemFile.getFileName() + "temp", suffix);
  48. try (
  49. InputStream inputStream = new ByteArrayInputStream(bytes);
  50. FileOutputStream outputStream = new FileOutputStream(tempFile)
  51. ) {
  52. IOUtils.copy(inputStream, outputStream); // 将输入流复制到临时文件
  53. }
  54. Integer total = importTaskAMVO.getAllNum();
  55. Integer lastIndex = importTaskAMVO.getDoneNum();
  56. Integer sourceId = importTaskAMVO.getSourceId();
  57. //解析数据源类,通过数据来源id(如1:智慧芽)解析数据源配置文件,获得数据源配置文件对象jsonData
  58. ExcuteDataToVOService excuteDataToVOService = applicationContext.getBean(ExcuteDataToVOService.class);
  59. ExcuteUploadSettingService excuteUploadSettingService = applicationContext.getBean(ExcuteUploadSettingService.class);
  60. List<UploadSettingVO.Column> jsonData = excuteUploadSettingService.ExcuteUploadSetting(sourceId.toString());
  61. //解析Excel文件获得工作簿
  62. Sheet sheet = ReadExcelUtils.readExcel(tempFile);
  63. Integer x = 0;
  64. //遍历专利总数量,在循环中将专利一个一个存入消费者专利队列
  65. for (int i = lastIndex; i < total; i++) {
  66. if (!importTaskAMVO.getState().equals(1)) {
  67. System.out.println(importTaskAMVO.getId());
  68. break;
  69. }
  70. PatentData patentData = ReadExcelUtils.readExcelOneRow(tempFile, sheet, i + 1);
  71. //调用装载数据类(根据数据源配置文件对象和专利源数据生成专利数据)
  72. UploadParamsVO uploadParamsVO = excuteDataToVOService.fileToPatentVO(patentData, jsonData);
  73. this.loadPatent(uploadParamsVO);
  74. UploadPatentWebDTO uploadPatentWebDTO = new UploadPatentWebDTO();
  75. uploadPatentWebDTO.setHaveSaveCounter(0);
  76. uploadPatentWebDTO.setTotalCounter(importTaskAMVO.getThreadCounter());
  77. uploadPatentWebDTO.setPictureData(uploadParamsVO.getPictureData());
  78. uploadPatentWebDTO.setPatent(uploadParamsVO.getPatent());
  79. if (uploadParamsVO.getCustomerFieldList() != null && uploadParamsVO.getCustomerFieldList().size() != 0) {
  80. CustomFieldService customFieldService = applicationContext.getBean(CustomFieldService.class);
  81. List<EsCustomFieldDTO> esCustomFieldDTOList = customFieldService.getFieldDTOByStrings(uploadParamsVO.getCustomerFieldList(), importTaskAMVO.getProjectId());
  82. uploadPatentWebDTO.setEsCustomFieldDTOList(esCustomFieldDTOList);
  83. }
  84. //专利丢入消费者队列,并唤醒消费者线程
  85. savePatentToEsThread.awakeTask(uploadPatentWebDTO);
  86. x++;
  87. getPatentPictureFromExcelThread.awakeTask(uploadPatentWebDTO);
  88. }
  89. //专利取完通知消费者线程
  90. savePatentToEsThread.setIfProductAll(true);
  91. System.out.println("从excel获取专利:" + x);
  92. getPatentPictureFromExcelThread.setIfProductAll(true);
  93. //删除临时文件tempFile
  94. new File(tempFile.getPath()).delete();
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. public GetPatentFromExcelThread(TaskThread taskThread, SavePatentToEsThread savePatentToEsThread, GetPatentPictureFromExcelThread getPatentPictureFromExcelThread) {
  100. this.importTaskAMVO = taskThread.getImportTaskAMVO();
  101. this.savePatentToEsThread = savePatentToEsThread;
  102. this.applicationContext = taskThread.getApplicationContext();
  103. this.getPatentPictureFromExcelThread = getPatentPictureFromExcelThread;
  104. }
  105. public void loadPatent(UploadParamsVO uploadParamsVO) {
  106. Patent patent = uploadParamsVO.getPatent();
  107. String patentNo = patent.getPatentNo();
  108. String appNo = patent.getAppNo();
  109. String publicNo = patent.getPublicNo();
  110. String grantNo = patent.getGrantNo();
  111. String usedNo = null;
  112. if (appNo != null) {
  113. usedNo = appNo;
  114. } else if (publicNo != null) {
  115. usedNo = publicNo;
  116. } else if (grantNo != null) {
  117. usedNo = grantNo;
  118. }
  119. String contry = usedNo.substring(0, 2);
  120. if (contry.equals("CN")) {
  121. patentNo = appNo;
  122. } else {
  123. if (publicNo != null) {
  124. patentNo = publicNo;
  125. } else if (grantNo != null) {
  126. patentNo = grantNo;
  127. }
  128. }
  129. patent.setPatentNo(patentNo);
  130. //装载国家
  131. patent.setAppCountry(contry);
  132. //装载标题
  133. if (uploadParamsVO.getTitle() != null) {
  134. List<Text> texts = new ArrayList<>();
  135. if (uploadParamsVO.getTitle().getName() != null) {
  136. Text text = new Text();
  137. text.setTextContent(uploadParamsVO.getTitle().getName());
  138. text.setIfOrigin(true);
  139. text.setLanguage(contry);
  140. texts.add(text);
  141. }
  142. if (uploadParamsVO.getTitle().getNameOut() != null) {
  143. Text text = new Text();
  144. text.setTextContent(uploadParamsVO.getTitle().getNameOut());
  145. text.setIfOrigin(false);
  146. text.setLanguage("CN");
  147. texts.add(text);
  148. }
  149. if (texts.size() != 0) {
  150. patent.setTitle(texts);
  151. }
  152. }
  153. //装载摘要
  154. if (uploadParamsVO.getAbstractStr() != null) {
  155. List<Text> texts = new ArrayList<>();
  156. if (uploadParamsVO.getAbstractStr().getName() != null) {
  157. Text text = new Text();
  158. text.setTextContent(uploadParamsVO.getAbstractStr().getName());
  159. text.setIfOrigin(true);
  160. text.setLanguage(contry);
  161. texts.add(text);
  162. }
  163. if (uploadParamsVO.getAbstractStr().getNameOut() != null) {
  164. Text text = new Text();
  165. text.setTextContent(uploadParamsVO.getAbstractStr().getNameOut());
  166. text.setIfOrigin(false);
  167. text.setLanguage("CN");
  168. texts.add(text);
  169. }
  170. if (texts.size() != 0) {
  171. patent.setAbstractStr(texts);
  172. }
  173. }
  174. //装载权利要求
  175. if (uploadParamsVO.getPatentRight() != null) {
  176. List<Text> texts = new ArrayList<>();
  177. if (uploadParamsVO.getPatentRight().getName() != null) {
  178. Text text = new Text();
  179. text.setTextContent(uploadParamsVO.getPatentRight().getName());
  180. text.setIfOrigin(true);
  181. text.setLanguage(contry);
  182. texts.add(text);
  183. }
  184. if (uploadParamsVO.getPatentRight().getNameOut() != null) {
  185. Text text = new Text();
  186. text.setTextContent(uploadParamsVO.getPatentRight().getNameOut());
  187. text.setIfOrigin(false);
  188. text.setLanguage("CN");
  189. texts.add(text);
  190. }
  191. if (texts.size() != 0) {
  192. patent.setClaim(texts);
  193. }
  194. }
  195. //简单同族
  196. if (uploadParamsVO.getSimpleFamily() != null) {
  197. EsService esService = applicationContext.getBean(EsService.class);
  198. try {
  199. EsPatentFamilyDTO esPatentFamilyDTO = esService.addEsPatentFamily(patent, uploadParamsVO.getSimpleFamily(), FamilyType.SIMPLE);
  200. if (esPatentFamilyDTO != null) {
  201. patent.setSimpleFamilyId(esPatentFamilyDTO.getPatentFamilyId());
  202. patent.setSimpleFamilyNum(esPatentFamilyDTO.getFamilyNum());
  203. }
  204. } catch (Exception e) {
  205. }
  206. }
  207. //inpadapc同族
  208. if (uploadParamsVO.getInpadocFamily() != null) {
  209. EsService esService = applicationContext.getBean(EsService.class);
  210. try {
  211. EsPatentFamilyDTO esPatentFamilyDTO = esService.addEsPatentFamily(patent, uploadParamsVO.getSimpleFamily(), FamilyType.INPADOC);
  212. if (esPatentFamilyDTO != null) {
  213. patent.setInpadocFamilyId(esPatentFamilyDTO.getPatentFamilyId());
  214. patent.setInpadocFamilyNum(esPatentFamilyDTO.getFamilyNum());
  215. }
  216. } catch (Exception e) {
  217. }
  218. }
  219. //patsnap同族
  220. if (uploadParamsVO.getPatSnapFamily() != null) {
  221. EsService esService = applicationContext.getBean(EsService.class);
  222. try {
  223. EsPatentFamilyDTO esPatentFamilyDTO = esService.addEsPatentFamily(patent, uploadParamsVO.getSimpleFamily(), FamilyType.PATSNAP);
  224. if (esPatentFamilyDTO != null) {
  225. patent.setPatsnapFamilyId(esPatentFamilyDTO.getPatentFamilyId());
  226. patent.setPatsnapFamilyNum(esPatentFamilyDTO.getFamilyNum());
  227. }
  228. } catch (Exception e) {
  229. }
  230. }
  231. List<String> applicantNames = uploadParamsVO.getPatentApplicantOriginalName();
  232. //申请人
  233. if (applicantNames != null && applicantNames.size() != 0) {
  234. List<PatentPerson> patentPeople = new ArrayList<>();
  235. for (int i = 0; i < applicantNames.size(); i++) {
  236. PatentPerson patentPerson = new PatentPerson();
  237. patentPerson.setOrder(i + 1);
  238. patentPerson.setType("1");
  239. patentPerson.setName(applicantNames.get(i));
  240. patentPeople.add(patentPerson);
  241. }
  242. patent.setApplicant(patentPeople);
  243. }
  244. //权利人
  245. List<String> rightNames = uploadParamsVO.getPatentApplicantCurrentName();
  246. if (rightNames != null && rightNames.size() != 0) {
  247. List<PatentPerson> patentPeople = new ArrayList<>();
  248. for (int i = 0; i < rightNames.size(); i++) {
  249. PatentPerson patentPerson = new PatentPerson();
  250. patentPerson.setOrder(i + 1);
  251. patentPerson.setType("1");
  252. patentPerson.setName(rightNames.get(i));
  253. patentPeople.add(patentPerson);
  254. }
  255. patent.setRightHolder(patentPeople);
  256. }
  257. //发明人
  258. List<PatentInventor> patentInventorList = uploadParamsVO.getPatentInventorList();
  259. if (patentInventorList != null && patentInventorList.size() > 0) {
  260. List<PatentPerson> patentPeople = new ArrayList<>();
  261. for (int i = 0; i < patentInventorList.size(); i++) {
  262. PatentPerson patentPerson = new PatentPerson();
  263. patentPerson.setOrder(i + 1);
  264. patentPerson.setType("1");
  265. patentPerson.setName(patentInventorList.get(i).getName());
  266. patentPeople.add(patentPerson);
  267. }
  268. patent.setInventor(patentPeople);
  269. }
  270. List<String> ipcList = uploadParamsVO.getIpcList();
  271. String mainIpc = uploadParamsVO.getMainIpc();
  272. //装载IPC分类号
  273. if (ipcList == null && mainIpc != null) {
  274. ipcList = new ArrayList<>();
  275. ipcList.add(uploadParamsVO.getMainIpc());
  276. }
  277. if (ipcList != null && ipcList.size() > 0) {
  278. List<PatentClassify> patentClassifies = new ArrayList<>();
  279. for (int i = 0; i < ipcList.size(); i++) {
  280. PatentClassify patentClassify = PatentClassifySplitter.split(ipcList.get(i));
  281. if (patentClassify != null) {
  282. patentClassifies.add(patentClassify);
  283. if (i == 0) {
  284. patent.setMipc(patentClassify);
  285. }
  286. }
  287. }
  288. patent.setIpc(patentClassifies);
  289. }
  290. //装载CPC分类号
  291. List<String> cpcList = uploadParamsVO.getCpcList();
  292. if (cpcList != null && cpcList.size() > 0) {
  293. List<PatentClassify> patentClassifies = new ArrayList<>();
  294. for (int i = 0; i < cpcList.size(); i++) {
  295. PatentClassify patentClassify = PatentClassifySplitter.split(cpcList.get(i));
  296. patentClassifies.add(patentClassify);
  297. }
  298. patent.setCpc(patentClassifies);
  299. }
  300. //装载说明书
  301. if (uploadParamsVO.getPatentInstructionText() != null) {
  302. List<Text> texts = new ArrayList<>();
  303. if (uploadParamsVO.getPatentInstructionText().getManual() != null) {
  304. Text text = new Text();
  305. text.setTextContent(uploadParamsVO.getPatentInstructionText().getManual());
  306. text.setIfOrigin(true);
  307. text.setLanguage(contry);
  308. texts.add(text);
  309. }
  310. if (uploadParamsVO.getPatentInstructionText().getManualOut() != null) {
  311. Text text = new Text();
  312. text.setTextContent(uploadParamsVO.getPatentInstructionText().getManualOut());
  313. text.setIfOrigin(false);
  314. text.setLanguage("CN");
  315. texts.add(text);
  316. }
  317. if (texts.size() != 0) {
  318. patent.setPublicFullText(texts);
  319. }
  320. }
  321. //装载优先权
  322. if (uploadParamsVO.getPriorityNo() != null && uploadParamsVO.getPriorityNo().size() > 0) {
  323. List<String> prDates = uploadParamsVO.getPriorityDate();
  324. List<Priorities> priorities = new ArrayList<>();
  325. for (int i = 0; i < uploadParamsVO.getPriorityNo().size(); i++) {
  326. String prNo = uploadParamsVO.getPriorityNo().get(i);
  327. Priorities prioritie = new Priorities();
  328. prioritie.setPriorityNo(prNo);
  329. String pCountry = prNo.substring(0, 2);
  330. prioritie.setPriorityCountry(pCountry);
  331. if (prDates != null && prDates.size() > i) {
  332. prioritie.setPriorityDate(prDates.get(i));
  333. }
  334. priorities.add(prioritie);
  335. }
  336. patent.setPriorities(priorities);
  337. }
  338. this.loadLegalStatus(patent);
  339. this.loadPatentType(patent);
  340. this.loadLegalStatusBySta(uploadParamsVO);
  341. }
  342. /**
  343. * 装载法律状态
  344. */
  345. private void loadLegalStatus(Patent patent) {
  346. if (patent != null && patent.getSimpleStatus() != null) {
  347. String status = patent.getSimpleStatus().trim();
  348. if (status != null) {
  349. switch (status) {
  350. case "有效":
  351. patent.setSimpleStatus("3");
  352. break;
  353. case "审中":
  354. patent.setSimpleStatus("1");
  355. case "失效":
  356. patent.setSimpleStatus("2");
  357. }
  358. }
  359. }
  360. }
  361. private void loadLegalStatusBySta(UploadParamsVO uploadParamsVO) {
  362. Patent patent =uploadParamsVO.getPatent();
  363. if (uploadParamsVO != null && uploadParamsVO.getPatentAffair() != null && uploadParamsVO.getPatentAffair().getStatus()!=null&&patent.getSimpleStatus()==null) {
  364. String status = uploadParamsVO.getPatentAffair().getStatus().split("\\|")[0];
  365. if (status != null) {
  366. switch (status) {
  367. case "公开":
  368. case "授权":
  369. patent.setSimpleStatus("3");
  370. break;
  371. case "实质审查":
  372. patent.setSimpleStatus("1");
  373. break;
  374. default:
  375. patent.setSimpleStatus("3");
  376. }
  377. }
  378. }
  379. }
  380. private void loadPatentType(Patent patent) {
  381. String type = patent.getPatentType();
  382. if (type != null) {
  383. type = type.trim();
  384. switch (type) {
  385. case "授权发明":
  386. case "发明申请":
  387. type = "1";
  388. break;
  389. case "实用新型":
  390. type = "2";
  391. break;
  392. case "外观设计":
  393. type = "3";
  394. break;
  395. default:
  396. type = null;
  397. break;
  398. }
  399. patent.setPatentType(type);
  400. }
  401. }
  402. }