Browse Source

1.0.4 bug

xiaofei 11 months ago
parent
commit
3bec7b74cc

+ 5 - 0
paper-library-business/src/main/java/com/qmth/paper/library/business/service/FileUploadService.java

@@ -14,7 +14,12 @@ public interface FileUploadService {
 
     File downloadFile(String path, String filePathName) throws Exception;
 
+    InputStream downloadInputStream(String path, String ossType);
+
+    boolean copy(String sourcePath, String destPath);
+
     String filePreview(String path);
+
     String filePreview(FilePathVo filePathVo);
 
     boolean deleteFile(String path);

+ 25 - 0
paper-library-business/src/main/java/com/qmth/paper/library/business/service/impl/FileUploadServiceImpl.java

@@ -21,6 +21,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.io.*;
+import java.nio.file.Files;
 import java.util.Objects;
 
 /**
@@ -93,6 +94,30 @@ public class FileUploadServiceImpl implements FileUploadService {
         }
     }
 
+    @Override
+    public InputStream downloadInputStream(String path, String ossType) {
+        InputStream inputStream;
+        try {
+            if (Objects.equals(ossType, SystemConstant.OSS)) {
+                inputStream = fileStoreUtil.ossDownloadIs(path);
+            } else {
+                inputStream = Files.newInputStream(new File(path).toPath());
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        return inputStream;
+    }
+
+    @Override
+    public boolean copy(String sourcePath, String destPath) {
+        if(StringUtils.isNotBlank(sourcePath) && StringUtils.isNotBlank(destPath)){
+            return fileStoreUtil.copy(sourcePath, destPath);
+        } else {
+            return false;
+        }
+    }
+
     /**
      * 预览文件
      *

+ 84 - 8
paper-library-business/src/main/java/com/qmth/paper/library/business/service/impl/PaperLibraryServiceImpl.java

@@ -2,14 +2,17 @@ package com.qmth.paper.library.business.service.impl;
 
 import com.alibaba.fastjson.JSON;
 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.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.boot.api.exception.ApiException;
 import com.qmth.paper.library.business.bean.result.PaperLibraryImageResult;
 import com.qmth.paper.library.business.bean.result.PaperLibraryResult;
 import com.qmth.paper.library.business.bean.result.TaskStudentResult;
 import com.qmth.paper.library.business.bean.vo.FilePathVo;
+import com.qmth.paper.library.business.bean.vo.PathSequenceVo;
 import com.qmth.paper.library.business.bean.vo.RecognitionResultVo;
 import com.qmth.paper.library.business.entity.PaperLibrary;
 import com.qmth.paper.library.business.entity.PaperScanTask;
@@ -18,17 +21,17 @@ import com.qmth.paper.library.business.service.*;
 import com.qmth.paper.library.common.bean.dto.syssetting.SimpleObject;
 import com.qmth.paper.library.common.contant.SysSettingConstant;
 import com.qmth.paper.library.common.contant.SystemConstant;
+import com.qmth.paper.library.common.entity.BasicExam;
 import com.qmth.paper.library.common.entity.BasicSchool;
+import com.qmth.paper.library.common.entity.BasicSemester;
 import com.qmth.paper.library.common.entity.ExamStudent;
-import com.qmth.paper.library.common.enums.ExceptionResultEnum;
-import com.qmth.paper.library.common.enums.RecognitionTypeEnum;
-import com.qmth.paper.library.common.enums.RoleTypeEnum;
-import com.qmth.paper.library.common.enums.UploadFileEnum;
+import com.qmth.paper.library.common.enums.*;
 import com.qmth.paper.library.common.service.BasicSchoolService;
+import com.qmth.paper.library.common.service.BasicSemesterService;
 import com.qmth.paper.library.common.service.CommonCacheService;
-import com.qmth.paper.library.common.service.SysUserService;
 import com.qmth.paper.library.common.util.*;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.json.JSONArray;
 import org.springframework.stereotype.Service;
@@ -41,7 +44,6 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 /**
  * <p>
@@ -56,6 +58,12 @@ public class PaperLibraryServiceImpl extends ServiceImpl<PaperLibraryMapper, Pap
     @Resource
     private BasicSchoolService basicSchoolService;
     @Resource
+    private BasicExamService basicExamService;
+    @Resource
+    private BusinessCacheService businessCacheService;
+    @Resource
+    private BasicSemesterService basicSemesterService;
+    @Resource
     private ExamStudentService examStudentService;
     @Resource
     private FileUploadService fileUploadService;
@@ -252,11 +260,23 @@ public class PaperLibraryServiceImpl extends ServiceImpl<PaperLibraryMapper, Pap
         if (paperLibrary.getStudentId() != null && paperLibrary.getStudentId().equals(studentId)) {
             return true;
         }
+        PathSequenceVo pathSequenceVo = null;
+        try {
+            // 将图片复制到学号目录下,并删除原图片
+            pathSequenceVo = copyImageToStudentCode(paperLibrary, studentId, UploadFileEnum.IMAGE);
+        } catch (Exception e) {
+            log.error("纠错操作复制图片失败");
+        }
 
         // 更新绑定考生ID
         UpdateWrapper<PaperLibrary> updateWrapper = new UpdateWrapper<>();
-        updateWrapper.lambda().set(PaperLibrary::getStudentId, studentId)
-                .eq(PaperLibrary::getId, paperLibraryId);
+        LambdaUpdateWrapper<PaperLibrary> lambda = updateWrapper.lambda();
+        lambda.set(PaperLibrary::getStudentId, studentId);
+        if (pathSequenceVo != null) {
+            lambda.set(PaperLibrary::getPath, pathSequenceVo.getPath());
+            lambda.set(PaperLibrary::getSequence, pathSequenceVo.getSequence());
+        }
+        lambda.eq(PaperLibrary::getId, paperLibraryId);
         this.update(updateWrapper);
         // 更新考生绑定数量
         if (paperLibrary.getStudentId() != null) {
@@ -266,6 +286,62 @@ public class PaperLibraryServiceImpl extends ServiceImpl<PaperLibraryMapper, Pap
         return true;
     }
 
+    private PathSequenceVo copyImageToStudentCode(PaperLibrary paperLibrary, Long studentId, UploadFileEnum uploadFileEnum) {
+        PaperScanTask paperScanTask = paperScanTaskService.getById(paperLibrary.getPaperScanTaskId());
+        BasicSchool basicSchool = basicSchoolService.getById(paperScanTask.getSchoolId());
+        BasicExam basicExam = basicExamService.getById(paperScanTask.getExamId());
+        BasicSemester basicSemester = basicSemesterService.getById(basicExam.getSemesterId());
+        String courseNameCode = String.format("%s(%s)", paperScanTask.getCourseName(), paperScanTask.getCourseCode());
+        ExamStudent examStudent = examStudentService.getById(studentId);
+        String rootPath = fileStoreUtil.buildPath(UploadFileEnum.IMAGE, false, basicSchool.getName(), basicSemester.getName(), basicExam.getName(), courseNameCode);
+        if (StoreTypeEnum.ROOM.equals(paperScanTask.getStoreType())) {
+            rootPath = fileStoreUtil.buildPath(UploadFileEnum.IMAGE, false, basicSchool.getName(), basicSemester.getName(), basicExam.getName(), courseNameCode, examStudent.getExamRoom());
+        } else if (StoreTypeEnum.CLASS.equals(paperScanTask.getStoreType())) {
+            rootPath = fileStoreUtil.buildPath(UploadFileEnum.IMAGE, false, basicSchool.getName(), basicSemester.getName(), basicExam.getName(), courseNameCode, examStudent.getClassName());
+        } else if (StoreTypeEnum.COURSE.equals(paperScanTask.getStoreType())) {
+            rootPath = fileStoreUtil.buildPath(UploadFileEnum.IMAGE, false, basicSchool.getName(), basicSemester.getName(), basicExam.getName(), courseNameCode, examStudent.getStudentCode());
+        }
+        // 根目录
+        boolean isAllCopy = true;
+        List<FilePathVo> objects = new ArrayList<>();
+        int sequence = businessCacheService.getSequence(paperScanTask.getId(), examStudent.getStudentCode());
+        String sequenceStr = String.valueOf(sequence);
+        try {
+            int idx = 1;
+            String prefix = examStudent.getStudentCode() + "-" + sequenceStr;
+            List<FilePathVo> filePathVoList = JSON.parseArray(paperLibrary.getPath(), FilePathVo.class);
+            for (FilePathVo filePath : filePathVoList) {
+                String suffix = filePath.getPath().substring(filePath.getPath().lastIndexOf("."));
+                // 目标文件名
+                String dirName = rootPath + File.separator + prefix + "-" + idx + suffix;
+                File file = new File(dirName);
+                if(!file.exists()){
+                    file.getParentFile().mkdirs();
+                    file.createNewFile();
+                }
+                boolean success = fileUploadService.copy(filePath.getPath(), dirName);
+                if (isAllCopy) {
+                    isAllCopy = success;
+                }
+                objects.add(new FilePathVo(dirName, uploadFileEnum, filePath.getType(), filePath.getMd5()));
+                idx++;
+            }
+            if (isAllCopy) {
+                for (FilePathVo filePathVo : filePathVoList) {
+                    fileUploadService.deleteFile(filePathVo.getPath());
+                }
+            }
+        } catch (Exception e) {
+            log.error(SystemConstant.LOG_ERROR, e);
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        }
+        return isAllCopy ? new PathSequenceVo(JSON.toJSONString(objects), sequence) : null;
+    }
+
     @Override
     public int countByStudentId(Long studentId) {
         QueryWrapper<PaperLibrary> queryWrapper = new QueryWrapper<>();

+ 22 - 0
paper-library-common/src/main/java/com/qmth/paper/library/common/util/FileStoreUtil.java

@@ -77,6 +77,17 @@ public class FileStoreUtil {
         return this.saveLocal(fileStore.read(dirName), destPath);
     }
 
+    /**
+     * 从文件存储上下载文件到InputStream
+     *
+     * @param objectName 文件地址
+     * @throws Exception 异常
+     */
+    public InputStream ossDownloadIs(String objectName) throws Exception {
+        log.info("oss Download is come in");
+        return fileStore.read(objectName);
+    }
+
     /**
      * 文件存在某本地路径
      *
@@ -274,4 +285,15 @@ public class FileStoreUtil {
         }
         return true;
     }
+
+    public boolean copy(String sourcePath, String destPath) {
+        try {
+            sourcePath = sourcePath.replace(dictionaryConfig.fssDomain().getConfig(), "");
+            destPath = destPath.replace(dictionaryConfig.fssDomain().getConfig(), "");
+            fileStore.copy(sourcePath, destPath);
+            return fileStore.exist(destPath);
+        } catch (Exception e) {
+            return false;
+        }
+    }
 }