user paper exam

dev-tmp1
luotaiqian 2026-08-24 15:06:29 +08:00
parent 9eab9bf747
commit 1329ddc81a
6 changed files with 64 additions and 20 deletions

View File

@ -3,10 +3,10 @@ package org.qinan.cedu.controller;
import com.jjb.saas.framework.auth.utils.AuthContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.qinan.cedu.model.dto.AnswerQuestionDTO;
import org.qinan.cedu.model.dto.StartExamDTO;
import org.qinan.cedu.model.dto.SubmitExamDTO;
import org.qinan.cedu.model.vo.StartExamVO;
import org.qinan.cedu.service.PaperExamService;
import org.qinan.safetyeval.client.dto.SingleResponse;
@ -14,7 +14,6 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
@ -53,8 +52,8 @@ public class PaperExamController {
*/
@ApiOperation("交卷")
@PostMapping("/submitExam")
public SingleResponse<Void> submitExam(@ApiParam(value = "试卷id", required = true) @RequestParam Long paperId) {
paperExamService.submitExam(paperId, AuthContext.getUserId());
public SingleResponse<Void> submitExam(@Validated @RequestBody SubmitExamDTO dto) {
paperExamService.submitExam(dto, AuthContext.getUserId());
return SingleResponse.success();
}
}

View File

@ -2,11 +2,22 @@ package org.qinan.cedu.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.qinan.cedu.model.entity.PaperExamQuestionUserDO;
import java.math.BigDecimal;
/**
* Mapper
*/
@Mapper
public interface PaperExamQuestionUserMapper extends BaseMapper<PaperExamQuestionUserDO> {
/**
* SUM0
*
* @param paperExamId id
* @return 0
*/
BigDecimal sumAnswerScore(@Param("paperExamId") Long paperExamId);
}

View File

@ -0,0 +1,19 @@
package org.qinan.cedu.model.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
*
*/
@Data
@ApiModel(value = "SubmitExamDTO", description = "考试交卷入参")
public class SubmitExamDTO {
@ApiModelProperty(value = "试卷id", required = true)
@NotNull(message = "试卷id不能为空")
private Long paperId;
}

View File

@ -6,6 +6,7 @@ import org.qinan.cedu.model.dto.AnswerQuestionDTO;
import org.qinan.cedu.model.dto.PaperExamDTO;
import org.qinan.cedu.model.dto.PaperExamPageQueryDTO;
import org.qinan.cedu.model.dto.StartExamDTO;
import org.qinan.cedu.model.dto.SubmitExamDTO;
import org.qinan.cedu.model.entity.PaperExamDO;
import org.qinan.cedu.model.vo.PaperExamVO;
import org.qinan.cedu.model.vo.StartExamVO;
@ -74,8 +75,8 @@ public interface PaperExamService extends IService<PaperExamDO> {
/**
*
*
* @param paperId id
* @param userId id
* @param dto id
* @param userId id
*/
void submitExam(Long paperId, Long userId);
void submitExam(SubmitExamDTO dto, Long userId);
}

View File

@ -1,5 +1,6 @@
package org.qinan.cedu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -10,12 +11,14 @@ import org.qinan.cedu.enums.AnswerStatusEnum;
import org.qinan.cedu.enums.DeleteEnum;
import org.qinan.cedu.enums.PaperExamStatusEnum;
import org.qinan.cedu.mapper.PaperExamMapper;
import org.qinan.cedu.mapper.PaperExamQuestionUserMapper;
import org.qinan.cedu.mapper.PaperMapper;
import org.qinan.cedu.mapper.QuestionMapper;
import org.qinan.cedu.model.dto.AnswerQuestionDTO;
import org.qinan.cedu.model.dto.PaperExamDTO;
import org.qinan.cedu.model.dto.PaperExamPageQueryDTO;
import org.qinan.cedu.model.dto.StartExamDTO;
import org.qinan.cedu.model.dto.SubmitExamDTO;
import org.qinan.cedu.model.entity.PaperDO;
import org.qinan.cedu.model.entity.PaperExamDO;
import org.qinan.cedu.model.entity.PaperExamQuestionUserDO;
@ -36,7 +39,6 @@ import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@ -57,6 +59,8 @@ public class PaperExamServiceImpl extends ServiceImpl<PaperExamMapper, PaperExam
private final PaperExamQuestionUserService paperExamQuestionUserService;
private final PaperExamQuestionUserMapper paperExamQuestionUserMapper;
@Override
public IPage<PaperExamVO> pageQuery(PaperExamPageQueryDTO query) {
return this.lambdaQuery()
@ -139,14 +143,20 @@ public class PaperExamServiceImpl extends ServiceImpl<PaperExamMapper, PaperExam
@Override
@Transactional(rollbackFor = Exception.class)
public void answerQuestion(AnswerQuestionDTO dto, Long userId) {
// 仅查询后续用到的字段updateById 默认忽略 null 字段,未查询字段不会被覆盖
PaperExamQuestionUserDO questionUser = paperExamQuestionUserService.lambdaQuery()
.select(PaperExamQuestionUserDO::getId, PaperExamQuestionUserDO::getQuestionId,
PaperExamQuestionUserDO::getQuestionScore)
.eq(PaperExamQuestionUserDO::getId, dto.getId())
.eq(PaperExamQuestionUserDO::getUserId, userId)
.one();
if (questionUser == null) {
throw new IllegalArgumentException("作答记录不存在");
}
QuestionDO question = questionMapper.selectById(questionUser.getQuestionId());
// 仅查询判分需要的参考答案字段
QuestionDO question = questionMapper.selectOne(new LambdaQueryWrapper<QuestionDO>()
.select(QuestionDO::getAnswer, QuestionDO::getAnswerQuestionOptionIds)
.eq(QuestionDO::getId, questionUser.getQuestionId()));
if (question == null) {
throw new IllegalArgumentException("试题不存在");
}
@ -162,9 +172,9 @@ public class PaperExamServiceImpl extends ServiceImpl<PaperExamMapper, PaperExam
@Override
@Transactional(rollbackFor = Exception.class)
public void submitExam(Long paperId, Long userId) {
public void submitExam(SubmitExamDTO dto, Long userId) {
PaperExamDO paperExam = this.lambdaQuery()
.eq(PaperExamDO::getPaperId, paperId)
.eq(PaperExamDO::getPaperId, dto.getPaperId())
.eq(PaperExamDO::getUserId, userId)
.eq(PaperExamDO::getPaperExamStatus, PaperExamStatusEnum.DOING.getValue())
.eq(PaperExamDO::getDeleteEnum, DeleteEnum.FALSE.getCode())
@ -211,17 +221,10 @@ public class PaperExamServiceImpl extends ServiceImpl<PaperExamMapper, PaperExam
}
/**
* 0
* SUM0
*/
private BigDecimal sumAnswerScore(Long paperExamId) {
List<PaperExamQuestionUserDO> questionUserList = paperExamQuestionUserService.lambdaQuery()
.eq(PaperExamQuestionUserDO::getPaperExamId, paperExamId)
.eq(PaperExamQuestionUserDO::getDeleteEnum, DeleteEnum.FALSE.getCode())
.list();
return questionUserList.stream()
.map(PaperExamQuestionUserDO::getAnswerScore)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
return paperExamQuestionUserMapper.sumAnswerScore(paperExamId);
}
/**

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.qinan.cedu.mapper.PaperExamQuestionUserMapper">
<select id="sumAnswerScore" resultType="java.math.BigDecimal">
SELECT IFNULL(SUM(answer_score), 0)
FROM paper_exam_question_user
WHERE paper_exam_id = #{paperExamId}
AND delete_enum = 'false'
</select>
</mapper>