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 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 response = client.search(builder.build(), LegalEvent.class); List> hits = response.hits().hits(); List legalEvents = new ArrayList<>(); if (hits.size() <= 0) { return legalEvents; } hits.forEach(item -> { legalEvents.add(item.source()); }); return legalEvents; } public List getStrLegalEvent(String patentNo) { List stringList = new ArrayList<>(); try { List 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 legalEvents = new ArrayList<>(); //根据专利号查询是否有引用信息 try { this.deleteByPatentNo(patent.getPatentNo()); } catch (Exception e) { e.printStackTrace(); throw new XiaoShiException(ExceptionEnum.BUSINESS_ERROR,"删除事务错误"); } List 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; } } }