|
|
@@ -0,0 +1,204 @@
|
|
|
+package com.management.platform.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.management.platform.entity.ProjectApproval;
|
|
|
+import com.management.platform.entity.ProjectApprovalDocument;
|
|
|
+import com.management.platform.entity.User;
|
|
|
+import com.management.platform.entity.vo.ContractFileDelVO;
|
|
|
+import com.management.platform.mapper.ProjectApprovalDocumentMapper;
|
|
|
+import com.management.platform.mapper.ProjectApprovalMapper;
|
|
|
+import com.management.platform.mapper.SysFunctionMapper;
|
|
|
+import com.management.platform.mapper.UserMapper;
|
|
|
+import com.management.platform.service.ProjectApprovalDocumentService;
|
|
|
+import com.management.platform.util.DocumentTypeUtil;
|
|
|
+import com.management.platform.util.FileUtil;
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
+import com.management.platform.util.MessageUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ProjectApprovalDocumentServiceImpl extends ServiceImpl<ProjectApprovalDocumentMapper, ProjectApprovalDocument> implements ProjectApprovalDocumentService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+ @Resource
|
|
|
+ private ProjectApprovalMapper projectApprovalMapper;
|
|
|
+ @Resource
|
|
|
+ private ProjectApprovalDocumentMapper projectApprovalDocumentMapper;
|
|
|
+ @Resource
|
|
|
+ private SysFunctionMapper sysFunctionMapper;
|
|
|
+ @Value(value = "${upload.path}")
|
|
|
+ private String path;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg fileUpload(HttpServletRequest request, Integer projectApprovalId, Integer folderId, MultipartFile[] files) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("token"));
|
|
|
+ ProjectApproval projectApproval = projectApprovalMapper.selectById(projectApprovalId);
|
|
|
+ if (projectApproval == null) {
|
|
|
+ msg.setError("立项不存在");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ boolean canManage = user.getId().equals(projectApproval.getCreatorId()) || user.getId().equals(projectApproval.getInchargerId());
|
|
|
+ if (!canManage) {
|
|
|
+ msg.setError(MessageUtils.message("access.operationError"));
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ProjectApprovalDocument record = new ProjectApprovalDocument();
|
|
|
+ record.setCreatorId(user.getId());
|
|
|
+ record.setCreatorName(user.getName());
|
|
|
+ record.setDocumentName(file.getOriginalFilename());
|
|
|
+ record.setFolderId(folderId);
|
|
|
+ record.setProjectApprovalId(projectApprovalId);
|
|
|
+ record.setIndate(LocalDateTime.now());
|
|
|
+ record.setIsDeleted(0);
|
|
|
+ record.setIsFolder(0);
|
|
|
+
|
|
|
+ String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
+ record.setDocumentType(DocumentTypeUtil.DocumentType(fileSuffix));
|
|
|
+
|
|
|
+ File dir = new File(path);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ String fileName = UUID.randomUUID().toString().replaceAll("-", "") + fileSuffix;
|
|
|
+ File saveFile = new File(dir, fileName);
|
|
|
+ saveFile.createNewFile();
|
|
|
+ file.transferTo(saveFile);
|
|
|
+ long fileSize = saveFile.length();
|
|
|
+ record.setServerName(path + fileName);
|
|
|
+ record.setSize(FileUtil.getReadableFileSize(fileSize));
|
|
|
+ record.setUrl("/upload/" + fileName);
|
|
|
+ projectApprovalDocumentMapper.insert(record);
|
|
|
+ msg.data = record;
|
|
|
+ } catch (IOException e) {
|
|
|
+ msg.setError(e.getMessage());
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg fileDown(HttpServletRequest request, HttpServletResponse response, Integer folderId, Integer projectApprovalId, Integer fileId) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("token"));
|
|
|
+ ProjectApproval projectApproval = projectApprovalMapper.selectById(projectApprovalId);
|
|
|
+ if (projectApproval == null) {
|
|
|
+ msg.setError("立项不存在");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ boolean canView = user.getId().equals(projectApproval.getCreatorId()) || user.getId().equals(projectApproval.getInchargerId());
|
|
|
+ if (!canView) {
|
|
|
+ msg.setError(MessageUtils.message("access.viewError"));
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ List<ProjectApprovalDocument> files = projectApprovalDocumentMapper.selectList(
|
|
|
+ new QueryWrapper<ProjectApprovalDocument>()
|
|
|
+ .eq("project_approval_id", projectApprovalId)
|
|
|
+ .eq("is_deleted", 0)
|
|
|
+ );
|
|
|
+ try {
|
|
|
+ for (ProjectApprovalDocument file : files) {
|
|
|
+ if (fileId.equals(file.getId())) {
|
|
|
+ File uploadFile = new File(file.getServerName());
|
|
|
+ if (!uploadFile.exists()) {
|
|
|
+ msg.setError(MessageUtils.message("file.error"));
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ long fileLength = uploadFile.length();
|
|
|
+ String fileName = URLEncoder.encode(file.getDocumentName(), "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.reset();
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
|
|
|
+ response.addHeader("Accept-Ranges", "bytes");
|
|
|
+
|
|
|
+ long start = 0;
|
|
|
+ long end = fileLength - 1;
|
|
|
+ String range = request.getHeader("Range");
|
|
|
+ if (range != null && range.startsWith("bytes=")) {
|
|
|
+ String[] ranges = range.substring(6).split("-");
|
|
|
+ start = Long.parseLong(ranges[0]);
|
|
|
+ if (ranges.length > 1 && ranges[1] != null && !ranges[1].isEmpty()) {
|
|
|
+ end = Long.parseLong(ranges[1]);
|
|
|
+ }
|
|
|
+ if (end >= fileLength) {
|
|
|
+ end = fileLength - 1;
|
|
|
+ }
|
|
|
+ response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
|
|
+ response.addHeader("Content-Range", "bytes " + start + "-" + end + "/" + fileLength);
|
|
|
+ } else {
|
|
|
+ response.setStatus(HttpServletResponse.SC_OK);
|
|
|
+ }
|
|
|
+
|
|
|
+ try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(uploadFile));
|
|
|
+ ServletOutputStream os = response.getOutputStream()) {
|
|
|
+ if (start > 0) {
|
|
|
+ bis.skip(start);
|
|
|
+ }
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ long position = start;
|
|
|
+ int len;
|
|
|
+ while ((len = bis.read(buffer)) != -1 && position <= end) {
|
|
|
+ if (position + len - 1 > end) {
|
|
|
+ len = (int) (end - position + 1);
|
|
|
+ }
|
|
|
+ os.write(buffer, 0, len);
|
|
|
+ position += len;
|
|
|
+ if (position > end) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ os.flush();
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ msg.setError(MessageUtils.message("file.error"));
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg fileDelete(HttpServletRequest request, ContractFileDelVO contractFileDelVO) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("Token"));
|
|
|
+ if (user == null) {
|
|
|
+ msg.setError("用户不存在");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ List<Integer> fileIds = contractFileDelVO.getFileIds();
|
|
|
+ List<ProjectApprovalDocument> docs = projectApprovalDocumentMapper.selectBatchIds(fileIds);
|
|
|
+ for (ProjectApprovalDocument doc : docs) {
|
|
|
+ doc.setIsDeleted(1);
|
|
|
+ projectApprovalDocumentMapper.updateById(doc);
|
|
|
+ }
|
|
|
+ msg.msg = MessageUtils.message("file.deleteSuc");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+}
|