ExcutePatentDataStar.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. package cn.cslg.pas.service.upLoadPatent;
  2. import cn.cslg.pas.common.model.dto.UploadFileDTO;
  3. import cn.cslg.pas.common.model.outApi.PatentStarListDto;
  4. import cn.cslg.pas.common.model.vo.KeyValueVO;
  5. import cn.cslg.pas.common.model.vo.UploadParamsVO;
  6. import cn.cslg.pas.common.model.vo.outApi.StarPatentVO;
  7. import cn.cslg.pas.common.utils.FileUtils;
  8. import cn.cslg.pas.common.utils.RemoveHtmlTagsUtils;
  9. import cn.cslg.pas.common.utils.ThrowException;
  10. import cn.cslg.pas.domain.*;
  11. import cn.cslg.pas.service.TaskService;
  12. import cn.cslg.pas.service.UploadPatentBatchService;
  13. import cn.cslg.pas.service.outApi.PatentStarApiService;
  14. import com.alibaba.fastjson.JSON;
  15. import com.alibaba.fastjson2.JSONObject;
  16. import lombok.RequiredArgsConstructor;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.fileupload.FileItem;
  19. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  24. import java.io.*;
  25. import java.net.URL;
  26. import java.sql.Timestamp;
  27. import java.text.DateFormat;
  28. import java.text.ParseException;
  29. import java.text.SimpleDateFormat;
  30. import java.util.*;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. /**
  34. * 专利之星类型任务解析获取专利类
  35. *
  36. * @Author chenyu
  37. * @Date 2023/6/25
  38. */
  39. @Slf4j
  40. @RequiredArgsConstructor
  41. @Service
  42. public class ExcutePatentDataStar implements IExcutePatentData {
  43. private final PatentStarApiService patentStarApiService;
  44. private final FileUtils fileUtils;
  45. private final PantentQueueService pantentQueueService;
  46. private final UploadPatentBatchService uploadPatentBatchService;
  47. private final TaskService taskService;
  48. private Integer pTaskId = 0;
  49. private Integer pTaskStatus = 0;
  50. /**
  51. * 解析获取专利数据
  52. *
  53. * @param task 任务
  54. */
  55. @Override
  56. public void startExcute(Task task) throws IOException {
  57. try {
  58. Integer total = task.getTotal(); //总条数
  59. Integer successNum = task.getSuccessNum(); //成功条数(即上一次下载到的位置)
  60. String conditions = task.getConditions(); //检索式
  61. String orderBy = task.getOrderBy(); //排序字段
  62. String orderByType = task.getOrderByType(); //排序类型
  63. String dbType = task.getDBType(); //检索数据库类型
  64. List<String> cells = task.getConfigCells(); //下载字段
  65. Integer startNumber = task.getStartNumber(); //下载起始条数位置
  66. Integer endNumber = task.getEndNumber(); //下载终止条数位置
  67. List<String> isAddPatentNos = task.getIsAddPatentNos(); //需要额外下载的专利号
  68. List<String> isDeletePatentNos = task.getIsDeletePatentNos(); //起止条数中不需要下载的专利号
  69. //startToEndNum:起止条数中需要下载的专利总数量
  70. int startToEndNum = 0;
  71. if (endNumber > 0) {
  72. startToEndNum = endNumber - startNumber + 1;
  73. if (isDeletePatentNos != null && isDeletePatentNos.size() > 0) {
  74. startToEndNum = startToEndNum - isDeletePatentNos.size();
  75. }
  76. }
  77. //若有完成条数,即表示此次任务为暂停后的继续执行,则将起始条数 startNumber重置为成功条数 + 1
  78. if (successNum > 0) {
  79. startNumber = successNum + 1;
  80. }
  81. //若有起止条数,并且完成条数在起止条数内(即起止条数的专利还没有下载完)
  82. if (endNumber > 0 && startNumber <= endNumber && successNum < startToEndNum) {
  83. int startPage; //检索开始页数
  84. int startNum; //检索开始页数的开始专利位置
  85. int endPage; //检索结束页数
  86. int endNum; //检索结束页数的结束专利位置
  87. if (startNumber % 50 != 0) {
  88. startPage = startNumber / 50 + 1;
  89. startNum = startNumber % 50;
  90. } else {
  91. startPage = startNumber / 50;
  92. startNum = 50;
  93. }
  94. if (endNumber % 50 != 0) {
  95. endPage = endNumber / 50 + 1;
  96. endNum = endNumber % 50;
  97. } else {
  98. endPage = endNumber / 50;
  99. endNum = 50;
  100. }
  101. //根据计算出的起止页数,一页一页检索
  102. int pageNum = startPage;
  103. for (int i = startPage; i <= endPage; i++) {
  104. PatentStarListDto patentStarListDto = new PatentStarListDto()
  105. .setCurrentQuery(conditions)
  106. .setOrderBy(orderBy)
  107. .setOrderByType(orderByType)
  108. .setPageNum(pageNum++)
  109. .setRowCount(50)
  110. .setDBType(dbType);
  111. //调用一般接口返回一批专利著录相关数据
  112. Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDto);
  113. if (resultMap == null || (Integer) resultMap.get("total") == 0) {
  114. ThrowException.throwXiaoShiException("未检索到相关专利");
  115. }
  116. List<StarPatentVO> starPatents = (List<StarPatentVO>) resultMap.get("records");
  117. //遍历这一页的专利
  118. for (int j = 0; j < starPatents.size(); j++) {
  119. //若任务状态为已暂停,则结束生产
  120. if (pTaskId.equals(task.getId()) && pTaskStatus == 4) {
  121. return;
  122. }
  123. //若还没到开始页的开始位置,则跳过,不下载它
  124. if (i == startPage && j < startNum - 1) {
  125. continue;
  126. }
  127. //若到了结束页的结束位置
  128. if (i == endPage && j == endNum) {
  129. break;
  130. }
  131. //若当前遍历到的专利号是需要删除的专利,则跳过,不下载它
  132. if (isDeletePatentNos != null && isDeletePatentNos.size() > 0 && isDeletePatentNos.contains(starPatents.get(j).getApplicationNo())) {
  133. continue;
  134. }
  135. UploadParamsVO uploadParamsVO = new UploadParamsVO();
  136. Patent patent = new Patent();
  137. uploadParamsVO.setPatent(patent);
  138. uploadParamsVO.getPatent().setPatentNo(starPatents.get(j).getPatentNo());
  139. uploadPatentBatchService.getOneOrInsertOne(uploadParamsVO);
  140. PQueueData pQueueData = new PQueueData()
  141. .setTask(task)
  142. .setStarPatent(starPatents.get(j))
  143. .setUploadParamsVO(uploadParamsVO);
  144. //专利丢入著录生产者队列,并唤醒著录生产者线程
  145. if (cells.contains("1")) {
  146. pantentQueueService.zhuluToPQueue(pQueueData);
  147. }
  148. //专利丢入权要生产者队列,并唤醒权要生产者线程
  149. if (cells.contains("2")) {
  150. pantentQueueService.rightToPQueue(pQueueData);
  151. }
  152. //专利丢入说明书文本生产者队列,并唤醒说明书文本生产者线程
  153. if (cells.contains("3")) {
  154. pantentQueueService.instructionTextToPQueue(pQueueData);
  155. }
  156. //专利丢入说明书pdf生产者队列,并唤醒说明书pdf生产者线程
  157. if (cells.contains("4")) {
  158. pantentQueueService.instructionPDFToPQueue(pQueueData);
  159. }
  160. //专利丢入摘要附图生产者队列,并唤醒摘要附图生产者线程
  161. if (cells.contains("6")) {
  162. pantentQueueService.imageToPQueue(pQueueData);
  163. }
  164. }
  165. }
  166. }
  167. //若是暂停后继续的任务,若 successNum > startToEndNum,即上一次执行不仅下载完了起止条数中的专利,还下载了isAdd中的部分专利
  168. int fromIndex = 0;
  169. if (successNum > 0 && successNum > startToEndNum) {
  170. fromIndex = successNum - startToEndNum;
  171. }
  172. //下载isAdd中的专利 / 下载多个专利号导入接口的专利 ↓
  173. if (isAddPatentNos != null && isAddPatentNos.size() > 0) {
  174. //fromIndex > 0 表示上一次执行的下载已经下载了isAdd中的部分专利,此次需要下载剩余专利
  175. if (fromIndex > 0) {
  176. isAddPatentNos = isAddPatentNos.subList(fromIndex, isAddPatentNos.size() - 1);
  177. }
  178. //若是检索页面网站导入,则isAddPatentNos是前台传的多个申请号,即包含"."
  179. if (isAddPatentNos.get(0).contains(".")) {
  180. downLoadIsAddPatentNos(isAddPatentNos, task, orderBy, orderByType, dbType, cells, null);
  181. } else {
  182. //若是多个专利号导入的下载,则isAddPatentNos是多个专利号,不包含"."
  183. downLoadIsAddPatentNos(isAddPatentNos, task, orderBy, orderByType, dbType, cells, conditions);
  184. }
  185. }
  186. } catch (IOException e) {
  187. e.printStackTrace();
  188. //生产消费到一半时,发生错误异常,将任务状态置为完成
  189. task = taskService.getById(task.getId());
  190. if (task.getStatus().equals(1)) {
  191. task.setStatus(2);
  192. taskService.updateById(task);
  193. }
  194. }
  195. }
  196. /**
  197. * 装载著录方法
  198. *
  199. * @param starPatent 专利之星著录对象
  200. * @param uploadParamsVO 专利实体类对象
  201. */
  202. public void setPatentZhuLu(StarPatentVO starPatent, UploadParamsVO uploadParamsVO) {
  203. //以下 ↓装载的是调用"一般检索"接口返回的专利相关数据
  204. //Patent patent = new Patent();
  205. //装载专利号
  206. uploadParamsVO.getPatent().setPatentNo(starPatent.getPatentNo());
  207. //装载摘要
  208. uploadParamsVO.getPatent().setAbstractStr(starPatent.getAbstractStr());
  209. //装载标题
  210. uploadParamsVO.getPatent().setName(starPatent.getName());
  211. //装载申请号
  212. uploadParamsVO.getPatent().setApplicationNo(starPatent.getApplicationNo());
  213. //装载申请日
  214. if (starPatent.getApplicationDate() != null && !starPatent.getApplicationDate().equals("")) {
  215. int timeStamp = stringDateToTimeStamp(starPatent.getApplicationDate());
  216. uploadParamsVO.getPatent().setApplicationDate(timeStamp);
  217. }
  218. //装载公开号
  219. uploadParamsVO.getPatent().setPublicNo(starPatent.getPublicNo());
  220. //装载公开日
  221. if (starPatent.getPublicDate() != null && !starPatent.getPublicDate().equals("")) {
  222. int timeStamp = stringDateToTimeStamp(starPatent.getPublicDate());
  223. uploadParamsVO.getPatent().setPublicDate(timeStamp);
  224. }
  225. //装载申请人
  226. if (starPatent.getApplicantStr() != null && !starPatent.getApplicantStr().equals("")) {
  227. uploadParamsVO.setPatentApplicantOriginalName(Arrays.asList(starPatent.getApplicantStr().split(";")));
  228. }
  229. //装载权利人
  230. if (starPatent.getCurrentApplicantStr() != null && !starPatent.getCurrentApplicantStr().equals("")) {
  231. uploadParamsVO.setPatentApplicantCurrentName(Arrays.asList(starPatent.getCurrentApplicantStr().split(";")));
  232. }
  233. //装载IPC分类号
  234. if (starPatent.getIpcListStr() != null && !starPatent.getIpcListStr().equals("")) {
  235. String[] ipcArr = starPatent.getIpcListStr().split(";");
  236. //装载主分类号
  237. uploadParamsVO.setMainIpc(ipcArr[0]);
  238. uploadParamsVO.setIpcList(Arrays.asList(ipcArr));
  239. }
  240. //装载法律状态(os_patent表status字段值)
  241. Integer status = starPatent.getLG();
  242. if (status != null) {
  243. //有效(有权)
  244. if (status == 1) {
  245. uploadParamsVO.getPatent().setSimpleStatus(3);
  246. //失效(无权)
  247. } else if (status == 2) {
  248. uploadParamsVO.getPatent().setSimpleStatus(2);
  249. //审中(审中)
  250. } else if (status == 3) {
  251. uploadParamsVO.getPatent().setSimpleStatus(1);
  252. }
  253. }
  254. uploadParamsVO.setPatentSimpleStatus(starPatent.getSimpleStatus());
  255. uploadParamsVO.setSimpleStatus(starPatent.getSimpleStatus());
  256. //以下 ↓装载的是调用"获得中国专利著录"接口返回的专利相关数据
  257. String appNo;
  258. if (starPatent.getApplicationNo().contains(".")) {
  259. appNo = starPatent.getApplicationNo().substring(0, starPatent.getApplicationNo().lastIndexOf("."));
  260. } else {
  261. appNo = starPatent.getApplicationNo();
  262. }
  263. //调用中国专利著录接口返回的专利相关数据最外层是一个集合"[]",但是集合中只有一个对象"{}",以下方式处理
  264. String chinaPatentZhuLuStr = patentStarApiService.getCnBibApi(appNo);
  265. if (chinaPatentZhuLuStr != null && !chinaPatentZhuLuStr.trim().equals("") && !chinaPatentZhuLuStr.equals("{}")) {
  266. List<ChinaPatentZhuLu> chinaPatentZhuLus = JSON.parseArray(chinaPatentZhuLuStr, ChinaPatentZhuLu.class);
  267. ChinaPatentZhuLu chinaPatentZhuLu = chinaPatentZhuLus.get(0);
  268. //装载申请人地址
  269. if (chinaPatentZhuLu.getDZ() != null && !chinaPatentZhuLu.getDZ().equals("")) {
  270. ArrayList<String> patentApplicantOriginalAddresss = new ArrayList<>();
  271. if (chinaPatentZhuLu.getDZ().contains(" ")) {
  272. patentApplicantOriginalAddresss.add(chinaPatentZhuLu.getDZ().substring(chinaPatentZhuLu.getDZ().indexOf(" ") + 1));
  273. } else {
  274. patentApplicantOriginalAddresss.add(chinaPatentZhuLu.getDZ());
  275. }
  276. uploadParamsVO.setPatentApplicantOriginalAddress(patentApplicantOriginalAddresss);
  277. }
  278. //装载代理人
  279. if (chinaPatentZhuLu.getAT() != null && !chinaPatentZhuLu.getAT().equals("")) {
  280. List<String> patentAgents = Arrays.asList(chinaPatentZhuLu.getAT().split(";"));
  281. ArrayList<PatentAgent> patentAgentList = new ArrayList<>();
  282. for (String n : patentAgents) {
  283. PatentAgent patentAgent = new PatentAgent();
  284. patentAgent.setName(n);
  285. patentAgentList.add(patentAgent);
  286. }
  287. uploadParamsVO.setPatentAgentList(patentAgentList);
  288. }
  289. //装载代理机构地址
  290. if (chinaPatentZhuLu.getAGN() != null && !chinaPatentZhuLu.getAGN().equals("")) {
  291. String agencyAddress;
  292. if (chinaPatentZhuLu.getAGN().contains(" ")) {
  293. agencyAddress = chinaPatentZhuLu.getAGN().substring(0, chinaPatentZhuLu.getAGN().lastIndexOf(" "));
  294. } else {
  295. agencyAddress = chinaPatentZhuLu.getAGN();
  296. }
  297. uploadParamsVO.getPatent().setAgencyId(agencyAddress);
  298. }
  299. //装载发明人
  300. if (chinaPatentZhuLu.getIV() != null && !chinaPatentZhuLu.getIV().equals("")) {
  301. List<String> patentInventorNames = Arrays.asList(chinaPatentZhuLu.getIV().split(";"));
  302. ArrayList<PatentInventor> patentInventors = new ArrayList<>();
  303. for (String patentInventorName : patentInventorNames) {
  304. PatentInventor patentInventor = new PatentInventor();
  305. patentInventor.setName(patentInventorName);
  306. patentInventors.add(patentInventor);
  307. }
  308. uploadParamsVO.setPatentInventorList(patentInventors);
  309. }
  310. //装载优先权号、优先权国家、优先权日
  311. uploadParamsVO.getPatent().setPriorityNo(chinaPatentZhuLu.getPR());
  312. }
  313. //以下 ↓装载的是调用"获得同族专利"接口返回的专利相关数据
  314. if (starPatent.getPatentNo() != null && !starPatent.getPatentNo().equals("")) {
  315. String familyPatentNoStr = patentStarApiService.getFamilyByPubNoApi(starPatent.getPatentNo());
  316. if (!familyPatentNoStr.equals("no data")) {
  317. FamilyPatentNo familyPatentNo = JSON.parseObject(familyPatentNoStr, FamilyPatentNo.class);
  318. //装载同族号
  319. if (familyPatentNo.getFamilyinfo() != null && !familyPatentNo.getFamilyinfo().equals("")) {
  320. List<String> simpleFamily = Arrays.asList(familyPatentNo.getFamilyinfo().split(";"));
  321. uploadParamsVO.setSimpleFamily(simpleFamily);
  322. }
  323. }
  324. }
  325. //以下 ↓装载的是调用"获得中国专利法律状态"接口返回的专利相关数据
  326. PatentAffair patentAffair = new PatentAffair();
  327. String cnLegalApiStr = patentStarApiService.getCnLegalApi(appNo);
  328. List<ChinaLeagalStatus> chinaLeagalStatuses = JSON.parseArray(cnLegalApiStr, ChinaLeagalStatus.class);
  329. if (chinaLeagalStatuses != null && chinaLeagalStatuses.size() > 0) {
  330. ArrayList<KeyValueVO> lst = new ArrayList<>();
  331. HashMap<Integer, String> nameMap = new HashMap<>();
  332. HashMap<Integer, String> contentMap = new HashMap<>();
  333. nameMap.put(0, "发生日期");
  334. nameMap.put(1, "法律状态");
  335. nameMap.put(2, "详细法律状态");
  336. nameMap.put(3, "详细信息");
  337. for (int i = 0; i < chinaLeagalStatuses.size(); i++) {
  338. if (i == 0) {
  339. patentAffair.setStatus(chinaLeagalStatuses.get(i).getLegalStatus());
  340. }
  341. contentMap.put(0, chinaLeagalStatuses.get(i).getLegalDate());
  342. contentMap.put(1, chinaLeagalStatuses.get(i).getLegalStatus());
  343. contentMap.put(2, chinaLeagalStatuses.get(i).getLegalStatusInfo());
  344. contentMap.put(3, chinaLeagalStatuses.get(i).getDETAIL());
  345. ArrayList<KeyValueVO.InpadocData> inpadocDatas = new ArrayList<>();
  346. for (int j = 0; j < 4; j++) {
  347. KeyValueVO.InpadocData inpadocData = new KeyValueVO.InpadocData();
  348. inpadocData.setName(nameMap.get(j));
  349. inpadocData.setContent(contentMap.get(j));
  350. inpadocDatas.add(inpadocData);
  351. }
  352. KeyValueVO keyValueVO = new KeyValueVO();
  353. keyValueVO.setInpadocData(inpadocDatas);
  354. lst.add(keyValueVO);
  355. }
  356. //装载事务表的status(公开|授权|驳回|权力转移。。。)
  357. String content = JSONObject.toJSONString(lst);
  358. patentAffair.setContent(content);
  359. uploadParamsVO.setPatentAffair(patentAffair);
  360. }
  361. }
  362. /**
  363. * 装载权要方法
  364. *
  365. * @param starPatent 专利之星著录对象
  366. * @param uploadParamsVO 专利实体类对象
  367. */
  368. public void setPatentClaim(StarPatentVO starPatent, UploadParamsVO uploadParamsVO) {
  369. try {
  370. String appNo;
  371. if (starPatent.getApplicationNo().contains(".")) {
  372. appNo = starPatent.getApplicationNo().substring(0, starPatent.getApplicationNo().lastIndexOf("."));
  373. } else {
  374. appNo = starPatent.getApplicationNo();
  375. }
  376. //根据申请号调用"获得中国专利全文文本"接口,获得包含各种xml标签的专利全文内容的长字符串 cnFullXmlStr
  377. String cnFullXmlStr = patentStarApiService.getCnFullXmlApi(appNo);
  378. //使用正则表达式拼接出权要原文
  379. String regex = "(?<=<claim id=.{1,110}>)[\\w\\W]+?(?=</claim>)";
  380. Pattern compile = Pattern.compile(regex);
  381. Matcher matcher = compile.matcher(cnFullXmlStr);
  382. StringBuilder builder = new StringBuilder();
  383. while (matcher.find()) {
  384. String right = matcher.group().replaceAll("<.*?>", "");
  385. builder.append(right).append("\r\n");
  386. }
  387. String patentRightText = builder + "";
  388. //使用工具类去除字符串文本中的所有HTML格式标签
  389. patentRightText = RemoveHtmlTagsUtils.removeHtmlTags(patentRightText);
  390. //装载权利要求原文
  391. PatentRight patentRight = new PatentRight();
  392. patentRight.setContent(patentRightText);
  393. uploadParamsVO.setPatentRight(patentRight);
  394. } catch (Exception e) {
  395. e.printStackTrace();
  396. }
  397. }
  398. /**
  399. * 装载说明书文本
  400. *
  401. * @param starPatent 专利之星著录对象
  402. * @param uploadParamsVO 专利实体类对象
  403. */
  404. public void setPatentInstructionText(StarPatentVO starPatent, UploadParamsVO uploadParamsVO) {
  405. try {
  406. String appNo;
  407. if (starPatent.getApplicationNo().contains(".")) {
  408. appNo = starPatent.getApplicationNo().substring(0, starPatent.getApplicationNo().lastIndexOf("."));
  409. } else {
  410. appNo = starPatent.getApplicationNo();
  411. }
  412. //根据申请号调用"获得中国专利全文文本"接口,获得包含各种xml标签的专利全文内容的长字符串 cnFullXmlStr
  413. String cnFullXmlStr = patentStarApiService.getCnFullXmlApi(appNo);
  414. //使用正则表达式拼接出说明书文本全文
  415. String regex = "(?<=<p id=\"p)[\\w\\W]+?(?=</p>)";
  416. Pattern compile = Pattern.compile(regex);
  417. Matcher matcher = compile.matcher(cnFullXmlStr);
  418. StringBuilder builder = new StringBuilder();
  419. while (matcher.find()) {
  420. String oldRow = matcher.group();
  421. if (oldRow.contains("num=\"n")) {
  422. oldRow = oldRow.substring(oldRow.indexOf("num=\"n") + 6);
  423. oldRow = "[" + oldRow;
  424. oldRow = oldRow.replace("\">", "]");
  425. } else if (oldRow.contains("num=")) {
  426. oldRow = oldRow.substring(oldRow.indexOf("num=") + 5);
  427. oldRow = "[" + oldRow;
  428. oldRow = oldRow.replace("\">", "]");
  429. } else {
  430. oldRow = oldRow.substring(oldRow.indexOf("\">") + 2);
  431. }
  432. builder.append(oldRow).append("\r\n");
  433. }
  434. String instructionText = builder + "";
  435. //使用工具类去除字符串文本中的所有HTML格式标签
  436. instructionText = RemoveHtmlTagsUtils.removeHtmlTags(instructionText);
  437. //装载说明书文本全文
  438. PatentInstructionText patentInstructionText = new PatentInstructionText();
  439. patentInstructionText.setManual(instructionText);
  440. uploadParamsVO.setPatentInstructionText(patentInstructionText);
  441. } catch (Exception e) {
  442. e.printStackTrace();
  443. }
  444. }
  445. /**
  446. * 装载说明书pdf
  447. *
  448. * @param starPatent 专利之星著录对象
  449. * @param uploadParamsVO 专利实体类对象
  450. */
  451. public void setPatentInstructionPDF(StarPatentVO starPatent, UploadParamsVO uploadParamsVO) {
  452. try {
  453. //根据专利号调用"获得世界专利pdf"接口,获得pdf的url地址
  454. if (starPatent.getPatentNo() != null && !starPatent.getPatentNo().equals("")) {
  455. String pdfUrl = patentStarApiService.getEnPdfApi(starPatent.getPatentNo());
  456. URL url = new URL(pdfUrl); //想要读取的url地址
  457. InputStream in = url.openStream();
  458. File file = File.createTempFile("new_url", ".pdf"); //创建文件
  459. OutputStream os = new FileOutputStream(file); //创建文件输出流
  460. int bytesRead;
  461. byte[] buffer = new byte[8192];
  462. int len = 8192;
  463. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  464. os.write(buffer, 0, bytesRead);
  465. }
  466. //关闭释放流
  467. os.close();
  468. in.close();
  469. DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(16, null);
  470. FileItem item = diskFileItemFactory.createItem(file.getName(), "text/plain", true, file.getName());
  471. buffer = new byte[8192];
  472. FileInputStream fis = new FileInputStream(file);
  473. OutputStream fos = item.getOutputStream();
  474. len = 8192;
  475. while ((bytesRead = fis.read(buffer, 0, len)) != -1) {
  476. fos.write(buffer, 0, bytesRead);
  477. }
  478. //关闭释放流
  479. fos.close();
  480. fis.close();
  481. MultipartFile multipartFile = new CommonsMultipartFile(item);
  482. UploadFileDTO fileDTO = fileUtils.uploadFile(multipartFile);
  483. uploadParamsVO.setFileDTO(fileDTO);
  484. }
  485. } catch (Exception e) {
  486. e.printStackTrace();
  487. }
  488. }
  489. /**
  490. * 装载摘要附图
  491. *
  492. * @param starPatent 专利之星著录对象
  493. * @param uploadParamsVO 专利实体类对象
  494. */
  495. public void setPatentPicture(StarPatentVO starPatent, UploadParamsVO uploadParamsVO) {
  496. //取出一般接口返回的摘要附图路径
  497. String pictureUrl = starPatent.getAbstractPath();
  498. try {
  499. URL url = new URL(pictureUrl); //想要读取的url地址
  500. InputStream in = url.openStream();
  501. File file = File.createTempFile("new_url", ".jpg"); //创建文件
  502. OutputStream os = new FileOutputStream(file); //创建文件输出流
  503. int bytesRead;
  504. byte[] buffer = new byte[8192];
  505. int len = 8192;
  506. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  507. os.write(buffer, 0, bytesRead);
  508. }
  509. //关闭释放流
  510. os.close();
  511. in.close();
  512. DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(16, null);
  513. FileItem item = diskFileItemFactory.createItem(file.getName(), "text/plain", true, file.getName());
  514. buffer = new byte[8192];
  515. FileInputStream fis = new FileInputStream(file);
  516. OutputStream fos = item.getOutputStream();
  517. len = 8192;
  518. while ((bytesRead = fis.read(buffer, 0, len)) != -1) {
  519. fos.write(buffer, 0, bytesRead);
  520. }
  521. //关闭释放流
  522. fos.close();
  523. fis.close();
  524. MultipartFile multipartFile = new CommonsMultipartFile(item);
  525. UploadFileDTO fileDTO = fileUtils.uploadFile(multipartFile);
  526. uploadParamsVO.setFileDTO(fileDTO);
  527. } catch (Exception e) {
  528. e.printStackTrace();
  529. }
  530. }
  531. /**
  532. * 下载指定多个专利号专利
  533. *
  534. * @param isAddPatentNos 起止条数以外,用户手动勾选的多个专利号
  535. */
  536. public void downLoadIsAddPatentNos(List<String> isAddPatentNos, Task task, String orderBy, String orderByType, String dbType, List<String> cells, String conditions) throws IOException {
  537. if (conditions == null) {
  538. conditions = "(AN=(" + StringUtils.join(isAddPatentNos, " OR ") + "))";
  539. }
  540. int pageNum = 1;
  541. for (int i = 0; i < isAddPatentNos.size(); i += 50) {
  542. PatentStarListDto patentStarListDto = new PatentStarListDto()
  543. .setCurrentQuery(conditions)
  544. .setOrderBy(orderBy)
  545. .setOrderByType(orderByType)
  546. .setPageNum(pageNum++)
  547. .setRowCount(50)
  548. .setDBType(dbType);
  549. Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDto);
  550. if (resultMap == null || (Integer) resultMap.get("total") == 0) {
  551. ThrowException.throwXiaoShiException("发生未知错误,一般接口未检索出专利");
  552. }
  553. List<StarPatentVO> starPatents = (List<StarPatentVO>) resultMap.get("records");
  554. //遍历这一页的专利
  555. for (StarPatentVO starPatent : starPatents) {
  556. //判断若任务状态为已暂停,则结束生产
  557. if (pTaskId.equals(task.getId()) && pTaskStatus == 4) {
  558. return;
  559. }
  560. try {
  561. UploadParamsVO uploadParamsVO = new UploadParamsVO();
  562. Patent patent = new Patent();
  563. uploadParamsVO.setPatent(patent);
  564. uploadParamsVO.getPatent().setPatentNo(starPatent.getPatentNo());
  565. uploadPatentBatchService.getOneOrInsertOne(uploadParamsVO);
  566. PQueueData pQueueData = new PQueueData()
  567. .setTask(task)
  568. .setStarPatent(starPatent)
  569. .setUploadParamsVO(uploadParamsVO);
  570. //装载专利著录
  571. if (cells.contains("1")) {
  572. pantentQueueService.zhuluToPQueue(pQueueData);
  573. }
  574. //装载权要
  575. if (cells.contains("2")) {
  576. pantentQueueService.rightToPQueue(pQueueData);
  577. }
  578. //装载说明书文本
  579. if (cells.contains("3")) {
  580. pantentQueueService.instructionTextToPQueue(pQueueData);
  581. }
  582. //装载说明书pdf
  583. if (cells.contains("4")) {
  584. pantentQueueService.instructionPDFToPQueue(pQueueData);
  585. }
  586. //装载摘要附图
  587. if (cells.contains("6")) {
  588. pantentQueueService.imageToPQueue(pQueueData);
  589. }
  590. } catch (Exception e) {
  591. e.printStackTrace();
  592. }
  593. }
  594. }
  595. }
  596. /**
  597. * 专利之星返回日期格式为字符串 yyyyMMdd,如 "20230713",本方法将其转成10位数字时间戳
  598. *
  599. * @param dateStr yyyyMMdd格式字符串日期
  600. * @return 返回10位数字时间戳
  601. */
  602. public int stringDateToTimeStamp(String dateStr) {
  603. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  604. Date date;
  605. try {
  606. date = dateFormat.parse(dateStr);
  607. } catch (ParseException e) {
  608. //日期格式转换异常
  609. e.printStackTrace();
  610. return Integer.parseInt(dateStr);
  611. }
  612. long timeStamp = date.getTime() / 1000;
  613. return (int) timeStamp;
  614. }
  615. public void setTaskStatus(Integer taskId, Integer taskStatus) {
  616. this.pTaskId = taskId;
  617. this.pTaskStatus = taskStatus;
  618. }
  619. public Integer getpTaskId() {
  620. return pTaskId;
  621. }
  622. public Integer getpTaskStatus() {
  623. return pTaskStatus;
  624. }
  625. }