EsService.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.core.base.IfConstant;
  3. import cn.cslg.pas.common.dto.PatentColumnDTO;
  4. import cn.cslg.pas.common.dto.PatentDTO;
  5. //import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
  6. import cn.cslg.pas.common.dto.PatentStarListDTO;
  7. import cn.cslg.pas.common.dto.business.ClaimDetailDTO;
  8. import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
  9. import cn.cslg.pas.common.dto.business.SelectClaimDTO;
  10. import cn.cslg.pas.common.model.request.MapRequest;
  11. import cn.cslg.pas.common.model.request.OrderDTO;
  12. import cn.cslg.pas.common.model.request.QueryRequest;
  13. import cn.cslg.pas.common.model.request.StringRequest;
  14. import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
  15. import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
  16. import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
  17. import cn.cslg.pas.common.vo.ChinaLeagalStatus;
  18. import cn.cslg.pas.common.vo.PatentWithIdVO;
  19. import cn.cslg.pas.common.vo.StarPatentVO;
  20. import cn.cslg.pas.domain.es.*;
  21. import cn.cslg.pas.service.common.PatentStarApiService;
  22. import cn.cslg.pas.service.query.FormatQueryService;
  23. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  24. import co.elastic.clients.elasticsearch._types.SortOrder;
  25. import co.elastic.clients.elasticsearch._types.aggregations.AggregateBuilders;
  26. import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
  27. import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders;
  28. import co.elastic.clients.elasticsearch._types.query_dsl.*;
  29. import co.elastic.clients.elasticsearch.core.*;
  30. import co.elastic.clients.elasticsearch.core.search.Hit;
  31. import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
  32. import co.elastic.clients.elasticsearch.core.search.TotalHits;
  33. import com.alibaba.fastjson.JSON;
  34. import com.alibaba.fastjson.JSONArray;
  35. import com.alibaba.fastjson2.JSONObject;
  36. import lombok.RequiredArgsConstructor;
  37. import org.apache.commons.lang3.StringUtils;
  38. import org.springframework.beans.BeanUtils;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.context.annotation.Lazy;
  41. import org.springframework.stereotype.Service;
  42. import org.springframework.util.CollectionUtils;
  43. import java.io.IOException;
  44. import java.util.ArrayList;
  45. import java.util.Arrays;
  46. import java.util.List;
  47. import java.util.Map;
  48. import java.util.stream.Collectors;
  49. @Service
  50. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  51. public class EsService {
  52. private final ElasticsearchClient client;
  53. @Autowired
  54. private FormatQueryService formatQueryService;
  55. @Autowired
  56. private PatentStarApiService patentStarApiService;
  57. /**
  58. * @param patent
  59. * @throws Exception
  60. */
  61. public String addPatent(Patent patent) throws Exception {
  62. IndexResponse indexResponse = client.index(i -> i
  63. .index("patent")
  64. //传入user对象
  65. .document(patent)
  66. );
  67. return indexResponse.id();
  68. }
  69. /**
  70. * @param patent
  71. * @throws Exception
  72. */
  73. public String addChildPatent(Patent patent, String id) throws Exception {
  74. IndexResponse indexResponse = client.index(i -> i
  75. .index("patent")
  76. .routing(id)
  77. //传入user对象
  78. .document(patent)
  79. );
  80. return indexResponse.id();
  81. }
  82. /**
  83. * 根据专利号获取专利id
  84. *
  85. * @param patentNo
  86. * @return
  87. * @throws Exception
  88. */
  89. public PatentWithIdVO getIdByPatentNo(String patentNo) throws Exception {
  90. SearchRequest.Builder builder = new SearchRequest.Builder();
  91. //设置查询索引
  92. builder.index("patent");
  93. PatentWithIdVO patentWithIdVO = null;
  94. String id = null;
  95. Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
  96. //公开号
  97. Query q2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
  98. //授权号
  99. Query q3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
  100. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  101. builder.query(query);
  102. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  103. List<Hit<Patent>> hits = response.hits().hits();
  104. if (hits != null && hits.size() > 0) {
  105. id = hits.get(0).id();
  106. Patent patent = hits.get(0).source();
  107. patentWithIdVO = new PatentWithIdVO();
  108. patentWithIdVO.setPatent(patent);
  109. patentWithIdVO.setId(id);
  110. }
  111. return patentWithIdVO;
  112. }
  113. /**
  114. * Es检索
  115. *
  116. * @param queryRequest 检索条件
  117. * @return
  118. */
  119. public PatentDTO esSearch(QueryRequest queryRequest) throws Exception {
  120. PatentDTO dto = new PatentDTO();
  121. String searchCondition = "";
  122. Integer projectId = queryRequest.getProjectId();
  123. Long current = queryRequest.getCurrent();
  124. Long size = queryRequest.getSize();
  125. //判断表达式
  126. if (queryRequest instanceof StringRequest) {
  127. searchCondition = ((StringRequest) queryRequest).getSearchQuery();
  128. } else if (queryRequest instanceof MapRequest) {
  129. Map<String, Object> map = ((MapRequest) queryRequest).getSearchQuery();
  130. StringBuilder stringBuilder = new StringBuilder();
  131. for (String key : map.keySet()) {
  132. Object value = map.get(key);
  133. if (!"".contentEquals(stringBuilder)) {
  134. stringBuilder = stringBuilder.append(" AND ").append(key).append("=").append(value);
  135. } else {
  136. stringBuilder = stringBuilder.append(key).append("=").append(value);
  137. }
  138. }
  139. searchCondition = stringBuilder.toString();
  140. }
  141. if (projectId != null) {
  142. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  143. searchCondition = "projectId = " + projectId + " AND " + searchCondition;
  144. } else {
  145. searchCondition = "projectId = " + projectId;
  146. }
  147. }
  148. SearchRequest.Builder builder = new SearchRequest.Builder();
  149. //设置查询索引
  150. builder.index("patent");
  151. //1. 解析检索条件
  152. treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
  153. //格式化检索式
  154. //3. 从es中检索数据
  155. Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
  156. //4. 返回数据
  157. builder.query(q);
  158. //排序
  159. List<OrderDTO> dtoList = queryRequest.getOrderDTOList();
  160. if (!CollectionUtils.isEmpty(dtoList)) {
  161. for (OrderDTO orderDTO : dtoList) {
  162. if (orderDTO.getOrderType().equals(IfConstant.NO)) {
  163. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  164. .field(fieldSortBuilder -> fieldSortBuilder
  165. .field(orderDTO.getOrderBy()).order(SortOrder.Asc)));
  166. } else {
  167. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  168. .field(fieldSortBuilder -> fieldSortBuilder
  169. .field(orderDTO.getOrderBy()).order(SortOrder.Desc)));
  170. }
  171. }
  172. } else {
  173. builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  174. .field(fieldSortBuilder -> fieldSortBuilder
  175. .field("patent_no.keyword").order(SortOrder.Desc)));
  176. }
  177. //分页
  178. if (current > 0 && size > 0) {
  179. builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
  180. }
  181. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  182. List<PatentColumnDTO> list = new ArrayList<>();
  183. List<Hit<Patent>> hits = response.hits().hits();
  184. long total = response.hits().total().value();
  185. for (Hit<Patent> hit : hits) {
  186. Patent esMess = hit.source();
  187. PatentColumnDTO columnDTO = new PatentColumnDTO();
  188. // columnDTO.setPatentNo(esMess.getPatentNo());
  189. BeanUtils.copyProperties(esMess, columnDTO);
  190. list.add(columnDTO);
  191. }
  192. dto.setTotal(total);
  193. dto.setPatents(list);
  194. dto.setPageNum(current);
  195. dto.setPageSize(size);
  196. return dto;
  197. }
  198. /**
  199. * @param key
  200. * @param page
  201. * @param limit
  202. * @return
  203. */
  204. public List<Patent> search(String key, Integer page, Integer limit) throws IOException {
  205. SearchRequest.Builder builder = new SearchRequest.Builder();
  206. //设置查询索引
  207. builder.index("patent");
  208. //组装查询条件
  209. BoolQuery.Builder boolQuery = new BoolQuery.Builder();
  210. boolQuery.should(q -> q.match(v -> v
  211. .query(key)
  212. //字段名
  213. .field("patent_no")
  214. ));
  215. //多字段匹配
  216. // boolQuery.should(q -> q.matchPhrasePrefix(m -> m.query(key).field("content")));
  217. builder.query(q -> q.bool(boolQuery.build()));
  218. //分页
  219. if (page != null && limit != null) {
  220. builder.from(page).size(limit);
  221. }
  222. //排序
  223. // builder.sort(sortOptionsBuilder -> sortOptionsBuilder
  224. // .field(fieldSortBuilder -> fieldSortBuilder
  225. // .field("createTime").order(SortOrder.Desc)));
  226. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  227. List<Patent> list = new ArrayList<>();
  228. List<Hit<Patent>> hits = response.hits().hits();
  229. for (Hit<Patent> hit : hits) {
  230. Patent esMess = hit.source();
  231. list.add(esMess);
  232. }
  233. return list;
  234. }
  235. public Integer updatePatent(Patent patent, String id) {
  236. UpdateRequest<Patent, Patent> req;
  237. req = UpdateRequest.of(
  238. b -> b.index("patent").id(id)
  239. .doc(patent)
  240. );
  241. try {
  242. client.update(req, Patent.class);
  243. return 1;
  244. } catch (IOException e) {
  245. return -1;
  246. }
  247. }
  248. /**
  249. * @param key
  250. * @param page
  251. * @param limit
  252. * @return
  253. * @throws IOException
  254. */
  255. public List<Patent> searchChild(String key, Integer page, Integer limit) throws IOException {
  256. SearchRequest.Builder builder = new SearchRequest.Builder();
  257. //设置查询索引
  258. builder.index("patent");
  259. //组装查询条件
  260. HasChildQuery.Builder hasChildQuery = new HasChildQuery.Builder();
  261. hasChildQuery.type("project");
  262. hasChildQuery.query(q -> q.match(m -> m
  263. .query(key)
  264. //字段名
  265. .field("project_id")
  266. ));
  267. builder.query(q -> q.hasChild(hasChildQuery.build()));
  268. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  269. List<Patent> list = new ArrayList<>();
  270. List<Hit<Patent>> hits = response.hits().hits();
  271. for (Hit<Patent> hit : hits) {
  272. Patent esMess = hit.source();
  273. list.add(esMess);
  274. }
  275. return list;
  276. }
  277. /**
  278. * 查询是否存在专利
  279. *
  280. * @param parentId
  281. * @param projectId
  282. * @return
  283. */
  284. public Boolean searchPatent(String parentId, Integer projectId) throws IOException {
  285. boolean flag = false;
  286. SearchRequest.Builder builder = new SearchRequest.Builder();
  287. //设置查询索引
  288. builder.index("patent");
  289. Query q1 = QueryBuilders.term(t -> t.field("project_id").value(projectId));
  290. Query q2 = QueryBuilders.parentId(parent -> parent.type("project").id(parentId));
  291. Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
  292. builder.query(bool);
  293. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  294. long total = response.hits().total().value();
  295. if (total > 0) {
  296. flag = true;
  297. }
  298. return flag;
  299. }
  300. /**
  301. * 查询是否存在专利
  302. *
  303. * @param parentId
  304. * @param projectId
  305. * @return
  306. */
  307. public Boolean ifInTask(String parentId, Integer projectId, Integer taskId) throws IOException {
  308. boolean flag = false;
  309. SearchRequest.Builder builder = new SearchRequest.Builder();
  310. //设置查询索引
  311. builder.index("patent");
  312. Query q1 = QueryBuilders.term(t -> t.field("import_task.project_id").value(projectId));
  313. Query q3 = QueryBuilders.term(t -> t.field("import_task.task_id").value(projectId));
  314. Query q2 = QueryBuilders.parentId(parent -> parent.type("import_task").id(parentId));
  315. Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3));
  316. builder.query(bool);
  317. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  318. long total = response.hits().total().value();
  319. if (total > 0) {
  320. flag = true;
  321. }
  322. return flag;
  323. }
  324. /**
  325. * 查询同族
  326. *
  327. * @param nos
  328. * @param type
  329. * @return
  330. * @throws IOException
  331. */
  332. public EsPatentFamilyDTO addEsPatentFamily(List<String> nos, String type) throws IOException {
  333. EsPatentFamilyDTO esDTO = new EsPatentFamilyDTO();
  334. List<String> absentList = new ArrayList<>();
  335. PatentFamilyMessage patentFamilyMessage = null;
  336. String id = "";
  337. //遍历,根据专利号查询同族
  338. for (String no : nos) {
  339. SearchRequest.Builder builder = new SearchRequest.Builder();
  340. //设置查询索引
  341. builder.index("patent_family");
  342. //申请号
  343. Query q1 = QueryBuilders.term(t -> t.field("app_no").value(no));
  344. //公开号
  345. Query q2 = QueryBuilders.term(t -> t.field("public_no").value(no));
  346. //授权号
  347. Query q3 = QueryBuilders.term(t -> t.field("grant_no").value(no));
  348. //类型
  349. Query q4 = QueryBuilders.term(t -> t.field("family_type").value(type));
  350. Query bool = QueryBuilders.bool(i -> i.must(q4).should(q1, q2, q3));
  351. builder.query(bool);
  352. SearchResponse<PatentFamilyMessage> response = client.search(builder.build(), PatentFamilyMessage.class);
  353. long total = response.hits().total().value();
  354. if (total > 0) {
  355. List<FamilyPatent> list = new ArrayList<>();
  356. List<Hit<PatentFamilyMessage>> hits = response.hits().hits();
  357. Hit<PatentFamilyMessage> hit = hits.get(0);
  358. id = hit.id();
  359. patentFamilyMessage = hit.source();
  360. break;
  361. }
  362. }
  363. List<String> notInNos = new ArrayList<>();
  364. //当查询到同族时
  365. if (patentFamilyMessage != null) {
  366. List<FamilyPatent> familyPatents = patentFamilyMessage.getPatent();
  367. for (String no : nos) {
  368. FamilyPatent familyPatent = familyPatents.stream()
  369. .filter(item -> item.getAppNo() != null && item.getAppNo().equals(no) ||
  370. item.getPublicNo() != null && item.getPublicNo().equals(no) ||
  371. item.getGrantNo() != null && item.getGrantNo().equals(no))
  372. .findFirst().orElse(null);
  373. if (familyPatent == null) {
  374. notInNos.add(no);
  375. }
  376. }
  377. }
  378. //当未查询到同族时
  379. else {
  380. patentFamilyMessage = new PatentFamilyMessage();
  381. patentFamilyMessage.setFamilyType(type);
  382. patentFamilyMessage.setPatent(new ArrayList<>());
  383. notInNos.addAll(nos);
  384. }
  385. if (notInNos.size() > 0) {
  386. //所有专利号的专利详情
  387. List<StarPatentVO> starPatents = new ArrayList<>();
  388. PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
  389. List<PatentStarListDTO> patentStarListDtos = patentStarApiService.splitPatentNoByType(notInNos, patentStarListDTO);
  390. while (patentStarListDtos.size() > 0) {
  391. PatentStarListDTO patentStarListDTO1 = patentStarListDtos.remove(0);
  392. patentStarListDTO1.setPageNum(1);
  393. patentStarListDTO1.setRowCount(50);
  394. Map<String, Object> resultMap = patentStarApiService.patentStarSearchApi(patentStarListDTO1);
  395. if (resultMap == null || (Integer) resultMap.get("total") == 0) {
  396. continue;
  397. }
  398. starPatents.addAll((List<StarPatentVO>) resultMap.get("records"));
  399. }
  400. List<FamilyPatent> patents = new ArrayList<>();
  401. starPatents.forEach(item -> {
  402. FamilyPatent familyPatent = new FamilyPatent();
  403. familyPatent.setAppNo(item.getApplicationNo());
  404. familyPatent.setPublicNo(item.getPublicNo());
  405. familyPatent.setGrantNo(item.getPublicAccreditNo());
  406. patents.add(familyPatent);
  407. });
  408. patentFamilyMessage.getPatent().addAll(patents);
  409. //当id不为null时
  410. if (!id.equals("")) {
  411. this.updatePatentFamily(patentFamilyMessage, id);
  412. } else {
  413. try {
  414. id = this.addPatentFamily(patentFamilyMessage);
  415. } catch (Exception e) {
  416. }
  417. }
  418. }
  419. esDTO.setPatentFamilyId(id);
  420. esDTO.setFamilyNum(patentFamilyMessage.getPatent().size());
  421. return esDTO;
  422. }
  423. /**
  424. * 添加同族
  425. *
  426. * @param message
  427. * @return
  428. * @throws Exception
  429. */
  430. public String addPatentFamily(PatentFamilyMessage message) throws Exception {
  431. IndexResponse indexResponse = client.index(i -> i
  432. .index("patent_family")
  433. .document(message)
  434. );
  435. return indexResponse.id();
  436. }
  437. /**
  438. * 更新同族
  439. *
  440. * @param message
  441. * @param id
  442. * @return
  443. */
  444. public Integer updatePatentFamily(PatentFamilyMessage message, String id) {
  445. UpdateRequest<PatentFamilyMessage, PatentFamilyMessage> req;
  446. req = UpdateRequest.of(
  447. b -> b.index("patent_family")
  448. .id(id)
  449. .doc(message)
  450. );
  451. try {
  452. client.update(req, PatentFamilyMessage.class);
  453. return 1;
  454. } catch (IOException e) {
  455. return -1;
  456. }
  457. }
  458. /**
  459. * 更新专利事务
  460. *
  461. * @param
  462. * @param id
  463. * @return
  464. */
  465. public Integer updateLegalEvent(LegalEvent legalEvent, String id) {
  466. UpdateRequest<LegalEvent, LegalEvent> req;
  467. req = UpdateRequest.of(
  468. b -> b.index("legal_event")
  469. .id(id)
  470. .doc(legalEvent)
  471. );
  472. try {
  473. client.update(req, LegalEvent.class);
  474. return 1;
  475. } catch (IOException e) {
  476. return -1;
  477. }
  478. }
  479. /**
  480. * 添加法律事务
  481. *
  482. * @param
  483. * @return
  484. * @throws Exception
  485. */
  486. public String addLegalEvent(LegalEvent legalEvent) throws Exception {
  487. IndexResponse indexResponse = client.index(i -> i
  488. .index("legal_event")
  489. .document(legalEvent)
  490. );
  491. return indexResponse.id();
  492. }
  493. /**
  494. * 更新专利事务
  495. *
  496. * @param
  497. * @param id
  498. * @return
  499. */
  500. public Integer updateQuotePatent(PatentQuoteMessage patentQuoteMessage, String id) {
  501. UpdateRequest<PatentQuoteMessage, PatentQuoteMessage> req;
  502. req = UpdateRequest.of(
  503. b -> b.index("quote_patent")
  504. .id(id)
  505. .doc(patentQuoteMessage)
  506. );
  507. try {
  508. client.update(req, PatentQuoteMessage.class);
  509. return 1;
  510. } catch (IOException e) {
  511. return -1;
  512. }
  513. }
  514. /**
  515. * 添加法律事务
  516. *
  517. * @param
  518. * @return
  519. * @throws Exception
  520. */
  521. public String addQuotePatent(PatentQuoteMessage patentQuoteMessage) throws Exception {
  522. IndexResponse indexResponse = client.index(i -> i
  523. .index("quote_patent")
  524. .document(patentQuoteMessage)
  525. );
  526. return indexResponse.id();
  527. }
  528. public String addEsQuotePatent(String no, List<String> nos) throws Exception {
  529. StarPatentVO starPatentVO = patentStarApiService.getPatentByNo(no);
  530. if (starPatentVO == null) {
  531. return null;
  532. }
  533. QuotePatent quotePatent = this.reQuotePatent(starPatentVO);
  534. PatentQuoteMessage patentQuoteMessage = null;
  535. //根据申请号和
  536. String id = "";
  537. //根据专利号查询是否有引用信息
  538. SearchRequest.Builder builder = new SearchRequest.Builder();
  539. //设置查询索引
  540. builder.index("quote_patent");
  541. //申请号
  542. Query q1 = QueryBuilders.term(t -> t.field("patent.app_no").value(starPatentVO.getApplicationNo()));
  543. Query bool = QueryBuilders.bool(i -> i.must(q1));
  544. builder.query(bool);
  545. SearchResponse<PatentQuoteMessage> response = client.search(builder.build(), PatentQuoteMessage.class);
  546. long total = response.hits().total().value();
  547. if (total > 0) {
  548. patentQuoteMessage = response.hits().hits().get(0).source();
  549. id = response.hits().hits().get(0).id();
  550. }
  551. List<QuotePatent> quotePatents = new ArrayList<>();
  552. if (patentQuoteMessage != null && patentQuoteMessage.getQuotedPatents() != null) {
  553. quotePatents = patentQuoteMessage.getQuotedPatents();
  554. }
  555. for (String patentNo : nos) {
  556. StarPatentVO starPatentVO1 = patentStarApiService.getPatentByNo(patentNo);
  557. QuotePatent quotePatent1 = null;
  558. if (quotePatents.size() > 0) {
  559. quotePatent1 = quotePatents.stream()
  560. .filter(item -> item.getAppNo().equals(starPatentVO1.getApplicationNo())).findFirst().orElse(null);
  561. }
  562. if (quotePatent1 == null) {
  563. quotePatent1 = this.reQuotePatent(starPatentVO1);
  564. quotePatents.add(quotePatent1);
  565. }
  566. }
  567. if (patentQuoteMessage == null) {
  568. if (quotePatents.size() != 0) {
  569. patentQuoteMessage = new PatentQuoteMessage();
  570. patentQuoteMessage.setPatent(quotePatent);
  571. patentQuoteMessage.setQuotedPatents(quotePatents);
  572. this.addQuotePatent(patentQuoteMessage);
  573. }
  574. } else {
  575. patentQuoteMessage.getQuotedPatents().addAll(quotePatents);
  576. this.updateQuotePatent(patentQuoteMessage, id);
  577. }
  578. return "";
  579. }
  580. private QuotePatent reQuotePatent(StarPatentVO starPatentVO) {
  581. QuotePatent quotePatent = new QuotePatent();
  582. //装载申请人
  583. if (starPatentVO.getApplicantStr() != null && !starPatentVO.getApplicantStr().equals("")) {
  584. List<String> names = Arrays.asList(starPatentVO.getApplicantStr().split(";"));
  585. List<PatentPerson> patentPeople = new ArrayList<>();
  586. for (int i = 0; i < names.size(); i++) {
  587. PatentPerson patentPerson = new PatentPerson();
  588. patentPerson.setOrder(i + 1);
  589. patentPerson.setType("1");
  590. patentPerson.setName(names.get(i));
  591. patentPeople.add(patentPerson);
  592. }
  593. quotePatent.setApplicant(patentPeople);
  594. }
  595. //装载权利人
  596. if (starPatentVO.getCurrentApplicantStr() != null && !starPatentVO.getCurrentApplicantStr().equals("")) {
  597. List<String> names = Arrays.asList(starPatentVO.getCurrentApplicantStr().split(";"));
  598. List<PatentPerson> patentPeople = new ArrayList<>();
  599. for (int i = 0; i < names.size(); i++) {
  600. PatentPerson patentPerson = new PatentPerson();
  601. patentPerson.setOrder(0);
  602. patentPerson.setType("1");
  603. patentPerson.setName(names.get(i));
  604. patentPeople.add(patentPerson);
  605. }
  606. quotePatent.setRightHolder(patentPeople);
  607. }
  608. quotePatent.setAppNo(starPatentVO.getApplicationNo());
  609. return quotePatent;
  610. }
  611. public String addEsLegalEvent(String patentNo) {
  612. LegalEvent legalEvent = null;
  613. String id = "";
  614. //根据专利号查询
  615. StarPatentVO starPatentVO = patentStarApiService.getPatentByNo(patentNo);
  616. if (starPatentVO == null) {
  617. return null;
  618. }
  619. String cnLegalApiStr = patentStarApiService.getCnLegalApi(starPatentVO.getRowApplicationNo());
  620. if (cnLegalApiStr != null && !cnLegalApiStr.equals("")) {
  621. List<LegalEvent> legalEvents = new ArrayList<>();
  622. //根据专利号查询是否有引用信息
  623. SearchRequest.Builder builder = new SearchRequest.Builder();
  624. //设置查询索引
  625. builder.index("legal_event");
  626. //申请号
  627. Query q1 = QueryBuilders.term(t -> t.field("app_no").value(starPatentVO.getApplicationNo()));
  628. Query bool = QueryBuilders.bool(i -> i.must(q1));
  629. builder.query(bool);
  630. SearchResponse<LegalEvent> response = null;
  631. try {
  632. response = client.search(builder.build(), LegalEvent.class);
  633. } catch (IOException e) {
  634. e.printStackTrace();
  635. }
  636. long total = response.hits().total().value();
  637. if (total > 0) {
  638. response.hits().hits().forEach(item -> {
  639. legalEvents.add(item.source());
  640. });
  641. }
  642. List<ChinaLeagalStatus> chinaLeagalStatuses = JSON.parseArray(cnLegalApiStr, ChinaLeagalStatus.class);
  643. chinaLeagalStatuses.forEach(item -> {
  644. if (legalEvents.size() > 0) {
  645. LegalEvent tem = legalEvents.stream().filter(em -> em.getEventDate() != null && em.getEventDate().compareTo(item.getLegalDate()) == 0).findFirst().orElse(null);
  646. if (tem == null) {
  647. LegalEvent legalEvent1 = new LegalEvent();
  648. legalEvent1.setEventDate(item.getLegalDate());
  649. legalEvent1.setCode(item.getLegalCode());
  650. legalEvent1.setAppNo(starPatentVO.getApplicationNo());
  651. legalEvent1.setDescription(item.getLegalStatusInfo());
  652. legalEvent1.setName(item.getLegalStatus());
  653. try {
  654. this.addLegalEvent(legalEvent1);
  655. } catch (Exception e) {
  656. }
  657. }
  658. }
  659. });
  660. }
  661. return "";
  662. }
  663. /**
  664. * 查询权利要求
  665. * @param patentNo
  666. * @return
  667. */
  668. public SelectClaimDTO selectClaim(String patentNo) throws IOException {
  669. SearchRequest.Builder builder = new SearchRequest.Builder();
  670. //设置查询索引
  671. builder.index("patent");
  672. //原始数据
  673. Query q = QueryBuilders.term(t -> t.field("claim.if_origin").value(true));
  674. //申请号
  675. Query q1 = QueryBuilders.term(t -> t.field("app_no").value(patentNo));
  676. //公开号
  677. Query q2 = QueryBuilders.term(t -> t.field("public_no").value(patentNo));
  678. //授权号
  679. Query q3 = QueryBuilders.term(t -> t.field("grant_no").value(patentNo));
  680. Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
  681. Query bool = QueryBuilders.bool(i -> i.must(q,query));
  682. builder.query(bool);
  683. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  684. SelectClaimDTO dto = new SelectClaimDTO();
  685. List<Hit<Patent>> hits = response.hits().hits();
  686. long total = response.hits().total().value();
  687. for (Hit<Patent> hit : hits) {
  688. Patent esMess = hit.source();
  689. // dto.setClaim(JSON.toJSONString(esMess.getClaim()));
  690. if (esMess != null) {
  691. String claim = StringUtils.strip(JSON.toJSONString(esMess.getClaim()), "[]");
  692. ClaimDetailDTO claimDetailDTO = JSONObject.parseObject(claim, ClaimDetailDTO.class);
  693. if (claimDetailDTO.getIfOrigin()) {
  694. dto.setClaim(claimDetailDTO.getTextContent());
  695. }
  696. }
  697. }
  698. return dto;
  699. }
  700. }