123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package cn.cslg.pas.service.business.es;
- import cn.cslg.pas.common.vo.ChinaLeagalStatus;
- import cn.cslg.pas.common.vo.StarPatentVO;
- import cn.cslg.pas.domain.es.*;
- import cn.cslg.pas.exception.ExceptionEnum;
- import cn.cslg.pas.exception.XiaoShiException;
- import cn.cslg.pas.service.common.PatentStarApiService;
- import cn.cslg.pas.service.importPatent.WebVOTransformService;
- import co.elastic.clients.elasticsearch.ElasticsearchClient;
- import co.elastic.clients.elasticsearch._types.InlineScript;
- import co.elastic.clients.elasticsearch._types.Refresh;
- import co.elastic.clients.elasticsearch._types.Script;
- import co.elastic.clients.elasticsearch._types.WaitForActiveShards;
- import co.elastic.clients.elasticsearch._types.query_dsl.Query;
- import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
- import co.elastic.clients.elasticsearch.core.*;
- import co.elastic.clients.elasticsearch.core.search.Hit;
- import com.alibaba.fastjson.JSON;
- import lombok.RequiredArgsConstructor;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- import java.io.IOException;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class EsLegalEventService {
- private final ElasticsearchClient client;
- private final PatentStarApiService patentStarApiService;
- private final WebVOTransformService webVOTransformService;
- //根据专利号查询同族专利号
- public List<LegalEvent> getEsQutePatentByNos(String patentNo) throws Exception {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("legal_event");
- Query q1 = QueryBuilders.term(t -> t.field("app_no").value(patentNo));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("public_no").value(patentNo));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("grant_no").value(patentNo));
- Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
- builder.query(query);
- SearchResponse<LegalEvent> response = client.search(builder.build(), LegalEvent.class);
- List<Hit<LegalEvent>> hits = response.hits().hits();
- List<LegalEvent> legalEvents = new ArrayList<>();
- if (hits.size() <= 0) {
- return legalEvents;
- }
- hits.forEach(item -> {
- legalEvents.add(item.source());
- });
- return legalEvents;
- }
- public List<String> getStrLegalEvent(String patentNo) {
- List<String> stringList = new ArrayList<>();
- try {
- List<LegalEvent> legalEvents = this.getEsQutePatentByNos(patentNo);
- legalEvents = legalEvents.stream().sorted(Comparator.comparing(LegalEvent::getEventDate)).collect(Collectors.toList());
- legalEvents.forEach(item -> {
- stringList.add(item.getName());
- });
- } catch (Exception e) {
- throw new XiaoShiException("获得事务错误");
- }
- return stringList;
- }
- /**
- * 添加法律事务
- *
- * @param
- * @return
- * @throws Exception
- */
- public String addLegalEvent(LegalEvent legalEvent) throws Exception {
- IndexResponse indexResponse = client.index(i -> i
- .index("legal_event")
- .document(legalEvent).refresh(Refresh.True).waitForActiveShards(WaitForActiveShards.of(t -> t.count(1)))
- );
- return indexResponse.id();
- }
- public String addEsLegalEvent(Patent patent, String ANO) {
- //根据专利号查询
- if (patent == null) {
- return null;
- }
- String cnLegalApiStr = patentStarApiService.getCnLegalApi(ANO);
- if (cnLegalApiStr != null && !cnLegalApiStr.equals("")) {
- try {
- this.deleteByPatentNo(patent.getPatentNo());
- } catch (Exception e) {
- }
- List<LegalEvent> legalEvents = new ArrayList<>();
- //根据专利号查询是否有引用信息
- try {
- this.deleteByPatentNo(patent.getPatentNo());
- } catch (Exception e) {
- e.printStackTrace();
- throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR,"删除事务错误");
- }
- List<ChinaLeagalStatus> chinaLeagalStatuses = JSON.parseArray(cnLegalApiStr, ChinaLeagalStatus.class);
- chinaLeagalStatuses.forEach(item -> {
- LegalEvent legalEvent1 = new LegalEvent();
- legalEvent1.setEventDate(item.getLegalDate());
- legalEvent1.setCode(item.getLegalCode());
- legalEvent1.setAppNo(patent.getAppNo());
- legalEvent1.setGrantNo(patent.getGrantNo());
- legalEvent1.setPublicNo(patent.getPublicNo());
- legalEvent1.setDescription(item.getLegalStatusInfo());
- legalEvent1.setName(item.getLegalStatus());
- legalEvents.add(legalEvent1);
- try {
- String reId = this.addLegalEvent(legalEvent1);
- } catch (Exception e) {
- throw new XiaoShiException(e.getMessage());
- }
- });
- webVOTransformService.transLegalEvent(patent, legalEvents);
- }
- return "";
- }
- public Integer deleteByPatentNo(String patentNo) throws IOException {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("legal_event");
- Query q1 = QueryBuilders.term(t -> t.field("app_no").value(patentNo));
- //公开号
- Query q2 = QueryBuilders.term(t -> t.field("public_no").value(patentNo));
- //授权号
- Query q3 = QueryBuilders.term(t -> t.field("grant_no").value(patentNo));
- Query query = QueryBuilders.bool(i -> i.should(q1, q2, q3));
- DeleteByQueryRequest request = DeleteByQueryRequest.of(i -> i.index("legal_event").refresh(true).query(query));
- try {
- client.deleteByQuery(request);
- return 1;
- } catch (IOException e) {
- return -1;
- }
- }
- }
|