From 8458e64aab474c0fc2f49ae4ff22fb11ce5cf6e2 Mon Sep 17 00:00:00 2001
From: “djh” <“3298565835@qq.com”>
Date: Mon, 11 Nov 2024 16:55:28 +0800
Subject: [PATCH] 批次新增学员查询条件,新增题目导入接口
---
exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExPaperStudentServiceImpl.java | 234 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 201 insertions(+), 33 deletions(-)
diff --git a/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExPaperStudentServiceImpl.java b/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExPaperStudentServiceImpl.java
index dea8694..63de6e3 100644
--- a/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExPaperStudentServiceImpl.java
+++ b/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExPaperStudentServiceImpl.java
@@ -1,29 +1,31 @@
package com.gkhy.exam.system.service.impl;
import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.api.CommonPage;
+import com.gkhy.exam.common.constant.Constant;
import com.gkhy.exam.common.domain.entity.SysUser;
+import com.gkhy.exam.common.enums.PaperStudentStateEnum;
import com.gkhy.exam.common.enums.QuestionTypeEnum;
+import com.gkhy.exam.common.enums.StudentAnswerPassEnum;
import com.gkhy.exam.common.enums.UserTypeEnum;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
-import com.gkhy.exam.system.domain.ExExamPaper;
-import com.gkhy.exam.system.domain.ExPaperStudent;
-import com.gkhy.exam.system.domain.ExStudent;
-import com.gkhy.exam.system.mapper.ExExamPaperMapper;
-import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
-import com.gkhy.exam.system.mapper.ExStudentAnswerMapper;
-import com.gkhy.exam.system.mapper.ExStudentMapper;
+import com.gkhy.exam.common.utils.StringUtils;
+import com.gkhy.exam.system.domain.*;
+import com.gkhy.exam.system.domain.vo.BatchPaperStudentVO;
+import com.gkhy.exam.system.domain.vo.ExPaperStudentVO;
+import com.gkhy.exam.system.mapper.*;
import com.gkhy.exam.system.service.ExPaperStudentService;
+import com.gkhy.exam.system.service.ExStudentAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
+import java.util.stream.Collectors;
/**
* <p>
@@ -38,9 +40,15 @@
@Autowired
private ExExamPaperMapper examPaperMapper;
@Autowired
+ private ExStudentAnswerService studentAnswerService;
+ @Autowired
private ExStudentAnswerMapper studentAnswerMapper;
@Autowired
private ExStudentMapper studentMapper;
+ @Autowired
+ private ExPhaseStudentMapper phaseStudentMapper;
+ @Autowired
+ private ExQuestionMapper questionMapper;
@Override
@@ -67,6 +75,7 @@
@Override
public int addPaperStudent(ExPaperStudent paperStudent) {
+ checkUserAllowed(paperStudent);
checkStudentUnique(Collections.singletonList(paperStudent));
paperStudent.setCreateId(SecurityUtils.getUserId());
int row=baseMapper.insert(paperStudent);
@@ -77,8 +86,38 @@
}
@Override
- public int batchAddPaperStudent(List<ExPaperStudent> paperStudents) {
- checkStudentUnique(paperStudents);
+ public int batchAddPaperStudent(BatchPaperStudentVO batchPaperStudentVO) {
+ List<Long> phaseIds= batchPaperStudentVO.getPhaseIds();
+ List<Long> studentIds= batchPaperStudentVO.getStudentIds();
+ Long paperId=batchPaperStudentVO.getPaperId();
+ if(ObjectUtil.isEmpty(phaseIds) && ObjectUtil.isEmpty(studentIds)){
+ throw new ApiException("批次id与学员id不能同时为空");
+ }
+ List<Long> allStudentIds=new ArrayList<>();
+ //按批次绑定用户
+ if(phaseIds!=null&&!phaseIds.isEmpty()){
+ List<ExPhaseStudent> phaseStudentList=phaseStudentMapper.selectList( Wrappers.<ExPhaseStudent>lambdaQuery()
+ .in(ExPhaseStudent::getPhaseId, phaseIds));
+ allStudentIds=phaseStudentList.stream().map(item -> item.getStudentId()).distinct().collect(Collectors.toList());
+ }
+ if(studentIds!=null&&!studentIds.isEmpty()){
+ allStudentIds.addAll(studentIds);
+ }
+ List<ExPaperStudent> paperStudentList=baseMapper.selectList( Wrappers.<ExPaperStudent>lambdaQuery()
+ .eq(true,ExPaperStudent::getPaperId, paperId)
+ .in(true,ExPaperStudent::getStudentId,allStudentIds));
+ if(paperStudentList.size()>0) {
+ List<Long> existStudentIds = paperStudentList.stream().map(item -> item.getStudentId()).distinct().collect(Collectors.toList());
+ allStudentIds.removeAll(existStudentIds);
+ }
+ if(allStudentIds.isEmpty()){
+ throw new ApiException("未匹配到学员");
+ }
+ allStudentIds= allStudentIds.stream().distinct().collect(Collectors.toList());
+ List<ExPaperStudent> paperStudents=allStudentIds.stream().map(item -> {
+ return new ExPaperStudent().setPaperId(paperId).setStudentId(item).setCreateId(SecurityUtils.getUserId());
+ }).collect(Collectors.toList());
+ // checkStudentUnique(paperStudents);
int row=baseMapper.batchInsert(paperStudents);
if(row<1){
throw new ApiException("批量分配学员失败");
@@ -98,9 +137,60 @@
}
@Override
+ @Transactional(rollbackFor = RuntimeException.class)
+ public void doReview(ExPaperStudentVO paperStudentVO) {
+ checkUserAllowed(new ExPaperStudent().setPaperId(paperStudentVO.getPaperId()));
+ ExPaperStudent paperStudent=baseMapper.selectById(paperStudentVO.getId());
+ ExExamPaper examPaper=examPaperMapper.selectById(paperStudent.getPaperId());
+ if(paperStudent.getState().equals(PaperStudentStateEnum.WAIT_EXAM.getCode())){
+ throw new ApiException("待考试状态的试卷不能批改!");
+ }
+ List<ExStudentAnswer> studentAnswerList=studentAnswerMapper.selectList(Wrappers.<ExStudentAnswer>lambdaQuery()
+ .eq(true,ExStudentAnswer::getStudentId,paperStudent.getStudentId())
+ .eq(true,ExStudentAnswer::getPaperId,paperStudent.getPaperId()));
+ Map<Long, ExStudentAnswer> collectMap = studentAnswerList.stream().collect(Collectors.toMap(ExStudentAnswer::getQuestionId, k -> k));
+ List<ExPaperStudentVO.QuestionVO> questionVOList=paperStudentVO.getQuestions();
+ List<ExStudentAnswer> updateStudentAnswerList=new ArrayList<>();
+ for (ExPaperStudentVO.QuestionVO questionVO:questionVOList){
+ ExStudentAnswer sa=collectMap.get(questionVO.getQuestionId());
+ if(sa!=null){
+ int score=questionVO.getScore();
+ sa.setScore(score);
+ if(score>examPaper.getEasyScore()){
+ throw new ApiException("得分不能超过题目的最高分!");
+ }else if(score==examPaper.getEasyScore()){
+ sa.setPassed(StudentAnswerPassEnum.CORRECT.getCode());
+ }else{
+ sa.setPassed(StudentAnswerPassEnum.ERROR.getCode());
+ }
+ updateStudentAnswerList.add(sa);
+ }
+ }
+ int totalScore=0;
+ for(ExStudentAnswer studentAnswer:studentAnswerList){
+ if(studentAnswer.getScore()!=null) {
+ totalScore = totalScore + studentAnswer.getScore();
+ }
+ }
+ paperStudent.setState(PaperStudentStateEnum.DONE_REVIEW.getCode());
+ paperStudent.setScore(totalScore);
+ if(totalScore<examPaper.getPassScore()){
+ paperStudent.setPassed(Constant.EXAM_UNPASS);
+ }else{
+ paperStudent.setPassed(Constant.EXAM_PASS);
+ }
+ paperStudent.setUpdateBy(SecurityUtils.getUsername());
+ baseMapper.updateById(paperStudent);
+ if(!updateStudentAnswerList.isEmpty()){
+ studentAnswerService.updateBatchById(updateStudentAnswerList);
+ }
+ }
+
+
+ @Override
public CommonPage selectPaperStudentList(ExPaperStudent paperStudent) {
if(paperStudent.getPaperId()==null){
- throw new ApiException("试卷id不能为空");
+ throw new ApiException("考卷id不能为空");
}
PageUtils.startPage();
List<ExPaperStudent> paperStudentList=baseMapper.selectPaperStudentList(paperStudent);
@@ -109,13 +199,14 @@
@Override
public int deletePaperStudent(Long paperStudentId) {
- ExPaperStudent paperStudent=baseMapper.selectPaperStudentById(paperStudentId);
+ ExPaperStudent paperStudent=baseMapper.selectSimplePaperStudentById(paperStudentId);
if(ObjectUtil.isNull(paperStudent)){
- throw new ApiException(String.format("该试卷下不存在该学员<>",paperStudent.getStudentName()));
+ throw new ApiException("学员与试卷关系不存在");
}
+ checkUserAllowed(paperStudent);
int studentAnswerCount=studentAnswerMapper.countByPaperId(paperStudent.getPaperId(),paperStudent.getStudentId());
if(studentAnswerCount>0){
- throw new ApiException(String.format("学员<%s>已进行答题,不能删除",paperStudent.getStudentName()));
+ throw new ApiException("学员已进行答题,不能删除");
}
//删除学员与试卷关系
return baseMapper.deleteById(paperStudentId);
@@ -130,35 +221,112 @@
}
+ public void checkUserAllowed(ExPaperStudent paperStudent) {
+ SysUser currentUser= SecurityUtils.getLoginUser().getUser();
+ if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
+ return;
+ }
+ if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
+ throw new ApiException("没有权限操作");
+ }
+ ExExamPaper examPaper=examPaperMapper.selectById(paperStudent.getPaperId());
+ if(!currentUser.getCompanyId().equals(examPaper.getCompanyId())){
+ throw new ApiException("没有权限操作其他企业考卷");
+ }
+ }
@Override
- public void endExam(ExPaperStudent paperStudent) {
+ public void endExam(ExPaperStudent pStudent) {
+ if(pStudent.getId()==null){
+ throw new ApiException("学员与试卷关系id不能为空");
+ }
+ ExPaperStudent existPs=baseMapper.selectById(pStudent.getId());
+ if(existPs==null){
+ throw new ApiException("学员与试卷关系不存在");
+ }
Long endTime=System.currentTimeMillis();
- //异步计算
+ existPs.setEndTime(endTime);
+ handlePaperData(existPs);
+
+ }
+
+ @Override
+ @Transactional(rollbackFor = RuntimeException.class)
+ public void handlePaperData(ExPaperStudent paperStudent){
//计算考试成绩
ExExamPaper paper=examPaperMapper.selectById(paperStudent.getPaperId());
- int singleCount=studentAnswerMapper.selectPassCount(paperStudent.getPaperId(),paperStudent.getStudentId(), QuestionTypeEnum.SINGLE.getCode());
- int multiCount=studentAnswerMapper.selectPassCount(paperStudent.getPaperId(),paperStudent.getStudentId(), QuestionTypeEnum.MULTI.getCode());
- int judgeCount=studentAnswerMapper.selectPassCount(paperStudent.getPaperId(),paperStudent.getStudentId(), QuestionTypeEnum.JUDGE.getCode());
- int score=singleCount*paper.getSingleScore()+multiCount*paper.getMultiScore()+judgeCount*paper.getJudgeScore();
- ExPaperStudent exPaperStudent = new ExPaperStudent()
- .setPaperId(paperStudent.getPaperId())
- .setStudentId(paperStudent.getStudentId())
- .setCompleted(1)
- .setEndTime(endTime)
- .setScore(score);
- if(score>=paper.getPassScore()){
- exPaperStudent.setPassed(1);
- }else{
- exPaperStudent.setPassed(0);
+ paperStudent.setState(PaperStudentStateEnum.WAIT_REVIEW.getCode());
+ List<ExStudentAnswer> studentAnswerList=studentAnswerMapper.selectList(Wrappers.<ExStudentAnswer>lambdaQuery()
+ .eq(true,ExStudentAnswer::getStudentId,paperStudent.getStudentId())
+ .eq(true,ExStudentAnswer::getPaperId,paperStudent.getPaperId()));
+ List<ExQuestion> questionList=questionMapper.selectQuestionByPaperId(paperStudent.getPaperId());
+ Map<Long, ExStudentAnswer> collectMap = studentAnswerList.stream().collect(Collectors.toMap(ExStudentAnswer::getQuestionId, k -> k));
+ List<ExStudentAnswer> updateStudentAnswers=new ArrayList<>();
+ int totalScore=0;
+ boolean easyViewFlag=false;
+ for(ExQuestion question:questionList){
+ ExStudentAnswer sa=collectMap.get(question.getId());
+ if(sa!=null){
+ if(question.getQuestionType().equals(QuestionTypeEnum.EASY.getCode())) {
+ if(StringUtils.isBlank(sa.getAnswer())){
+ sa.setPassed(StudentAnswerPassEnum.ERROR.getCode());
+ sa.setScore(0);
+ }else{
+ sa.setPassed(StudentAnswerPassEnum.WAIT_REVIEW.getCode());
+ easyViewFlag=true;
+ }
+ }else{
+ if (sa.getAnswer().equals(question.getAnswer())) {
+ sa.setPassed(StudentAnswerPassEnum.CORRECT.getCode());
+ sa.setScore(getScore(paper, question.getQuestionType()));
+ totalScore = totalScore + sa.getScore();
+ }else {
+ sa.setPassed(StudentAnswerPassEnum.ERROR.getCode());
+ sa.setScore(0);
+ }
+ }
+ }else{
+ sa=new ExStudentAnswer();
+ sa.setPassed(StudentAnswerPassEnum.ERROR.getCode());
+ sa.setScore(0);
+ sa.setStudentId(paperStudent.getStudentId());
+ sa.setPaperId(paperStudent.getPaperId());
+ sa.setQuestionId(question.getId());
+ }
+ updateStudentAnswers.add(sa);
}
- int row=baseMapper.updateById(exPaperStudent);
+
+ studentAnswerService.saveOrUpdateBatch(updateStudentAnswers);
+ paperStudent.setScore(totalScore);
+ if(!easyViewFlag){
+ //没有简答题,直接批改试卷
+ paperStudent.setState(PaperStudentStateEnum.DONE_REVIEW.getCode());
+ if(paper.getPassScore()>totalScore){
+ paperStudent.setPassed(Constant.EXAM_UNPASS);
+ }else{
+ paperStudent.setPassed(Constant.EXAM_PASS);
+ }
+ }
+ int row=baseMapper.updateById(paperStudent);
if(row<1){
throw new ApiException("提交考试失败");
}
}
+ private Integer getScore(ExExamPaper examPaper,Integer questionType){
+ if(questionType.equals(QuestionTypeEnum.SINGLE.getCode())){
+ return examPaper.getSingleScore();
+ }else if(questionType.equals(QuestionTypeEnum.MULTI.getCode())){
+ return examPaper.getMultiScore();
+ }else if(questionType.equals(QuestionTypeEnum.JUDGE.getCode())){
+ return examPaper.getJudgeScore();
+ }else if(questionType.equals(QuestionTypeEnum.EASY.getCode())){
+ return examPaper.getEasyScore();
+ }
+ return 0;
+ }
+
}
--
Gitblit v1.9.2