|
@@ -0,0 +1,401 @@
|
|
|
+package cn.cslg.pas.service.business.es;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.dto.es.EsCustomFieldDTO;
|
|
|
+import cn.cslg.pas.common.dto.es.FieldHistoryDTO;
|
|
|
+import cn.cslg.pas.common.dto.es.QueryEsCustomFieldDTO;
|
|
|
+import cn.cslg.pas.common.utils.FormatUtil;
|
|
|
+import cn.cslg.pas.common.vo.PatentWithIdVO;
|
|
|
+import cn.cslg.pas.common.vo.es.FieldHistoryVO;
|
|
|
+import cn.cslg.pas.common.vo.es.QueryESCustomFieldVO;
|
|
|
+import cn.cslg.pas.domain.business.CustomField;
|
|
|
+import cn.cslg.pas.domain.business.TreeNode;
|
|
|
+import cn.cslg.pas.domain.es.*;
|
|
|
+import cn.cslg.pas.exception.XiaoShiException;
|
|
|
+import cn.cslg.pas.service.business.TreeNodeService;
|
|
|
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
+import co.elastic.clients.elasticsearch._types.SortOrder;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
|
|
+import co.elastic.clients.elasticsearch.core.IndexResponse;
|
|
|
+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 lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Array;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
+public class EsCustomFieldService {
|
|
|
+ private final ElasticsearchClient client;
|
|
|
+ private final EsService esService;
|
|
|
+ private final TreeNodeService treeNodeService;
|
|
|
+
|
|
|
+ //添加自定义栏位值
|
|
|
+ public void addCustomField(EsCustomFieldDTO esCustomFieldDTO) throws Exception {
|
|
|
+ ESCustomField esCustomField = new ESCustomField();
|
|
|
+ String id = "";
|
|
|
+ //判断类型
|
|
|
+ Integer fieldType = esCustomFieldDTO.getFieldType();
|
|
|
+ Integer taskId = esCustomFieldDTO.getTaskId();
|
|
|
+ Integer optionType = esCustomFieldDTO.getOptionType();
|
|
|
+ String patentNo = esCustomFieldDTO.getPatentNo();
|
|
|
+ String fieldId = esCustomFieldDTO.getFieldId();
|
|
|
+ List<String> addValues = esCustomFieldDTO.getFieldValue();
|
|
|
+ Integer projectId = esCustomFieldDTO.getProjectId();
|
|
|
+ Date createDate = new Date();
|
|
|
+
|
|
|
+ if (optionType.equals(1) && (addValues == null || addValues.size() == 0)) {
|
|
|
+ throw new XiaoShiException("请填写值");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //根据专利号查询 是否专利关联过栏位
|
|
|
+ PatentWithIdVO patentWithIdVO = this.getEsCustomField(esCustomFieldDTO, patentNo);
|
|
|
+ if (patentWithIdVO != null) {
|
|
|
+ esCustomField = patentWithIdVO.getPatent().getESCustomField();
|
|
|
+ id = patentWithIdVO.getId();
|
|
|
+ } else {
|
|
|
+ //设置栏位
|
|
|
+ esCustomField.setField(esCustomFieldDTO.getFieldId());
|
|
|
+ //设置专题库或报告
|
|
|
+ esCustomField.setProjectId(esCustomFieldDTO.getProjectId());
|
|
|
+ //设置创建时间
|
|
|
+ esCustomField.setCreateTime(createDate);
|
|
|
+ //设置创建人
|
|
|
+ esCustomField.setPersonId("1");
|
|
|
+ //设置类型
|
|
|
+ esCustomField.setFieldType(fieldType);
|
|
|
+ //设置任务id
|
|
|
+ esCustomField.setTaskId(taskId);
|
|
|
+ }
|
|
|
+ List<String> values = esCustomField.getFieldValue();
|
|
|
+ //设置值
|
|
|
+ switch (fieldType) {
|
|
|
+ case 0:
|
|
|
+
|
|
|
+ ;
|
|
|
+ case 1:
|
|
|
+
|
|
|
+ ;
|
|
|
+ case 2: //文本类型
|
|
|
+ if (optionType.equals(1) || optionType.equals(2)) {
|
|
|
+ if (addValues != null && addValues.size() == 1 && !values.contains(addValues))
|
|
|
+ esCustomField.setFieldValue(esCustomFieldDTO.getFieldValue());
|
|
|
+ } else if (optionType.equals(0)) {
|
|
|
+ esCustomField.setFieldType(null);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 4: //单选
|
|
|
+ if (optionType.equals(1) || optionType.equals(2)) {
|
|
|
+ if (addValues != null && addValues.size() == 1 && !values.contains(addValues)) {
|
|
|
+ esCustomField.setStatsValue(addValues);
|
|
|
+ }
|
|
|
+ } else if (optionType.equals(0)) {
|
|
|
+ esCustomField.setFieldValue(null);
|
|
|
+ esCustomField.setStatsValue(null);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ if (optionType.equals(1)) {
|
|
|
+ if (addValues != null && addValues.size() == 1 && !values.contains(addValues)) {
|
|
|
+ esCustomField.setStatsValue(addValues);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 6: //树类型
|
|
|
+
|
|
|
+ case 7:
|
|
|
+
|
|
|
+ case 8:
|
|
|
+
|
|
|
+ case 9:
|
|
|
+ if (optionType.equals(1)) {
|
|
|
+ //根据值获得树节点
|
|
|
+ List<String> reValues = FormatUtil.getDistinctList(addValues, values);
|
|
|
+ //根据 reValues获得 reStateValues
|
|
|
+ List<Integer> fieldIds = FormatUtil.StringTOIntegerList(reValues);
|
|
|
+ //根据树节点id查询树节点
|
|
|
+ LambdaQueryWrapper<TreeNode> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(TreeNode::getId, fieldIds);
|
|
|
+ List<TreeNode> treeNodes = treeNodeService.list(queryWrapper);
|
|
|
+ //遍历节点
|
|
|
+ List<String> reStateValues = new ArrayList<>(reValues);
|
|
|
+ treeNodes.forEach(item -> {
|
|
|
+ String path = item.getPath();
|
|
|
+ if (path != null && path.trim() != "") {
|
|
|
+ List<String> a = Arrays.asList(path.split("/"));
|
|
|
+ reStateValues.addAll(a);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ List<String> disReStateValues = new ArrayList<>(new HashSet<>(reStateValues));
|
|
|
+ esCustomField.setStatsValue(disReStateValues);
|
|
|
+ } else if (optionType.equals(0)) {
|
|
|
+ if (values != null && values.size() > 0 && addValues != null && addValues.size() > 0) {
|
|
|
+ values.remove(addValues);
|
|
|
+ esCustomField.setFieldValue(values);
|
|
|
+ if (values.size() > 0) {
|
|
|
+ //根据 reValues获得 reStateValues
|
|
|
+ List<Integer> fieldIds = FormatUtil.StringTOIntegerList(values);
|
|
|
+ //根据树节点id查询树节点
|
|
|
+ LambdaQueryWrapper<TreeNode> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(TreeNode::getId, fieldIds);
|
|
|
+ List<TreeNode> treeNodes = treeNodeService.list(queryWrapper);
|
|
|
+ //遍历节点
|
|
|
+ List<String> reStateValues = new ArrayList<>(values);
|
|
|
+ treeNodes.forEach(item -> {
|
|
|
+ String path = item.getPath();
|
|
|
+ if (path != null && path.trim() != "") {
|
|
|
+ List<String> a = Arrays.asList(path.split("/"));
|
|
|
+ reStateValues.addAll(a);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ List<String> disReStateValues = new ArrayList<>(new HashSet<>(reStateValues));
|
|
|
+ esCustomField.setStatsValue(disReStateValues);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (optionType.equals(2)) {
|
|
|
+ esCustomField.setFieldValue(addValues);
|
|
|
+ if (addValues == null || addValues.size() == 0) {
|
|
|
+ esCustomField.setStatsValue(new ArrayList<>());
|
|
|
+ } else {
|
|
|
+ //根据 reValues获得 reStateValues
|
|
|
+ List<Integer> fieldIds = FormatUtil.StringTOIntegerList(addValues);
|
|
|
+ //根据树节点id查询树节点
|
|
|
+ LambdaQueryWrapper<TreeNode> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(TreeNode::getId, fieldIds);
|
|
|
+ List<TreeNode> treeNodes = treeNodeService.list(queryWrapper);
|
|
|
+ //遍历节点
|
|
|
+ List<String> reStateValues = new ArrayList<>(addValues);
|
|
|
+ treeNodes.forEach(item -> {
|
|
|
+ String path = item.getPath();
|
|
|
+ if (path != null && path.trim() != "") {
|
|
|
+ List<String> a = Arrays.asList(path.split("/"));
|
|
|
+ reStateValues.addAll(a);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ List<String> disReStateValues = new ArrayList<>(new HashSet<>(reStateValues));
|
|
|
+ esCustomField.setStatsValue(disReStateValues);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!id.trim().equals("")) {
|
|
|
+ Patent patent = patentWithIdVO.getPatent();
|
|
|
+ patent.setESCustomField(esCustomField);
|
|
|
+ esService.updatePatent(patent, patentWithIdVO.getId());
|
|
|
+ ESCustomFieldHistory esCustomFieldHistory = new ESCustomFieldHistory();
|
|
|
+ esCustomFieldHistory.setField(fieldId);
|
|
|
+ esCustomFieldHistory.setFieldType(fieldType);
|
|
|
+ esCustomFieldHistory.setCreateTime(createDate);
|
|
|
+ esCustomFieldHistory.setFieldValue(addValues);
|
|
|
+ esCustomFieldHistory.setHistoryType(optionType);
|
|
|
+ esCustomFieldHistory.setTaskId(taskId);
|
|
|
+ esCustomFieldHistory.setPersonId("1");
|
|
|
+ esCustomFieldHistory.setProjectId(projectId);
|
|
|
+ esCustomFieldHistory.setCustomFieldId(id);
|
|
|
+ this.addCustomFieldHistory(esCustomFieldHistory);
|
|
|
+ } else {
|
|
|
+ this.addEsCustomFieldToEs(esCustomField, patentNo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //添加操作历史
|
|
|
+ public String addCustomFieldHistory(ESCustomFieldHistory customFieldHistory) throws Exception {
|
|
|
+ IndexResponse indexResponse = client.index(i -> i
|
|
|
+ .index("custom_field_history")
|
|
|
+ .document(customFieldHistory)
|
|
|
+ );
|
|
|
+ return indexResponse.id();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询自定义字段
|
|
|
+ public PatentWithIdVO getEsCustomField(EsCustomFieldDTO esCustomFieldDTO, String patentNo) throws Exception {
|
|
|
+ PatentWithIdVO patentWithIdVO = new PatentWithIdVO();
|
|
|
+ Integer projectId = esCustomFieldDTO.getProjectId();
|
|
|
+ Integer taskId = esCustomFieldDTO.getTaskId();
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ //设置查询索引
|
|
|
+ builder.index("patent");
|
|
|
+
|
|
|
+ //专利号
|
|
|
+ Query q1 = QueryBuilders.term(t -> t.field("custom_field.project_id").value(projectId));
|
|
|
+ //栏位
|
|
|
+ Query q2 = QueryBuilders.term((t -> t.field("custom_field.field").value(esCustomFieldDTO.getFieldId())));
|
|
|
+
|
|
|
+ Query q4 = QueryBuilders.term(t -> t.field("custom_field.field_type").value(esCustomFieldDTO.getFieldType()));
|
|
|
+
|
|
|
+ Query parentQ1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
|
|
|
+ //公开号
|
|
|
+ Query parentQ2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
|
|
|
+ //授权号
|
|
|
+ Query parentQ3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
|
|
|
+
|
|
|
+ Query parentQ = QueryBuilders.bool(i -> i.should(parentQ1, parentQ2, parentQ3));
|
|
|
+
|
|
|
+ Query q3 = QueryBuilders.hasParent(t -> t.parentType("patent").query(parentQ));
|
|
|
+ Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3, q4));
|
|
|
+
|
|
|
+ builder.query(bool);
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ long total = response.hits().total().value();
|
|
|
+ if (total > 0) {
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
+ Hit<Patent> hit = hits.get(0);
|
|
|
+ Patent patent = hit.source();
|
|
|
+ patentWithIdVO.setPatent(patent);
|
|
|
+ patentWithIdVO.setId(hit.id());
|
|
|
+ } else {
|
|
|
+ patentWithIdVO = null;
|
|
|
+ }
|
|
|
+ return patentWithIdVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ //将自定义字段添加到专利
|
|
|
+ public String addEsCustomFieldToEs(ESCustomField esCustomField, String patentNo) throws Exception {
|
|
|
+ //根据专利号查询专利
|
|
|
+ PatentWithIdVO patentWithIdVO = esService.getIdByPatentNo(patentNo);
|
|
|
+ String patentId = patentWithIdVO.getId();
|
|
|
+ Patent patentChild = new Patent();
|
|
|
+ PatentJoin patentJoin = new PatentJoin();
|
|
|
+ patentJoin.setParent(patentId);
|
|
|
+ patentJoin.setName("project_customfield");
|
|
|
+ patentChild.setPatentJoin(patentJoin);
|
|
|
+ patentChild.setESCustomField(esCustomField);
|
|
|
+ String id = esService.addChildPatent(patentChild, patentId);
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据专利号 和 projectId ,以及栏位类型栏位值查询所有customField
|
|
|
+ */
|
|
|
+ public List<QueryESCustomFieldVO> getEsCustomField(QueryEsCustomFieldDTO queryEsCustomFieldDTO) throws Exception {
|
|
|
+ List<QueryESCustomFieldVO> vos = new ArrayList<>();
|
|
|
+
|
|
|
+ String fieldId = queryEsCustomFieldDTO.getFieldId();
|
|
|
+ Integer fieldType = queryEsCustomFieldDTO.getFieldType();
|
|
|
+ String patentNo = queryEsCustomFieldDTO.getPatentNo();
|
|
|
+ Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
|
|
|
+
|
|
|
+ Query parentQ1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
|
|
|
+ //公开号
|
|
|
+ Query parentQ2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
|
|
|
+ //授权号
|
|
|
+ Query parentQ3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
|
|
|
+
|
|
|
+ Query parentQ = QueryBuilders.bool(i -> i.should(parentQ1, parentQ2, parentQ3));
|
|
|
+ Query q3 = QueryBuilders.hasParent(t -> t.parentType("patent").query(parentQ));
|
|
|
+ Query bool = null;
|
|
|
+ if (fieldId != null && fieldType != null) {
|
|
|
+ Query q2 = QueryBuilders.term((t -> t.field("custom_field.field").value(fieldId)));
|
|
|
+ Query q4 = QueryBuilders.term(t -> t.field("custom_field.field_type").value(fieldType));
|
|
|
+ bool = QueryBuilders.bool(i -> i.must(q1, q2, q3, q4));
|
|
|
+ } else {
|
|
|
+ bool = QueryBuilders.bool(i -> i.must(q1, q3));
|
|
|
+ }
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ builder.query(bool);
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ long total = response.hits().total().value();
|
|
|
+ if (total > 0) {
|
|
|
+ List<Hit<Patent>> hits = response.hits().hits();
|
|
|
+ hits.forEach(item -> {
|
|
|
+ ESCustomField esCustomField = item.source().getESCustomField();
|
|
|
+ QueryESCustomFieldVO queryESCustomFieldVO = new QueryESCustomFieldVO();
|
|
|
+ queryESCustomFieldVO.setField(esCustomField.getField());
|
|
|
+ queryESCustomFieldVO.setFieldType(esCustomField.getFieldType());
|
|
|
+ queryESCustomFieldVO.setFieldValue(esCustomField.getFieldValue());
|
|
|
+ vos.add(queryESCustomFieldVO);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ return vos;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getEsCustomFieldId(FieldHistoryDTO fieldHistoryDTO) throws Exception {
|
|
|
+ String id = "";
|
|
|
+ List<QueryESCustomFieldVO> vos = new ArrayList<>();
|
|
|
+
|
|
|
+ String fieldId = fieldHistoryDTO.getFieldId();
|
|
|
+ Integer fieldType = fieldHistoryDTO.getFieldType();
|
|
|
+ String patentNo = fieldHistoryDTO.getPatentNo();
|
|
|
+ Query q1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
|
|
|
+
|
|
|
+ Query parentQ1 = QueryBuilders.term(t -> t.field("app_no.keyword").value(patentNo));
|
|
|
+ //公开号
|
|
|
+ Query parentQ2 = QueryBuilders.term(t -> t.field("public_no.keyword").value(patentNo));
|
|
|
+ //授权号
|
|
|
+ Query parentQ3 = QueryBuilders.term(t -> t.field("grant_no.keyword").value(patentNo));
|
|
|
+
|
|
|
+ Query parentQ = QueryBuilders.bool(i -> i.should(parentQ1, parentQ2, parentQ3));
|
|
|
+ Query q3 = QueryBuilders.hasParent(t -> t.parentType("patent").query(parentQ));
|
|
|
+
|
|
|
+
|
|
|
+ Query q2 = QueryBuilders.term((t -> t.field("custom_field.field").value(fieldId)));
|
|
|
+ Query q4 = QueryBuilders.term(t -> t.field("custom_field.field_type").value(fieldType));
|
|
|
+ Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3, q4));
|
|
|
+
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ builder.query(bool);
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ long total = response.hits().total().value();
|
|
|
+ if (total > 0) {
|
|
|
+ id = response.hits().hits().get(0).id();
|
|
|
+
|
|
|
+ }
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询历史
|
|
|
+ */
|
|
|
+ public List<FieldHistoryVO> getCustomFieldHistory(FieldHistoryDTO fieldHistoryDTO) throws Exception {
|
|
|
+ List<FieldHistoryVO> fieldHistoryVOS =new ArrayList<>();
|
|
|
+ String customId = this.getEsCustomFieldId(fieldHistoryDTO);
|
|
|
+ Query q1 = QueryBuilders.term(t -> t.field("custom_field_id").value(customId));
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ builder.query(q1);
|
|
|
+ builder.sort(sortOptionsBuilder -> sortOptionsBuilder
|
|
|
+ .field(fieldSortBuilder -> fieldSortBuilder
|
|
|
+ .field("create_time").order(SortOrder.Desc)));
|
|
|
+ SearchResponse<ESCustomFieldHistory> response = client.search(builder.build(), ESCustomFieldHistory.class);
|
|
|
+ long total = response.hits().total().value();
|
|
|
+ if (total > 0) {
|
|
|
+ List<Hit<ESCustomFieldHistory>> hits = response.hits().hits();
|
|
|
+ hits.forEach(item -> {
|
|
|
+ FieldHistoryVO fieldHistoryVO = new FieldHistoryVO();
|
|
|
+ BeanUtils.copyProperties(item.source(),fieldHistoryVO);
|
|
|
+ fieldHistoryVOS.add(fieldHistoryVO);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ return fieldHistoryVOS;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回退
|
|
|
+ */
|
|
|
+ public void BackTo(String historyId) throws Exception{
|
|
|
+ //根据历史id获得历史
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ builder.index("patent");
|
|
|
+ Query q =QueryBuilders.ids(i->i.values(Arrays.asList(historyId)));
|
|
|
+ builder.query(q);
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ long total = response.hits().total().value();
|
|
|
+ System.out.println(total);
|
|
|
+ //根据历史的创建时间,查询出所有创建时间大于当前创建时间的历史
|
|
|
+ //遍历历史,根据操作类型,反推
|
|
|
+ //根据反推内容保存并保存历史为回退;
|
|
|
+ }
|
|
|
+}
|