From a1ad89c1367edf95bf488002480f1dc5bb963b79 Mon Sep 17 00:00:00 2001
From: Pancaihua <75729660@qq.com>
Date: Mon, 12 Aug 2024 20:59:10 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=97=A5=E5=BF=97?=
=?UTF-8?q?=E8=BE=93=E5=87=BA=E7=95=8C=E9=9D=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
yudao-dependencies/pom.xml | 13 ++
yudao-framework/yudao-common/pom.xml | 15 ++
.../common/util/elasticsearch/BaseSaveVo.java | 5 +
.../util/elasticsearch/BaseSearchVo.java | 14 ++
.../util/elasticsearch/EsearchUtils.java | 144 ++++++++++++++++++
.../banner/core/BannerApplicationRunner.java | 14 +-
yudao-module-bpm/pom.xml | 13 +-
.../module/bpm/enums/ErrorCodeConstants.java | 1 -
.../admin/knows/vo/KnowledgeSaveReqVO.java | 3 +-
.../service/knows/KnowledgeServiceImpl.java | 43 +++++-
10 files changed, 251 insertions(+), 14 deletions(-)
create mode 100644 yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSaveVo.java
create mode 100644 yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSearchVo.java
create mode 100644 yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java
diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml
index 1905342..f049278 100644
--- a/yudao-dependencies/pom.xml
+++ b/yudao-dependencies/pom.xml
@@ -76,6 +76,7 @@
1.7.8
2.12.2
4.6.0
+ 7.17.15
@@ -632,6 +633,18 @@
${xercesImpl.version}
+
+
+ org.springframework.boot
+ spring-boot-starter-data-elasticsearch
+ ${spring.boot.version}
+
+
+ org.elasticsearch.client
+ elasticsearch-rest-high-level-client
+ ${elasticsearch-client.version}
+
+
diff --git a/yudao-framework/yudao-common/pom.xml b/yudao-framework/yudao-common/pom.xml
index f364df7..10c1f05 100644
--- a/yudao-framework/yudao-common/pom.xml
+++ b/yudao-framework/yudao-common/pom.xml
@@ -138,6 +138,21 @@
easy-trans-anno
+
+
+ org.springframework.boot
+ spring-boot-starter-data-elasticsearch
+
+
+ org.elasticsearch.client
+ elasticsearch-rest-high-level-client
+
+
+ com.google.guava
+ guava
+
+
+
org.springframework.boot
diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSaveVo.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSaveVo.java
new file mode 100644
index 0000000..ff46754
--- /dev/null
+++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSaveVo.java
@@ -0,0 +1,5 @@
+package cn.iocoder.yudao.framework.common.util.elasticsearch;
+
+public interface BaseSaveVo {
+ Long getId();
+}
diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSearchVo.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSearchVo.java
new file mode 100644
index 0000000..d963975
--- /dev/null
+++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/BaseSearchVo.java
@@ -0,0 +1,14 @@
+package cn.iocoder.yudao.framework.common.util.elasticsearch;
+
+public interface BaseSearchVo {
+ String getSearchBy();
+
+ Integer getPageSize();
+ String getSortBy();
+
+ String getSortType();
+
+ String getSearchKey();
+
+ String getSearchValue();
+}
diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java
new file mode 100644
index 0000000..48268b3
--- /dev/null
+++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/elasticsearch/EsearchUtils.java
@@ -0,0 +1,144 @@
+package cn.iocoder.yudao.framework.common.util.elasticsearch;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.google.common.collect.Lists;
+import java.util.List;
+
+import org.apache.http.HttpHost;
+import org.elasticsearch.client.RestClient;
+import org.elasticsearch.xcontent.XContentType;
+import org.elasticsearch.action.delete.DeleteRequest;
+import org.elasticsearch.action.delete.DeleteResponse;
+import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.index.IndexResponse;
+import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.action.support.WriteRequest;
+import org.elasticsearch.action.update.UpdateRequest;
+import org.elasticsearch.action.update.UpdateResponse;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestHighLevelClient;
+
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.elasticsearch.search.sort.SortOrder;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * ElasticSearch 客户端操作类,适用于
+ *
+ * @author pch
+ */
+public class EsearchUtils {
+ private static String ipStr = "127.0.0.1";
+ private static int port = 9200;
+ private static String modeStr = "http";
+ private static RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(ipStr,port,modeStr)));
+
+ public static void saveIndex(long id,T createReqVO, String indexName) throws IOException {
+ String indexStatus = null;
+ // Long kId = id;
+ String knowStr = JSON.toJSONString(createReqVO);
+ JSONObject knowObj = JSONObject.parseObject(knowStr);
+ knowObj.put("id", String.valueOf(id));
+ IndexRequest esRequest = new IndexRequest( indexName );
+ esRequest.id(String.valueOf(id));
+ esRequest.source(knowObj.toJSONString(), XContentType.JSON);
+ //System.out.println("========保存数据:" + knowObj.toJSONString());
+ esRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
+ //System.out.println("========保存结果:" + indexResult);
+ try {
+ IndexResponse indexResult = esClient.index(esRequest, RequestOptions.DEFAULT);
+ //indexStatus = indexResult.status().toString();
+ }catch (IOException e) {
+ System.err.println(("EsearchUtils:saveIndex函数保存异常..."));
+ e.printStackTrace();
+ }
+ }
+
+ public static void updateIndex(long id,T createReqVO, String indexName){
+ JSONObject kmsMainObj = (JSONObject) JSONObject.toJSON(createReqVO);
+ //kmsMainObj.put("createTime",createReqVO.getCreateTime()!=null?createReqVO.getCreateTime().getTime():0L);
+ //kmsMainObj.put("publishDate",createReqVO.getPublishDate()!=null?createReqVO.getPublishDate().getTime():0L);
+ UpdateRequest updateRequest = new UpdateRequest(indexName, id+"").doc(kmsMainObj);
+ updateRequest.docAsUpsert(true);
+ UpdateResponse updateResponse = null;
+ try {
+ updateResponse = esClient.update(updateRequest, RequestOptions.DEFAULT);
+ System.out.println("知识更新:" + updateResponse.status().toString());
+ } catch (IOException e) {
+ System.err.println(("EsearchUtils:updateIndex函数更新异常..."));
+ e.printStackTrace();
+ }
+ }
+
+ public static String deleteKmsMainById(Long id,String indexName)
+ {
+ DeleteRequest deleteRequest = new DeleteRequest(indexName, id.toString());
+ deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
+ DeleteResponse delete = null;
+ try {
+ delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
+ System.out.println(("删除结果:" + delete.status().toString()));
+ } catch (IOException e) {
+ System.err.println(("EsearchUtils:deleteKmsMainById删除异常..."));
+ e.printStackTrace();
+ }
+ return delete.status().toString();
+ }
+ public static List selectForEs(T1 kmsSearchDto,T2 KmsMainVo, String indexName ) {
+ // 1.创建并设置SearchSourceBuilder对象
+ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
+ // 2.创建BoolQueryBuilder对象
+ BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
+ // 3.设置boolQueryBuilder条件
+ //MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue());
+ // 4.添加查询条件到boolQueryBuilder中
+ //boolQueryBuilder.must(matchPhraseQueryBuilder);
+ if("1".equals(kmsSearchDto.getSearchBy())) {
+ boolQueryBuilder.should(QueryBuilders.termsQuery(kmsSearchDto.getSearchKey(), kmsSearchDto.getSearchValue()));
+ }
+ else if("2".equals(kmsSearchDto.getSearchBy())) {
+ boolQueryBuilder.should(QueryBuilders.wildcardQuery(kmsSearchDto.getSearchKey(), "*" + kmsSearchDto.getSearchValue() + "*"));
+ }
+ // 查询条件--->生成DSL查询语句
+ searchSourceBuilder.query(boolQueryBuilder);
+ Integer pageNum = 1;
+ Integer pageSize = kmsSearchDto.getPageSize();
+ // 第几页
+ searchSourceBuilder.from((pageNum - 1) * pageSize);
+ // 每页多少条数据
+ searchSourceBuilder.size(pageSize);
+ // 设置排序规则
+ String sortBy = kmsSearchDto.getSortBy();
+ SortOrder sortType = kmsSearchDto.getSortType().equals("1") ? SortOrder.DESC : SortOrder.ASC;
+ searchSourceBuilder.sort(sortBy, sortType);
+ SearchRequest searchRequest = new SearchRequest(indexName);
+ searchRequest.source(searchSourceBuilder);
+
+ List rsList = Lists.newArrayList();
+ //返回信息
+// List rsList = Lists.newArrayList();
+// try {
+// SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
+// //解析搜索结果
+// for (SearchHit sh : response.getHits()) {
+// Map sourceAsMap = sh.getSourceAsMap();
+// /*for (Map.Entry stringObjectEntry : sourceAsMap.entrySet()) {
+// System.out.println(sourceAsMap.get(stringObjectEntry.getKey()));
+// }*/
+// KmsMainVo main = JSON.parseObject(JSON.toJSONString(sourceAsMap), KmsMainVo.class);
+// rsList.add(main);
+// }
+// } catch (IOException e) {
+// e.printStackTrace();
+// }
+ return rsList;
+ }
+}
diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java
index 2e7d42a..2b1b4b9 100644
--- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java
+++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/banner/core/BannerApplicationRunner.java
@@ -20,15 +20,15 @@ public class BannerApplicationRunner implements ApplicationRunner {
public void run(ApplicationArguments args) {
ThreadUtil.execute(() -> {
ThreadUtil.sleep(1, TimeUnit.SECONDS); // 延迟 1 秒,保证输出到结尾
- log.info("\n----------------------------------------------------------\n\t" +
+ log.info("\n----------------程序猿------------------------------------------\n\t" +
"写字楼里写字间,写字间里程序员; \n\t" +
- "程序人员写程序,又拿程序换酒钱。\n\t" +
- "酒醒只在网上坐,酒醉还来网下眠; \n\t" +
+ "程序员来写程序,又拿程序换酒钱。\n\t" +
+ "酒醒只在网上坐,酒醉还在网下眠; \n\t" +
"酒醉酒醒日复日,网上网下年复年。\n\t" +
- "但愿老死电脑间,不愿鞠躬客户前;\n\t" +
- "奔驰宝马贵者趣,公交地铁程序员。\n\t" +
- "别人笑我忒疯癫,我笑自己命太贱;\n\t" +
- "不见满街漂亮妹,哪个归得程序员?\n\t" +
+ "但愿老死程序间,不愿匍匐客户前;\n\t" +
+ "宝马奔驰富者趣,地铁公交程序员。\n\t" +
+ "别人笑我忒疯癫,我笑他人程序烂;\n\t" +
+ "不见满街小姐姐,哪个归得程序员?\n\t" +
"---------------------------------------------------------OK");
// log.info("\n----------------------------------------------------------\n\t" +
diff --git a/yudao-module-bpm/pom.xml b/yudao-module-bpm/pom.xml
index 95b7989..33277f3 100644
--- a/yudao-module-bpm/pom.xml
+++ b/yudao-module-bpm/pom.xml
@@ -23,5 +23,16 @@
工作流基于 Flowable 6 实现,分成流程定义、流程表单、流程实例、流程任务等功能模块。
-
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-elasticsearch
+
+
+ org.elasticsearch.client
+ elasticsearch-rest-high-level-client
+
+
+
diff --git a/yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/ErrorCodeConstants.java b/yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/ErrorCodeConstants.java
index f37462e..662437d 100644
--- a/yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/ErrorCodeConstants.java
+++ b/yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/ErrorCodeConstants.java
@@ -89,5 +89,4 @@ public interface ErrorCodeConstants {
ErrorCode STAR2_NOT_EXISTS = new ErrorCode(1_009_018_000, "收藏不存在");
// ========== 点赞管理 我的收藏 1_009_019_000 ==========
ErrorCode TBUP_NOT_EXISTS = new ErrorCode( 1_009_019_000, "点赞不存在");
-
}
diff --git a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/knows/vo/KnowledgeSaveReqVO.java b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/knows/vo/KnowledgeSaveReqVO.java
index d2e2609..60ac750 100644
--- a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/knows/vo/KnowledgeSaveReqVO.java
+++ b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/knows/vo/KnowledgeSaveReqVO.java
@@ -7,6 +7,7 @@ import java.util.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.knows.CommentDO;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.NotEmpty;
+import cn.iocoder.yudao.framework.common.util.elasticsearch.BaseSaveVo;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
@@ -15,7 +16,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.IOException;
@Schema(description = "管理后台 - 知识发布新增/修改 Request VO")
@Data
-public class KnowledgeSaveReqVO {
+public class KnowledgeSaveReqVO {
@Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED, example = "7888")
private Long id;
diff --git a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java
index 0ce0a05..dfd9ae4 100644
--- a/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java
+++ b/yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/knows/KnowledgeServiceImpl.java
@@ -3,11 +3,13 @@ package cn.iocoder.yudao.module.bpm.service.knows;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
-import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOALeaveDO;
import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum;
+
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
+
+import java.io.IOException;
import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.knows.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.knows.KnowledgeDO;
@@ -19,6 +21,8 @@ import cn.iocoder.yudao.module.bpm.dal.mysql.knows.CommentMapper;
import javax.annotation.Resource;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
+import cn.iocoder.yudao.framework.common.util.elasticsearch.EsearchUtils;
+
/**
* 知识发布 Service 实现类
@@ -35,9 +39,12 @@ public class KnowledgeServiceImpl implements KnowledgeService {
private CommentMapper commentMapper;
@Resource
private BpmProcessInstanceApi processInstanceApi;
+
+ private boolean isEsFlag = true;
+ private String indexName = "knows_index";
@Override
@Transactional(rollbackFor = Exception.class)
- public Long createKnowledge(Long userId,KnowledgeSaveReqVO createReqVO) {
+ public Long createKnowledge(Long userId,KnowledgeSaveReqVO createReqVO){
// 插入
KnowledgeDO knowledge = BeanUtils.toBean(createReqVO, KnowledgeDO.class)
.setUserId(userId).setFlowStatus(BpmTaskStatusEnum.RUNNING.getStatus());;
@@ -53,6 +60,14 @@ public class KnowledgeServiceImpl implements KnowledgeService {
// 将工作流的编号,更新到 OA 请假单中
knowledgeMapper.updateById(new KnowledgeDO().setId(knowledge.getId()).setProcessInstanceId(processInstanceId));
+ try {
+ if (isEsFlag){
+ EsearchUtils.saveIndex(knowledge.getId(),createReqVO,indexName);
+ }
+ //saveIndex(createReqVO);
+ } catch (IOException e) {
+ e.printStackTrace(); // 处理异常,例如打印错误信息
+ }
// 返回
return knowledge.getId();
}
@@ -65,7 +80,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
// 更新
KnowledgeDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgeDO.class);
knowledgeMapper.updateById(updateObj);
-
+ if (isEsFlag){
+ EsearchUtils.updateIndex(updateReqVO.getId(),updateReqVO,indexName);
+ }
// 更新子表
updateCommentList(updateReqVO.getId(), updateReqVO.getComments());
}
@@ -77,7 +94,9 @@ public class KnowledgeServiceImpl implements KnowledgeService {
validateKnowledgeExists(id);
// 删除
knowledgeMapper.deleteById(id);
-
+ if (isEsFlag){
+ EsearchUtils.deleteKmsMainById(id,indexName);
+ }
// 删除子表
deleteCommentByKnowId(id);
}
@@ -147,4 +166,20 @@ public class KnowledgeServiceImpl implements KnowledgeService {
validateLeaveExists(id);
knowledgeMapper.updateById(new KnowledgeDO().setId(id).setFlowStatus(status));
}
+
+// private void saveIndex(KnowledgeSaveReqVO createReqVO) throws IOException {
+// String indexStatus = null;
+// Long kId = createReqVO.getId();
+// String knowStr = JSON.toJSONString(createReqVO);
+// JSONObject knowObj = JSONObject.parseObject(knowStr);
+// knowObj.put("id", String.valueOf(kId));
+// IndexRequest esRequest = new IndexRequest("knows_index");
+// esRequest.id(String.valueOf(kId));
+// esRequest.source(knowObj.toJSONString(), XContentType.JSON);
+// System.out.println("========保存数据:" + knowObj.toJSONString());
+// esRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
+// IndexResponse indexResult = esClient.index(esRequest, RequestOptions.DEFAULT);
+// indexStatus = indexResult.status().toString();
+// System.out.println("========保存结果:" + indexResult);
+// }
}