修改了日志输出界面

This commit is contained in:
Pancaihua 2024-08-12 20:59:10 +08:00
parent d06a1a9c10
commit a1ad89c136
10 changed files with 251 additions and 14 deletions

View File

@ -76,6 +76,7 @@
<jimureport.version>1.7.8</jimureport.version>
<xercesImpl.version>2.12.2</xercesImpl.version>
<weixin-java.version>4.6.0</weixin-java.version>
<elasticsearch-client.version>7.17.15</elasticsearch-client.version>
</properties>
<dependencyManagement>
@ -632,6 +633,18 @@
<version>${xercesImpl.version}</version>
</dependency>
<!-- elasticsearch start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch-client.version}</version>
</dependency>
<!-- elasticsearch end -->
</dependencies>
</dependencyManagement>

View File

@ -138,6 +138,21 @@
<artifactId>easy-trans-anno</artifactId> <!-- 默认引入的原因,方便 xxx-module-api 包使用 -->
</dependency>
<!-- es start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<!-- es end -->
<!-- Test 测试相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -0,0 +1,5 @@
package cn.iocoder.yudao.framework.common.util.elasticsearch;
public interface BaseSaveVo {
Long getId();
}

View File

@ -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();
}

View File

@ -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 <T> 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 <T> 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 <T1 extends BaseSearchVo,T2 extends BaseSaveVo> List<String> 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<String> rsList = Lists.newArrayList();
//返回信息
// List<KmsMainVo> rsList = Lists.newArrayList();
// try {
// SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
// //解析搜索结果
// for (SearchHit sh : response.getHits()) {
// Map<String, Object> sourceAsMap = sh.getSourceAsMap();
// /*for (Map.Entry<String, Object> 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;
}
}

View File

@ -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" +

View File

@ -23,5 +23,16 @@
工作流基于 Flowable 6 实现,分成流程定义、流程表单、流程实例、流程任务等功能模块。
</description>
<dependencies>
<!-- es start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<!-- es end -->
</dependencies>
</project>

View File

@ -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, "点赞不存在");
}

View File

@ -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;

View File

@ -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,6 +39,9 @@ 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){
@ -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);
// }
}