2025年4月19日15:02:59
parent
dab0fbe735
commit
033d68ee77
|
|
@ -5,10 +5,8 @@ import com.zhangmeng.online.exam.dao.ExamDao;
|
|||
import com.zhangmeng.online.exam.dao.PaperDao;
|
||||
import com.zhangmeng.online.exam.dao.UserDao;
|
||||
import com.zhangmeng.online.exam.dto.Result;
|
||||
import com.zhangmeng.online.exam.entity.Exam;
|
||||
import com.zhangmeng.online.exam.entity.Paper;
|
||||
import com.zhangmeng.online.exam.entity.Role;
|
||||
import com.zhangmeng.online.exam.entity.User;
|
||||
import com.zhangmeng.online.exam.entity.*;
|
||||
import com.zhangmeng.online.exam.service.ExamService;
|
||||
import com.zhangmeng.online.exam.utils.UserUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
|
@ -48,6 +46,9 @@ public class ExamController {
|
|||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
@Autowired
|
||||
private ExamService examService;
|
||||
|
||||
@RequestMapping("/user/list")
|
||||
public Result userList(String paperId, Integer pageNum, Integer pageSize) {
|
||||
|
||||
|
|
@ -229,4 +230,32 @@ public class ExamController {
|
|||
return all.get(0);
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/saveAnswer")
|
||||
public Result saveAnswer(@RequestParam @RequestBody Map<String, Object> params) {
|
||||
|
||||
String jsonData = params.get("json_data").toString();
|
||||
|
||||
try {
|
||||
List<Map> maps = JSONArray.parseArray(jsonData, Map.class);
|
||||
// 如果解析成功,继续处理 maps
|
||||
for (Map map : maps) {
|
||||
this.examService.saveAnswer(map);
|
||||
}
|
||||
|
||||
} catch (com.alibaba.fastjson2.JSONException e) {
|
||||
// 处理 jsonData 格式不正确的情况
|
||||
return Result.error("jsonData 格式不正确,请提供有效的 JSON 数据。");
|
||||
} catch (Exception e) {
|
||||
// 处理其他未知错误的情况
|
||||
return Result.error("发生未知错误,请联系系统管理员。");
|
||||
}
|
||||
|
||||
|
||||
|
||||
return Result.success(null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package com.zhangmeng.online.exam.dao;
|
||||
|
||||
import com.zhangmeng.online.exam.entity.ExamAnswer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/4/19 12:19
|
||||
* @version: 1.0
|
||||
*/
|
||||
public interface ExamAnswerDao extends JpaRepository<ExamAnswer, Long>, JpaSpecificationExecutor<ExamAnswer> {
|
||||
}
|
||||
|
|
@ -25,6 +25,9 @@ public class ExamAnswer extends BaseEntity<Long> {
|
|||
@ManyToOne
|
||||
private Exam exam;
|
||||
|
||||
@ManyToOne
|
||||
private Paper paper;//试卷
|
||||
|
||||
@ManyToOne
|
||||
private Question question;//问题
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package com.zhangmeng.online.exam.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/4/19 12:13
|
||||
* @version: 1.0
|
||||
*/
|
||||
public interface ExamService {
|
||||
void saveAnswer(Map<String, Object> params);
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
package com.zhangmeng.online.exam.service.impl;
|
||||
|
||||
import com.zhangmeng.online.exam.dao.*;
|
||||
import com.zhangmeng.online.exam.entity.*;
|
||||
import com.zhangmeng.online.exam.service.ExamService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zm
|
||||
* @date 2025/4/19 12:13
|
||||
* @version: 1.0
|
||||
*/
|
||||
@Service
|
||||
public class ExamServiceImpl implements ExamService {
|
||||
|
||||
@Autowired
|
||||
private ExamDao examDao;
|
||||
|
||||
@Autowired
|
||||
private QuestionDao questionDao;
|
||||
|
||||
@Autowired
|
||||
private QuestionOptionDao questionOptionDao;
|
||||
|
||||
@Autowired
|
||||
private PaperDao paperDao;
|
||||
|
||||
@Autowired
|
||||
private ExamAnswerDao examAnswerDao;
|
||||
|
||||
|
||||
@Override
|
||||
public void saveAnswer(Map<String, Object> map) {
|
||||
String option_id = map.get("option_id") == null? "" : (String) map.get("option_id");
|
||||
String question_id = map.get("question_id") == null? "" : (String) map.get("question_id");
|
||||
String paper_id = map.get("paper_id") == null? "" : String.valueOf( map.get("paper_id").toString());
|
||||
String answer_content = map.get("answer_content") == null? "" : (String) map.get("answer_content");
|
||||
|
||||
Paper paper = this.paperDao.getById(Long.parseLong(paper_id));
|
||||
Question question = this.questionDao.getById(Long.parseLong(question_id));
|
||||
QuestionOption questionOption = this.questionOptionDao.getById(Long.parseLong(option_id));
|
||||
|
||||
ExamAnswer examAnswer = null;
|
||||
|
||||
Specification<ExamAnswer> specification = (root, criteriaQuery, criteriaBuilder) -> {
|
||||
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
predicates.add(criteriaBuilder.equal(root.get("paper").get("id"), paper.getId()));
|
||||
predicates.add(criteriaBuilder.equal(root.get("question").get("id"), question.getId()));
|
||||
|
||||
List<Long> questionOptionIds = new ArrayList<>();
|
||||
questionOptionIds.add(questionOption.getId());// 假设这是包含所有需要查询的 questionOption 的 id 的集合
|
||||
|
||||
Join<ExamAnswer, QuestionOption> join = root.join("questionOptions");
|
||||
predicates.add(join.get("id").in(questionOptionIds));
|
||||
|
||||
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
|
||||
|
||||
};
|
||||
|
||||
List<ExamAnswer> all = this.examAnswerDao.findAll(specification);
|
||||
if (!all.isEmpty()) {
|
||||
examAnswer = all.get(0);
|
||||
} else {
|
||||
examAnswer = new ExamAnswer();
|
||||
}
|
||||
|
||||
examAnswer.setPaper(paper);
|
||||
examAnswer.setQuestion(question);
|
||||
|
||||
switch (question.getType()) {
|
||||
case SINGLE_CHOICE, JUDGMENT -> examAnswer.setQuestionOptions(List.of(questionOption));
|
||||
case MULTIPLE_CHOICE -> {
|
||||
|
||||
}
|
||||
case SHORT_ANSWER -> examAnswer.setAnswer(answer_content);
|
||||
}
|
||||
|
||||
examAnswerDao.save(examAnswer);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue