From 247cb86585a1d1894596ed18a6c93efecb992946 Mon Sep 17 00:00:00 2001
From: luohairen <3399054449@qq.com>
Date: 星期四, 14 十一月 2024 10:34:27 +0800
Subject: [PATCH] 班级成绩图表分析

---
 src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java |  187 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 174 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java b/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
index c68c3e0..fe69d1a 100644
--- a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
+++ b/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
@@ -2,6 +2,7 @@
 
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -79,6 +80,7 @@
     private final UserMapper userMapper;
     private final ExamPaperScoreMapper examPaperScoreMapper;
     private final ExamPaperScoreService examPaperScoreService;
+    private final QuestionAnswerRecordMapper questionAnswerRecordMapper;
 
     private final Producer producer;
 
@@ -93,10 +95,20 @@
         Exam entity = ExamForm.getEntityByForm(form, null);
         entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), null));
         entity.setTeacherId(webContext.getCurrentUser().getId());
-        // 璁剧疆涔愯閿佺増鏈�
-        entity.setUpdateVersion(0);
-        if (baseMapper.insert(entity) > 0) {
-            this.sendMQ(entity, 0);
+
+        // 鏌ュ嚭鑰冭瘯璇曞嵎
+        ExamPaper examPaper = examPaperMapper.selectById(entity.getExamPaperId());
+        // 鏍¢獙鑳藉惁鐢熸垚涓�寮犺瘯鍗�
+        try {
+            this.checkGenExam(examPaper);
+            // 璁剧疆涔愯閿佺増鏈�
+            entity.setUpdateVersion(0);
+            if (baseMapper.insert(entity) > 0) {
+                this.sendMQ(entity, 0);
+            }
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+            return Result.fail(500, e.getMessage());
         }
         return Result.ok("娣诲姞鎴愬姛");
     }
@@ -118,9 +130,18 @@
         }
         BeanUtils.copyProperties(form, entity);
         entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), new Date()));
-        // 濡傛灉淇敼鎴愬姛鍙戦�乵q娑堟伅
-        if (baseMapper.updateById(entity) > 0) {
-            this.sendMQ(entity, entity.getUpdateVersion());
+
+        // 鏌ュ嚭鑰冭瘯璇曞嵎
+        ExamPaper examPaper = examPaperMapper.selectById(entity.getExamPaperId());
+        try {
+            this.checkGenExam(examPaper);
+            // 濡傛灉淇敼鎴愬姛鍙戦�乵q娑堟伅
+            if (baseMapper.updateById(entity) > 0) {
+                this.sendMQ(entity, entity.getUpdateVersion());
+            }
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+            return Result.fail(500, e.getMessage());
         }
         return Result.ok("淇敼鎴愬姛");
     }
@@ -223,9 +244,103 @@
     public Result studentPage(ExamQuery query) {
         IPage<ExamVO> page = PageUtil.getPage(query, ExamVO.class);
         baseMapper.studentPage(page, query, webContext.getCurrentUser().getId());
+        for (ExamVO record : page.getRecords()) {
+            ExamSubmitTemp one = new LambdaQueryChainWrapper<>(examSubmitTempMapper)
+                    .eq(ExamSubmitTemp::getExamId, record.getId())
+                    .eq(ExamSubmitTemp::getUserId, webContext.getCurrentUser().getId())
+                    .one();
+            record.setIsContinue(Objects.isNull(one) || ExamSubmitTempStatusEnum.temp.equals(one.getExamSubmit()));
+        }
         return Result.ok().data(page.getRecords()).total(page.getTotal());
     }
 
+    /**
+     * 娴嬭瘯璇曞嵎閰嶇疆鑳藉惁鐢熸垚璇曞嵎
+     *
+     * @param examPaper
+     */
+    public void checkGenExam(ExamPaper examPaper) throws RuntimeException {
+        // 璇曞嵎鍐呭
+        List<PaperFixQuestionVO> examData = new ArrayList<>();
+        // 鎷垮埌棰樼洰鍓湰鏁版嵁
+        List<QuestionAnswerCopyVO> questionAnswerCopyVOList = new ArrayList<>(32);
+        if (ExamPaperTypeEnum.Fixed.getCode().equals(examPaper.getPaperType())
+                || ExamPaperTypeEnum.RandomOrder.getCode().equals(examPaper.getPaperType())) {
+            if (!StringUtils.hasText(examPaper.getContent())) {
+                throw new RuntimeException("璇曞嵎棰樼洰涓虹┖");
+            }
+            // 杞崲
+            examData = this.coverTo(examPaper, questionAnswerCopyVOList);
+        } else if (ExamPaperTypeEnum.Random.getCode().equals(examPaper.getPaperType())) {
+            // 鏍规嵁闅忔満璇曞嵎鐨勯厤缃紝闅忔満鐢熸垚瀵瑰簲棰樼洰
+            if (!StringUtils.hasText(examPaper.getContent())) {
+                throw new RuntimeException("璇曞嵎閰嶇疆寮傚父锛岃鑱旂郴鑰佸笀");
+            }
+            List<PaperQuestionSettingDTO> paperSettingList = JSON.parseArray(examPaper.getContent(), PaperQuestionSettingDTO.class);
+            examData = new ArrayList<>(8);
+            for (PaperQuestionSettingDTO paperSetting : paperSettingList) {
+                PaperFixQuestionVO paperFixQuestionVO = new PaperFixQuestionVO();
+                paperFixQuestionVO.setTitle(paperSetting.getTitle());
+                paperFixQuestionVO.setQuestionType(paperSetting.getQuestionType());
+                //涓�涓被鍨嬬殑棰樼洰list
+                List<DoQuestionVO> childQuestionList = new ArrayList<>();
+                List<PaperSettingItem> settingList = paperSetting.getSettingList();
+                for (PaperSettingItem settingItem : settingList) {
+                    Integer num = settingItem.getNum();
+                    Integer difficult = settingItem.getDifficult();
+                    if(0 == difficult) {
+                        difficult = null;
+                    }
+                    //闇�瑕侀厤缃殑棰樼洰鏁伴噺涓�0鍒欒烦杩�
+                    if (num == null || num == 0) continue;
+                    List<Question> questions = questionMapper.getRandomQuestion(settingItem.getSubjectId(), paperSetting.getQuestionType(), difficult, settingItem.getNum());
+                    if (org.springframework.util.CollectionUtils.isEmpty(questions) || settingItem.getNum() > questions.size()) {
+                        throw new RuntimeException("璇曞嵎閰嶇疆鐨勯鐩暟涓嶈冻浠ョ敓鎴愯瘯鍗�");
+                    }
+                    // 鎷垮埌棰樼洰鍚庣粍瑁呬负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯
+                    List<DoQuestionVO> childQuestions = questions.stream().map(item -> {
+                        DoQuestionVO doQuestionVO = new DoQuestionVO();
+                        doQuestionVO.setQuestionType(item.getQuestionType());
+                        //浠庨厤缃噷鎷块鐩垎鏁�
+                        doQuestionVO.setQuestionScore(settingItem.getScore());
+                        if (StringUtils.hasText(item.getContent())) {
+                            QuestionObject questionObject = JSON.parseObject(item.getContent(), QuestionObject.class);
+                            doQuestionVO.setQuestionItemList(questionObject.getQuestionItemObjects());
+                            doQuestionVO.setTitle(questionObject.getTitleContent());
+                        }
+                        doQuestionVO.setId(item.getId());
+                        doQuestionVO.setOriginalFile(item.getOriginalFile());
+                        doQuestionVO.setAudioFile(item.getAudioFile());
+
+                        // 棰樼洰鍓湰
+                        QuestionAnswerCopyVO copy = new QuestionAnswerCopyVO();
+                        copy.setId(item.getId());
+                        copy.setDifficult(item.getDifficult());
+                        copy.setAnalyze(JSON.parseObject(item.getContent(), PaperQuestion.class).getAnalyze());
+                        //濉┖鐨勭瓟妗堝湪Json閲�
+                        if (QuestionTypeEnum.GapFilling.getCode().equals(item.getQuestionType())) {
+                            List<String> gapAnswer = new ArrayList<>();
+                            for (QuestionItemObject questionItemObject : doQuestionVO.getQuestionItemList()) {
+                                gapAnswer.add(questionItemObject.getContent());
+                            }
+                            copy.setCorrect(String.join(ANSWER_SPLIT, gapAnswer));
+                        } else {
+                            copy.setCorrect(item.getCorrect());
+                        }
+                        questionAnswerCopyVOList.add(copy);
+
+                        return doQuestionVO;
+                    }).collect(Collectors.toList());
+                    //娣诲姞鍒拌繖涓被鍨嬬殑list涓�
+                    childQuestionList.addAll(childQuestions);
+                }
+                paperFixQuestionVO.setQuestionList(childQuestionList);
+                if (! CollectionUtils.isEmpty(childQuestionList)) {
+                    examData.add(paperFixQuestionVO);
+                }
+            }
+        }
+    }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -300,6 +415,9 @@
                 for (PaperSettingItem settingItem : settingList) {
                     Integer num = settingItem.getNum();
                     Integer difficult = settingItem.getDifficult();
+                    if(0 == difficult){
+                        difficult = null;
+                    }
                     //闇�瑕侀厤缃殑棰樼洰鏁伴噺涓�0鍒欒烦杩�
                     if (num == null || num == 0) continue;
                     List<Question> questions = questionMapper.getRandomQuestion(settingItem.getSubjectId(), paperSetting.getQuestionType(), difficult, settingItem.getNum());
@@ -344,7 +462,9 @@
                     childQuestionList.addAll(childQuestions);
                 }
                 paperFixQuestionVO.setQuestionList(childQuestionList);
-                examData.add(paperFixQuestionVO);
+                if (! CollectionUtils.isEmpty(childQuestionList)) {
+                    examData.add(paperFixQuestionVO);
+                }
             }
         }
         ExamSubmitTemp examSubmitTemp = new ExamSubmitTemp();
@@ -439,10 +559,21 @@
      * @return
      */
     @Override
-    public Result all() {
-        List<Exam> entities = baseMapper.selectList(null);
+    public Result all(ExamQuery query) {
+        List<Exam> entities;
+        // 鍒ゆ柇濡傛灉examName涓虹┖鎴栫┖瀛楃涓诧紝鍒欐煡璇㈡墍鏈�
+        if (query.getExamName() == null || query.getExamName().isEmpty()) {
+            entities = baseMapper.selectList(null);
+        }else {
+            entities = baseMapper.selectList(new LambdaQueryWrapper<>(Exam.class).like(Exam::getExamName,query.getExamName()));
+        }
         List<ExamVO> vos = entities.stream()
-                .map(entity -> ExamVO.getVoByEntity(entity, null))
+                .map(entity -> {
+                    ExamVO vo = new ExamVO();
+                    vo = ExamVO.getVoByEntity(entity, vo);
+                    vo.setStatus(entity.getStatus().getDesc());
+                    return vo;
+                })
                 .collect(Collectors.toList());
         return Result.ok().data(vos);
     }
@@ -874,10 +1005,12 @@
         examPaperScore.setTotalScore(new BigDecimal(examPaperMark.getTotalScore()));
         examPaperScore.setJudgeUser(userId);
         examPaperScore.setJudgeTime(new Date());
-        if (!StringUtils.isEmpty(examPaperMark.getTitleItems())) {
+        if (!org.springframework.util.CollectionUtils.isEmpty(examPaperMark.getTitleItems())) {
             examPaperScore.setPaperContent(JSON.toJSONString(examPaperMark.getTitleItems()));
+            // 淇濆瓨绛旈璁板綍
+            this.saveQuestionAnswerRecord(examPaperMark.getUserId(), examPaperMark.getTitleItems(), examPaperMark.getExamId(), examPaperMark.getPaperId());
         }
-        if (!StringUtils.isEmpty(examPaperMark.getNavbar())) {
+        if (!org.springframework.util.CollectionUtils.isEmpty(examPaperMark.getNavbar())) {
             examPaperScore.setNavbar(JSON.toJSONString(examPaperMark.getNavbar()));
         }
         long questionCorrect = 0;
@@ -915,6 +1048,34 @@
         return Result.ok();
     }
 
+    /**
+     * 淇濆瓨绛旈璁板綍
+     *
+     * @param titleItems
+     */
+    private void saveQuestionAnswerRecord(Integer studentUserId, List<PaperFixQuestionVO> titleItems, Integer examId, Integer paperId) {
+        // 鍒犻櫎鍘熸潵鐨勭瓟棰樿褰�
+        new LambdaUpdateChainWrapper<>(questionAnswerRecordMapper)
+                .eq(QuestionAnswerRecord::getExamId, examId)
+                .eq(QuestionAnswerRecord::getUserId, studentUserId).remove();
+        for (PaperFixQuestionVO titleItem : titleItems) {
+            for (DoQuestionVO question : titleItem.getQuestionList()) {
+                QuestionAnswerRecord record = new QuestionAnswerRecord();
+                record.setQuestionType(titleItem.getQuestionType());
+                record.setUserId(studentUserId);
+                record.setDoRight(question.getRight());
+                record.setExamId(examId);
+                record.setPaperId(paperId);
+                record.setQuestionId(question.getId());
+                record.setScore(question.getScore());
+                record.setUserAnswer(StringUtils.hasText(question.getAnswer()) ? question.getAnswer() : question.getAnswerList().stream().collect(Collectors.joining("銆�")));
+                questionAnswerRecordMapper.insert(record);
+            }
+        }
+
+
+    }
+
 
     @Override
     public Result monitorList(ExamQuery query) {

--
Gitblit v1.8.0