|
@@ -0,0 +1,140 @@
|
|
|
|
+package com.example.xiaoshiweixinback.service.importPatent;
|
|
|
|
+
|
|
|
|
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchRequest;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.SearchResponse;
|
|
|
|
+import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.example.xiaoshiweixinback.business.exception.BusinessException;
|
|
|
|
+import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
|
|
|
|
+import com.example.xiaoshiweixinback.business.utils.CacheUtil;
|
|
|
|
+import com.example.xiaoshiweixinback.business.utils.LoginUtils;
|
|
|
|
+import com.example.xiaoshiweixinback.business.utils.ToolUtil;
|
|
|
|
+import com.example.xiaoshiweixinback.domain.AssoPersonProduct;
|
|
|
|
+import com.example.xiaoshiweixinback.domain.es.Patent;
|
|
|
|
+import com.example.xiaoshiweixinback.domain.es.PatentJoin;
|
|
|
|
+import com.example.xiaoshiweixinback.domain.es.PatentVector;
|
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.patent.CollectPatentDTO;
|
|
|
|
+import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
|
|
|
|
+import com.example.xiaoshiweixinback.entity.vo.esPicture.EsPictureNoVo;
|
|
|
|
+import com.example.xiaoshiweixinback.mapper.AssoPersonProductMapper;
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
|
+public class EsPatentService {
|
|
|
|
+
|
|
|
|
+ private final ElasticsearchClient client;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private EsService esService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private CacheUtil cacheUtils;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private AssoPersonProductMapper assoPersonProductMapper;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ *
|
|
|
|
+ * @param patentDTO
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public boolean collectPatent(CollectPatentDTO patentDTO) throws IOException {
|
|
|
|
+ boolean flag = false;
|
|
|
|
+ List<String> patentNos = patentDTO.getPatentNos();
|
|
|
|
+ Integer productId = patentDTO.getProductId();
|
|
|
|
+ //获取用户
|
|
|
|
+ PersonnelVO personnelVO = cacheUtils.getLoginUser(LoginUtils.getToken());
|
|
|
|
+ if (ToolUtil.isEmpty(personnelVO)) {
|
|
|
|
+ throw new BusinessException(ExceptionEnum.THE_GET_INFORMATION_TOKEN_INVALID);
|
|
|
|
+ }
|
|
|
|
+ String uuid = personnelVO.getUuid();
|
|
|
|
+ //获取人员产品关联表id
|
|
|
|
+ AssoPersonProduct assoPersonProduct = assoPersonProductMapper.selectOne(new LambdaQueryWrapper<AssoPersonProduct>()
|
|
|
|
+ .eq(AssoPersonProduct::getProductId, productId)
|
|
|
|
+ .eq(AssoPersonProduct::getPersonUuid, uuid));
|
|
|
|
+ Integer personProductId = assoPersonProduct.getId();
|
|
|
|
+ //判断该专利是否已经被收藏
|
|
|
|
+ boolean b = this.selectPatentByProductId(personProductId);
|
|
|
|
+ if (!b) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ //根据专利号获取专利id
|
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
|
+ for (String patentNo : patentNos) {
|
|
|
|
+ String patentId = this.selectPatentByPatentNo(patentNo);
|
|
|
|
+ Patent patent = new Patent();
|
|
|
|
+ patent.setProductId(personProductId);
|
|
|
|
+ PatentJoin patentJoin = new PatentJoin();
|
|
|
|
+ patentJoin.setParent(patentId);
|
|
|
|
+ patentJoin.setName("product");
|
|
|
|
+ patent.setPatentJoin(patentJoin);
|
|
|
|
+ try {
|
|
|
|
+ String id = esService.addChildPatent(patent, patentId);
|
|
|
|
+ list.add(id);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (ToolUtil.equals(list.size(), patentNos.size())) {
|
|
|
|
+ flag = true;
|
|
|
|
+ }
|
|
|
|
+ return flag;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据专利号获取专利id
|
|
|
|
+ * @param patentNo
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public String selectPatentByPatentNo(String patentNo) throws IOException {
|
|
|
|
+ String id = "";
|
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
|
+ //设置查询索引
|
|
|
|
+ builder.index("wxpatent");
|
|
|
|
+ Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
|
|
|
|
+ builder.query(q);
|
|
|
|
+ builder.size(10);
|
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
|
+ for (Hit<Patent> hit : hits) {
|
|
|
|
+ id = hit.id();
|
|
|
|
+ }
|
|
|
|
+ return id;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断该专利是否已经被收藏
|
|
|
|
+ * @param personProductId
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public boolean selectPatentByProductId(Integer personProductId) throws IOException {
|
|
|
|
+ boolean flag = false;
|
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
|
+ //设置查询索引
|
|
|
|
+ builder.index("wxpatent");
|
|
|
|
+ Query q = QueryBuilders.term(i -> i.field("product_id").value(personProductId));
|
|
|
|
+ Query query = QueryBuilders.hasChild(i -> i.type("product").query(q));
|
|
|
|
+ builder.query(query);
|
|
|
|
+ builder.size(10);
|
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
|
+ long count = response.hits().total().value();
|
|
|
|
+ if (count > 0) {
|
|
|
|
+ flag = true;
|
|
|
|
+ }
|
|
|
|
+// List<Hit<Patent>> hits = response.hits().hits();
|
|
|
|
+// int size = hits.size();
|
|
|
|
+ return flag;
|
|
|
|
+ }
|
|
|
|
+}
|