|
@@ -1,19 +1,30 @@
|
|
|
package com.wtkj.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.wtkj.dto.SearchProjectFileDTO;
|
|
|
import com.wtkj.dto.UploadFileDTO;
|
|
|
import com.wtkj.dto.UploadInnerFileDTO;
|
|
|
import com.wtkj.entity.FileAndFolder;
|
|
|
import com.wtkj.entity.FileAndFolderHistory;
|
|
|
import com.wtkj.entity.InnerFileAndFolder;
|
|
|
-import com.wtkj.service.ICommonService;
|
|
|
-import com.wtkj.service.IFileAndFolderHistoryService;
|
|
|
-import com.wtkj.service.IFileAndFolderService;
|
|
|
-import com.wtkj.service.IInnerFileAndFolderService;
|
|
|
+import com.wtkj.entity.ProjectAuth;
|
|
|
+import com.wtkj.handler.es.Document;
|
|
|
+import com.wtkj.service.*;
|
|
|
import com.wutong.file.feign.IFileClient;
|
|
|
import com.wutong.file.vo.FileVO;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.elasticsearch.action.search.SearchRequest;
|
|
|
+import org.elasticsearch.action.search.SearchResponse;
|
|
|
+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.SearchHits;
|
|
|
+import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
import org.springblade.core.secure.utils.AuthUtil;
|
|
|
import org.springblade.core.tool.api.R;
|
|
@@ -23,10 +34,10 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
+import static com.wtkj.config.MagicValue.PROJECT_RESOURCE_INDEX;
|
|
|
import static com.wtkj.config.MagicValue.TWO;
|
|
|
|
|
|
/**
|
|
@@ -42,6 +53,8 @@ public class CommonServiceImpl implements ICommonService {
|
|
|
private final IFileAndFolderService fileAndFolderService;
|
|
|
private final IFileAndFolderHistoryService historyService;
|
|
|
private final IInnerFileAndFolderService innerFileAndFolderService;
|
|
|
+ private final IProjectAuthService projectAuthService;
|
|
|
+ private final RestHighLevelClient client;
|
|
|
|
|
|
|
|
|
@Override
|
|
@@ -232,4 +245,118 @@ public class CommonServiceImpl implements ICommonService {
|
|
|
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @Override
|
|
|
+ public IPage<FileAndFolder> esSearch(SearchProjectFileDTO dto, IPage<FileAndFolder> page) {
|
|
|
+ LambdaQueryWrapper<FileAndFolder> lqw = new LambdaQueryWrapper<>();
|
|
|
+ Long deptId = dto.getTopDeptId();
|
|
|
+ String text = dto.getText();
|
|
|
+ Long projectId = dto.getProjectId();
|
|
|
+ Long createUser = dto.getCreateUser();
|
|
|
+ Set<Long> projectIds = new HashSet<>();
|
|
|
+ if (projectId != null && projectId > 0L) {
|
|
|
+ projectIds.add(projectId);
|
|
|
+ } else {
|
|
|
+ //如果projectId为空 则查询机构下所有项目
|
|
|
+ //此机构下的项目
|
|
|
+ List<ProjectAuth> auths = projectAuthService.getByDeptAndUser(deptId, null, null);
|
|
|
+ if (!CollectionUtils.isEmpty(auths)) {
|
|
|
+ Set<Long> ids = auths.stream().map(ProjectAuth::getProjectId).filter(Objects::nonNull).collect(Collectors.toSet());
|
|
|
+ projectIds.addAll(ids);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // select * from xxx where projectId in (...) and create_user = #{createUser} and (title like %text% or content like %text%)
|
|
|
+
|
|
|
+ SearchRequest request = new SearchRequest();
|
|
|
+ request.indices(PROJECT_RESOURCE_INDEX);
|
|
|
+ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
+
|
|
|
+ // 1.创建BoolQueryBuilder
|
|
|
+ BoolQueryBuilder condition = QueryBuilders.boolQuery();
|
|
|
+
|
|
|
+ //2. 添加must子句
|
|
|
+ condition.must(QueryBuilders.termsQuery("projectId", projectIds));
|
|
|
+ if (createUser != null) {
|
|
|
+ condition.must(QueryBuilders.termQuery("createUser", createUser));
|
|
|
+ }
|
|
|
+ if (StringUtil.isNotBlank(text)) {
|
|
|
+ // 3.添加should子句
|
|
|
+ condition.should(QueryBuilders.matchQuery("title", text));
|
|
|
+ condition.should(QueryBuilders.matchQuery("content", text));
|
|
|
+ }
|
|
|
+ searchSourceBuilder.query(condition);
|
|
|
+ searchSourceBuilder.from(0);
|
|
|
+ searchSourceBuilder.size(100);
|
|
|
+ request.source(searchSourceBuilder);
|
|
|
+
|
|
|
+ //执行查询
|
|
|
+ SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
|
|
+ Set<Document> set = new HashSet<>();
|
|
|
+ if (response != null) {
|
|
|
+ SearchHits hits = response.getHits();
|
|
|
+ int size = hits.getHits().length;
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ String str = hits.getHits()[i].getSourceAsString();
|
|
|
+ Document document = JSON.parseObject(str, Document.class);
|
|
|
+ set.add(document);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(set)) {
|
|
|
+ Set<Long> fileIds = set.stream().map(Document::getFileId).collect(Collectors.toSet());
|
|
|
+ lqw.in(FileAndFolder::getBladeFileId, fileIds);
|
|
|
+ }
|
|
|
+ return fileAndFolderService.page(page, lqw);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @Override
|
|
|
+ public Object openSearchFile(Long projectId, Long fileId, String text) {
|
|
|
+ Long userId = AuthUtil.getUserId();
|
|
|
+ List<ProjectAuth> list = projectAuthService.getByUserId(userId);
|
|
|
+ if (!CollectionUtils.isEmpty(list)) {
|
|
|
+ Set<Long> collect = list.stream().map(ProjectAuth::getProjectId).collect(Collectors.toSet());
|
|
|
+ if (!collect.contains(projectId)) {
|
|
|
+ throw new ServiceException("对不起,你暂未拥有此项目权限");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SearchRequest request = new SearchRequest();
|
|
|
+ request.indices(PROJECT_RESOURCE_INDEX);
|
|
|
+ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
+
|
|
|
+ // 1.创建BoolQueryBuilder
|
|
|
+ BoolQueryBuilder condition = QueryBuilders.boolQuery();
|
|
|
+
|
|
|
+ //2. 添加must子句
|
|
|
+ condition.must(QueryBuilders.termQuery("projectId", projectId));
|
|
|
+ condition.must(QueryBuilders.termQuery("fileId", fileId));
|
|
|
+ condition.must(QueryBuilders.matchQuery("title", text));
|
|
|
+
|
|
|
+ searchSourceBuilder.query(condition);
|
|
|
+ searchSourceBuilder.from(0);
|
|
|
+ searchSourceBuilder.size(100);
|
|
|
+ request.source(searchSourceBuilder);
|
|
|
+
|
|
|
+ searchSourceBuilder.query(condition);
|
|
|
+ searchSourceBuilder.from(0);
|
|
|
+ searchSourceBuilder.size(100);
|
|
|
+ request.source(searchSourceBuilder);
|
|
|
+
|
|
|
+ //执行查询
|
|
|
+ SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
|
|
+ Set<Document> set = new HashSet<>();
|
|
|
+ if (response != null) {
|
|
|
+ SearchHits hits = response.getHits();
|
|
|
+ int size = hits.getHits().length;
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ String str = hits.getHits()[i].getSourceAsString();
|
|
|
+ Document document = JSON.parseObject(str, Document.class);
|
|
|
+ set.add(document);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|