瀏覽代碼

存储业务文件

‘chenzhilei’ 3 月之前
父節點
當前提交
63020d2407

+ 26 - 0
src/main/java/cn/hnthyy/thmz/controller/mz/FileController.java

@@ -4,6 +4,7 @@ package cn.hnthyy.thmz.controller.mz;
 import cn.hnthyy.thmz.Utils.ImageUtil;
 import cn.hnthyy.thmz.Utils.JsonUtil;
 import cn.hnthyy.thmz.Utils.TokenUtil;
+import cn.hnthyy.thmz.comment.PassToken;
 import cn.hnthyy.thmz.comment.UserLoginToken;
 import cn.hnthyy.thmz.entity.thmz.FileUpload;
 import cn.hnthyy.thmz.entity.thmz.User;
@@ -253,6 +254,8 @@ public class FileController {
 //    }
 
 
+
+
     /**
      * 查询文件列表
      *
@@ -282,5 +285,28 @@ public class FileController {
         return resultMap;
     }
 
+    @RequestMapping(value = "/uploadPatientFileData", method = {RequestMethod.POST})
+    @PassToken
+    public Map<String, Object> uploadPatientFileData(@RequestParam("file") MultipartFile file,
+                                                     @RequestParam("inpatientNo") String inpatientNo,
+                                                     @RequestParam("reqNo") String reqNo,
+                                                     @RequestParam("path") String path,
+                                                     @RequestParam("times") String times) {
+        return fileUploadService.uploadPatientFileData(file,inpatientNo,times,reqNo,path);
+
+
+    }
+
+    @RequestMapping(value = "/uploadPatientFileDataAboutXueTou", method = {RequestMethod.POST})
+    @PassToken
+    public Map<String, Object> uploadPatientFileData(@RequestParam("file") MultipartFile file,
+                                                     @RequestParam("inpatientNo") String inpatientNo,
+                                                     @RequestParam("reqNo") String reqNo,
+                                                     @RequestParam("times") String times) {
+        return fileUploadService.uploadPatientFileData(file,inpatientNo,times,reqNo,"血液透析");
+
+
+    }
+
 
 }

+ 22 - 4
src/main/java/cn/hnthyy/thmz/mapper/thmz/FileUploadMapper.java

@@ -1,10 +1,7 @@
 package cn.hnthyy.thmz.mapper.thmz;
 
 import cn.hnthyy.thmz.entity.thmz.FileUpload;
-import org.apache.ibatis.annotations.Insert;
-import org.apache.ibatis.annotations.Options;
-import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.*;
 
 import java.util.List;
 
@@ -71,4 +68,25 @@ public interface FileUploadMapper {
                     + "</script>"})
     List<FileUpload> selectFileUploadWithPage(FileUpload fileUpload);
 
+    /**
+     * 插入文件数据
+     * @return
+     */
+    @Insert("insert into patient_file_data(inpatient_no,times,req_no,file_id) values(#{inpatientNo},#{times},#{reqNo},#{fileId})")
+    int insertPatientFileData(String inpatientNo,String times,String reqNo,String fileId);
+
+    /**
+     * 删除文件数据
+     * @return
+     */
+    @Delete("delete from patient_file_data where req_no=#{reqNo}")
+    int deletePatientFileDataByReqNo(String reqNo);
+
+//    /**
+//     * 查询文件数据
+//     * @return
+//     */
+//    @Select("select * from patient_file_data where req_no=#{reqNo}")
+//    int selectPatientFileDataByReqNo(String reqNo);
+
 }

+ 119 - 0
src/main/java/cn/hnthyy/thmz/service/impl/thmz/FileUploadServiceImpl.java

@@ -6,11 +6,18 @@ import cn.hnthyy.thmz.enums.FileTypeEnum;
 import cn.hnthyy.thmz.mapper.thmz.FileUploadMapper;
 import cn.hnthyy.thmz.mapper.thmz.UserMapper;
 import cn.hnthyy.thmz.service.thmz.FileUploadService;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+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 java.io.*;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.UUID;
 
 
 @Service
@@ -22,6 +29,10 @@ public class FileUploadServiceImpl implements FileUploadService {
     @Autowired
     private UserMapper userMapper;
 
+    //叫号通知接口地址
+    @Value("${web.upload.path}")
+    private String uploadPath;
+
 
     @Override
     public FileUpload queryByName(String name) {
@@ -55,4 +66,112 @@ public class FileUploadServiceImpl implements FileUploadService {
     public List<FileUpload> queryFileUploadWithPage(FileUpload fileUpload) {
         return fileUploadMapper.selectFileUploadWithPage(fileUpload);
     }
+
+    public Map<String, Object> uploadPatientFileData(MultipartFile file, String inpatientNo, String times, String reqNo, String path) {
+        Map<String, Object> resultMap = new HashMap<>();
+        String[] nameAndSuffixs = file.getOriginalFilename().split("\\.");
+        StringBuilder nameBuilder = new StringBuilder();
+        nameBuilder.append(UUID.randomUUID().toString().replaceAll("-", "")).append(".").append(nameAndSuffixs[nameAndSuffixs.length - 1]);
+        System.out.println(nameBuilder);
+        String filePath = uploadFileSingle(path, nameBuilder.toString(),file);
+        if (StringUtils.isEmpty(filePath)) {
+            resultMap.put("code", -1);
+            resultMap.put("message", "保存文件失败!");
+        } else {
+            fileUploadMapper.deletePatientFileDataByReqNo(reqNo);
+            fileUploadMapper.insertPatientFileData(inpatientNo,times,reqNo,filePath);
+            resultMap.put("code", 0);
+            resultMap.put("message", "保存文件成功!");
+        }
+        return resultMap;
+
+    }
+
+    private String uploadFileSingle(String fileOrderPath,String name,MultipartFile file) {
+        String path = "";
+        StringBuilder sb = new StringBuilder();
+        if (StringUtils.isNotEmpty(fileOrderPath)) {
+            if(!fileOrderPath.startsWith("/")) {
+                sb.append("/");
+            }
+            sb.append(fileOrderPath);
+        } else {
+            sb.append("/temp");
+        }
+        if(!fileOrderPath.endsWith("/")) {
+            sb.append("/");
+        }
+        String filePath = sb.toString();
+        if (createMkdir(uploadPath + filePath)) {
+            path = saveFile(file, filePath, name);
+        }
+
+        return path;
+    }
+
+    /**
+     * 根据路径创建目录
+     *
+     * @param path
+     * @return
+     */
+    private boolean createMkdir(String path) {
+        File baseFolder = new File(path);
+        if (baseFolder.exists()) {
+            //logger.info(path + ": 文件夹已存在!");
+            return true;
+        } else {
+            if (baseFolder.mkdirs()) {
+                System.out.println(path + ": 文件夹创建成功!");
+                return true;
+            } else {
+                System.out.println(path + ": 文件夹创建失败!");
+                return false;
+            }
+        }
+    }
+
+    /**
+     * 根据路径保存文件
+     *
+     * @param file
+     * @param path
+     * @return
+     */
+    private String saveFile(MultipartFile file, String path, String newName) {
+        //获取文件名
+        String fileName = file.getOriginalFilename();
+        fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
+        if (StringUtils.isNotEmpty(newName)) {
+            fileName = newName;
+        }
+        String filePath = path + "/" + fileName;
+        File newFile = new File(uploadPath + filePath);
+
+        //同名文件覆盖
+        if (newFile.exists()) {
+            newFile.delete();
+        }
+
+        try {
+            InputStream in = file.getInputStream();
+            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
+            byte[] buffer = new byte[1024 * 3];
+            int len;
+            while ((len = in.read(buffer)) > 0) {
+                bos.write(buffer, 0, len);
+            }
+            bos.flush();
+            bos.close();
+            in.close();
+
+            StringBuffer pathBuffer = new StringBuffer();
+//            pathBuffer.append("/locate").append(path).append("/").append(fileName);
+            pathBuffer.append(path).append(fileName);
+            return pathBuffer.toString();
+        } catch (IOException e) {
+//            logger.info("saveFile ioException");
+            return "-1";
+        }
+    }
 }

+ 8 - 0
src/main/java/cn/hnthyy/thmz/service/thmz/FileUploadService.java

@@ -1,8 +1,10 @@
 package cn.hnthyy.thmz.service.thmz;
 
 import cn.hnthyy.thmz.entity.thmz.FileUpload;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
+import java.util.Map;
 
 public interface FileUploadService {
 
@@ -27,4 +29,10 @@ public interface FileUploadService {
      * @return
      */
     List<FileUpload> queryFileUploadWithPage(FileUpload fileUpload);
+
+    /**
+     * 保存文件至业务文件表
+     * @return
+     */
+    Map<String, Object> uploadPatientFileData(MultipartFile file, String inpatientNo, String times, String reqNo, String path);
 }

+ 4 - 0
src/main/resources/application-demo.yml

@@ -18,6 +18,10 @@ server:
   #项目名,如果不设定,默认是 /
   servlet:
     context-path: "/thmz"
+web:
+  upload:
+    #    path: E:/project/medical_pension/uploadroot
+    path: ../uploadroot
 spring:
   #jackson:
     #date-format: "yyyy-MM-dd HH:mm:ss"

+ 4 - 0
src/main/resources/application-dev.yml

@@ -18,6 +18,10 @@ server:
   #项目名,如果不设定,默认是 /
   servlet:
     context-path: "/thmz"
+web:
+  upload:
+    #    path: E:/project/medical_pension/uploadroot
+    path: ../uploadroot
 spring:
   #jackson:
     #date-format: "yyyy-MM-dd HH:mm:ss"

+ 4 - 0
src/main/resources/application-hh.yml

@@ -17,6 +17,10 @@ server:
 #项目名,如果不设定,默认是 /
   servlet:
     context-path: "/thmz"
+web:
+  upload:
+    #    path: E:/project/medical_pension/uploadroot
+    path: ../uploadroot
 spring:
   #jackson:
     #date-format: "yyyy-MM-dd HH:mm:ss"

+ 4 - 0
src/main/resources/application-mhyy.yml

@@ -17,6 +17,10 @@ server:
   #项目名,如果不设定,默认是 /
   servlet:
     context-path: "/thmz"
+web:
+  upload:
+    #    path: E:/project/medical_pension/uploadroot
+    path: ../uploadroot
 spring:
   #jackson:
   #date-format: "yyyy-MM-dd HH:mm:ss"

+ 4 - 0
src/main/resources/application-pnfy.yml

@@ -17,6 +17,10 @@ server:
 #项目名,如果不设定,默认是 /
   servlet:
     context-path: "/thmz"
+web:
+  upload:
+    #    path: E:/project/medical_pension/uploadroot
+    path: ../uploadroot
 spring:
   #jackson:
     #date-format: "yyyy-MM-dd HH:mm:ss"

+ 4 - 0
src/main/resources/application-prod.yml

@@ -17,6 +17,10 @@ server:
 #项目名,如果不设定,默认是 /
   servlet:
     context-path: "/thmz"
+web:
+  upload:
+    #    path: E:/project/medical_pension/uploadroot
+    path: ../uploadroot
 spring:
   #jackson:
     #date-format: "yyyy-MM-dd HH:mm:ss"