|
@@ -1,28 +1,37 @@
|
|
|
package com.ynfy.buss.practice.userpractice.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
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;
|
|
|
-import com.ynfy.buss.exam.exam.dto.ExamSubmitDTO;
|
|
|
+import com.ynfy.buss.exam.exam.dto.ExamAnswerDTO;
|
|
|
+import com.ynfy.buss.exam.question.enums.QuestionType;
|
|
|
import com.ynfy.buss.practice.userpractice.entity.UserPractice;
|
|
|
+import com.ynfy.buss.practice.userpractice.entity.dto.PracticeQuestionDTO;
|
|
|
+import com.ynfy.buss.practice.userpractice.entity.dto.UserPracticeDTO;
|
|
|
import com.ynfy.buss.practice.userpractice.mapper.UserPracticeMapper;
|
|
|
import com.ynfy.buss.practice.userpractice.service.IUserPracticeService;
|
|
|
+import com.ynfy.buss.practice.userpracticequestion.entity.UserPracticeQuestion;
|
|
|
+import com.ynfy.buss.practice.userpracticequestion.service.IUserPracticeQuestionService;
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
+import org.jeecg.common.exception.JeecgBootException;
|
|
|
import org.jeecg.common.system.vo.LoginUser;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
-/**
|
|
|
- * @Description: 用户练习表
|
|
|
- * @Author: jeecg-boot
|
|
|
- * @Date: 2023-11-19
|
|
|
- * @Version: V1.0
|
|
|
- */
|
|
|
@Service
|
|
|
public class UserPracticeServiceImpl extends ServiceImpl<UserPracticeMapper, UserPractice> implements IUserPracticeService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IUserPracticeQuestionService userPracticeQuestionService;
|
|
|
+
|
|
|
/**
|
|
|
* 获取用户最近的练习
|
|
|
*
|
|
@@ -41,7 +50,53 @@ public class UserPracticeServiceImpl extends ServiceImpl<UserPracticeMapper, Use
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void submitPractice(ExamSubmitDTO dto) {
|
|
|
-
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void submitPractice(UserPracticeDTO dto) {
|
|
|
+ if (CollectionUtils.isEmpty(dto.getQuestionList())) {
|
|
|
+ throw new JeecgBootException("练习题目为空");
|
|
|
+ }
|
|
|
+ UserPractice userPractice = new UserPractice();
|
|
|
+ LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
|
|
+ userPractice.setUserId(user.getId());
|
|
|
+ userPractice.setType(1);
|
|
|
+ userPractice.setRepositoryId(dto.getRepositoryId());
|
|
|
+ userPractice.setMode(dto.getMode());
|
|
|
+ userPractice.setQuestionType(dto.getQuestionType());
|
|
|
+ List<PracticeQuestionDTO> questionList = dto.getQuestionList();
|
|
|
+ Long count = questionList.stream().filter(q -> !Objects.isNull(q.getIsRight()) && q.getIsRight()).count();
|
|
|
+ userPractice.setRightNumber(count.intValue());
|
|
|
+ userPractice.setTotalNumber(questionList.size());
|
|
|
+ //正确率
|
|
|
+ userPractice.setAccuracy(NumberUtil.div(NumberUtil.mul(count.intValue(), 100), questionList.size(), 2));
|
|
|
+ save(userPractice);
|
|
|
+ String userPracticeId = userPractice.getId();
|
|
|
+ List<ExamAnswerDTO> answerList = dto.getAnswers();
|
|
|
+ int sort = 1;
|
|
|
+ List<UserPracticeQuestion> userPracticeQuestionList = new ArrayList<>();
|
|
|
+ for (PracticeQuestionDTO item : questionList) {
|
|
|
+ UserPracticeQuestion upq = new UserPracticeQuestion();
|
|
|
+ upq.setUserPracticeId(userPracticeId);
|
|
|
+ upq.setQuestionId(item.getId());
|
|
|
+ upq.setQuestionType(item.getType());
|
|
|
+ upq.setAnswered(item.getAnswered());
|
|
|
+ upq.setIsRight(item.getIsRight());
|
|
|
+ upq.setSort(sort);
|
|
|
+ ExamAnswerDTO answerDTO = answerList.stream().filter(a -> a.getIndex().equals(item.getQuestionIndex())).findFirst().orElse(null);
|
|
|
+ if (!Objects.isNull(answerDTO)) {
|
|
|
+ String[] answers = answerDTO.getAnswers();
|
|
|
+ if (!item.getType().equals(QuestionType.BLANK.getCode())) { //单选,多选,判断
|
|
|
+ if (!Objects.isNull(answers) && answers.length > 0) {
|
|
|
+ upq.setAnswer(String.join(",", answers));
|
|
|
+ }
|
|
|
+ } else {//填空
|
|
|
+ upq.setAnswer(answerDTO.getBlankAnswer());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userPracticeQuestionList.add(upq);
|
|
|
+ sort++;
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(userPracticeQuestionList)) {
|
|
|
+ userPracticeQuestionService.saveBatch(userPracticeQuestionList);
|
|
|
+ }
|
|
|
}
|
|
|
}
|