CommonServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package com.wtkj.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.wtkj.dto.SearchProjectFileDTO;
  7. import com.wtkj.dto.UploadFileDTO;
  8. import com.wtkj.dto.UploadInnerFileDTO;
  9. import com.wtkj.entity.FileAndFolder;
  10. import com.wtkj.entity.FileAndFolderHistory;
  11. import com.wtkj.entity.InnerFileAndFolder;
  12. import com.wtkj.entity.ProjectAuth;
  13. import com.wtkj.handler.es.Document;
  14. import com.wtkj.service.*;
  15. import com.wtkj.vo.AuthUserInfo;
  16. import com.wutong.file.feign.IFileClient;
  17. import com.wutong.file.vo.FileVO;
  18. import lombok.AllArgsConstructor;
  19. import lombok.SneakyThrows;
  20. import org.elasticsearch.action.search.SearchRequest;
  21. import org.elasticsearch.action.search.SearchResponse;
  22. import org.elasticsearch.client.RequestOptions;
  23. import org.elasticsearch.client.RestHighLevelClient;
  24. import org.elasticsearch.index.query.BoolQueryBuilder;
  25. import org.elasticsearch.index.query.QueryBuilders;
  26. import org.elasticsearch.search.SearchHits;
  27. import org.elasticsearch.search.builder.SearchSourceBuilder;
  28. import org.springblade.core.log.exception.ServiceException;
  29. import org.springblade.core.secure.BladeUser;
  30. import org.springblade.core.secure.utils.AuthUtil;
  31. import org.springblade.core.tool.api.R;
  32. import org.springblade.core.tool.utils.BeanUtil;
  33. import org.springblade.core.tool.utils.Func;
  34. import org.springblade.core.tool.utils.StringUtil;
  35. import org.springblade.core.tool.utils.WebUtil;
  36. import org.springblade.system.entity.Dept;
  37. import org.springblade.system.feign.ISysClient;
  38. import org.springblade.system.user.entity.User;
  39. import org.springblade.system.user.feign.IUserClient;
  40. import org.springframework.stereotype.Service;
  41. import org.springframework.transaction.annotation.Transactional;
  42. import org.springframework.util.CollectionUtils;
  43. import java.util.*;
  44. import java.util.stream.Collectors;
  45. import static com.wtkj.config.MagicValue.*;
  46. /**
  47. * @author Blizzard
  48. * @create at 2023-09-14 16:07
  49. * @describe
  50. */
  51. @Service
  52. @AllArgsConstructor
  53. public class CommonServiceImpl implements ICommonService {
  54. private final IFileClient fileClient;
  55. private final IFileAndFolderService fileAndFolderService;
  56. private final IFileAndFolderHistoryService historyService;
  57. private final IInnerFileAndFolderService innerFileAndFolderService;
  58. private final IProjectAuthService projectAuthService;
  59. private final RestHighLevelClient client;
  60. private final ISysClient sysClient;
  61. private final IUserClient userClient;
  62. @Override
  63. public boolean uploadFile(List<UploadFileDTO> files) {
  64. boolean flag = false;
  65. List<FileAndFolder> fileList = new ArrayList<>();
  66. if (!CollectionUtils.isEmpty(files)) {
  67. Long projectId = files.get(0).getProjectId();
  68. Long parentId = files.get(0).getParentId();
  69. Long stageId = files.get(0).getStageId();
  70. files.forEach(file -> {
  71. Long fileId = file.getBladeFileId();
  72. R<FileVO> rpc = fileClient.findById(fileId);
  73. if (rpc.isSuccess()) {
  74. FileAndFolder newFile = new FileAndFolder();
  75. newFile.setProjectId(projectId);
  76. newFile.setStageId(stageId);
  77. newFile.setParentId(parentId);
  78. newFile.setBladeFileId(file.getBladeFileId());
  79. newFile.setType(1);
  80. newFile.setTitle(rpc.getData().getOriginalFileName());
  81. if (parentId != null && parentId > 0L) {
  82. FileAndFolder folder = fileAndFolderService.getById(parentId);
  83. if (folder != null) {
  84. newFile.setIsPrivate(folder.getIsPrivate());
  85. newFile.setPrivateUser(folder.getPrivateUser());
  86. }
  87. }
  88. fileList.add(newFile);
  89. }
  90. });
  91. flag = fileAndFolderService.saveBatch(fileList);
  92. }
  93. return flag;
  94. }
  95. @Override
  96. @Transactional(rollbackFor = Exception.class)
  97. public boolean coverFile(List<UploadFileDTO> files) {
  98. boolean flag = false;
  99. List<FileAndFolder> fileList = new ArrayList<>();
  100. List<FileAndFolderHistory> histories = new ArrayList<>();
  101. if (!CollectionUtils.isEmpty(files)) {
  102. Long projectId = files.get(0).getProjectId();
  103. Long parentId = files.get(0).getParentId();
  104. Long stageId = files.get(0).getStageId();
  105. files.forEach(file -> {
  106. Long fileId = file.getBladeFileId();
  107. R<FileVO> rpc = fileClient.findById(fileId);
  108. if (rpc.isSuccess()) {
  109. //是否存在同名文件
  110. LambdaQueryWrapper<FileAndFolder> lqw = Wrappers.lambdaQuery();
  111. lqw.eq(FileAndFolder::getStageId, stageId);
  112. lqw.eq(FileAndFolder::getParentId, parentId);
  113. lqw.eq(FileAndFolder::getTitle, rpc.getData().getOriginalFileName());
  114. FileAndFolder old = fileAndFolderService.getOne(lqw);
  115. if (old != null) {
  116. //存在同名文件
  117. //此处不删除旧的同名文件,直接修改值 ---> 再保存历史记录信息 修改已存在的的数据 更新url 文件大小等信息
  118. //先根据旧的fileId判断是否存在历史记录
  119. List<FileAndFolderHistory> hisList = historyService.getByLatestId(old.getBladeFileId());
  120. if (!CollectionUtils.isEmpty(hisList)) {
  121. hisList.forEach(h -> {
  122. h.setLatestId(String.valueOf(fileId));
  123. histories.add(h);
  124. });
  125. }
  126. //历史文件和新上传文件不是同一个的时候才保存
  127. if (!fileId.equals(old.getBladeFileId())) {
  128. FileAndFolderHistory his = new FileAndFolderHistory();
  129. his.setLatestId(String.valueOf(fileId));
  130. his.setHistoryId(String.valueOf(old.getBladeFileId()));
  131. histories.add(his);
  132. }
  133. old.setBladeFileId(fileId);
  134. fileList.add(old);
  135. } else {
  136. FileAndFolder newFile = new FileAndFolder();
  137. newFile.setProjectId(projectId);
  138. newFile.setStageId(stageId);
  139. newFile.setParentId(parentId);
  140. newFile.setBladeFileId(file.getBladeFileId());
  141. newFile.setType(1);
  142. newFile.setTitle(rpc.getData().getOriginalFileName());
  143. if (parentId != null && parentId > 0L) {
  144. FileAndFolder folder = fileAndFolderService.getById(parentId);
  145. if (folder != null) {
  146. newFile.setIsPrivate(folder.getIsPrivate());
  147. newFile.setPrivateUser(folder.getPrivateUser());
  148. }
  149. }
  150. fileList.add(newFile);
  151. }
  152. }
  153. });
  154. if (!CollectionUtils.isEmpty(histories)) {
  155. historyService.saveOrUpdateBatch(histories);
  156. }
  157. flag = fileAndFolderService.saveOrUpdateBatch(fileList);
  158. }
  159. return flag;
  160. }
  161. @Override
  162. public boolean remove(List<Long> ids) {
  163. boolean flag = false;
  164. if (!CollectionUtils.isEmpty(ids)) {
  165. ids.forEach(id -> {
  166. FileAndFolder byId = fileAndFolderService.getById(id);
  167. //如果是文件夹
  168. if (Objects.equals(byId.getType(), TWO)) {
  169. List<FileAndFolder> children = fileAndFolderService.getChildren(null, id);
  170. if (!CollectionUtils.isEmpty(children)) {
  171. throw new ServiceException("删除文件夹失败:请先删除下级");
  172. }
  173. }
  174. });
  175. flag = fileAndFolderService.removeByIds(ids);
  176. }
  177. return flag;
  178. }
  179. @Override
  180. public boolean move(String ids, Long stageId, Long parentId) {
  181. boolean flag = false;
  182. if (StringUtil.isNotBlank(ids)) {
  183. List<FileAndFolder> list = new ArrayList<>();
  184. List<Long> idList = Func.toLongList(ids);
  185. idList.forEach(id -> {
  186. FileAndFolder byId = fileAndFolderService.getById(id);
  187. if (byId != null) {
  188. byId.setStageId(stageId);
  189. byId.setParentId(parentId);
  190. list.add(byId);
  191. }
  192. });
  193. flag = fileAndFolderService.updateBatchById(list);
  194. }
  195. return flag;
  196. }
  197. @Override
  198. public boolean delete(List<Long> ids) {
  199. boolean flag = false;
  200. Long deptId = Func.firstLong(AuthUtil.getDeptId());
  201. if (!CollectionUtils.isEmpty(ids)) {
  202. ids.forEach(id -> {
  203. InnerFileAndFolder byId = innerFileAndFolderService.getById(id);
  204. //如果是文件夹
  205. if (byId != null && Objects.equals(byId.getType(), TWO)) {
  206. if (!deptId.equals(byId.getDeptId())) {
  207. throw new ServiceException("非你部门下文件,禁止删除");
  208. }
  209. List<InnerFileAndFolder> children = innerFileAndFolderService.getChildren(byId.getDeptId(), id);
  210. if (!CollectionUtils.isEmpty(children)) {
  211. throw new ServiceException("删除文件夹失败:请先删除下级");
  212. }
  213. }
  214. });
  215. flag = innerFileAndFolderService.removeByIds(ids);
  216. }
  217. return flag;
  218. }
  219. @Override
  220. public boolean uploadInnerFile(List<UploadInnerFileDTO> files) {
  221. boolean flag = false;
  222. List<InnerFileAndFolder> fileList = new ArrayList<>();
  223. if (!CollectionUtils.isEmpty(files)) {
  224. Long parentId = files.get(0).getParentId();
  225. Long deptId = files.get(0).getDeptId();
  226. files.forEach(file -> {
  227. Long fileId = file.getBladeFileId();
  228. R<FileVO> rpc = fileClient.findById(fileId);
  229. if (rpc.isSuccess()) {
  230. InnerFileAndFolder newFile = new InnerFileAndFolder();
  231. newFile.setDeptId(deptId);
  232. newFile.setParentId(parentId);
  233. newFile.setBladeFileId(file.getBladeFileId());
  234. newFile.setType(1);
  235. newFile.setTitle(rpc.getData().getOriginalFileName());
  236. fileList.add(newFile);
  237. }
  238. });
  239. flag = innerFileAndFolderService.saveBatch(fileList);
  240. }
  241. return flag;
  242. }
  243. @SneakyThrows
  244. @Override
  245. public IPage<FileAndFolder> esSearch(SearchProjectFileDTO dto, IPage<FileAndFolder> page) {
  246. LambdaQueryWrapper<FileAndFolder> lqw = new LambdaQueryWrapper<>();
  247. Long deptId = dto.getTopDeptId();
  248. String text = dto.getText();
  249. Long projectId = dto.getProjectId();
  250. Long createUser = dto.getCreateUser();
  251. Set<Long> projectIds = new HashSet<>();
  252. if (projectId != null && projectId > 0L) {
  253. projectIds.add(projectId);
  254. } else {
  255. //如果projectId为空 则查询机构下所有项目
  256. //此机构下的项目
  257. List<ProjectAuth> auths = projectAuthService.getByDeptAndUser(deptId, null, null);
  258. if (!CollectionUtils.isEmpty(auths)) {
  259. Set<Long> ids = auths.stream().map(ProjectAuth::getProjectId).filter(Objects::nonNull).collect(Collectors.toSet());
  260. projectIds.addAll(ids);
  261. }
  262. }
  263. // select * from xxx where projectId in (...) and create_user = #{createUser} and (title like %text% or content like %text%)
  264. SearchRequest request = new SearchRequest();
  265. request.indices(PROJECT_RESOURCE_INDEX);
  266. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  267. // 1.创建BoolQueryBuilder
  268. BoolQueryBuilder condition = QueryBuilders.boolQuery();
  269. //2. 添加must子句
  270. condition.must(QueryBuilders.termsQuery("projectId", projectIds));
  271. if (createUser != null) {
  272. condition.must(QueryBuilders.termQuery("createUser", createUser));
  273. }
  274. if (StringUtil.isNotBlank(text)) {
  275. // 3.添加should子句
  276. condition.should(QueryBuilders.matchQuery("title", text));
  277. condition.should(QueryBuilders.matchQuery("content", text));
  278. }
  279. searchSourceBuilder.query(condition);
  280. searchSourceBuilder.from(0);
  281. searchSourceBuilder.size(100);
  282. request.source(searchSourceBuilder);
  283. //执行查询
  284. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  285. Set<Document> set = new HashSet<>();
  286. if (response != null) {
  287. SearchHits hits = response.getHits();
  288. int size = hits.getHits().length;
  289. for (int i = 0; i < size; i++) {
  290. String str = hits.getHits()[i].getSourceAsString();
  291. Document document = JSON.parseObject(str, Document.class);
  292. set.add(document);
  293. }
  294. }
  295. if (!CollectionUtils.isEmpty(set)) {
  296. Set<Long> fileIds = set.stream().map(Document::getFileId).collect(Collectors.toSet());
  297. lqw.in(FileAndFolder::getBladeFileId, fileIds);
  298. }
  299. lqw.eq(FileAndFolder::getType, 1);
  300. return fileAndFolderService.page(page, lqw);
  301. }
  302. @SneakyThrows
  303. @Override
  304. public Set<Integer> openSearchFile(Long projectId, Long fileId, String text) {
  305. Long userId = AuthUtil.getUserId();
  306. List<ProjectAuth> list = projectAuthService.getByUserId(userId);
  307. if (!CollectionUtils.isEmpty(list)) {
  308. Set<Long> collect = list.stream().map(ProjectAuth::getProjectId).collect(Collectors.toSet());
  309. if (!collect.contains(projectId)) {
  310. throw new ServiceException("对不起,你暂未拥有此项目权限");
  311. }
  312. }
  313. SearchRequest request = new SearchRequest();
  314. request.indices(PROJECT_RESOURCE_INDEX);
  315. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  316. // 1.创建BoolQueryBuilder
  317. BoolQueryBuilder condition = QueryBuilders.boolQuery();
  318. //2. 添加must子句
  319. condition.must(QueryBuilders.termQuery("projectId", projectId));
  320. condition.must(QueryBuilders.termQuery("fileId", fileId));
  321. condition.must(QueryBuilders.matchQuery("title", text));
  322. searchSourceBuilder.query(condition);
  323. searchSourceBuilder.from(0);
  324. searchSourceBuilder.size(100);
  325. request.source(searchSourceBuilder);
  326. searchSourceBuilder.query(condition);
  327. searchSourceBuilder.from(0);
  328. searchSourceBuilder.size(100);
  329. request.source(searchSourceBuilder);
  330. //执行查询
  331. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
  332. //返回页码
  333. Set<Integer> set = new HashSet<>();
  334. if (response != null) {
  335. SearchHits hits = response.getHits();
  336. int size = hits.getHits().length;
  337. for (int i = 0; i < size; i++) {
  338. String str = hits.getHits()[i].getSourceAsString();
  339. Document document = JSON.parseObject(str, Document.class);
  340. set.add(document.getCurrentPage());
  341. }
  342. }
  343. return set;
  344. }
  345. @Override
  346. public AuthUserInfo userInfo() {
  347. BladeUser user = AuthUtil.getUser();
  348. if (user != null) {
  349. AuthUserInfo userInfo = Objects.requireNonNull(BeanUtil.copy(user, AuthUserInfo.class));
  350. String deptIdStr = userInfo.getDeptId();
  351. if (StringUtil.isNotBlank(deptIdStr)) {
  352. R<Dept> rpc = sysClient.getDept(Long.valueOf(deptIdStr));
  353. if (rpc.isSuccess()) {
  354. userInfo.setDeptCategory(rpc.getData().getDeptCategory());
  355. userInfo.setDeptName(rpc.getData().getDeptName());
  356. String ancestors = rpc.getData().getAncestors();
  357. // ancestors 0,1689540492698267649
  358. if (ancestors != null && ancestors.contains(String.valueOf(ZERO))) {
  359. List<Long> longList = Func.toLongList(ancestors);
  360. if (!CollectionUtils.isEmpty(longList)) {
  361. if (longList.size() > 1) {
  362. Long deptId = longList.get(1);
  363. userInfo.setTopDept(deptId);
  364. R<Dept> dept = sysClient.getDept(deptId);
  365. if (dept.isSuccess()) {
  366. userInfo.setTopDeptName(dept.getData().getDeptName());
  367. }
  368. } else {
  369. userInfo.setTopDept(Long.valueOf(deptIdStr));
  370. userInfo.setTopDeptName(rpc.getData().getDeptName());
  371. }
  372. }
  373. }
  374. }
  375. }
  376. Long userId = user.getUserId();
  377. R<User> userR = userClient.userInfoById(userId);
  378. if (userR.isSuccess()) {
  379. userInfo.setUser(userR.getData());
  380. }
  381. //登录成功通知 仅pc端
  382. String platform = WebUtil.getHeader("platform");
  383. if (StringUtil.isNotBlank(platform) && platform.equals("pc")) {
  384. }
  385. return userInfo;
  386. }
  387. return null;
  388. }
  389. }