|
@@ -0,0 +1,260 @@
|
|
|
|
+import cn.com.qmth.stmms.ms.Application;
|
|
|
|
+import cn.com.qmth.stmms.ms.commons.utils.RandomUtil;
|
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.ExamQuestion;
|
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.Paper;
|
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.Student;
|
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.Work;
|
|
|
|
+import cn.com.qmth.stmms.ms.core.domain.enums.TrialEnum;
|
|
|
|
+import cn.com.qmth.stmms.ms.core.repository.ExamQuestionRepo;
|
|
|
|
+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.repository.WorkRepo;
|
|
|
|
+import cn.com.qmth.stmms.ms.core.vo.Subject;
|
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.junit.runner.RunWith;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Random;
|
|
|
|
+
|
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
|
+@SpringBootTest(classes = {Application.class})
|
|
|
|
+// 指定启动类
|
|
|
|
+public class BatchCreateTaskTest {
|
|
|
|
+
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(BatchCreateTaskTest.class);
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ExamQuestionRepo examQuestionRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StudentRepo studentRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private PaperRepo paperRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private WorkRepo workRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ RandomUtil randomUtil;
|
|
|
|
+
|
|
|
|
+ //考生数量
|
|
|
|
+ private int initNumber = 20000;
|
|
|
|
+ private static int COUNT = 60000;
|
|
|
|
+ private static Long WORK_ID = 10l;
|
|
|
|
+ private List<Subject> subjects = Arrays.asList(Subject.SX, Subject.SC, Subject.SM);
|
|
|
|
+ private String baseDir = "D:\\my_workspaces\\ide_workspace_qmth\\idea_workspace\\stmms-ms\\stmms-ms-org-server\\upload";
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void createSxTask() throws Exception {
|
|
|
|
+ //默认用id为1000的工作
|
|
|
|
+ Work work = workRepo.findOne(WORK_ID);
|
|
|
|
+ if (work == null) {
|
|
|
|
+ logger.info("指定id为10的工作不存在");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ randomUtil.getRandom(work.getId(), false);
|
|
|
|
+
|
|
|
|
+ //生成exam_question信息
|
|
|
|
+ List<ExamQuestion> questions = createQuestions();
|
|
|
|
+// examQuestionRepo.save(questions);
|
|
|
|
+ logger.info("questions新增:{}", questions.size());
|
|
|
|
+
|
|
|
|
+ //生成student信息
|
|
|
|
+ List<Student> students = createStudent();
|
|
|
|
+ /*List<Student> data = new ArrayList<>();
|
|
|
|
+ for (Student stu : students) {
|
|
|
|
+ if (data.size() == 2000) {
|
|
|
|
+ studentRepo.save(data);
|
|
|
|
+ data.clear();
|
|
|
|
+ }
|
|
|
|
+ data.add(stu);
|
|
|
|
+ }
|
|
|
|
+ //将剩下的数据也导入
|
|
|
|
+ if (!data.isEmpty()) {
|
|
|
|
+ studentRepo.save(data);
|
|
|
|
+ }*/
|
|
|
|
+ logger.info("students新增:{}", students.size());
|
|
|
|
+
|
|
|
|
+ //生成paper信息
|
|
|
|
+ /*List<Paper> papers = createPaper(students, subjects);
|
|
|
|
+ List<Paper> data1 = new ArrayList<>();
|
|
|
|
+ for (Paper p : papers) {
|
|
|
|
+ if (data1.size() == 2000) {
|
|
|
|
+ paperRepo.save(data1);
|
|
|
|
+ data1.clear();
|
|
|
|
+ }
|
|
|
|
+ data1.add(p);
|
|
|
|
+ }
|
|
|
|
+ //将剩下的数据也导入
|
|
|
|
+ if (!data1.isEmpty()) {
|
|
|
|
+ paperRepo.save(data1);
|
|
|
|
+ }
|
|
|
|
+ logger.info("papers新增:{}", papers.size());
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ //生成image
|
|
|
|
+ createImage(students, subjects);
|
|
|
|
+ logger.info("image成功");
|
|
|
|
+
|
|
|
|
+ //生成sheet
|
|
|
|
+// createSheet(students, subjects);
|
|
|
|
+ logger.info("sheet成功");
|
|
|
|
+
|
|
|
|
+ //生成thumb
|
|
|
|
+// createThumb(students, subjects);
|
|
|
|
+ logger.info("thumb成功");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<ExamQuestion> createQuestions() {
|
|
|
|
+ List<ExamQuestion> questions = new ArrayList<>();
|
|
|
|
+ ExamQuestion examQuestion1 = new ExamQuestion();
|
|
|
|
+ examQuestion1.setId(1l);
|
|
|
|
+ examQuestion1.setAreaCode("1");
|
|
|
|
+ examQuestion1.setAreaName("省艺术培训协会");
|
|
|
|
+ examQuestion1.setName("速写1");
|
|
|
|
+ examQuestion1.setSubject(Subject.SX);
|
|
|
|
+ examQuestion1.setWorkId(WORK_ID);
|
|
|
|
+ examQuestion1.setTest(0);
|
|
|
|
+ questions.add(examQuestion1);
|
|
|
|
+
|
|
|
|
+ ExamQuestion examQuestion2 = new ExamQuestion();
|
|
|
|
+ examQuestion2.setId(2l);
|
|
|
|
+ examQuestion2.setAreaCode("1");
|
|
|
|
+ examQuestion2.setAreaName("省艺术培训协会");
|
|
|
|
+ examQuestion2.setName("色彩1");
|
|
|
|
+ examQuestion2.setSubject(Subject.SC);
|
|
|
|
+ examQuestion2.setWorkId(WORK_ID);
|
|
|
|
+ examQuestion2.setTest(0);
|
|
|
|
+ questions.add(examQuestion2);
|
|
|
|
+
|
|
|
|
+ ExamQuestion examQuestion3 = new ExamQuestion();
|
|
|
|
+ examQuestion3.setId(3l);
|
|
|
|
+ examQuestion3.setAreaCode("1");
|
|
|
|
+ examQuestion3.setAreaName("省艺术培训协会");
|
|
|
|
+ examQuestion3.setName("素描1");
|
|
|
|
+ examQuestion3.setSubject(Subject.SM);
|
|
|
|
+ examQuestion3.setWorkId(WORK_ID);
|
|
|
|
+ examQuestion3.setTest(0);
|
|
|
|
+ questions.add(examQuestion3);
|
|
|
|
+
|
|
|
|
+ return questions;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<Student> createStudent() {
|
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < COUNT; i++) {
|
|
|
|
+ Student student = new Student();
|
|
|
|
+ student.setAreaCode("1");
|
|
|
|
+ student.setAreaName("省艺术培训协会");
|
|
|
|
+ student.setExamNumber(String.valueOf(initNumber + i));
|
|
|
|
+ student.setExamRoom("第一考场");
|
|
|
|
+ student.setSchool("湖北美术学院");
|
|
|
|
+ student.setAbsent(false);
|
|
|
|
+ student.setName("考生" + i);
|
|
|
|
+ student.setUploadStatus("SX:1,SC:1,SM:1");
|
|
|
|
+ student.setWorkId(WORK_ID);
|
|
|
|
+ student.setSourceName("湖北");
|
|
|
|
+ student.setTest("0");
|
|
|
|
+ student.setRelateExamNumber(student.getExamNumber());
|
|
|
|
+ students.add(student);
|
|
|
|
+ }
|
|
|
|
+ return students;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<Paper> createPaper(List<Student> students, List<Subject> subjects) throws Exception {
|
|
|
|
+ List<Paper> papers = new ArrayList<>();
|
|
|
|
+ for (Subject subject : subjects) {
|
|
|
|
+ ExamQuestion examQuestion = examQuestionRepo.findByWorkIdAndSubjectAndAreaCodeAndTest(WORK_ID, subject, "1", TrialEnum.DEFAULT.getId());
|
|
|
|
+ for (Student student : students) {
|
|
|
|
+ Long random = getRandom(student.getWorkId(), student.getExamNumber());
|
|
|
|
+ Paper paper = new Paper(student.getWorkId(), null, subject, examQuestion, student, false, random);
|
|
|
|
+ papers.add(paper);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return papers;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void createImage(List<Student> students, List<Subject> subjects) throws IOException {
|
|
|
|
+ for (Subject subject : subjects) {
|
|
|
|
+ String imageDir = baseDir + File.separator + "images" + File.separator + WORK_ID + File.separator + subject.name() + File.separator + "1";
|
|
|
|
+ File out = new File(imageDir);
|
|
|
|
+ if (!out.exists()) {
|
|
|
|
+ out.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ File in = new File(imageDir, "source.jpg");
|
|
|
|
+ for (Student student : students) {
|
|
|
|
+ Thumbnails.of(in).scale(1f).toFile(new File(imageDir, student.getExamNumber() + ".jpg"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void createSheet(List<Student> students, List<Subject> subjects) throws IOException {
|
|
|
|
+ for (Subject subject : subjects) {
|
|
|
|
+ String sheetDir = baseDir + File.separator + "sheet" + File.separator + WORK_ID + File.separator + subject.name() + File.separator + "1";
|
|
|
|
+ File out = new File(sheetDir);
|
|
|
|
+ if (!out.exists()) {
|
|
|
|
+ out.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ File in = new File(sheetDir, "source.jpg");
|
|
|
|
+ for (Student student : students) {
|
|
|
|
+ Thumbnails.of(in).scale(1f).toFile(new File(sheetDir, student.getExamNumber() + ".jpg"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void createThumb(List<Student> students, List<Subject> subjects) throws IOException {
|
|
|
|
+ for (Subject subject : subjects) {
|
|
|
|
+ String thumbDir = baseDir + File.separator + "thumbs" + File.separator + WORK_ID + File.separator + subject.name() + File.separator + "1";
|
|
|
|
+ File out = new File(thumbDir);
|
|
|
|
+ if (!out.exists()) {
|
|
|
|
+ out.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ File in = new File(thumbDir, "source.jpg");
|
|
|
|
+ for (Student student : students) {
|
|
|
|
+ Thumbnails.of(in).scale(1f).toFile(new File(thumbDir, student.getExamNumber() + ".jpg"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取随机号
|
|
|
|
+ *
|
|
|
|
+ * @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.getRandomMap().get(workId).get(new Random().nextInt(randomUtil.getRandomMap().get(workId).size()));
|
|
|
|
+ result = paperRepo.countByWorkIdAndExamNumberAndRandomSeq(workId, examNumber, random);
|
|
|
|
+ if (result == 0 && random != Long.parseLong(examNumber.substring(3))) {
|
|
|
|
+ break;
|
|
|
|
+ } else {
|
|
|
|
+ count++;
|
|
|
|
+ }
|
|
|
|
+ if (count > 1000) {
|
|
|
|
+// throw new Exception("重复几率较高,建议重新生成随机号");
|
|
|
|
+ randomUtil.getRandom(workId, true);
|
|
|
|
+ getRandom(workId, examNumber);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return random;
|
|
|
|
+ }
|
|
|
|
+}
|