From 97c5552f705e03a3a6e5864e3be6b1078844a4d3 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期四, 09 五月 2024 18:09:33 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java |  182 +++++++++++++++++++++++++++++++++++++-------
 1 files changed, 151 insertions(+), 31 deletions(-)

diff --git a/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java b/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
index 4028398..f434bc1 100644
--- a/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
+++ b/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
@@ -1,40 +1,41 @@
 package com.mindskip.xzs.service.impl;
 
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
 import com.mindskip.xzs.configuration.spring.exception.QuestionException;
 import com.mindskip.xzs.domain.*;
-import com.mindskip.xzs.domain.TextContent;
 import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum;
 import com.mindskip.xzs.domain.enums.QuestionTypeEnum;
 import com.mindskip.xzs.domain.exam.ExamPaperQuestionItemObject;
 import com.mindskip.xzs.domain.exam.ExamPaperTitleItemObject;
 import com.mindskip.xzs.domain.other.KeyValue;
+import com.mindskip.xzs.domain.vo.PaperExcelVO;
+import com.mindskip.xzs.queue.ExamPaperTimeTask;
 import com.mindskip.xzs.repository.ExamPaperMapper;
 import com.mindskip.xzs.repository.QuestionMapper;
+import com.mindskip.xzs.repository.UserDepartmentMapper;
 import com.mindskip.xzs.service.*;
 import com.mindskip.xzs.service.enums.ActionEnum;
 import com.mindskip.xzs.utility.DateTimeUtil;
+import com.mindskip.xzs.utility.ExamUtil;
 import com.mindskip.xzs.utility.JsonUtil;
 import com.mindskip.xzs.utility.ModelMapperSingle;
-import com.mindskip.xzs.utility.ExamUtil;
-import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
-import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM;
-import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM;
-import com.mindskip.xzs.viewmodel.admin.exam.QuestionTypeVM;
+import com.mindskip.xzs.utility.minio.DateUtils;
+import com.mindskip.xzs.viewmodel.admin.exam.*;
+import com.mindskip.xzs.viewmodel.admin.question.ExamQuestionVO;
 import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM;
 import com.mindskip.xzs.viewmodel.student.dashboard.PaperFilter;
 import com.mindskip.xzs.viewmodel.student.dashboard.PaperInfo;
 import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM;
-import com.github.pagehelper.PageHelper;
-import com.github.pagehelper.PageInfo;
-import com.mindskip.xzs.domain.ExamPaper;
-import com.mindskip.xzs.domain.Question;
-import com.mindskip.xzs.domain.User;
 import org.modelmapper.ModelMapper;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.annotation.PostConstruct;
 import java.util.*;
+import java.util.concurrent.DelayQueue;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
@@ -50,9 +51,31 @@
     private final ExamPaperDepartmentService examPaperDepartmentService;
     private final ExamPaperSubjectService examPaperSubjectService;
     private final QuestionSubjectService questionSubjectService;
+    private final ExamPaperUserService examPaperUserService;
+    private final UserService userService;
+    private final UserDepartmentMapper userDepartmentMapper;
+    private final DepartmentService departmentService;
+    private final DelayQueue<ExamPaperTimeTask> QUEUE = new DelayQueue<>();
+
+
+    /**
+     * 灏嗚繘琛屼腑鐨勫畾鏃惰瘯鍗峰姞鍏ュ埌闃熷垪涓紝骞跺惎鍔ㄤ竴涓嚎绋嬫潵杞闃熷垪
+     */
+    @PostConstruct
+    public void pollQueue() {
+        examPaperMapper.selectTimeTaskPaper(DateUtils.getNowDate()).forEach(this::addTimeTask);
+        new Thread(() -> {
+            try {
+                ExamPaperTimeTask task = QUEUE.take();
+                saveMissExamUser(task.getExamPaperId());
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+            }
+        }).start();
+    }
 
     @Autowired
-    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService) {
+    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService, ExamPaperUserService examPaperUserService, UserService userService, UserDepartmentMapper userDepartmentMapper, DepartmentService departmentService) {
         super(examPaperMapper);
         this.examPaperMapper = examPaperMapper;
         this.questionMapper = questionMapper;
@@ -62,6 +85,10 @@
         this.examPaperDepartmentService = examPaperDepartmentService;
         this.examPaperSubjectService = examPaperSubjectService;
         this.questionSubjectService = questionSubjectService;
+        this.examPaperUserService = examPaperUserService;
+        this.userService = userService;
+        this.userDepartmentMapper = userDepartmentMapper;
+        this.departmentService = departmentService;
     }
 
 
@@ -116,6 +143,7 @@
             examPaper.setCreateTime(now);
             examPaper.setCreateUser(user.getId());
             examPaper.setDeleted(false);
+            examPaper.setUserIds(examPaperEditRequestVM.getMenuIds());
             examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM);
             examPaperMapper.insertSelective(examPaper);
 
@@ -131,16 +159,18 @@
             examPaperDepartmentService.removeByExamPaperId(examPaper.getId());
             examPaperSubjectService.removeByExamPaperId(examPaper.getId());
         }
-        addExamPaperDepartment(examPaperEditRequestVM, examPaper);
+//        addExamPaperDepartment(examPaperEditRequestVM, examPaper);
+        addExamPaperUser(examPaperEditRequestVM, examPaper);    //鎵归噺娣诲姞璇曞嵎鍏宠仈鐢ㄦ埛
         addExamPaperSubject(examPaperEditRequestVM, examPaper);
         return examPaper;
     }
 
     @Override
-    public ExamPaperEditRequestVM examPaperToVM(Integer id) {
+    public ExamPaperEditRequestVO examPaperToVM(Integer id) {
         ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(id);
-        ExamPaperEditRequestVM vm = modelMapper.map(examPaper, ExamPaperEditRequestVM.class);
+        ExamPaperEditRequestVO vm = modelMapper.map(examPaper, ExamPaperEditRequestVO.class);
         vm.setLevel(examPaper.getGradeLevel());
+        vm.setMenuIds(examPaper.getUserIds());
         TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId());
         List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), ExamPaperTitleItemObject.class);
         List<Integer> questionIds = examPaperTitleItemObjects.stream()
@@ -148,17 +178,29 @@
                         .map(q -> q.getId()))
                 .collect(Collectors.toList());
         List<Question> questions = questionMapper.selectByIds(questionIds);
+        //鍗曢�夋暟閲�
+        Integer singleChoice = questions.stream().filter(e -> e.getQuestionType() == 1).collect(Collectors.toList()).size();
+        //澶氶�夋暟閲�
+        Integer multipleChoice = questions.stream().filter(e -> e.getQuestionType() == 2).collect(Collectors.toList()).size();
+        //鍒ゆ柇鏁伴噺
+        Integer trueFalse = questions.stream().filter(e -> e.getQuestionType() == 3).collect(Collectors.toList()).size();
         Integer order = 0;
         Set<Integer> generatedNumbers = new HashSet<>();
         Random random = new Random();
-        List<ExamPaperTitleItemVM> examPaperTitleItemVMS = examPaperTitleItemObjects.stream().map(t -> {
-            ExamPaperTitleItemVM tTitleVM = modelMapper.map(t, ExamPaperTitleItemVM.class);
-            List<QuestionEditRequestVM> questionItemsVM = t.getQuestionItems().stream().map(i -> {
+        List<ExamPaperTitleItemVO> examPaperTitleItemVMS = examPaperTitleItemObjects.stream().map(t -> {
+            ExamPaperTitleItemVO tTitleVM = new ExamPaperTitleItemVO();
+            BeanUtils.copyProperties(t, tTitleVM);
+            List<ExamQuestionVO> questionItemsVM = t.getQuestionItems().stream().map(i -> {
                         Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
-                        QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(question);
-                        questionEditRequestVM.setItemOrder(getRandomNumber(t.getQuestionItems().size() - 1, generatedNumbers, random));
+                        ExamQuestionVO questionEditRequestVM = questionService.getQuestionEditRequestVM(question);
+                        questionEditRequestVM.setTitle("(" + QuestionTypeEnum.fromCode(questionEditRequestVM.getQuestionType()).getName() + ") " + questionEditRequestVM.getTitle());
+                        questionEditRequestVM.setItemOrder(generateRandomNumber(questionEditRequestVM.getQuestionType() == 1 ? 0 : ((questionEditRequestVM.getQuestionType() == 2 ? singleChoice : multipleChoice + singleChoice)),
+                                questionEditRequestVM.getQuestionType() == 1 ? singleChoice : ((questionEditRequestVM.getQuestionType() == 2 ? multipleChoice + singleChoice : trueFalse + multipleChoice + singleChoice)),
+                                generatedNumbers, random));
+//                        questionEditRequestVM.setItemOrder(getRandomNumber(t.getQuestionItems().size() - 1, generatedNumbers, random));
+
                         return questionEditRequestVM;
-                    }).sorted(Comparator.comparing(QuestionEditRequestVM::getItemOrder))
+                    }).sorted(Comparator.comparing(ExamQuestionVO::getItemOrder))
                     .collect(Collectors.toList());
             tTitleVM.setQuestionItems(questionItemsVM);
             return tTitleVM;
@@ -175,6 +217,13 @@
                 .stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new));
         vm.setDepartmentIds(examPaperDepartmentService.getByExamPaperId(examPaper.getId())
                 .stream().map(ExamPaperDepartment::getDepartmentId).toArray(Integer[]::new));
+        List<ExamPaperUser> examPaperUsers = examPaperUserService.getByExamPaperId(examPaper.getId());
+        Integer[][] userIds = new Integer[examPaperUsers.size()][2];
+        for (int i = 0; i < examPaperUsers.size(); i++) {
+            Integer[] userId = {userService.getUserById(examPaperUsers.get(i).getUserId()).getUserLevel(), examPaperUsers.get(i).getUserId()};
+            userIds[i] = userId;
+        }
+        vm.setUserId(userIds);
         return vm;
     }
 
@@ -209,6 +258,22 @@
     @Override
     public List<ExamPaper> gets(Integer[] ids) {
         return examPaperMapper.gets(ids);
+    }
+
+    @Override
+    public List<PaperExcelVO> getPaperExcelById(Integer id) {
+        List<PaperExcelVO> paperExcel = examPaperMapper.getPaperExcelById(id);
+        paperExcel = paperExcel.stream().map(e->{
+            e.setPaperScore(ExamUtil.scoreToVM(Integer.parseInt(e.getPaperScore())));
+            e.setUserScore(ExamUtil.scoreToVM(Integer.parseInt(e.getUserScore())));
+            List<UserDepartment> userDepartments = userDepartmentMapper.selectByUserId(Integer.parseInt(e.getUserId()));
+            if(userDepartments.size() != 0){
+                Department byId = departmentService.getById(userDepartments.get(0).getDepartmentId());
+                e.setDepartmentName(byId.getName());
+            }
+            return e;
+        }).collect(Collectors.toList());
+        return paperExcel;
     }
 
     private void examPaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper, List<ExamPaperTitleItemVM> titleItemsVM) {
@@ -259,7 +324,27 @@
         examPaperDepartmentService.saves(list);
     }
 
+    private void addExamPaperUser(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper) {
+        if (examPaperEditRequestVM.getUserIds().length == 0) {
+            return;
+        }
+        if (examPaperUserService.getByExamPaperId(examPaper.getId()).size() > 0) {
+            examPaperUserService.removeByExamPaperId(examPaper.getId());
+        }
+        List<ExamPaperUser> list = Arrays.asList(examPaperEditRequestVM.getUserIds()).stream().map(e -> {
+            ExamPaperUser examPaperUser = new ExamPaperUser();
+            examPaperUser.setUserId(e);
+            examPaperUser.setExamPaperId(examPaper.getId());
+            examPaperUser.setDeleted("0");
+            return examPaperUser;
+        }).collect(Collectors.toList());
+        examPaperUserService.saves(list);
+    }
+
     private void addExamPaperSubject(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper) {
+        if (examPaperSubjectService.getByExamPaperId(examPaper.getId()).size() > 0) {
+            examPaperSubjectService.removeByExamPaperId(examPaper.getId());
+        }
         List<ExamPaperSubject> subjectList = Arrays.asList(examPaperEditRequestVM.getSubjectId()).stream().map(e -> {
             ExamPaperSubject examPaperSubject = new ExamPaperSubject();
             examPaperSubject.setSubjectId(e);
@@ -291,6 +376,7 @@
         int remainingScore = totalScore;
 
         for (int i = 0; i < quantity - 1; i++) {
+            // 濡傛灉棰樻暟閲忚秴杩囦簡棰樺簱鏁伴噺锛屾姤閿�
             if (i >= scoresMap.size()) {
                 throw new QuestionException();
             }
@@ -305,7 +391,6 @@
                 keys.remove(index);
             }
         }
-
         if (!values.isEmpty()) {
             int index = random.nextInt(values.size());
             int score = values.get(index);
@@ -321,7 +406,7 @@
      * @param frameTextContentList
      * @param titleItemsVM           title
      */
-    private void randomQuestion(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException {
+    private void randomQuesrandomQuestiontion(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException {
         Map<Integer, Integer> subjectSource = examPaperEditRequestVM.getSubjectSource();
         //鎬绘暟閲�
         Integer sum = subjectSource.values().stream().mapToInt(Integer::intValue).sum();
@@ -414,13 +499,17 @@
      * @param titleItemsVM           title
      */
     private void randomQuestionType(ExamPaperEditRequestVM examPaperEditRequestVM, List<ExamPaperTitleItemObject> frameTextContentList, List<ExamPaperTitleItemVM> titleItemsVM) throws QuestionException {
+
+
         //鍗曢��
         Integer singleChoice = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getSingleChoice).sum();
         //澶氶��
         Integer multipleChoice = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getMultipleChoice).sum();
         //鍒ゆ柇
         Integer judgment = examPaperEditRequestVM.getQuestionTypeVMS().stream().mapToInt(QuestionTypeVM::getTrueFalse).sum();
-        if ((singleChoice * 4 + multipleChoice * 5 + judgment * 2) != 100) {
+
+
+        if ((singleChoice * 2 + multipleChoice * 2 + judgment * 2) != new Integer(100)) {
             throw new QuestionException();
         }
 
@@ -446,24 +535,24 @@
             Map<Integer, Integer> multiple = new HashMap<>();
             //澶氶��
             Map<Integer, Integer> multipleMap = list.stream()
-                    .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.MultipleChoice.getCode()))
+                    .filter(e -> Objects.equals(QuestionTypeEnum.MultipleChoice.getCode(), e.getQuestionType()))
                     .collect(Collectors.toMap(Question::getId, Question::getScore));
-            Integer multipleSource = questionTypeVM.getMultipleChoice() * 5;
+            Integer multipleSource = questionTypeVM.getMultipleChoice() * 2;
             selectRandomScores(multiple, multipleMap, questionTypeVM.getMultipleChoice(), multipleSource);
 
 
             //鍒ゆ柇
-            Map<Integer, Integer> judgmentMap = list.stream()
-                    .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.TrueFalse.getCode()))
-                    .collect(Collectors.toMap(Question::getId, Question::getScore));
-            Integer trueFalse = questionTypeVM.getTrueFalse() * 2;
+            List<Question> collect1 = list.stream().filter(e -> Objects.equals(e.getQuestionType(), QuestionTypeEnum.TrueFalse.getCode())).collect(Collectors.toList());
+            Map<Integer, Integer> judgmentMap = collect1.stream().collect(Collectors.toMap(Question::getId, Question::getScore));
+            Integer trueFalse1 = questionTypeVM.getTrueFalse();
+            Integer trueFalse = trueFalse1 * 2;
             selectRandomScores(multiple, judgmentMap, questionTypeVM.getTrueFalse(), trueFalse);
 
             //鍗曢�夊垎鏁�
             Integer radioSource = questionTypeVM.getSingleChoice() * 4;
             //鍗曢��
             Map<Integer, Integer> radioMap = list.stream()
-                    .filter(e -> e.getQuestionType().equals(QuestionTypeEnum.SingleChoice.getCode()))
+                    .filter(e -> Objects.equals(e.getQuestionType(), QuestionTypeEnum.SingleChoice.getCode()))
                     .collect(Collectors.toMap(Question::getId, Question::getScore));
             selectRandomScores(multiple, radioMap, questionTypeVM.getSingleChoice(), radioSource);
 
@@ -508,4 +597,35 @@
         generatedNumbers.add(randomNumber);
         return randomNumber;
     }
+
+    /**
+     * 涓や釜鏁颁箣鍓嶇殑闅忔満鏁帮紙涓嶉噸澶嶏級
+     *
+     * @param a
+     * @param b
+     * @return
+     */
+    public static int generateRandomNumber(Integer a, Integer b, Set<Integer> generatedNumbers, Random random) {
+
+        int randomNumber = random.nextInt(b - a) + a;
+        while (generatedNumbers.contains(randomNumber)) {
+            randomNumber = random.nextInt(b - a) + a;
+        }
+        generatedNumbers.add(randomNumber);
+
+        return randomNumber;
+    }
+
+    @Override
+    public void addTimeTask(ExamPaper examPaper) {
+        ExamPaperTimeTask examPaperTimeTask = new ExamPaperTimeTask();
+        examPaperTimeTask.setExamPaperId(examPaper.getId());
+        examPaperTimeTask.setExpiry(examPaper.getLimitEndTime().getTime());
+        QUEUE.add(examPaperTimeTask);
+    }
+
+    @Override
+    public void saveMissExamUser(long examPaperId) {
+        examPaperMapper.saveMissExamUser(examPaperId);
+    }
 }

--
Gitblit v1.8.0