Эх сурвалжийг харах

update offline submitPaper api

deason 3 жил өмнө
parent
commit
d63f582dca

+ 4 - 17
examcloud-core-oe-admin-api-provider/src/main/java/cn/com/qmth/examcloud/core/oe/admin/api/controller/OfflineExamController.java

@@ -103,24 +103,11 @@ public class OfflineExamController extends ControllerSupport {
         User user = getAccessUser();
 
         String fileSuffix = FileUtil.getFileSuffix(file.getOriginalFilename());
-
-        String fileType;
-        if (".zip".equals(fileSuffix)) {
-            fileType = "ZIP";
-        } else if (".pdf".equals(fileSuffix)) {
-            fileType = "PDF";
-        } else if (".jpg.jpeg.png".contains(fileSuffix)) {
-            fileType = "IMAGE";
-        } else {
-            fileType = "UNKNOWN";
-        }
-
         FileInfo fileInfo = new FileInfo();
         fileInfo.setOriginalFileName(file.getOriginalFilename());
         fileInfo.setFileBytes(file.getBytes());
         fileInfo.setFileSize(file.getSize());
         fileInfo.setFileSuffix(fileSuffix);
-        fileInfo.setFileType(fileType);
 
         offlineExamService.submitPaper(examRecordDataId, fileInfo, user.getUserId());
     }
@@ -135,10 +122,10 @@ public class OfflineExamController extends ControllerSupport {
      */
     @ApiOperation(value = "离线考试:多文件交卷")
     @PostMapping("/batchSubmitPaper")
-    public void batchSubmitPaper(@ApiParam(value = "文件数组") @RequestParam MultipartFile[] fileArray,
-                                 @ApiParam(value = "考试记录id") @RequestParam long examRecordDataId,
-                                 @ApiParam(value = "文件类型:ZIP/PDF/IMAGE") @RequestParam String fileType,
-                                 @ApiParam(value = "md5加密的文件摘要") @RequestParam String[] fileMd5Array) throws Exception {
+    public void batchSubmitPaper(@ApiParam(value = "考试记录id") @RequestParam Long examRecordDataId,
+                                 @ApiParam(value = "文件类型:ZIP、PDF、IMAGE(单选、三选一)") @RequestParam String fileType,
+                                 @ApiParam(value = "文件MD5信息") @RequestParam String[] fileMd5Array,
+                                 @ApiParam(value = "文件数组") @RequestPart MultipartFile[] fileArray) throws Exception {
         Check.isNull(fileArray, "files不能为空");
         Check.isNull(examRecordDataId, "examRecordDataId不能为空");
         if (fileArray.length != fileMd5Array.length) {

+ 1 - 14
examcloud-core-oe-admin-api-provider/src/main/java/cn/com/qmth/examcloud/core/oe/admin/api/controller/client/ExamProcessController.java

@@ -183,24 +183,11 @@ public class ExamProcessController extends ControllerSupport {
         User user = getAccessUser();
 
         String fileSuffix = FileUtil.getFileSuffix(file.getOriginalFilename());
-
-        String fileType;
-        if (".zip".equals(fileSuffix)) {
-            fileType = "ZIP";
-        } else if (".pdf".equals(fileSuffix)) {
-            fileType = "PDF";
-        } else if (".jpg.jpeg.png".contains(fileSuffix)) {
-            fileType = "IMAGE";
-        } else {
-            fileType = "UNKNOWN";
-        }
-
         FileInfo fileInfo = new FileInfo();
         fileInfo.setOriginalFileName(file.getOriginalFilename());
         fileInfo.setFileBytes(file.getBytes());
         fileInfo.setFileSize(file.getSize());
         fileInfo.setFileSuffix(fileSuffix);
-        fileInfo.setFileType(fileType);
 
         offlineExamService.submitPaper(examRecordDataId, fileInfo, user.getUserId());
     }
@@ -208,7 +195,7 @@ public class ExamProcessController extends ControllerSupport {
     @ApiOperation(value = "离线考试:多文件交卷")
     @PostMapping("/batchSubmitPaper")
     public void batchSubmitPaper(@ApiParam(value = "考试记录id") @RequestParam Long examRecordDataId,
-                                 @ApiParam(value = "文件类型:ZIP、PDF、IMAGE") @RequestParam String fileType,
+                                 @ApiParam(value = "文件类型:ZIP、PDF、IMAGE(单选、三选一)") @RequestParam String fileType,
                                  @ApiParam(value = "文件MD5信息") @RequestParam String[] fileMd5Array,
                                  @ApiParam(value = "文件数组") @RequestPart MultipartFile[] fileArray) throws Exception {
         Check.isNull(fileArray, "上传文件不能为空");

+ 0 - 6
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/ExamRecordForMarkingService.java

@@ -34,13 +34,7 @@ public interface ExamRecordForMarkingService {
 
     /**
      * 离线考试-保存阅卷相关数据-对内
-     *
-     * @param examRecordData
-     * @param fileName
-     * @param fileUrl
      */
-    void saveOffLineExamRecordForMarking(ExamRecordDataBean examRecordData, String fileName, String fileUrl);
-
     void saveOffLineExamRecordForMarking(ExamRecordDataBean examRecordData);
 
     /**

+ 8 - 0
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/OfflineExamService.java

@@ -39,6 +39,14 @@ public interface OfflineExamService {
      */
     void submitPaper(Long examRecordDataId, FileInfo fileInfo, Long userId);
 
+    /**
+     * 上传作答
+     *
+     * @param examRecordDataId
+     * @param fileInfoList
+     */
+    void batchSubmitPaper(Long examRecordDataId, List<FileInfo> fileInfoList, String fileType, Long userId);
+
     /**
      * 上传作答
      *

+ 0 - 24
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/ExamRecordForMarkingServiceImpl.java

@@ -11,7 +11,6 @@ import cn.com.qmth.examcloud.core.oe.admin.service.ExamRecordQuestionsService;
 import cn.com.qmth.examcloud.core.oe.admin.service.bean.examrecord.ExamRecordDataBean;
 import cn.com.qmth.examcloud.examwork.api.ExamCloudService;
 import cn.com.qmth.examcloud.support.enums.ExamProperties;
-import cn.com.qmth.examcloud.support.filestorage.FileStorageUtil;
 import cn.com.qmth.examcloud.support.helper.ExamCacheTransferHelper;
 import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
 import org.apache.commons.lang3.StringUtils;
@@ -248,29 +247,6 @@ public class ExamRecordForMarkingServiceImpl implements ExamRecordForMarkingServ
         });
     }
 
-    @Override
-    public void saveOffLineExamRecordForMarking(ExamRecordDataBean examRecordData, String offlineFileName, String fileUrl) {
-        ExamRecordForMarkingEntity examRecordForMarking = examRecordForMarkingRepo.findByExamRecordDataId(examRecordData.getId());
-        if (examRecordForMarking == null) {
-            examRecordForMarking = new ExamRecordForMarkingEntity();
-            examRecordForMarking.setCreationTime(new Date());
-        } else {
-            //将原文件删掉
-            FileStorageUtil.deleteFile(fileUrl);
-        }
-
-        examRecordForMarking.setExamId(examRecordData.getExamId());
-        examRecordForMarking.setExamRecordDataId(examRecordData.getId());
-        examRecordForMarking.setExamStudentId(examRecordData.getExamStudentId());
-        examRecordForMarking.setCourseId(examRecordData.getCourseId());
-        examRecordForMarking.setObjectiveScore(0D);
-        examRecordForMarking.setSubjectiveAnswerLength(0);
-        examRecordForMarking.setBasePaperId(examRecordData.getBasePaperId());
-        examRecordForMarking.setPaperType(examRecordData.getPaperType());
-        examRecordForMarking.setUpdateTime(new Date());
-        examRecordForMarkingRepo.save(examRecordForMarking);
-    }
-
     @Override
     public void saveOffLineExamRecordForMarking(ExamRecordDataBean examRecordData) {
         ExamRecordForMarkingEntity examRecordForMarking = examRecordForMarkingRepo.findByExamRecordDataId(examRecordData.getId());

+ 66 - 33
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/OfflineExamServiceImpl.java

@@ -33,6 +33,8 @@ import cn.com.qmth.examcloud.support.helper.ExamCacheTransferHelper;
 import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
 import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
 import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
+import com.google.common.collect.Lists;
+import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -272,6 +274,31 @@ public class OfflineExamServiceImpl implements OfflineExamService {
     @Override
     @Transactional
     public void submitPaper(Long examRecordDataId, FileInfo fileInfo, Long userId) {
+        if (!FileUtil.checkFileSuffix("jpg|jpeg|png|pdf|zip", fileInfo.getFileSuffix())) {
+            throw new StatusException("400401", "文件格式不正确");
+        }
+
+        if (fileInfo.getFileSize() > Constants.ANSWER_FILE_MAX_SIZE * Constants.M_SIZE) {
+            throw new StatusException("400402", "文件大小不能超过" + Constants.ANSWER_FILE_MAX_SIZE + "M");
+        }
+
+        String fileType;
+        if (".zip".equals(fileInfo.getFileSuffix())) {
+            fileType = "ZIP";
+        } else if (".pdf".equals(fileInfo.getFileSuffix())) {
+            fileType = "PDF";
+        } else if (".jpg.jpeg.png".contains(fileInfo.getFileSuffix())) {
+            fileType = "IMAGE";
+        } else {
+            fileType = "UNKNOWN";
+        }
+
+        this.batchSubmitPaper(examRecordDataId, Lists.newArrayList(fileInfo), fileType, userId);
+    }
+
+    @Override
+    @Transactional
+    public void batchSubmitPaper(Long examRecordDataId, List<FileInfo> fileInfoList, String fileType, Long userId) {
         Check.isNull(examRecordDataId, "examRecordDataId不能为空");
 
         SysPropertyCacheBean stuClientLoginLimit = CacheHelper.getSysProperty("STU_CLIENT_LOGIN_LIMIT");
@@ -283,14 +310,6 @@ public class OfflineExamServiceImpl implements OfflineExamService {
             throw new StatusException("4001", "系统维护中... ...");
         }
 
-        if (!FileUtil.checkFileSuffix("jpg|jpeg|png|pdf|zip", fileInfo.getFileSuffix())) {
-            throw new StatusException("400401", "文件格式不正确");
-        }
-
-        if (fileInfo.getFileSize() > Constants.ANSWER_FILE_MAX_SIZE * Constants.M_SIZE) {
-            throw new StatusException("400402", "文件大小不能超过" + Constants.ANSWER_FILE_MAX_SIZE + "M");
-        }
-
         Optional<ExamRecordDataEntity> optional = examRecordDataRepo.findById(examRecordDataId);
         if (!optional.isPresent()) {
             throw new StatusException("400403", "考试记录不存在");
@@ -313,25 +332,50 @@ public class OfflineExamServiceImpl implements OfflineExamService {
             throw new StatusException("400403", "当前考试设置不允许上传附件");
         }
 
-        if (!matchFileTypes(settingFileTypes, fileInfo.getFileSuffix())) {
+        String uploadFileType = fileType.toUpperCase();
+        if (!ArrayUtils.contains(settingFileTypes, uploadFileType)) {
             throw new StatusException("400403", "当前考试允许上传文件格式为:" + examProperty.getValue());
         }
 
-        // 上传文件
+        if (("ZIP".equals(uploadFileType) || "PDF".equals(uploadFileType)) && fileInfoList.size() > 1) {
+            throw new StatusException("400403", "上传" + uploadFileType + "类型文件时只能传一个文件");
+        }
+
         ExamRecordDataBean bean = of(examRecordData);
-        String fileNewName = createOfflineFileName(bean) + fileInfo.getFileSuffix();
-        String relativePath = upyunUploadUrl + examRecordData.getExamId() + "/" + fileNewName;
 
-        //通用存储
-        FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
-        env.setRootOrgId(String.valueOf(bean.getRootOrgId()));
-        env.setRelativePath(relativePath);
-        YunPathInfo pi = FileStorageUtil.saveFile("offlineFile", env, fileInfo.getFileBytes(), false);
+        // 上传文件
+        for (FileInfo fileInfo : fileInfoList) {
+            String fileNewName = createOfflineFileName(bean) + fileInfo.getFileSuffix();
+            String relativePath = upyunUploadUrl + examRecordData.getExamId() + "/" + fileNewName;
+
+            FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
+            env.setRootOrgId(String.valueOf(examRecordData.getRootOrgId()));
+            env.setRelativePath(relativePath);
+            YunPathInfo uploadResult = FileStorageUtil.saveFile("offlineFile", env, fileInfo.getFileBytes(), false);
+
+            fileInfo.setFilePath(uploadResult.getRelativePath());
+            fileInfo.setFileName(fileNewName);
+            fileInfo.setFileType(fileType);
+        }
 
-        examRecordForMarkingService.saveOffLineExamRecordForMarking(bean, fileNewName, pi.getRelativePath());
+        // 删除原作答文件记录
+        List<ExamRecordFileAnswerEntity> fileAnswerList = examRecordFileAnswerRepo.findByExamRecordDataId(examRecordDataId);
+        if (CollectionUtils.isNotEmpty(fileAnswerList)) {
+            for (ExamRecordFileAnswerEntity fileAnswer : fileAnswerList) {
+                examRecordFileAnswerRepo.delete(fileAnswer);
+                FileStorageUtil.deleteFile(fileAnswer.getFileUrl());
+            }
+        }
+
+        // 保存新作答文件记录
+        for (FileInfo fileInfo : fileInfoList) {
+            saveExamRecordFileAnswer(examRecordDataId, fileInfo.getOriginalFileName(), fileInfo.getFileName(), fileInfo.getFilePath(),
+                    fileInfo.getFileType().toLowerCase(), fileInfo.getFileSuffix().replaceFirst(".", ""));
+        }
 
-        saveExamRecordFileAnswer(examRecordDataId, fileInfo.getOriginalFileName(), fileNewName, pi.getRelativePath(),
-                fileInfo.getFileType().toLowerCase(), fileInfo.getFileSuffix().replaceFirst(".", ""));
+        fileInfoList.clear();
+
+        examRecordForMarkingService.saveOffLineExamRecordForMarking(bean);
 
         //更新考试记录状态,交卷(上传)时间
         examRecordDataRepo.updateExamRecordStatusById(examRecordDataId, ExamRecordStatus.EXAM_END, new Date());
@@ -439,18 +483,8 @@ public class OfflineExamServiceImpl implements OfflineExamService {
      * @param fileType
      * @param fileSuffix
      */
-    private void saveExamRecordFileAnswer(Long examRecordDataId, String originalFileName,
-                                          String fileName, String relativePath,
-                                          String fileType, String fileSuffix) {
-        //删除原文件
-        List<ExamRecordFileAnswerEntity> fileAnswerList = examRecordFileAnswerRepo.findByExamRecordDataId(examRecordDataId);
-        if (null != fileAnswerList && !fileAnswerList.isEmpty()) {
-            for (ExamRecordFileAnswerEntity erfa : fileAnswerList) {
-                examRecordFileAnswerRepo.deleteById(erfa.getId());
-                FileStorageUtil.deleteFile(erfa.getFileUrl());
-            }
-        }
-
+    private void saveExamRecordFileAnswer(Long examRecordDataId, String originalFileName, String fileName,
+                                          String relativePath, String fileType, String fileSuffix) {
         ExamRecordFileAnswerEntity entity = new ExamRecordFileAnswerEntity();
         entity.setExamRecordDataId(examRecordDataId);
         entity.setOriginalFileName(originalFileName);
@@ -461,7 +495,6 @@ public class OfflineExamServiceImpl implements OfflineExamService {
         examRecordFileAnswerRepo.save(entity);
     }
 
-
     private String createOfflineFileName(ExamRecordDataBean examRecordData) {
         long currentTime = System.currentTimeMillis();