You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.6 KiB
117 lines
4.6 KiB
package com.yf.exam.modules.user.exam.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.yf.exam.core.api.dto.PagingReqDTO;
|
|
import com.yf.exam.modules.user.UserUtils;
|
|
import com.yf.exam.modules.user.exam.dto.request.UserExamReqDTO;
|
|
import com.yf.exam.modules.user.exam.dto.response.UserExamRespDTO;
|
|
import com.yf.exam.modules.user.exam.entity.UserExam;
|
|
import com.yf.exam.modules.user.exam.mapper.UserExamMapper;
|
|
import com.yf.exam.modules.user.exam.service.UserExamService;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.Date;
|
|
|
|
/**
|
|
* <p>
|
|
* 考试记录业务实现类,实现了 UserExamService 接口,处理考试记录相关的业务逻辑。
|
|
* 继承自 MyBatis-Plus 的 ServiceImpl 类,可使用其提供的基础服务方法。
|
|
* </p>
|
|
*
|
|
* @author 聪明笨狗
|
|
* @since 2020-09-21 15:13
|
|
*/
|
|
@Service
|
|
public class UserExamServiceImpl extends ServiceImpl<UserExamMapper, UserExam> implements UserExamService {
|
|
|
|
/**
|
|
* 分页查询考试记录
|
|
* @param reqDTO 包含分页信息和查询条件的请求对象,泛型为 UserExamReqDTO
|
|
* @return 包含分页结果的 UserExamRespDTO 对象集合,封装在 IPage 中
|
|
*/
|
|
@Override
|
|
public IPage<UserExamRespDTO> paging(PagingReqDTO<UserExamReqDTO> reqDTO) {
|
|
// 调用 Mapper 层的分页查询方法,将请求的分页信息和查询参数传入
|
|
// 并将查询结果转换为包含 UserExamRespDTO 的分页数据
|
|
IPage<UserExamRespDTO> pageData = baseMapper.paging(reqDTO.toPage(), reqDTO.getParams());
|
|
return pageData;
|
|
}
|
|
|
|
/**
|
|
* 分页查询当前用户的考试记录
|
|
* @param reqDTO 包含分页信息和查询条件的请求对象,泛型为 UserExamReqDTO
|
|
* @return 包含分页结果的 UserExamRespDTO 对象集合,封装在 IPage 中
|
|
*/
|
|
@Override
|
|
public IPage<UserExamRespDTO> myPaging(PagingReqDTO<UserExamReqDTO> reqDTO) {
|
|
// 获取请求中的查询参数
|
|
UserExamReqDTO params = reqDTO.getParams();
|
|
|
|
// 若查询参数为空,则创建一个新的 UserExamReqDTO 对象
|
|
if(params == null){
|
|
params = new UserExamReqDTO();
|
|
}
|
|
|
|
// 设置当前用户的 ID 到查询参数中
|
|
params.setUserId(UserUtils.getUserId());
|
|
|
|
// 调用 Mapper 层的分页查询方法,将请求的分页信息和更新后的查询参数传入
|
|
// 并将查询结果转换为包含 UserExamRespDTO 的分页数据
|
|
IPage<UserExamRespDTO> pageData = baseMapper.paging(reqDTO.toPage(), params);
|
|
return pageData;
|
|
}
|
|
|
|
/**
|
|
* 记录用户考试结果,更新或创建考试记录
|
|
* @param userId 用户 ID
|
|
* @param examId 考试 ID
|
|
* @param score 用户本次考试得分
|
|
* @param passed 用户是否通过本次考试
|
|
*/
|
|
@Override
|
|
public void joinResult(String userId, String examId, Integer score, boolean passed) {
|
|
// 构建查询条件,根据用户 ID 和考试 ID 查询考试记录
|
|
QueryWrapper<UserExam> wrapper = new QueryWrapper<>();
|
|
wrapper.lambda().eq(UserExam::getUserId, userId)
|
|
.eq(UserExam::getExamId, examId);
|
|
|
|
// 根据查询条件获取一条考试记录
|
|
UserExam record = this.getOne(wrapper, false);
|
|
if(record == null){
|
|
// 若记录不存在,则创建一条新的考试记录
|
|
record = new UserExam();
|
|
// 设置记录创建时间
|
|
record.setCreateTime(new Date());
|
|
// 设置记录更新时间
|
|
record.setUpdateTime(new Date());
|
|
// 设置用户 ID
|
|
record.setUserId(userId);
|
|
// 设置考试 ID
|
|
record.setExamId(examId);
|
|
// 设置最高分数为本次考试得分
|
|
record.setMaxScore(score);
|
|
// 设置是否通过考试
|
|
record.setPassed(passed);
|
|
// 保存新的考试记录
|
|
this.save(record);
|
|
return;
|
|
}
|
|
|
|
// 修复低分数不加入统计问题,增加考试次数
|
|
record.setTryCount(record.getTryCount() + 1);
|
|
// 更新记录更新时间
|
|
record.setUpdateTime(new Date());
|
|
|
|
// 若本次考试得分高于之前的最高分数,则更新最高分数和是否通过考试状态
|
|
if(record.getMaxScore() < score){
|
|
record.setMaxScore(score);
|
|
record.setPassed(passed);
|
|
}
|
|
|
|
// 根据记录 ID 更新考试记录
|
|
this.updateById(record);
|
|
}
|
|
}
|