From bc43c3d892d45ded60e1ba28df9d6a37b7c4d787 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 21 十一月 2022 09:12:43 +0800
Subject: [PATCH] 数据检索:添加上传文件功能

---
 src/main/java/com/lf/server/controller/data/DataQueryController.java |   22 +++++++++-
 src/main/java/com/lf/server/service/data/DataQueryService.java       |   70 ++++++++++++++++++++++++++++++++++-
 src/main/resources/mapper/sys/AttachMapper.xml                       |    2 
 3 files changed, 88 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/lf/server/controller/data/DataQueryController.java b/src/main/java/com/lf/server/controller/data/DataQueryController.java
index f9f3f00..79973fc 100644
--- a/src/main/java/com/lf/server/controller/data/DataQueryController.java
+++ b/src/main/java/com/lf/server/controller/data/DataQueryController.java
@@ -4,9 +4,11 @@
 import com.lf.server.controller.all.BaseQueryController;
 import com.lf.server.entity.all.ResponseMsg;
 import com.lf.server.entity.sys.AttachEntity;
+import com.lf.server.entity.sys.UserEntity;
 import com.lf.server.helper.StringHelper;
 import com.lf.server.service.data.DataQueryService;
 import com.lf.server.service.sys.AttachService;
+import com.lf.server.service.sys.TokenService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
@@ -26,6 +28,9 @@
 @RestController
 @RequestMapping("/dataQuery")
 public class DataQueryController extends BaseQueryController {
+    @Autowired
+    TokenService tokenService;
+
     @Autowired
     AttachService attachService;
 
@@ -56,7 +61,6 @@
     @SysLog()
     @ApiOperation(value = "鍒犻櫎鏂囦欢")
     @ApiImplicitParams({
-            @ApiImplicitParam(name = "tabName", value = "琛ㄥ悕", dataType = "String", paramType = "query", example = "bd.b_pac_frozensoil"),
             @ApiImplicitParam(name = "ids", value = "ID鏁扮粍", dataType = "Integer", paramType = "query", example = "1,2")
     })
     @GetMapping(value = "/deletes")
@@ -77,10 +81,22 @@
     @SysLog()
     @ApiOperation(value = "涓婁紶鏂囦欢")
     @ResponseBody
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "tabName", value = "琛ㄥ悕", dataType = "String", paramType = "query", example = "lf.sys_style"),
+            @ApiImplicitParam(name = "eventid", value = "涓婚敭", dataType = "String", paramType = "query", example = "fa25979a5ef8b43ba82a0be35b3fb0d4")
+    })
     @PostMapping(value = "/uploadFiles")
-    public ResponseMsg<Integer> uploadFiles(@RequestParam("file") MultipartFile[] files, HttpServletRequest req) {
+    public ResponseMsg<Integer> uploadFiles(String tabName, String eventid, @RequestParam("file") MultipartFile[] files, HttpServletRequest req) {
         try {
-            Integer count = dataQueryService.uploadFiles(files, req);
+            if (StringHelper.isEmpty(tabName) || StringHelper.isEmpty(eventid)) {
+                return fail("鍙傛暟涓嶈兘涓虹┖", null);
+            }
+            if (null == files || files.length == 0) {
+                return fail("鏂囦欢涓婁紶涓虹┖", 0);
+            }
+
+            UserEntity ue = tokenService.getCurrentUser(req);
+            Integer count = dataQueryService.uploadFiles(ue, tabName, eventid, files, req);
 
             return success(count);
         } catch (Exception ex) {
diff --git a/src/main/java/com/lf/server/service/data/DataQueryService.java b/src/main/java/com/lf/server/service/data/DataQueryService.java
index fa9fa56..dd834f3 100644
--- a/src/main/java/com/lf/server/service/data/DataQueryService.java
+++ b/src/main/java/com/lf/server/service/data/DataQueryService.java
@@ -1,9 +1,20 @@
 package com.lf.server.service.data;
 
+import com.lf.server.entity.all.SettingData;
+import com.lf.server.entity.sys.AttachEntity;
+import com.lf.server.entity.sys.UserEntity;
+import com.lf.server.helper.FileHelper;
+import com.lf.server.helper.PathHelper;
+import com.lf.server.service.sys.AttachService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * 鏁版嵁妫�绱㈡湇鍔�
@@ -11,9 +22,64 @@
  */
 @Service
 public class DataQueryService {
-    public Integer uploadFiles(MultipartFile[] files, HttpServletRequest req) {
+    @Autowired
+    PathHelper pathHelper;
 
+    @Autowired
+    AttachService attachService;
 
-        return 0;
+    /**
+     * 涓婁紶鏂囦欢
+     */
+    public Integer uploadFiles(UserEntity ue, String tabName, String eventid, MultipartFile[] files, HttpServletRequest req) throws IOException {
+        List<AttachEntity> list = new ArrayList<AttachEntity>();
+        for (MultipartFile file : files) {
+            if (file.getSize() > SettingData.MAX_FILE_SIZE) {
+                continue;
+            }
+
+            // 浼犺緭鏂囦欢
+            String fileName = file.getOriginalFilename();
+            String filePath = pathHelper.getTempPath() + File.separator + fileName;
+            File newFile = new File(filePath);
+            file.transferTo(newFile);
+            double sizes = FileHelper.sizeToMb(file.getSize());
+
+            // 鑾峰彇MD5
+            String md5 = FileHelper.getFileMd5(filePath);
+            AttachEntity entity = attachService.selectByGuid(md5);
+
+            AttachEntity ae = getAttachEntity(ue, tabName, eventid, fileName, md5, sizes);
+            if (entity != null) {
+                ae.setPath(entity.getPath());
+                newFile.delete();
+            }else {
+                String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath();
+                newFile.renameTo(new File(targetPath));
+            }
+
+            list.add(ae);
+        }
+
+        return attachService.inserts(list);
+    }
+
+    /**
+     * 鑾峰彇闄勪欢瀹炰綋绫�
+     */
+    protected AttachEntity getAttachEntity(UserEntity ue, String tabName, String eventid, String fileName, String md5, double sizes) {
+        AttachEntity entity = new AttachEntity();
+        entity.setTab(tabName);
+        entity.setTabGuid(eventid);
+        entity.setName(fileName);
+        entity.setGuid(md5);
+        String subPath = PathHelper.getUploadPath() + File.separator + md5;
+        entity.setPath(subPath);
+        entity.setSizes(sizes);
+        if (ue != null) {
+            entity.setCreateUser(ue.getId());
+        }
+
+        return entity;
     }
 }
diff --git a/src/main/resources/mapper/sys/AttachMapper.xml b/src/main/resources/mapper/sys/AttachMapper.xml
index 358e358..0c79010 100644
--- a/src/main/resources/mapper/sys/AttachMapper.xml
+++ b/src/main/resources/mapper/sys/AttachMapper.xml
@@ -34,7 +34,7 @@
     </select>
 
     <select id="selectByTab" resultType="com.lf.server.entity.sys.AttachEntity">
-        select * from lf.sys_attach where tab = #{tab} and guid = #{guid}
+        select * from lf.sys_attach where tab = #{tab} and tab_guid = #{guid}
     </select>
 
     <insert id="insert" parameterType="com.lf.server.entity.sys.AttachEntity">

--
Gitblit v1.9.3