|
|
@@ -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";
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|