|
@@ -0,0 +1,241 @@
|
|
|
+package com.management.platform.controller;
|
|
|
+
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.management.platform.entity.*;
|
|
|
+import com.management.platform.mapper.*;
|
|
|
+import com.management.platform.service.ProjectDocumentService;
|
|
|
+import com.management.platform.util.DocumentTypeUtil;
|
|
|
+import com.management.platform.util.FileUtil;
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
+import com.management.platform.util.OpenOfficeService;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 前端控制器
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author Seyason
|
|
|
+ * @since 2022-04-20
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/task-files")
|
|
|
+public class TaskFilesController {
|
|
|
+ @Resource
|
|
|
+ private HttpServletRequest request;
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+ @Resource
|
|
|
+ private TaskFilesMapper taskFilesMapper;
|
|
|
+ @Resource
|
|
|
+ private ProjectDocumentService projectDocumentService;
|
|
|
+ @Value("${upload.path}")
|
|
|
+ private String uploadPath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取该项目下的所有有效的文件列表
|
|
|
+ * @param keyword
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/getDocumentList")
|
|
|
+ public HttpRespMsg getDocumentList(Integer projectId, String keyword) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ QueryWrapper<ProjectDocument> queryWrapper = new QueryWrapper<ProjectDocument>()
|
|
|
+ .eq("is_folder", 0).eq("is_deleted", 0).eq("project_id", projectId);
|
|
|
+ if (!StringUtils.isEmpty(keyword)) {
|
|
|
+ queryWrapper.like("document_name", keyword);
|
|
|
+ }
|
|
|
+ msg.data = projectDocumentService.list(queryWrapper);
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加关联的文件
|
|
|
+ * @param documentId
|
|
|
+ * @param taskId
|
|
|
+ * @param projectId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/addDocumentRef")
|
|
|
+ public HttpRespMsg addDocumentRef(Integer documentId, Integer taskId, Integer projectId) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("Token"));
|
|
|
+ ProjectDocument document = projectDocumentService.getById(documentId);
|
|
|
+ TaskFiles record = new TaskFiles();
|
|
|
+ record.setCreatorId(user.getId());
|
|
|
+ record.setCreatorName(user.getName());
|
|
|
+ record.setDocumentName(document.getDocumentName());
|
|
|
+ record.setTaskId(taskId);
|
|
|
+ record.setProjectId(projectId);
|
|
|
+ record.setDocumentType(document.getDocumentType());
|
|
|
+ record.setSize(document.getSize());
|
|
|
+ record.setServerName(document.getServerName());
|
|
|
+ record.setUrl(document.getUrl());
|
|
|
+ taskFilesMapper.insert(record);
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ * @param projectId 项目id
|
|
|
+ * @param taskId 任务id
|
|
|
+ * @param files 上传的文件
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @RequestMapping(value="uploadFile")
|
|
|
+ public HttpRespMsg uploadFile(
|
|
|
+ @RequestParam Integer projectId,
|
|
|
+ @RequestParam Integer taskId,
|
|
|
+ @RequestParam("file") MultipartFile[] files,
|
|
|
+ HttpServletResponse response) throws Exception {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("Token"));
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ TaskFiles record = new TaskFiles();
|
|
|
+ record.setCreatorId(user.getId());
|
|
|
+ record.setCreatorName(user.getName());
|
|
|
+ record.setDocumentName(file.getOriginalFilename());
|
|
|
+ record.setTaskId(taskId);
|
|
|
+ record.setProjectId(projectId);
|
|
|
+ if (file != null && !file.isEmpty()) {
|
|
|
+ //截取文件后缀
|
|
|
+ String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
+ record.setDocumentType(DocumentTypeUtil.DocumentType(fileSuffix));
|
|
|
+ //处理文件
|
|
|
+ File dir = new File(uploadPath);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdir();
|
|
|
+ }
|
|
|
+ String fileName= "";
|
|
|
+ if (file!=null && !file.isEmpty()) {
|
|
|
+ fileName = file.getOriginalFilename();
|
|
|
+
|
|
|
+ int pos = fileName.lastIndexOf(".");
|
|
|
+ String suffix = fileName.substring(pos).toLowerCase();
|
|
|
+ //用uuid替换原始的文件名
|
|
|
+ String purFName = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
+ fileName = purFName + suffix;
|
|
|
+ File saveFile = new File(dir, fileName);
|
|
|
+ try {
|
|
|
+ saveFile.createNewFile();
|
|
|
+ file.transferTo(saveFile);
|
|
|
+ //计算文件大小
|
|
|
+ long fileSize = saveFile.length();
|
|
|
+ String fileLength = FileUtil.getReadableFileSize(fileSize);
|
|
|
+ record.setServerName(uploadPath + fileName);
|
|
|
+ record.setSize(fileLength);
|
|
|
+ String pathPrefix = "/upload/";
|
|
|
+ record.setUrl(pathPrefix + fileName);
|
|
|
+ //上传图片到OSS
|
|
|
+// OSSClient ossClient=AliyunOSSClientUtil.getOSSClient();
|
|
|
+// String md5key = AliyunOSSClientUtil.uploadObject2OSS(ossClient, saveFile, OSSClientConstants.BUCKET_NAME, OSSClientConstants.FOLDER);
|
|
|
+// String url = AliyunOSSClientUtil.getFullUrl(fileName);
|
|
|
+// record.setUrl(url);
|
|
|
+ taskFilesMapper.insert(record);
|
|
|
+ String path = uploadPath;
|
|
|
+// if (OpenOfficeService.canTransferToPdf(suffix)) {
|
|
|
+// //上传完,需要生成pdf
|
|
|
+// String dFile1 = path + UUID.randomUUID().toString().replaceAll("-", "") + ".pdf";
|
|
|
+// File newFile = new File(dFile1);
|
|
|
+// if (!newFile.exists()) {
|
|
|
+// openOfficeService.office2PDF(path + fileName, dFile1);
|
|
|
+// PdfFile pdfFile = new PdfFile();
|
|
|
+// pdfFile.setPdfUrl(pathPrefix + dFile1.substring(path.length()));
|
|
|
+// pdfFile.setFileId(record.getId());
|
|
|
+// pdfFile.setType(1);
|
|
|
+// pdfFile.setSourceFileUrl(record.getUrl());
|
|
|
+// pdfFileMapper.insert(pdfFile);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ //生成原文件名称与服务器文件名称对应
|
|
|
+ msg.data = record;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ fileName = null;
|
|
|
+ msg.setError(e.getMessage()+", path="+dir.getAbsolutePath());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ fileName = null;
|
|
|
+ msg.setError(e.getMessage()+", path="+dir.getAbsolutePath());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ msg.setError("文件不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value="delete")
|
|
|
+ public HttpRespMsg delFile(@RequestParam Integer id) {
|
|
|
+ TaskFiles taskFiles = taskFilesMapper.selectById(id);
|
|
|
+ if (taskFiles.getDocumentId() == null) {
|
|
|
+ //仅当前任务上传的,需要把文件删掉
|
|
|
+ File dir = new File(uploadPath);
|
|
|
+ File targetFile = new File(dir, taskFiles.getServerName());
|
|
|
+ if (targetFile.exists()) {
|
|
|
+ targetFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ taskFilesMapper.deleteById(id);
|
|
|
+
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ msg.data="删除成功";
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value="createDocument")
|
|
|
+ public HttpRespMsg createDocument(Integer id, Integer projectId, String documentName, Integer folderId, Integer isFolder) {
|
|
|
+ User user = userMapper.selectById(request.getHeader("Token"));
|
|
|
+ ProjectDocument record = new ProjectDocument();
|
|
|
+ record.setId(id);
|
|
|
+ record.setIsFolder(isFolder);
|
|
|
+ record.setDocumentName(documentName);
|
|
|
+ record.setFolderId(folderId);
|
|
|
+ record.setProjectId(projectId);
|
|
|
+ record.setCreatorId(user.getId());
|
|
|
+ record.setCreatorName(user.getName());
|
|
|
+ projectDocumentService.saveOrUpdate(record);
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 所有文件列表
|
|
|
+ * taskId 任务id
|
|
|
+ */
|
|
|
+ @RequestMapping(value="getTaskFiles")
|
|
|
+ public HttpRespMsg getTaskFiles(Integer taskId) {
|
|
|
+ List<TaskFiles> list = taskFilesMapper.selectList(new QueryWrapper<TaskFiles>().eq("task_id", taskId));
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ msg.data = list;
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|