package com.gkhy.exam.system.service.impl;
|
|
import com.alibaba.fastjson2.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
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.domain.entity.SysUser;
|
import com.gkhy.exam.common.enums.PrivatizeEnum;
|
import com.gkhy.exam.common.enums.QuestionTypeEnum;
|
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.common.utils.StringUtils;
|
import com.gkhy.exam.system.domain.ExExamPaper;
|
import com.gkhy.exam.system.domain.ExPaperStudent;
|
import com.gkhy.exam.system.domain.ExQuestion;
|
import com.gkhy.exam.system.domain.ExQuestionBank;
|
import com.gkhy.exam.system.mapper.ExExamPaperMapper;
|
import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
|
import com.gkhy.exam.system.mapper.ExQuestionBankMapper;
|
import com.gkhy.exam.system.mapper.ExQuestionMapper;
|
import com.gkhy.exam.system.service.ExQuestionService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* <p>
|
* 考题表 服务实现类
|
* </p>
|
*
|
* @author kzy
|
* @since 2024-06-06 13:53:17
|
*/
|
@Service
|
public class ExQuestionServiceImpl extends ServiceImpl<ExQuestionMapper, ExQuestion> implements ExQuestionService {
|
@Autowired
|
private ExPaperStudentMapper paperStudentMapper;
|
@Autowired
|
private ExExamPaperMapper examPaperMapper;
|
@Autowired
|
private ExQuestionBankMapper questionBankMapper;
|
|
@Override
|
public CommonPage selectQuestionList(ExQuestion question) {
|
if(question.getBankId()==null){
|
throw new ApiException("题库id不能为空");
|
}
|
ExQuestionBank questionBank=questionBankMapper.selectById(question.getBankId());
|
if(!questionBank.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
|
if(!question.getCompanyId().equals(currentUser.getCompanyId())){
|
throw new ApiException("无权限查看其它企业题目");
|
}
|
}
|
}
|
PageUtils.startPage();
|
List<ExQuestion> questionList=baseMapper.selectQuestionList(question);
|
return CommonPage.restPage(questionList);
|
}
|
|
@Override
|
public ExQuestion selectQuestionById(Long questionId) {
|
ExQuestion question= baseMapper.selectById(questionId);
|
if(question.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
|
return question;
|
}
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
|
return question;
|
}
|
if(!question.getCompanyId().equals(currentUser.getCompanyId())){
|
throw new ApiException("无权限查看其它企业题目");
|
}
|
return question;
|
}
|
|
@Override
|
public int insertQuestion(ExQuestion question) {
|
checkUserAllowed(question);
|
SysUser user= SecurityUtils.getLoginUser().getUser();
|
//公开的题库新增题目,题目也是公开
|
ExQuestionBank questionBank=questionBankMapper.selectById(question.getBankId());
|
if(user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())||questionBank.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
|
question.setPrivatize(PrivatizeEnum.PUBLIC.getCode());
|
}else{
|
question.setCompanyId(user.getCompanyId());
|
question.setPrivatize(PrivatizeEnum.PRIVATE.getCode());
|
}
|
validData(question);
|
int row=baseMapper.insert(question);
|
if(row<1){
|
throw new ApiException("新增题目失败");
|
}
|
return row;
|
}
|
|
@Override
|
public int updateQuestion(ExQuestion question) {
|
validData(question);
|
checkUserAllowed(question);
|
int row=baseMapper.updateById(question);
|
if(row<1){
|
throw new ApiException("编辑题目失败");
|
}
|
return row;
|
}
|
|
public void validData(ExQuestion question){
|
if(!question.getQuestionType().equals(QuestionTypeEnum.JUDGE.getCode())){
|
if(StringUtils.isBlank(question.getContent())){
|
throw new ApiException("题目内容不能为空");
|
}
|
try {
|
JSONObject jsonObject = JSONObject.parseObject(question.getContent());
|
}catch (Exception e){
|
throw new ApiException("题目内容json格式不正确");
|
}
|
//确保答案是按字母顺序排序
|
String answer=question.getAnswer();
|
if(answer.contains(",")){
|
String[] ans = answer.split(",");
|
Arrays.sort(ans);
|
question.setAnswer(String.join(",",ans));
|
}
|
}
|
|
}
|
|
public void checkUserAllowed(ExQuestion question) {
|
SysUser currentUser= SecurityUtils.getLoginUser().getUser();
|
if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
|
return;
|
}
|
if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
|
throw new ApiException("没有权限操作");
|
}
|
if(!currentUser.getCompanyId().equals(question.getCompanyId())){
|
throw new ApiException("没有权限操作其他企业题目");
|
}
|
}
|
|
@Override
|
public int deleteQuestionById(Long questionId) {
|
checkUserAllowed(baseMapper.selectById(questionId));
|
return baseMapper.deleteById(questionId);
|
}
|
|
@Override
|
public List<Map> getExerciseQuestionList(Long bankId, Integer exerciseType) {
|
return baseMapper.getExerciseQuestionList(bankId,exerciseType,SecurityUtils.getUserId());
|
}
|
|
@Override
|
public ExQuestion getExerciseQuestionById(Long questionId) {
|
return baseMapper.getExeriseQuestionById(questionId,SecurityUtils.getUserId());
|
}
|
|
@Override
|
public List<ExQuestion> getExerciseQuestionByIds(List<Long> questionIds) {
|
return baseMapper.getExeriseQuestionByIds(questionIds,SecurityUtils.getUserId());
|
}
|
|
@Override
|
public List<Map> getPaperQuestionList(Long paperId,Integer viewType) {
|
LambdaQueryWrapper<ExPaperStudent> lambdaQueryWrapper = Wrappers.<ExPaperStudent>lambdaQuery()
|
.eq(ExPaperStudent::getPaperId, paperId)
|
.eq(ExPaperStudent::getStudentId, SecurityUtils.getUserId());
|
ExPaperStudent paperStudent=paperStudentMapper.selectOne(lambdaQueryWrapper);
|
if(paperStudent==null){
|
throw new ApiException("您分配的试卷未查询到");
|
}
|
if(viewType==1){//更新考试开始时间
|
Long startTime=System.currentTimeMillis();
|
if(paperStudent.getCompleted()==1){
|
throw new ApiException("考试已完成,不能重复考试");
|
}
|
if(paperStudent.getStartTime()!=null) {//判断考卷是否已完成
|
ExExamPaper examPaper = examPaperMapper.selectById(paperStudent.getPaperId());
|
if(examPaper.getLimit()==1) {
|
Long currentDateTime = System.currentTimeMillis();
|
if (currentDateTime-paperStudent.getStartTime()>=examPaper.getLimitTime()*60*1000){
|
paperStudent.setCompleted(1);
|
paperStudentMapper.updateById(paperStudent);
|
throw new ApiException("考试已超时,不能再考试");
|
}
|
}
|
}
|
int row=paperStudentMapper.updateById(new ExPaperStudent().setPaperId(paperStudent.getPaperId()).setStudentId(paperStudent.getStudentId()).setStartTime(startTime));
|
if(row<1){
|
throw new ApiException("设置考试开始时间失败");
|
}
|
}
|
return baseMapper.getPaperQuestionList(paperId,paperStudent.getCompleted(),SecurityUtils.getUserId());
|
}
|
|
@Override
|
public ExQuestion getPaperQuestionById(Long paperId, Long questionId) {
|
LambdaQueryWrapper<ExPaperStudent> lambdaQueryWrapper = Wrappers.<ExPaperStudent>lambdaQuery()
|
.eq(ExPaperStudent::getPaperId, paperId)
|
.eq(ExPaperStudent::getStudentId, SecurityUtils.getUserId());
|
ExPaperStudent paperStudent=paperStudentMapper.selectOne(lambdaQueryWrapper);
|
if(paperStudent==null){
|
throw new ApiException("您分配的试卷未查询到");
|
}
|
return baseMapper.getPaperQuestionById(paperId,questionId,paperStudent.getCompleted(),SecurityUtils.getUserId());
|
}
|
|
@Override
|
public List<ExQuestion> getPaperQuestionByIds(Long paperId, List<Long> questionIds) {
|
LambdaQueryWrapper<ExPaperStudent> lambdaQueryWrapper = Wrappers.<ExPaperStudent>lambdaQuery()
|
.eq(ExPaperStudent::getPaperId, paperId)
|
.eq(ExPaperStudent::getStudentId, SecurityUtils.getUserId());
|
ExPaperStudent paperStudent=paperStudentMapper.selectOne(lambdaQueryWrapper);
|
if(paperStudent==null){
|
throw new ApiException("您分配的试卷未查询到");
|
}
|
return baseMapper.getPaperQuestionByIds(paperId,questionIds,paperStudent.getCompleted(),SecurityUtils.getUserId());
|
}
|
|
@Override
|
public List<Long> getExerciseErrorQuestionList(Long bankId) {
|
return baseMapper.getExerciseErrorQuestionList(bankId,SecurityUtils.getUserId());
|
}
|
}
|