|
@@ -1,18 +1,26 @@
|
|
|
package com.qmth.teachcloud.mark.service.impl;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qmth.boot.core.exception.ParameterException;
|
|
|
+import com.qmth.teachcloud.common.bean.vo.FilePathVo;
|
|
|
+import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.teachcloud.common.enums.UploadFileEnum;
|
|
|
import com.qmth.teachcloud.common.enums.mark.MarkPaperStatus;
|
|
|
+import com.qmth.teachcloud.common.service.FileUploadService;
|
|
|
import com.qmth.teachcloud.common.service.TeachcloudCommonService;
|
|
|
import com.qmth.teachcloud.common.util.FileStoreUtil;
|
|
|
import com.qmth.teachcloud.mark.entity.MarkDocument;
|
|
@@ -32,6 +40,8 @@ import com.qmth.teachcloud.mark.service.MarkPaperService;
|
|
|
*/
|
|
|
@Service
|
|
|
public class MarkDocumentServiceImpl extends ServiceImpl<MarkDocumentMapper, MarkDocument> implements MarkDocumentService {
|
|
|
+ @Resource
|
|
|
+ private FileUploadService fileUploadService;
|
|
|
@Resource
|
|
|
private FileStoreUtil fileStoreUtil;
|
|
|
@Resource
|
|
@@ -58,13 +68,28 @@ public class MarkDocumentServiceImpl extends ServiceImpl<MarkDocumentMapper, Mar
|
|
|
||document.getType().equals(DocumentType.PROCESS_SCORE)){
|
|
|
throw ExceptionResultEnum.ERROR.exception("文档类型不正确");
|
|
|
}
|
|
|
- String filePath = fileStoreUtil.uploadFile(file, md5, UploadFileEnum.FILE);
|
|
|
- UpdateWrapper<MarkDocument> updateWrapper = new UpdateWrapper<>();
|
|
|
- updateWrapper.lambda().set(MarkDocument::getFilePath, filePath)
|
|
|
- .set(MarkDocument::getFileCount, 1)
|
|
|
- .eq(MarkDocument::getId, id);
|
|
|
- this.update(updateWrapper);
|
|
|
- return teachcloudCommonService.filePreview(filePath);
|
|
|
+ try {
|
|
|
+ String fileMd5 = DigestUtils.md5Hex(file.getBytes());
|
|
|
+ if (!Objects.equals(fileMd5, md5)) {
|
|
|
+ throw ExceptionResultEnum.MD5_EQUALS_FALSE.exception();
|
|
|
+ }
|
|
|
+ String format = FilenameUtils.getExtension(file.getOriginalFilename());
|
|
|
+ String name = document.getType().getName();
|
|
|
+ if(document.getType().equals(DocumentType.CUSTOM)){
|
|
|
+ name = document.getName();
|
|
|
+ }
|
|
|
+ String filePath = getDocumentUri(document.getExamId(),document.getPaperNumber(),String.valueOf(document.getType().getValue()),name,format);
|
|
|
+ FilePathVo vo = fileUploadService.uploadFile(file.getInputStream(),filePath ,UploadFileEnum.DOCUMENT);
|
|
|
+ UpdateWrapper<MarkDocument> updateWrapper = new UpdateWrapper<>();
|
|
|
+ updateWrapper.lambda().set(MarkDocument::getFilePath, vo.getPath())
|
|
|
+ .set(MarkDocument::getFileCount, 1)
|
|
|
+ .eq(MarkDocument::getId, id);
|
|
|
+ this.update(updateWrapper);
|
|
|
+ return teachcloudCommonService.filePreview(vo.getPath());
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ throw new ParameterException("原图上传失败", e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -85,15 +110,44 @@ public class MarkDocumentServiceImpl extends ServiceImpl<MarkDocumentMapper, Mar
|
|
|
if(markPaper==null ||markPaper.getStatus().equals(MarkPaperStatus.FINISH)){
|
|
|
throw ExceptionResultEnum.ERROR.exception("评卷未结束或试卷不存在");
|
|
|
}
|
|
|
- MarkDocument document = new MarkDocument();
|
|
|
+ QueryWrapper<MarkDocument> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(MarkDocument::getExamId, examId)
|
|
|
+ .eq(MarkDocument::getPaperNumber, paperNumber)
|
|
|
+ .eq(MarkDocument::getName, documentName);
|
|
|
+ MarkDocument document = this.getOne(queryWrapper);
|
|
|
+ if(document!=null){
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(document+"已存在");
|
|
|
+ }
|
|
|
+ document = new MarkDocument();
|
|
|
document.setExamId(examId);
|
|
|
document.setPaperNumber(paperNumber);
|
|
|
document.setCourseCode(markPaper.getCourseCode());
|
|
|
document.setCourseName(markPaper.getCourseName());
|
|
|
document.setType(DocumentType.CUSTOM);
|
|
|
+ document.setName(documentName);
|
|
|
document.setFileCount(1);
|
|
|
- String filePath = fileStoreUtil.uploadFile(file, md5, UploadFileEnum.FILE);
|
|
|
- document.setFilePath(filePath);
|
|
|
- return teachcloudCommonService.filePreview(filePath);
|
|
|
+ try {
|
|
|
+ String fileMd5 = DigestUtils.md5Hex(file.getBytes());
|
|
|
+ if (!Objects.equals(fileMd5, md5)) {
|
|
|
+ throw ExceptionResultEnum.MD5_EQUALS_FALSE.exception();
|
|
|
+ }
|
|
|
+ String format = FilenameUtils.getExtension(file.getOriginalFilename());
|
|
|
+ String name = document.getType().getName();
|
|
|
+ if(document.getType().equals(DocumentType.CUSTOM)){
|
|
|
+ name = document.getName();
|
|
|
+ }
|
|
|
+ String filePath = getDocumentUri(document.getExamId(),document.getPaperNumber(),String.valueOf(document.getType().getValue()),name,format);
|
|
|
+ FilePathVo vo = fileUploadService.uploadFile(file.getInputStream(),filePath ,UploadFileEnum.DOCUMENT);
|
|
|
+ document.setFilePath(vo.getPath());
|
|
|
+ this.save(document);
|
|
|
+ return teachcloudCommonService.filePreview(vo.getPath());
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ throw new ParameterException("文件上传失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getDocumentUri(Long examId, String paperNumber, String documentType, String no,String format) {
|
|
|
+ return UploadFileEnum.DOCUMENT.getPath(UploadFileEnum.DOCUMENT.getTitle(), examId, paperNumber, documentType, no, format.toLowerCase());
|
|
|
}
|
|
|
}
|