|
@@ -1,26 +1,9 @@
|
|
|
package cn.com.qmth.stmms.ms.admin.service;
|
|
|
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-
|
|
|
-import org.apache.commons.codec.digest.DigestUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-
|
|
|
import cn.com.qmth.stmms.ms.admin.dto.StudentDTO;
|
|
|
import cn.com.qmth.stmms.ms.commons.config.ImageCompressionConfig;
|
|
|
import cn.com.qmth.stmms.ms.commons.config.SystemConfig;
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.RandomUtil;
|
|
|
import cn.com.qmth.stmms.ms.commons.utils.excel.ExcelError;
|
|
|
import cn.com.qmth.stmms.ms.commons.utils.excel.ExcelReader;
|
|
|
import cn.com.qmth.stmms.ms.commons.utils.excel.ExcelReaderHandle;
|
|
@@ -33,6 +16,17 @@ import cn.com.qmth.stmms.ms.core.repository.MarkTaskRepo;
|
|
|
import cn.com.qmth.stmms.ms.core.repository.PaperRepo;
|
|
|
import cn.com.qmth.stmms.ms.core.repository.StudentRepo;
|
|
|
import cn.com.qmth.stmms.ms.core.vo.Subject;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Random;
|
|
|
|
|
|
/**
|
|
|
* 数据上传服务 Created by zhengmin on 2016/11/18.
|
|
@@ -68,7 +62,7 @@ public class DataUploadService {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
@Transactional
|
|
|
- public void uploadPaper(Long studentId, Subject subject, InputStream in, boolean isManual) throws IOException {
|
|
|
+ public void uploadPaper(Long studentId, Subject subject, InputStream in, boolean isManual) throws Exception {
|
|
|
Student student = studentRepo.findOne(studentId);
|
|
|
String savePath = systemConfig.getImageDir() + File.separator + student.getWorkId() + File.separator + subject
|
|
|
+ File.separator + student.getAreaCode();
|
|
@@ -99,7 +93,7 @@ public class DataUploadService {
|
|
|
|
|
|
Paper exist = paperRepo.findByWorkIdAndSubjectAndExamNumber(student.getWorkId(), subject,
|
|
|
student.getExamNumber());
|
|
|
-
|
|
|
+
|
|
|
FileInputStream sheetIn = new FileInputStream(savePath + File.separator + student.getExamNumber() + ".jpg");
|
|
|
FileInputStream slicein = new FileInputStream(thumbFileName);
|
|
|
String sheetMD5 = DigestUtils.md5Hex(sheetIn);
|
|
@@ -115,7 +109,8 @@ public class DataUploadService {
|
|
|
} else {
|
|
|
ExamQuestion examQuestion = examQuestionRepo.findByWorkIdAndSubjectAndAreaCode(student.getWorkId(), subject,
|
|
|
student.getAreaCode());
|
|
|
- Paper paper = new Paper(student.getWorkId(), null, subject, examQuestion, student, false);
|
|
|
+ Long random = getRandom(student.getWorkId(), student.getExamNumber());
|
|
|
+ Paper paper = new Paper(student.getWorkId(), null, subject, examQuestion, student, false, random);
|
|
|
paper.setSheetMD5(sheetMD5);
|
|
|
paper.setSliceMD5(sliceMD5);
|
|
|
paperRepo.save(paper);
|
|
@@ -215,18 +210,19 @@ public class DataUploadService {
|
|
|
}
|
|
|
|
|
|
@Transactional
|
|
|
- public Paper savePaper(Student student, Subject subject, boolean isManual) throws FileNotFoundException, IOException {
|
|
|
+ public Paper savePaper(Student student, Subject subject, boolean isManual) throws Exception {
|
|
|
ExamQuestion examQuestion = examQuestionRepo.findByWorkIdAndSubjectAndAreaCode(student.getWorkId(), subject, student.getAreaCode());
|
|
|
Paper paper = paperRepo.findByWorkIdAndSubjectAndExamNumber(student.getWorkId(), subject, student.getExamNumber());
|
|
|
if (paper == null) {
|
|
|
- paper = new Paper(student.getWorkId(), null, subject, examQuestion, student, isManual);
|
|
|
+ Long random = getRandom(student.getWorkId(), student.getExamNumber());
|
|
|
+ paper = new Paper(student.getWorkId(), null, subject, examQuestion, student, isManual, random);
|
|
|
}
|
|
|
paper.setUploadedOn(new Date());
|
|
|
paper.setManual(isManual);
|
|
|
String sheetPath = systemConfig.getSheetDir() + File.separator + student.getWorkId() + File.separator + subject
|
|
|
- + File.separator + student.getAreaCode() + File.separator + student.getExamNumber() + ".jpg";
|
|
|
+ + File.separator + student.getAreaCode() + File.separator + student.getExamNumber() + ".jpg";
|
|
|
String slicePath = systemConfig.getImageDir() + File.separator + student.getWorkId() + File.separator + subject
|
|
|
- + File.separator + student.getAreaCode() + File.separator + student.getExamNumber() + ".jpg";
|
|
|
+ + File.separator + student.getAreaCode() + File.separator + student.getExamNumber() + ".jpg";
|
|
|
FileInputStream sheetIn = new FileInputStream(sheetPath);
|
|
|
FileInputStream slicein = new FileInputStream(slicePath);
|
|
|
String sheetMD5 = DigestUtils.md5Hex(sheetIn);
|
|
@@ -246,4 +242,30 @@ public class DataUploadService {
|
|
|
}
|
|
|
return paper;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取随机号
|
|
|
+ *
|
|
|
+ * @param workId
|
|
|
+ * @param examNumber
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public Long getRandom(Long workId, String examNumber) throws Exception {
|
|
|
+ int count = 0, result = 0;
|
|
|
+ Long random = 0L;
|
|
|
+ while (true) {
|
|
|
+ random = RandomUtil.randomMap.get(workId).get(new Random().nextInt(RandomUtil.randomMap.get(workId).size()));
|
|
|
+ result = paperRepo.countByWorkIdAndExamNumberAndRandomReq(workId, examNumber, random);
|
|
|
+ if (result == 0 && random != Long.parseLong(examNumber.substring(3, examNumber.length()))) {
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ if (count > 100) {
|
|
|
+ throw new Exception("重复几率较高,建议重新生成随机号");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return random;
|
|
|
+ }
|
|
|
}
|