DocumentController.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. package jnpf.controller;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.text.StrPool;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import io.swagger.v3.oas.annotations.Operation;
  6. import io.swagger.v3.oas.annotations.Parameter;
  7. import io.swagger.v3.oas.annotations.Parameters;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import jakarta.validation.Valid;
  10. import jnpf.base.ActionResult;
  11. import jnpf.base.Page;
  12. import jnpf.base.controller.SuperController;
  13. import jnpf.base.entity.SuperBaseEntity;
  14. import jnpf.base.util.OptimizeUtil;
  15. import jnpf.base.vo.DownloadVO;
  16. import jnpf.base.vo.ListVO;
  17. import jnpf.config.ConfigValueUtil;
  18. import jnpf.constant.FileTypeConstant;
  19. import jnpf.constant.MsgCode;
  20. import jnpf.entity.DocumentEntity;
  21. import jnpf.entity.DocumentLogEntity;
  22. import jnpf.entity.DocumentShareEntity;
  23. import jnpf.entity.FileParameter;
  24. import jnpf.exception.DataException;
  25. import jnpf.extend.service.DocumentApi;
  26. import jnpf.flowable.entity.TaskEntity;
  27. import jnpf.flowable.model.task.FileModel;
  28. import jnpf.model.MergeChunkDto;
  29. import jnpf.model.UploaderVO;
  30. import jnpf.model.document.*;
  31. import jnpf.permission.entity.UserEntity;
  32. import jnpf.permission.service.UserService;
  33. import jnpf.service.DocumentLogService;
  34. import jnpf.service.DocumentService;
  35. import jnpf.service.FileService;
  36. import jnpf.util.*;
  37. import jnpf.util.treeutil.SumTree;
  38. import jnpf.util.treeutil.newtreeutil.TreeDotUtils;
  39. import jnpf.workflow.service.TaskApi;
  40. import lombok.Cleanup;
  41. import lombok.extern.slf4j.Slf4j;
  42. import org.apache.commons.io.FileUtils;
  43. import org.dromara.x.file.storage.core.FileInfo;
  44. import org.springframework.beans.factory.annotation.Autowired;
  45. import org.springframework.transaction.annotation.Transactional;
  46. import org.springframework.web.bind.annotation.*;
  47. import org.springframework.web.multipart.MultipartFile;
  48. import java.io.File;
  49. import java.io.FileOutputStream;
  50. import java.util.*;
  51. import java.util.stream.Collectors;
  52. /**
  53. * 文档管理
  54. *
  55. * @author JNPF开发平台组
  56. * @version V3.1.0
  57. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  58. * @date 2019年9月26日 上午9:18
  59. */
  60. @Slf4j
  61. @Tag(name = "知识管理", description = "Document")
  62. @RestController
  63. @RequestMapping("/api/file/Document")
  64. public class DocumentController extends SuperController<DocumentService, DocumentEntity> implements DocumentApi {
  65. @Autowired
  66. private DocumentService documentService;
  67. @Autowired
  68. private ConfigValueUtil configValueUtil;
  69. @Autowired
  70. private UserService userService;
  71. @Autowired
  72. private DocumentLogService documentLogService;
  73. @Autowired
  74. private TaskApi taskApi;
  75. @Autowired
  76. private FileService fileService;
  77. /**
  78. * 列表
  79. *
  80. * @param id 主键
  81. * @return
  82. */
  83. @Operation(summary = "列表")
  84. @GetMapping("/{id}")
  85. @Parameters({
  86. @Parameter(name = "id", description = "主键", required = true),
  87. })
  88. public ActionResult<DocumentInfoVO> info(@PathVariable("id") String id) throws DataException {
  89. DocumentEntity entity = documentService.getInfo(id);
  90. if (!Objects.equals(entity.getCreatorUserId(),UserProvider.getLoginUserId())){
  91. return ActionResult.fail(MsgCode.AD104.get());
  92. }
  93. DocumentInfoVO vo = JsonUtil.getJsonToBean(entity, DocumentInfoVO.class);
  94. //截取后缀
  95. if(Objects.equals(vo.getType(), 1) && vo.getFullName().contains(".")){
  96. vo.setFullName(vo.getFullName().substring(0, vo.getFullName().lastIndexOf(".")));
  97. }
  98. return ActionResult.success(vo);
  99. }
  100. /**
  101. * 新建
  102. *
  103. * @param documentCrForm 新建模型
  104. * @return
  105. */
  106. @Operation(summary = "新建")
  107. @PostMapping
  108. @Parameters({
  109. @Parameter(name = "documentCrForm", description = "知识模型", required = true),
  110. })
  111. public ActionResult create(@RequestBody @Valid DocumentCrForm documentCrForm) {
  112. DocumentEntity entity = JsonUtil.getJsonToBean(documentCrForm, DocumentEntity.class);
  113. if (documentService.isExistByFullName(documentCrForm.getFullName(), entity.getId(), documentCrForm.getParentId())) {
  114. return ActionResult.fail(MsgCode.EXIST004.get());
  115. }
  116. entity.setEnabledMark(1);
  117. documentService.create(entity);
  118. return ActionResult.success(MsgCode.SU001.get());
  119. }
  120. /**
  121. * 修改
  122. *
  123. * @param id 主键
  124. * @param documentUpForm 修改模型
  125. * @return
  126. */
  127. @Operation(summary = "修改")
  128. @PutMapping("/{id}")
  129. @Parameters({
  130. @Parameter(name = "id", description = "主键", required = true),
  131. @Parameter(name = "documentUpForm", description = "知识模型", required = true),
  132. })
  133. public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid DocumentUpForm documentUpForm) {
  134. DocumentEntity entity = JsonUtil.getJsonToBean(documentUpForm, DocumentEntity.class);
  135. if (documentService.isExistByFullName(documentUpForm.getFullName(), id, documentUpForm.getParentId())) {
  136. return ActionResult.fail(MsgCode.EXIST004.get());
  137. }
  138. DocumentEntity info = documentService.getInfo(id);
  139. //获取后缀名
  140. if(Objects.equals(info.getType(), 1) && StringUtil.isNotEmpty(info.getFileExtension())){
  141. entity.setFullName(entity.getFullName() + StrPool.DOT + info.getFileExtension());
  142. }
  143. if (!Objects.equals(info.getCreatorUserId(),UserProvider.getLoginUserId())){
  144. return ActionResult.fail(MsgCode.AD104.get());
  145. }
  146. boolean flag = documentService.update(id, entity);
  147. if (flag == false) {
  148. return ActionResult.fail(MsgCode.FA002.get());
  149. }
  150. return ActionResult.success(MsgCode.SU004.get());
  151. }
  152. /**
  153. * 删除
  154. *
  155. * @param id 主键
  156. * @return
  157. */
  158. @Operation(summary = "删除")
  159. @DeleteMapping("/{id}")
  160. @Parameters({
  161. @Parameter(name = "id", description = "主键", required = true),
  162. })
  163. public ActionResult delete(@PathVariable("id") String id) {
  164. DocumentEntity entity = documentService.getInfo(id);
  165. if (entity != null) {
  166. List<DocumentEntity> allList = documentService.getAllList(entity.getId());
  167. if (allList.size() > 0) {
  168. return ActionResult.fail(MsgCode.FA016.get());
  169. }
  170. if (!Objects.equals(entity.getCreatorUserId(),UserProvider.getLoginUserId())){
  171. return ActionResult.fail(MsgCode.AD104.get());
  172. }
  173. documentService.delete(entity);
  174. return ActionResult.success(MsgCode.SU003.get());
  175. }
  176. return ActionResult.fail(MsgCode.FA003.get());
  177. }
  178. /**
  179. * 列表
  180. *
  181. * @return
  182. */
  183. @Operation(summary = "获取知识管理列表(文件夹树)")
  184. @PostMapping("/FolderTree")
  185. @Parameters({
  186. @Parameter(name = "id", description = "主键", required = true),
  187. })
  188. public ActionResult<ListVO<DocumentFolderTreeVO>> folderTree(@RequestBody DocumentShareForm form) {
  189. List<DocumentEntity> data = documentService.getFolderList();
  190. if (StringUtil.isNotEmpty(form.getIds())) {
  191. form.getIds().forEach(t -> data.remove(documentService.getInfo(t)));
  192. }
  193. List<DocumentFolderTreeModel> treeList = new ArrayList<>();
  194. DocumentFolderTreeModel model = new DocumentFolderTreeModel();
  195. model.setId("-1");
  196. model.setFullName("全部文档");
  197. model.setParentId("0");
  198. model.setIcon("0");
  199. treeList.add(model);
  200. for (DocumentEntity entity : data) {
  201. DocumentFolderTreeModel treeModel = new DocumentFolderTreeModel();
  202. treeModel.setId(entity.getId());
  203. treeModel.setFullName(entity.getFullName());
  204. treeModel.setParentId(entity.getParentId());
  205. treeModel.setIcon("fa fa-folder");
  206. treeList.add(treeModel);
  207. }
  208. List<SumTree<DocumentFolderTreeModel>> trees = TreeDotUtils.convertListToTreeDotFilter(treeList);
  209. List<DocumentFolderTreeVO> listVO = JsonUtil.getJsonToList(trees, DocumentFolderTreeVO.class);
  210. ListVO vo = new ListVO();
  211. vo.setList(listVO);
  212. return ActionResult.success(vo);
  213. }
  214. /**
  215. * 列表(全部文档)
  216. *
  217. * @param page 分页模型
  218. * @return
  219. */
  220. @Operation(summary = "获取知识管理列表(全部文档)")
  221. @GetMapping
  222. public ActionResult<ListVO<DocumentListVO>> allList(PageDocument page) {
  223. List<DocumentEntity> data = documentService.getAllList(page.getParentId());
  224. if (StringUtil.isNotEmpty(page.getKeyword())) {
  225. data = documentService.getSearchAllList(page.getKeyword());
  226. }
  227. List<DocumentListVO> list = JsonUtil.getJsonToList(data, DocumentListVO.class);
  228. //读取允许文件预览类型
  229. String allowPreviewType = configValueUtil.getAllowPreviewFileType();
  230. String[] fileType = allowPreviewType.split(",");
  231. for (DocumentListVO documentListVO : list) {
  232. //文件预览类型检验
  233. String s = Arrays.asList(fileType).stream().filter(type -> type.equals(documentListVO.getFileExtension())).findFirst().orElse(null);
  234. documentListVO.setIsPreview(s);
  235. }
  236. ListVO vo = new ListVO();
  237. vo.setList(list);
  238. return ActionResult.success(vo);
  239. }
  240. /**
  241. * 列表(我的分享)
  242. *
  243. * @param page 分页模型
  244. * @return
  245. */
  246. @Operation(summary = "知识管理(我的共享列表)")
  247. @GetMapping("/Share")
  248. public ActionResult<ListVO<DocumentListVO>> shareOutList(PageDocument page) {
  249. List<DocumentEntity> data = documentService.getShareOutList();
  250. if (StringUtil.isNotEmpty(page.getKeyword())) {
  251. List<DocumentEntity> dataSearch = new ArrayList<>();
  252. for (DocumentEntity datum : data) {
  253. if (Objects.equals(datum.getType(), 0)) {
  254. List<DocumentEntity> childList = new ArrayList<>();
  255. documentService.getChildSrcList(datum.getId(), childList, 1);
  256. List<DocumentEntity> collect = childList.stream().filter(t -> !Objects.equals(t.getType(), 0)).collect(Collectors.toList());
  257. dataSearch.addAll(collect);
  258. } else {
  259. dataSearch.add(datum);
  260. }
  261. }
  262. data = dataSearch.stream().distinct().filter(t -> t.getFullName().contains(page.getKeyword())).collect(Collectors.toList());
  263. } else if (StringUtil.isNotEmpty(page.getParentId()) && !"0".equals(page.getParentId())) {
  264. data = documentService.getAllList(page.getParentId());
  265. }
  266. List<DocumentListVO> list = JsonUtil.getJsonToList(data, DocumentListVO.class);
  267. ListVO vo = new ListVO();
  268. vo.setList(list);
  269. return ActionResult.success(vo);
  270. }
  271. /**
  272. * 列表(共享给我)
  273. *
  274. * @param page 分页模型
  275. * @return
  276. */
  277. @Operation(summary = "获取知识管理列表(共享给我)")
  278. @GetMapping("/ShareTome")
  279. public ActionResult shareTomeList(PageDocument page) {
  280. List<DocumentShareEntity> shareTomeList = documentService.getShareTomeList();
  281. List<String> ids = shareTomeList.stream().map(DocumentShareEntity::getDocumentId).collect(Collectors.toList());
  282. List<DocumentEntity> list = documentService.getInfoByIds(ids);
  283. List<String> userIds = list.stream().map(SuperBaseEntity.SuperCBaseEntity::getCreatorUserId).collect(Collectors.toList());
  284. List<UserEntity> userNames = userService.getUserName(userIds);
  285. List<DocumentListVO> dataRes = new ArrayList<>();
  286. if (StringUtil.isNotEmpty(page.getParentId()) && !"0".equals(page.getParentId())) {
  287. List<DocumentListVO> listVOS = documentService.getChildListUserName(page.getParentId(), true);
  288. DocumentShareEntity documentShareEntity = documentService.getShareByParentId(page.getParentId());
  289. for (DocumentListVO item : listVOS) {
  290. if (documentShareEntity != null) {
  291. item.setShareTime(documentShareEntity.getShareTime());
  292. UserEntity userEntity = userNames.stream().filter(t -> t.getId().equals(documentShareEntity.getCreatorUserId())).findFirst().orElse(null);
  293. item.setCreatorUserId(userEntity != null ? userEntity.getRealName() + "/" + userEntity.getAccount() : "");
  294. }
  295. String creatorUserName = item.getCreatorUserName();
  296. String creatorUserAccount = item.getCreatorUserAccount();
  297. item.setCreatorUserId(creatorUserName+"/"+creatorUserAccount);
  298. dataRes.add(item);
  299. }
  300. } else {
  301. for (DocumentEntity datum : list) {
  302. if (StringUtil.isNotEmpty(page.getKeyword())) {
  303. DocumentShareEntity documentShareEntity = shareTomeList.stream().filter(t -> t.getDocumentId().equals(datum.getId())).findFirst().orElse(null);
  304. if (documentShareEntity != null) {
  305. if (Objects.equals(datum.getType(), 0)) {
  306. List<DocumentEntity> childList = new ArrayList<>();
  307. documentService.getChildSrcList(datum.getId(), childList, 1);
  308. for (DocumentEntity item : childList) {
  309. DocumentListVO documentListVO = BeanUtil.copyProperties(item, DocumentListVO.class);
  310. if (item.getFullName().contains(page.getKeyword()) && Objects.equals(item.getEnabledMark(), 1) && !Objects.equals(item.getType(), 0)) {
  311. documentListVO.setShareTime(documentShareEntity.getShareTime());
  312. UserEntity userEntity = userNames.stream().filter(t -> t.getId().equals(documentShareEntity.getCreatorUserId())).findFirst().orElse(null);
  313. documentListVO.setCreatorUserId(userEntity != null ? userEntity.getRealName() + "/" + userEntity.getAccount() : "");
  314. dataRes.add(documentListVO);
  315. }
  316. }
  317. }
  318. }
  319. } else {
  320. DocumentShareEntity documentShareEntity = shareTomeList.stream().filter(t -> t.getDocumentId().equals(datum.getId())).findFirst().orElse(null);
  321. if (documentShareEntity != null) {
  322. DocumentListVO documentListVO = BeanUtil.copyProperties(datum, DocumentListVO.class);
  323. documentListVO.setShareTime(documentShareEntity.getShareTime());
  324. UserEntity userEntity = userNames.stream().filter(t -> t.getId().equals(documentShareEntity.getCreatorUserId())).findFirst().orElse(null);
  325. documentListVO.setCreatorUserId(userEntity != null ? userEntity.getRealName() + "/" + userEntity.getAccount() : "");
  326. dataRes.add(documentListVO);
  327. }
  328. }
  329. }
  330. }
  331. ListVO vo = new ListVO();
  332. vo.setList(dataRes);
  333. return ActionResult.success(vo);
  334. }
  335. /**
  336. * 列表(回收站)
  337. *
  338. * @param page 分页模型
  339. * @return
  340. */
  341. @Operation(summary = "获取知识管理列表(回收站)")
  342. @GetMapping("/Trash")
  343. public ActionResult<ListVO<DocumentTrashListVO>> trashList(Page page) {
  344. List<DocumentTrashListVO> data = documentService.getTrashList(page.getKeyword());
  345. ListVO vo = new ListVO();
  346. vo.setList(data);
  347. return ActionResult.success(vo);
  348. }
  349. /**
  350. * 列表(共享人员)
  351. *
  352. * @param documentId 文档主键
  353. * @return
  354. */
  355. @Operation(summary = "获取知识管理列表(共享人员)")
  356. @GetMapping("/ShareUser/{documentId}")
  357. @Parameters({
  358. @Parameter(name = "documentId", description = "文档主键", required = true),
  359. })
  360. public ActionResult<ListVO<DocumentSuserListVO>> shareUserList(@PathVariable("documentId") String documentId) {
  361. List<DocumentShareEntity> data = documentService.getShareUserList(documentId);
  362. List<DocumentSuserListVO> list = JsonUtil.getJsonToList(data, DocumentSuserListVO.class);
  363. ListVO vo = new ListVO();
  364. vo.setList(list);
  365. return ActionResult.success(vo);
  366. }
  367. /**
  368. * app上传文件
  369. *
  370. * @param documentUploader 上传模型
  371. * @return
  372. */
  373. @Operation(summary = "知识管理上传文件")
  374. @PostMapping("/Uploader")
  375. public ActionResult uploader(DocumentUploader documentUploader) throws DataException {
  376. String fileType = UpUtil.getFileType(documentUploader.getFile());
  377. //验证类型
  378. if (!OptimizeUtil.fileType(configValueUtil.getAllowUploadFileType(), fileType)) {
  379. return ActionResult.fail(MsgCode.FA017.get());
  380. }
  381. //上传
  382. uploaderVO(documentUploader);
  383. return ActionResult.success(MsgCode.SU015.get());
  384. }
  385. /**
  386. * 分片组装
  387. *
  388. * @param mergeChunkDto 合并模型
  389. * @return
  390. */
  391. @Operation(summary = "分片组装")
  392. @PostMapping("/merge")
  393. public ActionResult merge(MergeChunkDto mergeChunkDto) {
  394. String identifier = XSSEscape.escapePath(mergeChunkDto.getIdentifier());
  395. String path = FileUploadUtils.getLocalBasePath() + configValueUtil.getTemporaryFilePath();
  396. String filePath = XSSEscape.escapePath(path + identifier);
  397. String partFile = XSSEscape.escapePath(path + mergeChunkDto.getFileName());
  398. try {
  399. List<File> mergeFileList = FileUtil.getFile(new File(filePath));
  400. @Cleanup FileOutputStream destTempfos = new FileOutputStream(partFile, true);
  401. for (int i = 0; i < mergeFileList.size(); i++) {
  402. String chunkName = identifier.concat("-") + (i + 1);
  403. File files = new File(filePath, chunkName);
  404. if (files.exists()) {
  405. FileUtils.copyFile(files, destTempfos);
  406. }
  407. }
  408. File partFiles = new File(partFile);
  409. if (partFiles.exists()) {
  410. MultipartFile multipartFile = FileUtil.createFileItem(partFiles);
  411. uploaderVO(new DocumentUploader(multipartFile, mergeChunkDto.getParentId()));
  412. FileUtil.deleteTmp(multipartFile);
  413. }
  414. } catch (Exception e) {
  415. log.error("合并分片失败: {}", e.getMessage());
  416. throw new DataException(MsgCode.FA033.get());
  417. } finally {
  418. FileUtils.deleteQuietly(new File(filePath));
  419. FileUtils.deleteQuietly(new File(partFile));
  420. }
  421. return ActionResult.success(MsgCode.SU015.get());
  422. }
  423. @Operation(summary = "流程归档文件上传接口")
  424. @PostMapping("/UploadBlob")
  425. public ActionResult UploadBlob(DocumentUploader dub) throws DataException {
  426. uploaderVO(dub);
  427. taskApi.updateIsFile(dub.getTaskId());
  428. return ActionResult.success(MsgCode.SU015.get());
  429. }
  430. /**
  431. * 获取下载文件链接
  432. *
  433. * @param id 主键
  434. * @return
  435. */
  436. @Operation(summary = "获取下载文件链接")
  437. @PostMapping("/Download/{id}")
  438. @Parameters({
  439. @Parameter(name = "id", description = "主键", required = true),
  440. })
  441. public ActionResult download(@PathVariable("id") String id) {
  442. DocumentEntity entity = documentService.getInfo(id);
  443. if (entity != null) {
  444. String name = entity.getFilePath();
  445. String fileName = name + "#" + FileTypeConstant.DOCUMENT + "#" + entity.getFullName() + "." + entity.getFileExtension();
  446. DownloadVO vo = DownloadVO.builder().name(entity.getFullName()).url(UploaderUtil.uploaderFile(fileName)).build();
  447. return ActionResult.success(vo);
  448. }
  449. return ActionResult.fail(MsgCode.FA018.get());
  450. }
  451. /**
  452. * 获取全部下载文件链接(打包下载)
  453. *
  454. * @return
  455. */
  456. @Operation(summary = "打包下载")
  457. @PostMapping("/PackDownload")
  458. public ActionResult packDownloadUrl(@RequestBody DocumentShareForm obj) {
  459. //单个文件直接下载
  460. if (obj.getIds().size() == 1) {
  461. DocumentEntity entity = documentService.getInfo(obj.getIds().get(0));
  462. if (entity != null && !Objects.equals(entity.getType(), 0)) {
  463. String name = entity.getFilePath();
  464. String fileName = name + "#" + FileTypeConstant.DOCUMENT + "#" + entity.getFullName() + "." + entity.getFileExtension();
  465. DownloadVO vo = DownloadVO.builder().name(entity.getFullName()).url(UploaderUtil.uploaderFile(fileName)).build();
  466. return ActionResult.success(vo);
  467. }
  468. }
  469. String servicePath = FilePathUtil.getFilePath(FileTypeConstant.FILEZIPDOWNTEMPPATH);
  470. String tempFilePath = FileUploadUtils.getLocalBasePath() + FilePathUtil.getFilePath(FileTypeConstant.FILEZIPDOWNTEMPPATH);
  471. String zipFileSrc = "Package_" + RandomUtil.uuId();
  472. String zipFileName = zipFileSrc + ".zip";
  473. String mainPath = servicePath + zipFileSrc + File.separator;
  474. String absMainPath = tempFilePath + zipFileSrc;
  475. new File(absMainPath).mkdirs();
  476. //递归生成文件夹下的文件
  477. createdFiles(obj.getIds(), mainPath);
  478. String filePath = tempFilePath + zipFileName;
  479. //打包
  480. FileUtil.toZip(filePath, true, tempFilePath + zipFileSrc);
  481. //删除源文件
  482. FileUtil.deleteFileAll(new File(tempFilePath + zipFileSrc));
  483. //上传压缩包到服务器
  484. MultipartFile multipartFile = FileUtil.createFileItem(new File(XSSEscape.escapePath(filePath)));
  485. FileInfo fileInfo = FileUploadUtils.uploadFile(new FileParameter(FilePathUtil.getFilePath(FileTypeConstant.FILEZIPDOWNTEMPPATH), zipFileName), multipartFile);
  486. // 删除压缩包
  487. FileUtil.deleteFileAll(new File(tempFilePath + zipFileName));
  488. //获取服务器下载路径
  489. DownloadVO vo = DownloadVO.builder()
  490. .name(zipFileName)
  491. .url(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#" + FileTypeConstant.FILEZIPDOWNTEMPPATH))
  492. .build();
  493. return ActionResult.success(vo);
  494. }
  495. /**
  496. * 递归获取文件夹下的文件
  497. *
  498. * @param fileIdList
  499. * @param mainPath
  500. */
  501. private void createdFiles(List<String> fileIdList, String mainPath) {
  502. for (String id : fileIdList) {
  503. DocumentEntity info = documentService.getInfo(id);
  504. if (info != null) {
  505. String fileId = StringUtil.isNotEmpty(info.getFilePath()) ? XSSEscape.escape(info.getFilePath()).trim() : "";
  506. String fileName = XSSEscape.escapePath(info.getFullName()).trim();
  507. if (Objects.equals(info.getType(), 0)) {
  508. //文件夹
  509. File file = new File(FileUploadUtils.getLocalBasePath() + mainPath + fileName);
  510. if (!file.exists()) {
  511. file.mkdir();
  512. }
  513. List<DocumentEntity> allList = documentService.getChildList(id, true);
  514. List<String> collect = allList.stream().map(DocumentEntity::getId).collect(Collectors.toList());
  515. createdFiles(collect, mainPath + fileName + File.separator);
  516. } else {
  517. try {
  518. //文件
  519. FileUploadUtils.downloadFileToLocal(new FileParameter(FilePathUtil.getFilePath(FileTypeConstant.DOCUMENT), fileId).setLocaFilelPath(mainPath).setLocalFileName(fileName));
  520. } catch (Exception e) {
  521. e.printStackTrace();
  522. }
  523. }
  524. }
  525. }
  526. }
  527. /**
  528. * 批量删除
  529. */
  530. @Operation(summary = "批量删除")
  531. @PostMapping("/BatchDelete")
  532. @Parameters({
  533. @Parameter(name = "ids", description = "主键", required = true),
  534. })
  535. public ActionResult BatchDelete(@RequestBody DocumentShareForm obj) {
  536. for (String id : obj.getIds()) {
  537. DocumentEntity entity = documentService.getInfo(id);
  538. if (entity != null) {
  539. if (!Objects.equals(entity.getCreatorUserId(),UserProvider.getLoginUserId())){
  540. return ActionResult.fail(MsgCode.AD104.get());
  541. }
  542. List<DocumentEntity> allList = new ArrayList<>();
  543. documentService.getChildSrcList(entity.getId(), allList, 1);
  544. allList.add(entity);
  545. //添加删除记录
  546. DocumentLogEntity logent = new DocumentLogEntity();
  547. logent.setDocumentId(id);
  548. List<String> collect = allList.stream().map(DocumentEntity::getId).collect(Collectors.toList());
  549. logent.setChildDocument(collect.stream().collect(Collectors.joining(",")));
  550. documentLogService.save(logent);
  551. for (DocumentEntity item : allList) {
  552. documentService.delete(item);
  553. }
  554. }
  555. }
  556. return ActionResult.success(MsgCode.SU003.get());
  557. }
  558. /**
  559. * 回收站(彻底删除)
  560. *
  561. * @return
  562. */
  563. @Operation(summary = "回收站(彻底删除)")
  564. @PostMapping("/Trash")
  565. @Parameters({
  566. @Parameter(name = "ids", description = "主键数组", required = true),
  567. })
  568. public ActionResult trashdelete(@RequestBody DocumentShareForm obj) {
  569. documentService.trashdelete(obj.getIds());
  570. return ActionResult.success(MsgCode.SU003.get());
  571. }
  572. /**
  573. * 回收站(还原文件)
  574. *
  575. * @return
  576. */
  577. @Operation(summary = "回收站(还原文件)")
  578. @PostMapping("/Trash/Actions/Recovery")
  579. @Parameters({
  580. @Parameter(name = "ids", description = "主键数组", required = true),
  581. })
  582. @Transactional
  583. public ActionResult trashRecovery(@RequestBody DocumentShareForm obj) {
  584. documentService.trashRecoveryConstainSrc(obj.getIds());
  585. return ActionResult.success(MsgCode.SU010.get());
  586. }
  587. /**
  588. * 共享文件(创建)
  589. *
  590. * @param documentShareForm 分享模型
  591. * @return
  592. */
  593. @Operation(summary = "分享文件/文件夹")
  594. @PostMapping("/Actions/Share")
  595. @Parameters({
  596. @Parameter(name = "documentShareForm", description = "分享模型", required = true),
  597. })
  598. public ActionResult shareCreate(@RequestBody DocumentShareForm documentShareForm) {
  599. documentService.sharecreate(documentShareForm);
  600. return ActionResult.success(MsgCode.SU005.get());
  601. }
  602. /**
  603. * 取消共享
  604. *
  605. * @param obj 主键值
  606. * @return
  607. */
  608. @Operation(summary = "取消分享文件/文件夹")
  609. @PostMapping("/Actions/CancelShare")
  610. @Parameters({
  611. @Parameter(name = "ids", description = "主键", required = true),
  612. })
  613. public ActionResult shareCancel(@RequestBody DocumentShareForm obj) {
  614. documentService.shareCancel(obj.getIds());
  615. return ActionResult.success(MsgCode.SU005.get());
  616. }
  617. @Operation(summary = "共享用户调整")
  618. @PostMapping("/Actions/ShareAdjustment/{id}")
  619. @Parameters({
  620. @Parameter(name = "id", description = "文档主键", required = true),
  621. @Parameter(name = "userIds", description = "共享用户组", required = true),
  622. })
  623. public ActionResult shareAdjustment(@PathVariable("id") String id, @RequestBody DocumentShareForm obj) {
  624. if (obj.getUserIds().size() > 0) {
  625. documentService.shareAdjustment(id, obj.getUserIds());
  626. }
  627. return ActionResult.success(MsgCode.SU005.get());
  628. }
  629. @Operation(summary = "移动文件/文件夹")
  630. @PutMapping("/Actions/MoveTo/{toId}")
  631. @Parameters({
  632. @Parameter(name = "ids", description = "主键", required = true),
  633. @Parameter(name = "toId", description = "将要移动到Id", required = true),
  634. })
  635. @Transactional
  636. public ActionResult moveTo(@RequestBody DocumentShareForm obj, @PathVariable("toId") String toId) {
  637. List<String> allIds = new ArrayList<>();
  638. allIds.addAll(obj.getIds());
  639. List<DocumentEntity> childList = new ArrayList<>();
  640. for (String oneId : obj.getIds()) {
  641. documentService.getChildSrcList(oneId, childList, 1);
  642. }
  643. allIds.addAll(childList.stream().map(DocumentEntity::getId).collect(Collectors.toList()));
  644. if (allIds.contains(toId)) {
  645. return ActionResult.fail(MsgCode.ETD103.get());
  646. }
  647. for (String id : obj.getIds()) {
  648. boolean flag = documentService.moveTo(id, toId);
  649. if (flag == false) {
  650. return ActionResult.fail(MsgCode.FA002.get());
  651. }
  652. }
  653. return ActionResult.success(MsgCode.SU004.get());
  654. }
  655. /**
  656. * 封装上传附件
  657. * 流程归档-文件上传
  658. *
  659. * @return
  660. */
  661. private void uploaderVO(DocumentUploader documentUploader) {
  662. MultipartFile file = documentUploader.getFile();
  663. String parentId = documentUploader.getParentId();
  664. String taskId = documentUploader.getTaskId();
  665. String fileName = StringUtil.isNotEmpty(documentUploader.getTaskId()) ? documentUploader.getTaskId() :
  666. StringUtil.isNotEmpty(documentUploader.getFileName()) ? documentUploader.getFileName() : file.getOriginalFilename();
  667. String fileType = UpUtil.getFileType(file);
  668. String creatorUserId = "";
  669. List<String> userList = new ArrayList<>();
  670. if (StringUtil.isNotEmpty(taskId)) {
  671. try { //获取流程信息
  672. FileModel fileModel = taskApi.getFileModel(taskId);
  673. fileName = fileModel.getFilename();
  674. creatorUserId = fileModel.getUserId();
  675. userList.addAll(fileModel.getUserList());
  676. fileType = "pdf";
  677. List<DocumentEntity> allList = documentService.getAllList("0", creatorUserId);
  678. DocumentEntity documentEntity = allList.stream().filter(t -> Objects.equals(t.getType(), 0) && FileModel.FOLDER_NAME.equals(t.getFullName())).findFirst().orElse(null);
  679. if (Objects.isNull(documentEntity)) {
  680. documentEntity = new DocumentEntity();
  681. documentEntity.setFullName(FileModel.FOLDER_NAME);
  682. documentEntity.setType(0);
  683. documentEntity.setParentId("0");
  684. documentEntity.setCreatorUserId(creatorUserId);
  685. documentService.create(documentEntity);
  686. }
  687. parentId = documentEntity.getId();
  688. } catch (Exception e) {
  689. throw new DataException(MsgCode.FA001.get());
  690. }
  691. }
  692. List<DocumentEntity> data = documentService.getAllList(parentId);
  693. String finalFileName = fileName;
  694. data = data.stream().filter(t -> finalFileName.equals(t.getFullName())).collect(Collectors.toList());
  695. if (data.size() > 0) {
  696. fileName = fileName.substring(0, fileName.lastIndexOf(".")) + "副本" + UUID.randomUUID().toString().substring(0, 5) + fileName.substring(fileName.lastIndexOf("."));
  697. }
  698. //上传
  699. MergeChunkDto mergeChunkDto = new MergeChunkDto();
  700. mergeChunkDto.setType(FileTypeConstant.DOCUMENT);
  701. mergeChunkDto.setFileType(fileType);
  702. UploaderVO uploaderVO = fileService.uploadFile(mergeChunkDto, file);
  703. DocumentEntity entity = new DocumentEntity();
  704. entity.setType(1);
  705. entity.setFullName(fileName);
  706. entity.setParentId(parentId);
  707. entity.setFileExtension(fileType);
  708. entity.setFilePath(uploaderVO.getName());
  709. entity.setFileSize(String.valueOf(file.getSize()));
  710. entity.setCreatorUserId(creatorUserId);
  711. entity.setEnabledMark(1);
  712. String desc = null;
  713. TaskEntity taskEntity = taskApi.getInfoSubmit(taskId, TaskEntity::getId, TaskEntity::getTemplateId);
  714. if (null != taskEntity) {
  715. desc = taskId + "-" + taskEntity.getTemplateId();
  716. }
  717. entity.setDescription(desc);
  718. entity.setUploaderUrl(UploaderUtil.uploaderImg("/api/file/Image/document/", uploaderVO.getName()));
  719. documentService.create(entity);
  720. if (StringUtil.isNotEmpty(taskId)) {
  721. DocumentShareForm documentShareForm = new DocumentShareForm();
  722. documentShareForm.setUserIds(userList);
  723. documentShareForm.setIds(new ArrayList() {{
  724. add(entity.getId());
  725. }});
  726. documentShareForm.setCreatorUserId(creatorUserId);
  727. documentService.sharecreate(documentShareForm);
  728. }
  729. }
  730. /**
  731. * 判断是否存在归档文件
  732. *
  733. * @param taskId 流程任务主键
  734. */
  735. @Override
  736. public Boolean checkFlowFile(String taskId) {
  737. QueryWrapper<DocumentEntity> wrapper = new QueryWrapper<>();
  738. wrapper.lambda().like(DocumentEntity::getDescription, taskId);
  739. return documentService.count(wrapper) < 1;
  740. }
  741. /**
  742. * 获取归档文件
  743. *
  744. * @param model 参数
  745. */
  746. @Override
  747. public List<Map<String, Object>> getFlowFile(FlowFileModel model) {
  748. return documentService.getFlowFile(model);
  749. }
  750. }