|
@@ -0,0 +1,272 @@
|
|
|
+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.common.base.Records;
|
|
|
+import com.example.xiaoshiweixinback.business.exception.BusinessException;
|
|
|
+import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
|
|
|
+import com.example.xiaoshiweixinback.business.utils.BeanUtil;
|
|
|
+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.*;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.patent.CollectPatentDTO;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.patent.DelCollectPatentDTO;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.patent.PatentColumnDTO;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.patent.SelectCollectPatentDTO;
|
|
|
+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.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@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 assoPersonProductId = assoPersonProduct.getId();
|
|
|
+ //判断该专利是否已经被收藏
|
|
|
+ List<String> patentNoList = this.selectPatentByProductId(assoPersonProductId);
|
|
|
+ patentNos.removeAll(patentNoList);
|
|
|
+ //根据专利号获取专利id
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ if (!CollectionUtils.isEmpty(patentNos)) {
|
|
|
+ for (String patentNo : patentNos) {
|
|
|
+ String patentId = this.selectPatentByPatentNo(patentNo);
|
|
|
+ Patent patent = new Patent();
|
|
|
+ patent.setProductId(assoPersonProductId);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询收藏专利
|
|
|
+ * @param dto
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public Records selectCollectPatent(SelectCollectPatentDTO dto) throws IOException {
|
|
|
+ Integer assoPersonProductId = dto.getAssoPersonProductId();
|
|
|
+ Long pageNum = dto.getCurrent();
|
|
|
+ Long pageSize = dto.getSize();
|
|
|
+
|
|
|
+ List<PatentColumnDTO> list = new ArrayList<>();
|
|
|
+
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ //设置查询索引
|
|
|
+ builder.index("wxpatent");
|
|
|
+ Query q = QueryBuilders.term(i -> i.field("product_id").value(assoPersonProductId));
|
|
|
+ Query query = QueryBuilders.hasChild(i -> i.type("product").query(q));
|
|
|
+ builder.query(query);
|
|
|
+ //分页
|
|
|
+ if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
|
|
|
+ builder.from((pageNum.intValue() - 1) * pageSize.intValue()).size(pageSize.intValue());
|
|
|
+ }
|
|
|
+ //解除最大条数限制
|
|
|
+ builder.trackTotalHits(i -> i.enabled(true));
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
+ long total = response.hits().total().value();
|
|
|
+ for (Hit<Patent> hit : hits) {
|
|
|
+ Patent patent = hit.source();
|
|
|
+ PatentColumnDTO columnDTO = this.getPatentColumnDTO(patent, null, null);
|
|
|
+ list.add(columnDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ Records records = new Records();
|
|
|
+ records.setCurrent(pageNum);
|
|
|
+ records.setSize(pageSize);
|
|
|
+ records.setData(list);
|
|
|
+ records.setTotal(total);
|
|
|
+ return records;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除收藏专利
|
|
|
+ * @param patentDTO
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public boolean delCollectPatent(DelCollectPatentDTO patentDTO) throws IOException {
|
|
|
+ Integer assoPersonProductId = patentDTO.getAssoPersonProductId();
|
|
|
+ List<String> patentNos = patentDTO.getPatentNos();
|
|
|
+ List<String> patentIds = new ArrayList<>();
|
|
|
+ for (String patentNo : patentNos) {
|
|
|
+ List<String> list = this.selectPatentId(patentNo, assoPersonProductId);
|
|
|
+ patentIds.addAll(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(patentIds)) {
|
|
|
+ Integer num = esService.deleteWxPatent(patentIds);
|
|
|
+ if (num < 1) {
|
|
|
+ throw new BusinessException(ExceptionEnum.THE_FAIL_TO_DELETE);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据专利号、assoPersonProductId查询相关专利id
|
|
|
+ public List<String> selectPatentId(String patentNo,Integer assoPersonProductId) throws IOException {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ //设置查询索引
|
|
|
+ builder.index("wxpatent");
|
|
|
+ Query query = QueryBuilders.term(i -> i.field("product_id").value(assoPersonProductId));
|
|
|
+ Query q = QueryBuilders.hasChild(i -> i.type("product").query(query));
|
|
|
+ Query q1 = QueryBuilders.term(i -> i.field("patent_no.keyword").value(patentNo));
|
|
|
+ Query bool = QueryBuilders.bool(i -> i.must(q, q1));
|
|
|
+ builder.query(bool);
|
|
|
+ //解除最大条数限制
|
|
|
+ builder.trackTotalHits(i -> i.enabled(true));
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
+ for (Hit<Patent> hit : hits) {
|
|
|
+ String id = hit.id();
|
|
|
+ list.add(id);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ //装载专利清单列表的数据
|
|
|
+ public PatentColumnDTO getPatentColumnDTO(Patent patent, Integer projectId, String id) {
|
|
|
+ PatentColumnDTO columnDTO = new PatentColumnDTO();
|
|
|
+ BeanUtil.copy(patent, columnDTO);
|
|
|
+ if (ToolUtil.isNotEmpty(patent.getApplicantAddr())) {
|
|
|
+ PersonAddress applicantAddr = patent.getApplicantAddr();
|
|
|
+ columnDTO.setAppAddress(applicantAddr.getAddress());
|
|
|
+ columnDTO.setApplicantCountry(applicantAddr.getCountry());
|
|
|
+ columnDTO.setAppProvince(applicantAddr.getProvince());
|
|
|
+ columnDTO.setAppCity(applicantAddr.getCity());
|
|
|
+ columnDTO.setAppDistrict(applicantAddr.getDistrict());
|
|
|
+ }
|
|
|
+ if (ToolUtil.isNotEmpty(patent.getRightHolderAddr())) {
|
|
|
+ PersonAddress rightAddr = patent.getRightHolderAddr();
|
|
|
+ columnDTO.setRightAddress(rightAddr.getAddress());
|
|
|
+ columnDTO.setRightCountry(rightAddr.getCountry());
|
|
|
+ columnDTO.setRightProvince(rightAddr.getProvince());
|
|
|
+ columnDTO.setRightCity(rightAddr.getCity());
|
|
|
+ columnDTO.setRightDistrict(rightAddr.getDistrict());
|
|
|
+ }
|
|
|
+ columnDTO.setApplicant(this.loadName(patent.getApplicant()));
|
|
|
+ columnDTO.setRightHolder(this.loadName(patent.getRightHolder()));
|
|
|
+ columnDTO.setInventor(this.loadName(patent.getInventor()));
|
|
|
+ return columnDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ //通用返回申请人、权利人、发明人
|
|
|
+ public List<String> loadName(List<PatentPerson> list) {
|
|
|
+ List<String> collect = new ArrayList<>();
|
|
|
+ if (!CollectionUtils.isEmpty(list)) {
|
|
|
+ collect = list.stream().filter(i -> StringUtils.isNotEmpty(i.getName())).map(PatentPerson::getName).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return collect;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据专利号获取专利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 List<String> selectPatentByProductId(Integer personProductId) throws IOException {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ 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);
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
+ for (Hit<Patent> hit : hits) {
|
|
|
+ Patent patent = hit.source();
|
|
|
+ list.add(patent.getPatentNo());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+}
|