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 | 388 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 378 insertions(+), 10 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 378a11c..ce84882 100644 --- a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java +++ b/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java @@ -1,21 +1,41 @@ package com.ycl.jxkg.service.impl; +import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.core.metadata.IPage; -import com.ycl.jxkg.domain.entity.Exam; -import com.ycl.jxkg.mapper.ExamMapper; -import com.ycl.jxkg.service.ExamService; -import com.ycl.jxkg.base.Result; +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.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.vo.ExamVO; +import com.ycl.jxkg.domain.form.ForceSubmitForm; import com.ycl.jxkg.domain.query.ExamQuery; -import org.springframework.stereotype.Service; -import lombok.RequiredArgsConstructor; +import com.ycl.jxkg.domain.question.QuestionObject; +import com.ycl.jxkg.domain.vo.*; +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.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; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; /** @@ -29,37 +49,49 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements ExamService { private final ExamMapper examMapper; + private final WebContext webContext; + private final QuestionMapper questionMapper; + private final ExamSubmitTempMapper examSubmitTempMapper; + private final ClassesUserMapper classesUserMapper; + private final ExamPaperMapper examPaperMapper; + private final ExamPaperService examPaperService; + private final WebsocketServer websocketServer; /** * 娣诲姞 + * * @param form * @return */ @Override public Result add(ExamForm form) { Exam entity = ExamForm.getEntityByForm(form, null); + entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), null)); + entity.setTeacherId(webContext.getCurrentUser().getId()); baseMapper.insert(entity); return Result.ok("娣诲姞鎴愬姛"); } /** * 淇敼 + * * @param form * @return */ @Override public Result update(ExamForm form) { Exam entity = baseMapper.selectById(form.getId()); - // 涓虹┖鎶汭llegalArgumentException锛屽仛鍏ㄥ眬寮傚父澶勭悊 Assert.notNull(entity, "璁板綍涓嶅瓨鍦�"); BeanUtils.copyProperties(form, entity); + entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), null)); baseMapper.updateById(entity); return Result.ok("淇敼鎴愬姛"); } /** * 鎵归噺鍒犻櫎 + * * @param ids * @return */ @@ -71,6 +103,7 @@ /** * id鍒犻櫎 + * * @param id * @return */ @@ -82,18 +115,176 @@ /** * 鍒嗛〉鏌ヨ + * * @param query * @return */ @Override public Result page(ExamQuery query) { IPage<ExamVO> page = PageUtil.getPage(query, ExamVO.class); - baseMapper.getPage(page, query); + baseMapper.getPage(page, query, webContext.getCurrentUser().getId()); + page.getRecords().stream().forEach(item -> { + if (! StringUtils.hasText(item.getClassName())) { + item.setClassName("鐝骇涓嶅瓨鍦ㄦ垨琚垹闄�"); + item.setClassesId(null); + } + if (! StringUtils.hasText(item.getExamPaperName())) { + item.setExamPaperName("璇曞嵎涓嶅瓨鍦ㄦ垨琚垹闄�"); + item.setExamPaperId(null); + } + }); return Result.ok().data(page.getRecords()).total(page.getTotal()); + } + + + @Override + public Result studentPage(ExamQuery query) { + IPage<ExamVO> page = PageUtil.getPage(query, ExamVO.class); + baseMapper.studentPage(page, query, webContext.getCurrentUser().getId()); + return Result.ok().data(page.getRecords()).total(page.getTotal()); + } + + + @Override + @Transactional(rollbackFor = Exception.class) + public Result start(Integer id) { + Exam exam = baseMapper.selectById(id); + if (Objects.isNull(exam)) { + throw new RuntimeException("璇ヨ�冭瘯涓嶅瓨鍦�"); + } + 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("璇曞嵎涓嶅瓨鍦�"); + } + // 濡傛灉宸茬粡鍙傚姞杩囪�冭瘯锛岀洿鎺ヨ繑鍥炴暟鎹� + 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())) { + // 鏍规嵁闅忔満璇曞嵎鐨勯厤缃紝闅忔満鐢熸垚瀵瑰簲棰樼洰 + 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 Result.ok(); + } + + /** + * 灏嗘暟鎹簱瀛樺偍鐨勯鐩紝杞负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯 + * + * @param examPaper 璇曞嵎 + * @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(); + vo.setTitle(item.getTitle()); + vo.setQuestionType(item.getQuestionType()); + List<DoQuestionVO> doQuestionVOS = item.getQuestionList().stream().map(question -> { + 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()); + doQuestionVO.setAudioFile(question.getAudioFile()); + return doQuestionVO; + }).collect(Collectors.toList()); + if (ExamPaperTypeEnum.RandomOrder.getCode().equals(examPaper.getPaperType())) { + // 闅忓簭璇曞嵎鎵撲贡椤哄簭 + Collections.shuffle(doQuestionVOS); + } + vo.setQuestionList(doQuestionVOS); + return vo; + }).collect(Collectors.toList()); } /** * 鏍规嵁id鏌ユ壘 + * * @param id * @return */ @@ -106,6 +297,7 @@ /** * 鍒楄〃 + * * @return */ @Override @@ -116,4 +308,180 @@ .collect(Collectors.toList()); return Result.ok().data(vos); } + + /** + * 涓诲姩鎻愪氦璇曞嵎 + * + * @param submitData 璇曞嵎鍋氶鎻愪氦鏁版嵁 + * @return + */ + @Override + public Result examSubmit(StartExamVO submitData) { + // 鏍¢獙 + Exam exam = examMapper.selectById(submitData.getId()); + if (Objects.isNull(exam)) { + throw new RuntimeException("璇ヨ�冭瘯涓嶅瓨鍦�"); + } +// // 鍒ゆ柇鍗曢�夈�佸閫夈�佸垽鏂瀵归敊 +// List<Integer> questionIds = new ArrayList<>(24); +// submitData.getPaperQuestionList().forEach(item -> { +// List<Integer> ids = item.getQuestionList().stream().map(DoQuestionVO::getId).collect(Collectors.toList()); +// questionIds.addAll(ids); +// }); +// List<Question> questionList = questionMapper.getAnswerInfo(questionIds); +// Map<Integer, Question> answerMap = questionList.stream().collect(Collectors.toMap(Question::getId, entity -> entity)); +// submitData.getQuestionList().stream().forEach(item -> { +// Question question = answerMap.get(item.getId()); +// if (Objects.nonNull(question) +// && (QuestionTypeEnum.SingleChoice.getCode().equals(question.getQuestionType()) +// || QuestionTypeEnum.MultipleChoice.getCode().equals(question.getQuestionType()) +// || QuestionTypeEnum.TrueFalse.getCode().equals(question.getQuestionType()) +// )) { +// String correct = question.getCorrect(); +// if (QuestionTypeEnum.MultipleChoice.getCode().equals(question.getQuestionType())) { +// // 濡傛灉鏄�夋嫨棰橈紝閭d箞灏嗙瓟妗堣浆涓簂ist +// List<String> answerList = JSON.parseArray(correct, String.class); +// item.setRight(answerList.containsAll(item.getAnswerList())); +// } else { +// item.setRight(question.getCorrect().equals(item.getAnswer())); +// } +// } +// }); + // 闃呭嵎鍚庢墠寰�exam_paper_answer淇濆瓨鑰冭瘯鎴愮哗銆佷互鍙婁繚瀛樺埌exam_paper_customer_answer + // 鐜板湪鍙渶瑕佷繚瀛樺埌涓�寮犱复鏃惰〃 + // 璇ユ帴鍙f槸涓诲姩鎻愪氦锛屾墍浠ョ姸鎬侀兘璁剧疆涓哄畬鎴愶紝浠ヤ究鍚庣画鑰佸笀闃呭嵎 + saveTempExam(submitData, ExamSubmitTempStatusEnum.FINISH); + return Result.ok(); + } + + /** + * 涓存椂淇濆瓨璇曞嵎 + * + * @param submitData + * @return + */ + @Override + public Result timingSubmit(StartExamVO submitData) { + saveTempExam(submitData, ExamSubmitTempStatusEnum.TEMP); + return Result.ok(); + } + + /** + * 淇濆瓨璇曞嵎锛氬鏋滄帴鍙f槸瀹氭椂淇濆瓨閭d箞鏄复鏃惰瘯鍗枫�傚鏋滄帴鍙f槸鑷富鎻愪氦閭d箞鏄畬鎴愯瘯鍗� + * + * @param submitData 鍓嶇浼犻�掔殑璇曞嵎鏁版嵁 + * @param status 璇曞嵎鐨勭姸鎬� + */ + public void saveTempExam(StartExamVO submitData, ExamSubmitTempStatusEnum status) { + ExamSubmitTemp one = new LambdaQueryChainWrapper<>(examSubmitTempMapper) + .eq(ExamSubmitTemp::getExamId, submitData.getId()) + .eq(ExamSubmitTemp::getUserId, webContext.getCurrentUser().getId()) + .one(); + if (Objects.nonNull(one)) { + if (ExamSubmitTempStatusEnum.FINISH.equals(one.getStatus())) { + return; + } + one.setDoTime(submitData.getDoTime()); + one.setExamSubmit(JSON.toJSONString(submitData.getTitleList())); + one.setCreateTime(new Date()); + one.setStatus(status); + examSubmitTempMapper.updateById(one); + } else { + ExamSubmitTemp examSubmitTemp = new ExamSubmitTemp(); + examSubmitTemp.setExamId(submitData.getId()); + examSubmitTemp.setDoTime(submitData.getDoTime()); + examSubmitTemp.setStatus(status); + examSubmitTemp.setUserId(webContext.getCurrentUser().getId()); + examSubmitTemp.setExamSubmit(JSON.toJSONString(submitData.getTitleList())); + examSubmitTemp.setMarkPaperStatus(ExamSubmitTempStatusEnum.TEMP); + examSubmitTempMapper.insert(examSubmitTemp); + } + } + + @Override + public Result getMarkResultInfo(Integer id) { + Exam exam = baseMapper.selectById(id); + if (Objects.isNull(exam)) { + throw new RuntimeException("璇ヨ�冭瘯涓嶅瓨鍦�"); + } + ExamPaper examPaper = examPaperMapper.selectById(exam.getExamPaperId()); + if (Objects.isNull(examPaper)) { + throw new RuntimeException("鑰冭瘯璇曞嵎涓嶅瓨鍦�"); + } + List<ExamSubmitTemp> examSubmitTempList = new LambdaQueryChainWrapper<>(examSubmitTempMapper) + .eq(ExamSubmitTemp::getExamId, id) + .list(); + // 鍙傝�冧汉鏁� + Integer joinExamNum = examSubmitTempList.size(); + // 鍙傝�冧絾鏈畬鎴愭彁浜や汉鏁� + Integer joinButNotFinishedNum = Math.toIntExact(examSubmitTempList.stream().filter(item -> ExamSubmitTempStatusEnum.TEMP.equals(item.getStatus())).count()); + + List<StudentExamInfoVO> studentExamList = classesUserMapper.getClassesUserList(exam.getClassesId()); + // 搴旇�冧汉鏁� + Integer shouldUserNum = studentExamList.size(); + + studentExamList.stream().forEach(item -> { + if (StringUtils.hasText(item.getExamSubmit())) { + item.setQuestionList(JSON.parseArray(item.getExamSubmit(), DoQuestionVO.class)); + } + }); + + MarkPaperVO markPaperVO = new MarkPaperVO(); + markPaperVO.setExamName(exam.getExamName()); + markPaperVO.setExamId(exam.getId()); + markPaperVO.setJoinButNotFinishNum(joinButNotFinishedNum); + markPaperVO.setShouldJoinNum(shouldUserNum); + markPaperVO.setStudentExamInfoVOList(studentExamList); + markPaperVO.setMissJoinNum(shouldUserNum - joinExamNum); + markPaperVO.setJoinNum(joinExamNum); + markPaperVO.setExamPaperName(examPaper.getName()); + markPaperVO.setSuggestTime(examPaper.getSuggestTime()); + 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