|
@@ -0,0 +1,164 @@
|
|
|
+package com.management.platform.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.management.platform.entity.ContactsDocument;
|
|
|
+import com.management.platform.entity.ContractDocument;
|
|
|
+import com.management.platform.entity.User;
|
|
|
+import com.management.platform.entity.dto.ContactsFileDto;
|
|
|
+import com.management.platform.mapper.ContactsDocumentMapper;
|
|
|
+import com.management.platform.mapper.UserMapper;
|
|
|
+import com.management.platform.service.ContactsDocumentService;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+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.transaction.annotation.Transactional;
|
|
|
+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.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author Seyason
|
|
|
+ * @since 2024-03-14
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ContactsDocumentServiceImpl extends ServiceImpl<ContactsDocumentMapper, ContactsDocument> implements ContactsDocumentService {
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ContactsDocumentMapper contactsDocumentMapper;
|
|
|
+
|
|
|
+ @Value(value = "${upload.path}")
|
|
|
+ private String path;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg fileUpload(HttpServletRequest request, Integer contactsId, Integer folderId, MultipartFile[] files) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("token"));
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ ContactsDocument record = new ContactsDocument();
|
|
|
+ record.setCreatorId(user.getId());
|
|
|
+ record.setCreatorName(user.getName());
|
|
|
+ record.setDocumentName(file.getOriginalFilename());
|
|
|
+ record.setFolderId(folderId);
|
|
|
+ record.setContactsId(contactsId);
|
|
|
+ if (file != null && !file.isEmpty()) {
|
|
|
+ //截取文件后缀
|
|
|
+ String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
+ record.setDocumentType(DocumentTypeUtil.DocumentType(fileSuffix));
|
|
|
+ //处理文件
|
|
|
+ File dir = new File(path);
|
|
|
+ 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(path + fileName);
|
|
|
+ record.setSize(fileLength);
|
|
|
+ String pathPrefix = "/upload/";
|
|
|
+ record.setUrl(pathPrefix + fileName);
|
|
|
+ contactsDocumentMapper.insert(record);
|
|
|
+ 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(MessageUtils.message("file.nonExistentError"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg fileDown(HttpServletRequest request, HttpServletResponse response, Integer folderId, Integer contactsId, Integer fileId) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("token"));
|
|
|
+ List<ContactsDocument> files = contactsDocumentMapper.selectList(new QueryWrapper<ContactsDocument>().eq("contacts_id", contactsId));
|
|
|
+ try {
|
|
|
+ ServletOutputStream os = response.getOutputStream();
|
|
|
+ for (ContactsDocument file : files) {
|
|
|
+ if (fileId.equals(file.getId())){
|
|
|
+ File uploadFile = new File(file.getServerName());
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getDocumentName(), "UTF-8"));
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ // 读取文件的字节流
|
|
|
+ os.write(FileUtil.readFileByBytes(uploadFile));
|
|
|
+ os.flush();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (IOException e) {
|
|
|
+ msg.setError(MessageUtils.message("file.error"));
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public HttpRespMsg fileDelete(HttpServletRequest request, ContactsFileDto contactsFileDto) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ User user = userMapper.selectById(request.getHeader("Token"));
|
|
|
+ List<Integer> fileIds = contactsFileDto.getFileIds();
|
|
|
+ List<ContactsDocument> contactsDocuments = contactsDocumentMapper.selectBatchIds(fileIds);
|
|
|
+ for (ContactsDocument contactsDocument : contactsDocuments) {
|
|
|
+ contactsDocumentMapper.update(null,new UpdateWrapper<ContactsDocument>().eq("contacts_id", contactsDocument.getContactsId()).set("is_deleted", 1));
|
|
|
+ }
|
|
|
+ msg.setMsg("删除成功");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public HttpRespMsg reNameFile(HttpServletRequest request, Integer fileId, Integer contactsId, String newName) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ ContactsDocument contactsDocument = contactsDocumentMapper.selectById(fileId);
|
|
|
+ if (contactsDocument==null){
|
|
|
+ msg.setError("文件不存在");
|
|
|
+ }
|
|
|
+ LambdaUpdateWrapper<ContactsDocument> ulw = new LambdaUpdateWrapper<>();
|
|
|
+ ulw.set(ContactsDocument::getDocumentName,newName).eq(ContactsDocument::getId,fileId);
|
|
|
+ contactsDocumentMapper.update(null,ulw);
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+}
|