123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- 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.entity.ProjectAuth;
- import com.wtkj.handler.es.Document;
- import com.wtkj.service.*;
- import com.wtkj.vo.AuthUserInfo;
- 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.BladeUser;
- import org.springblade.core.secure.utils.AuthUtil;
- import org.springblade.core.tool.api.R;
- import org.springblade.core.tool.utils.BeanUtil;
- import org.springblade.core.tool.utils.Func;
- import org.springblade.core.tool.utils.StringUtil;
- import org.springblade.core.tool.utils.WebUtil;
- import org.springblade.system.entity.Dept;
- import org.springblade.system.feign.ISysClient;
- import org.springblade.system.user.entity.User;
- import org.springblade.system.user.feign.IUserClient;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
- import java.util.*;
- import java.util.stream.Collectors;
- import static com.wtkj.config.MagicValue.*;
- /**
- * @author Blizzard
- * @create at 2023-09-14 16:07
- * @describe
- */
- @Service
- @AllArgsConstructor
- public class CommonServiceImpl implements ICommonService {
- private final IFileClient fileClient;
- private final IFileAndFolderService fileAndFolderService;
- private final IFileAndFolderHistoryService historyService;
- private final IInnerFileAndFolderService innerFileAndFolderService;
- private final IProjectAuthService projectAuthService;
- private final RestHighLevelClient client;
- private final ISysClient sysClient;
- private final IUserClient userClient;
- @Override
- public boolean uploadFile(List<UploadFileDTO> files) {
- boolean flag = false;
- List<FileAndFolder> fileList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(files)) {
- Long projectId = files.get(0).getProjectId();
- Long parentId = files.get(0).getParentId();
- Long stageId = files.get(0).getStageId();
- files.forEach(file -> {
- Long fileId = file.getBladeFileId();
- R<FileVO> rpc = fileClient.findById(fileId);
- if (rpc.isSuccess()) {
- FileAndFolder newFile = new FileAndFolder();
- newFile.setProjectId(projectId);
- newFile.setStageId(stageId);
- newFile.setParentId(parentId);
- newFile.setBladeFileId(file.getBladeFileId());
- newFile.setType(1);
- newFile.setTitle(rpc.getData().getOriginalFileName());
- if (parentId != null && parentId > 0L) {
- FileAndFolder folder = fileAndFolderService.getById(parentId);
- if (folder != null) {
- newFile.setIsPrivate(folder.getIsPrivate());
- newFile.setPrivateUser(folder.getPrivateUser());
- }
- }
- fileList.add(newFile);
- }
- });
- flag = fileAndFolderService.saveBatch(fileList);
- }
- return flag;
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public boolean coverFile(List<UploadFileDTO> files) {
- boolean flag = false;
- List<FileAndFolder> fileList = new ArrayList<>();
- List<FileAndFolderHistory> histories = new ArrayList<>();
- if (!CollectionUtils.isEmpty(files)) {
- Long projectId = files.get(0).getProjectId();
- Long parentId = files.get(0).getParentId();
- Long stageId = files.get(0).getStageId();
- files.forEach(file -> {
- Long fileId = file.getBladeFileId();
- R<FileVO> rpc = fileClient.findById(fileId);
- if (rpc.isSuccess()) {
- //是否存在同名文件
- LambdaQueryWrapper<FileAndFolder> lqw = Wrappers.lambdaQuery();
- lqw.eq(FileAndFolder::getStageId, stageId);
- lqw.eq(FileAndFolder::getParentId, parentId);
- lqw.eq(FileAndFolder::getTitle, rpc.getData().getOriginalFileName());
- FileAndFolder old = fileAndFolderService.getOne(lqw);
- if (old != null) {
- //存在同名文件
- //此处不删除旧的同名文件,直接修改值 ---> 再保存历史记录信息 修改已存在的的数据 更新url 文件大小等信息
- //先根据旧的fileId判断是否存在历史记录
- List<FileAndFolderHistory> hisList = historyService.getByLatestId(old.getBladeFileId());
- if (!CollectionUtils.isEmpty(hisList)) {
- hisList.forEach(h -> {
- h.setLatestId(String.valueOf(fileId));
- histories.add(h);
- });
- }
- //历史文件和新上传文件不是同一个的时候才保存
- if (!fileId.equals(old.getBladeFileId())) {
- FileAndFolderHistory his = new FileAndFolderHistory();
- his.setLatestId(String.valueOf(fileId));
- his.setHistoryId(String.valueOf(old.getBladeFileId()));
- histories.add(his);
- }
- old.setBladeFileId(fileId);
- fileList.add(old);
- } else {
- FileAndFolder newFile = new FileAndFolder();
- newFile.setProjectId(projectId);
- newFile.setStageId(stageId);
- newFile.setParentId(parentId);
- newFile.setBladeFileId(file.getBladeFileId());
- newFile.setType(1);
- newFile.setTitle(rpc.getData().getOriginalFileName());
- if (parentId != null && parentId > 0L) {
- FileAndFolder folder = fileAndFolderService.getById(parentId);
- if (folder != null) {
- newFile.setIsPrivate(folder.getIsPrivate());
- newFile.setPrivateUser(folder.getPrivateUser());
- }
- }
- fileList.add(newFile);
- }
- }
- });
- if (!CollectionUtils.isEmpty(histories)) {
- historyService.saveOrUpdateBatch(histories);
- }
- flag = fileAndFolderService.saveOrUpdateBatch(fileList);
- }
- return flag;
- }
- @Override
- public boolean remove(List<Long> ids) {
- boolean flag = false;
- if (!CollectionUtils.isEmpty(ids)) {
- ids.forEach(id -> {
- FileAndFolder byId = fileAndFolderService.getById(id);
- //如果是文件夹
- if (Objects.equals(byId.getType(), TWO)) {
- List<FileAndFolder> children = fileAndFolderService.getChildren(null, id);
- if (!CollectionUtils.isEmpty(children)) {
- throw new ServiceException("删除文件夹失败:请先删除下级");
- }
- }
- });
- flag = fileAndFolderService.removeByIds(ids);
- }
- return flag;
- }
- @Override
- public boolean move(String ids, Long stageId, Long parentId) {
- boolean flag = false;
- if (StringUtil.isNotBlank(ids)) {
- List<FileAndFolder> list = new ArrayList<>();
- List<Long> idList = Func.toLongList(ids);
- idList.forEach(id -> {
- FileAndFolder byId = fileAndFolderService.getById(id);
- if (byId != null) {
- byId.setStageId(stageId);
- byId.setParentId(parentId);
- list.add(byId);
- }
- });
- flag = fileAndFolderService.updateBatchById(list);
- }
- return flag;
- }
- @Override
- public boolean delete(List<Long> ids) {
- boolean flag = false;
- Long deptId = Func.firstLong(AuthUtil.getDeptId());
- if (!CollectionUtils.isEmpty(ids)) {
- ids.forEach(id -> {
- InnerFileAndFolder byId = innerFileAndFolderService.getById(id);
- //如果是文件夹
- if (byId != null && Objects.equals(byId.getType(), TWO)) {
- if (!deptId.equals(byId.getDeptId())) {
- throw new ServiceException("非你部门下文件,禁止删除");
- }
- List<InnerFileAndFolder> children = innerFileAndFolderService.getChildren(byId.getDeptId(), id);
- if (!CollectionUtils.isEmpty(children)) {
- throw new ServiceException("删除文件夹失败:请先删除下级");
- }
- }
- });
- flag = innerFileAndFolderService.removeByIds(ids);
- }
- return flag;
- }
- @Override
- public boolean uploadInnerFile(List<UploadInnerFileDTO> files) {
- boolean flag = false;
- List<InnerFileAndFolder> fileList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(files)) {
- Long parentId = files.get(0).getParentId();
- Long deptId = files.get(0).getDeptId();
- files.forEach(file -> {
- Long fileId = file.getBladeFileId();
- R<FileVO> rpc = fileClient.findById(fileId);
- if (rpc.isSuccess()) {
- InnerFileAndFolder newFile = new InnerFileAndFolder();
- newFile.setDeptId(deptId);
- newFile.setParentId(parentId);
- newFile.setBladeFileId(file.getBladeFileId());
- newFile.setType(1);
- newFile.setTitle(rpc.getData().getOriginalFileName());
- fileList.add(newFile);
- }
- });
- flag = innerFileAndFolderService.saveBatch(fileList);
- }
- return flag;
- }
- @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);
- }
- lqw.eq(FileAndFolder::getType, 1);
- return fileAndFolderService.page(page, lqw);
- }
- @SneakyThrows
- @Override
- public Set<Integer> 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<Integer> 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.getCurrentPage());
- }
- }
- return set;
- }
- @Override
- public AuthUserInfo userInfo() {
- BladeUser user = AuthUtil.getUser();
- if (user != null) {
- AuthUserInfo userInfo = Objects.requireNonNull(BeanUtil.copy(user, AuthUserInfo.class));
- String deptIdStr = userInfo.getDeptId();
- if (StringUtil.isNotBlank(deptIdStr)) {
- R<Dept> rpc = sysClient.getDept(Long.valueOf(deptIdStr));
- if (rpc.isSuccess()) {
- userInfo.setDeptCategory(rpc.getData().getDeptCategory());
- userInfo.setDeptName(rpc.getData().getDeptName());
- String ancestors = rpc.getData().getAncestors();
- // ancestors 0,1689540492698267649
- if (ancestors != null && ancestors.contains(String.valueOf(ZERO))) {
- List<Long> longList = Func.toLongList(ancestors);
- if (!CollectionUtils.isEmpty(longList)) {
- if (longList.size() > 1) {
- Long deptId = longList.get(1);
- userInfo.setTopDept(deptId);
- R<Dept> dept = sysClient.getDept(deptId);
- if (dept.isSuccess()) {
- userInfo.setTopDeptName(dept.getData().getDeptName());
- }
- } else {
- userInfo.setTopDept(Long.valueOf(deptIdStr));
- userInfo.setTopDeptName(rpc.getData().getDeptName());
- }
- }
- }
- }
- }
- Long userId = user.getUserId();
- R<User> userR = userClient.userInfoById(userId);
- if (userR.isSuccess()) {
- userInfo.setUser(userR.getData());
- }
- //登录成功通知 仅pc端
- String platform = WebUtil.getHeader("platform");
- if (StringUtil.isNotBlank(platform) && platform.equals("pc")) {
- }
- return userInfo;
- }
- return null;
- }
- }
|