From d0ebd33f5e4fdbff1f58a1af137d45b405366399 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期五, 28 六月 2024 16:00:03 +0800
Subject: [PATCH] rabbitmq

---
 src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java |  189 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 166 insertions(+), 23 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 e2473ff..ce84882 100644
--- a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
+++ b/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
@@ -3,26 +3,35 @@
 import com.alibaba.fastjson2.JSON;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ycl.jxkg.base.Result;
 import com.ycl.jxkg.context.WebContext;
-import com.ycl.jxkg.domain.entity.*;
+import com.ycl.jxkg.domain.entity.Exam;
+import com.ycl.jxkg.domain.entity.ExamPaper;
+import com.ycl.jxkg.domain.entity.ExamSubmitTemp;
+import com.ycl.jxkg.domain.entity.Question;
 import com.ycl.jxkg.domain.exam.PaperFixQuestionDTO;
+import com.ycl.jxkg.domain.exam.PaperQuestionSettingDTO;
+import com.ycl.jxkg.domain.form.AddTimeForm;
+import com.ycl.jxkg.domain.form.ExamForm;
+import com.ycl.jxkg.domain.form.ForceSubmitForm;
+import com.ycl.jxkg.domain.query.ExamQuery;
+import com.ycl.jxkg.domain.question.QuestionObject;
 import com.ycl.jxkg.domain.vo.*;
-import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperEditRequestVO;
 import com.ycl.jxkg.enums.ExamPaperTypeEnum;
 import com.ycl.jxkg.enums.QuestionTypeEnum;
+import com.ycl.jxkg.enums.WebsocketCommendEnum;
 import com.ycl.jxkg.enums.general.ExamStatusEnum;
 import com.ycl.jxkg.enums.general.ExamSubmitTempStatusEnum;
 import com.ycl.jxkg.mapper.*;
+import com.ycl.jxkg.server.WebsocketServer;
 import com.ycl.jxkg.service.ExamPaperService;
 import com.ycl.jxkg.service.ExamService;
-import com.ycl.jxkg.base.Result;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ycl.jxkg.domain.form.ExamForm;
-import com.ycl.jxkg.domain.query.ExamQuery;
-import org.springframework.stereotype.Service;
-import lombok.RequiredArgsConstructor;
 import com.ycl.jxkg.utils.PageUtil;
+import lombok.RequiredArgsConstructor;
 import org.springframework.beans.BeanUtils;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 import org.springframework.util.StringUtils;
 
@@ -46,6 +55,7 @@
     private final ClassesUserMapper classesUserMapper;
     private final ExamPaperMapper examPaperMapper;
     private final ExamPaperService examPaperService;
+    private final WebsocketServer websocketServer;
 
     /**
      * 娣诲姞
@@ -136,6 +146,7 @@
 
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Result start(Integer id) {
         Exam exam = baseMapper.selectById(id);
         if (Objects.isNull(exam)) {
@@ -144,25 +155,91 @@
         if (Objects.isNull(exam.getExamPaperId())) {
             throw new RuntimeException("鑰冭瘯鏈粦瀹氳瘯鍗�");
         }
+        if (ExamStatusEnum.NOT_START.equals(exam.getStatus())) {
+            throw new RuntimeException("鑰冭瘯杩樻湭寮�濮�");
+        }
+        if (ExamStatusEnum.FINISHED.equals(exam.getStatus())) {
+            throw new RuntimeException("鑰冭瘯宸茬粡缁撴潫");
+        }
         // 鏌ュ嚭鑰冭瘯璇曞嵎
         ExamPaper examPaper = examPaperMapper.selectById(exam.getExamPaperId());
         if (Objects.isNull(examPaper)) {
             throw new RuntimeException("璇曞嵎涓嶅瓨鍦�");
         }
-        if (StringUtils.hasText(examPaper.getContent())) {
-            throw new RuntimeException("璇曞嵎棰樼洰涓虹┖");
+        // 濡傛灉宸茬粡鍙傚姞杩囪�冭瘯锛岀洿鎺ヨ繑鍥炴暟鎹�
+        ExamSubmitTemp hasJoin = new LambdaQueryChainWrapper<>(examSubmitTempMapper)
+                .eq(ExamSubmitTemp::getExamId, id)
+                .eq(ExamSubmitTemp::getUserId, webContext.getCurrentUser().getId())
+                .one();
+        if (Objects.nonNull(hasJoin)) {
+            StartExamVO startExamVO = new StartExamVO();
+            startExamVO.setExamName(exam.getExamName());
+            startExamVO.setId(hasJoin.getExamId());
+            startExamVO.setTitleList(JSON.parseArray(hasJoin.getExamSubmit(), PaperFixQuestionVO.class));
+            startExamVO.setSuggestTime(examPaper.getSuggestTime());
+            startExamVO.setDoTime(hasJoin.getDoTime());
+            return Result.ok(startExamVO);
         }
-        // 灏嗛鐩浆鎹负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯
+        // 灏嗛鐩浆鎹负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯銆傚浐瀹氳瘯鍗峰拰闅忓簭璇曞嵎鐨勯鐩槸鐩存帴淇濆瓨鍒癱ontent瀛楁鐨�
         if (ExamPaperTypeEnum.Fixed.getCode().equals(examPaper.getPaperType())
                 || ExamPaperTypeEnum.RandomOrder.getCode().equals(examPaper.getPaperType())) {
+            if (StringUtils.hasText(examPaper.getContent())) {
+                throw new RuntimeException("璇曞嵎棰樼洰涓虹┖");
+            }
             // 杞崲
             List<PaperFixQuestionVO> data = this.coverTo(examPaper);
             return Result.ok().data(data);
         } else if (ExamPaperTypeEnum.Random.getCode().equals(examPaper.getPaperType())) {
-            // todo 闅忔満棰樼洰鐢熸垚
+            // 鏍规嵁闅忔満璇曞嵎鐨勯厤缃紝闅忔満鐢熸垚瀵瑰簲棰樼洰
+            if (! StringUtils.hasText(examPaper.getContent())) {
+                throw new RuntimeException("璇曞嵎閰嶇疆寮傚父锛岃鑱旂郴鑰佸笀");
+            }
+            List<PaperQuestionSettingDTO> paperSettingList = JSON.parseArray(examPaper.getContent(), PaperQuestionSettingDTO.class);
 
+            // 璇曞嵎鍐呭
+            List<PaperFixQuestionVO> examData = new ArrayList<>(8);
+            for (PaperQuestionSettingDTO paperSetting : paperSettingList) {
+                PaperFixQuestionVO paperFixQuestionVO = new PaperFixQuestionVO();
+                paperFixQuestionVO.setTitle(paperSetting.getTitle());
+                paperFixQuestionVO.setQuestionType(paperSetting.getQuestionType());
+                // 鎷垮埌璇剧洰涓嬫煇绫婚鍨嬬殑x閬撻殢鏈洪
+                List<Question> questions = questionMapper.getRandomQuestion(examPaper.getSubjectId(), paperSetting.getQuestionType(), paperSetting.getNum());
+                if (paperSetting.getNum() > questions.size()) {
+                    throw new RuntimeException("閰嶇疆鐨勯鐩暟涓嶈冻浠ョ敓鎴愯瘯鍗�");
+                }
+                // 鎷垮埌棰樼洰鍚庣粍瑁呬负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯
+                List<DoQuestionVO> childQuestions = questions.stream().map(item -> {
+                    DoQuestionVO doQuestionVO = new DoQuestionVO();
+                    doQuestionVO.setTitle(item.getTitle());
+                    doQuestionVO.setQuestionType(item.getQuestionType());
+                    if (StringUtils.hasText(item.getContent())) {
+                        QuestionObject questionObject = JSON.parseObject(item.getContent(), QuestionObject.class);
+                        doQuestionVO.setQuestionItemList(questionObject.getQuestionItemObjects());
+                    }
+                    doQuestionVO.setId(item.getId());
+                    doQuestionVO.setOriginalFile(item.getOriginalFile());
+                    doQuestionVO.setAudioFile(item.getAudioFile());
+                    return doQuestionVO;
+                }).collect(Collectors.toList());
+                paperFixQuestionVO.setQuestionList(childQuestions);
+                examData.add(paperFixQuestionVO);
+            }
+            ExamSubmitTemp examSubmitTemp = new ExamSubmitTemp();
+            examSubmitTemp.setExamId(id);
+            examSubmitTemp.setStatus(ExamSubmitTempStatusEnum.TEMP);
+            examSubmitTemp.setExamSubmit(JSON.toJSONString(examData));
+            examSubmitTemp.setCreateTime(new Date());
+            examSubmitTemp.setUserId(webContext.getCurrentUser().getId());
+            examSubmitTemp.setMarkPaperStatus(ExamSubmitTempStatusEnum.TEMP);
+            examSubmitTempMapper.insert(examSubmitTemp);
+            StartExamVO startExamVO = new StartExamVO();
+            startExamVO.setExamName(exam.getExamName());
+            startExamVO.setId(exam.getId());
+            startExamVO.setTitleList(examData);
+            startExamVO.setSuggestTime(examPaper.getSuggestTime());
+            return Result.ok(startExamVO);
         }
-        return null;
+        return Result.ok();
     }
 
     /**
@@ -172,6 +249,9 @@
      * @return
      */
     private List<PaperFixQuestionVO> coverTo(ExamPaper examPaper) {
+        if (! StringUtils.hasText(examPaper.getContent())) {
+            throw new RuntimeException("璇曞嵎鏈厤缃鐩紝璇疯仈绯昏�佸笀");
+        }
         List<PaperFixQuestionDTO> questionWarpList = JSON.parseArray(examPaper.getContent(), PaperFixQuestionDTO.class);
         return questionWarpList.stream().map(item -> {
             PaperFixQuestionVO vo = new PaperFixQuestionVO();
@@ -181,6 +261,12 @@
                 DoQuestionVO doQuestionVO = new DoQuestionVO();
                 doQuestionVO.setTitle(question.getTitle());
                 doQuestionVO.setQuestionType(item.getQuestionType());
+                // 濉┖棰橀渶瑕佹姽闄ontent(鍥犱负鏄瓟妗�)
+                if (QuestionTypeEnum.GapFilling.getCode().equals(doQuestionVO.getQuestionType())) {
+                    question.getItems().stream().forEach(option -> {
+                        option.setContent("");
+                    });
+                }
                 doQuestionVO.setQuestionItemList(question.getItems());
                 doQuestionVO.setId(question.getId());
                 doQuestionVO.setOriginalFile(question.getOriginalFile());
@@ -223,10 +309,16 @@
         return Result.ok().data(vos);
     }
 
+    /**
+     * 涓诲姩鎻愪氦璇曞嵎
+     *
+     * @param submitData 璇曞嵎鍋氶鎻愪氦鏁版嵁
+     * @return
+     */
     @Override
-    public Result examSubmit(ExamSubmitVO submitData) {
+    public Result examSubmit(StartExamVO submitData) {
         // 鏍¢獙
-        Exam exam = examMapper.selectById(submitData.getExamId());
+        Exam exam = examMapper.selectById(submitData.getId());
         if (Objects.isNull(exam)) {
             throw new RuntimeException("璇ヨ�冭瘯涓嶅瓨鍦�");
         }
@@ -262,21 +354,27 @@
         return Result.ok();
     }
 
+    /**
+     * 涓存椂淇濆瓨璇曞嵎
+     *
+     * @param submitData
+     * @return
+     */
     @Override
-    public Result timingSubmit(ExamSubmitVO submitData) {
+    public Result timingSubmit(StartExamVO submitData) {
         saveTempExam(submitData, ExamSubmitTempStatusEnum.TEMP);
         return Result.ok();
     }
 
     /**
-     * 淇濆瓨涓存椂璇曞嵎
+     * 淇濆瓨璇曞嵎锛氬鏋滄帴鍙f槸瀹氭椂淇濆瓨閭d箞鏄复鏃惰瘯鍗枫�傚鏋滄帴鍙f槸鑷富鎻愪氦閭d箞鏄畬鎴愯瘯鍗�
      *
      * @param submitData  鍓嶇浼犻�掔殑璇曞嵎鏁版嵁
      * @param status  璇曞嵎鐨勭姸鎬�
      */
-    public void saveTempExam(ExamSubmitVO submitData, ExamSubmitTempStatusEnum status) {
+    public void saveTempExam(StartExamVO submitData, ExamSubmitTempStatusEnum status) {
         ExamSubmitTemp one = new LambdaQueryChainWrapper<>(examSubmitTempMapper)
-                .eq(ExamSubmitTemp::getExamId, submitData.getExamId())
+                .eq(ExamSubmitTemp::getExamId, submitData.getId())
                 .eq(ExamSubmitTemp::getUserId, webContext.getCurrentUser().getId())
                 .one();
         if (Objects.nonNull(one)) {
@@ -284,24 +382,24 @@
                 return;
             }
             one.setDoTime(submitData.getDoTime());
-            one.setExamSubmit(JSON.toJSONString(submitData.getPaperQuestionList()));
+            one.setExamSubmit(JSON.toJSONString(submitData.getTitleList()));
             one.setCreateTime(new Date());
             one.setStatus(status);
             examSubmitTempMapper.updateById(one);
         } else {
             ExamSubmitTemp examSubmitTemp = new ExamSubmitTemp();
-            examSubmitTemp.setExamId(submitData.getExamId());
+            examSubmitTemp.setExamId(submitData.getId());
             examSubmitTemp.setDoTime(submitData.getDoTime());
             examSubmitTemp.setStatus(status);
             examSubmitTemp.setUserId(webContext.getCurrentUser().getId());
-            examSubmitTemp.setExamSubmit(JSON.toJSONString(submitData.getPaperQuestionList()));
+            examSubmitTemp.setExamSubmit(JSON.toJSONString(submitData.getTitleList()));
             examSubmitTemp.setMarkPaperStatus(ExamSubmitTempStatusEnum.TEMP);
             examSubmitTempMapper.insert(examSubmitTemp);
         }
     }
 
     @Override
-    public Result getMarkPaperInfo(Integer id) {
+    public Result getMarkResultInfo(Integer id) {
         Exam exam = baseMapper.selectById(id);
         if (Objects.isNull(exam)) {
             throw new RuntimeException("璇ヨ�冭瘯涓嶅瓨鍦�");
@@ -341,4 +439,49 @@
         return Result.ok().data(markPaperVO);
     }
 
+
+    @Override
+    public Result getMarkPaperInfo(Integer examId, Integer userId) {
+        ExamSubmitTemp userExam = new LambdaQueryChainWrapper<>(examSubmitTempMapper)
+                .eq(ExamSubmitTemp::getExamId, examId)
+                .eq(ExamSubmitTemp::getUserId, userId)
+                .one();
+        if (Objects.isNull(userExam)) {
+            throw new RuntimeException("璇ュ鍛樿�冭瘯璁板綍涓嶅瓨鍦�");
+        }
+        ExamSubmitVO vo = new ExamSubmitVO();
+        vo.setExamId(userExam.getExamId());
+        vo.setDoTime(userExam.getDoTime());
+        vo.setUpdateTime(userExam.getUpdateTime());
+        if (StringUtils.hasText(userExam.getExamSubmit())) {
+            vo.setPaperQuestionList(JSON.parseArray(userExam.getExamSubmit(), PaperFixQuestionVO.class));
+        }
+        return Result.ok(vo);
+    }
+
+    @Override
+    public Result monitorList(ExamQuery query) {
+        IPage<ExamSubmitTempVO> page = PageUtil.getPage(query, ExamSubmitTempVO.class);
+        return Result.ok((examSubmitTempMapper.monitorList(page, query)));
+    }
+
+    @Override
+    public Result addTime(AddTimeForm form) {
+        WebsocketDataVO websocket = new WebsocketDataVO();
+        websocket.setCommend(WebsocketCommendEnum.DELAYED.getCommend());
+        websocket.setData(form);
+        // 鍙戦�亀ebsocket娑堟伅
+        websocketServer.sendOneMessage(form.getUserId(), JSON.toJSONString(form));
+        return Result.ok("鎿嶄綔鎴愬姛");
+    }
+
+    @Override
+    public Result forceSubmit(ForceSubmitForm form) {
+        WebsocketDataVO websocket = new WebsocketDataVO();
+        websocket.setCommend(WebsocketCommendEnum.FORCE_SUBMIT.getCommend());
+        websocket.setData(form);
+        // 鍙戦�亀ebsocket娑堟伅
+        websocketServer.sendOneMessage(form.getUserId(), JSON.toJSONString(form));
+        return Result.ok("鎿嶄綔鎴愬姛");
+    }
 }

--
Gitblit v1.8.0