1、查看用户的答题情况
问卷:里面有题库ids(多个题库)
题库:循环遍历每个题库
问题:FId与题库关联
选项:FId与问题关联 , is_right代表正确答案
do_eval_question: id,问卷id,用户id
eval_do_question_detail:id,问卷问题Id,问题答案。
原本查询:
1、用doInvestigateId查到数据
2、 把题库分开,然后一个题库一个题库处理(循环)
3、根据题库id找到对应问题的FId,查到题目列表(循环)
4、遍历题目列表(循环)
5、获取到问题人做该题的细节(通过传入做问卷的id、问题id)
6、循环遍历该问题的选项,然后与5中获取到的对象所选这个题目的选项进行对比,如果一样,把isChoose设置为真(isChoose只在实体类中出现,表中没有的,因为我们只是要返回一个结果集。)
7、就把更新的数据(先是选项集,更新到题库;然后题库集更新到文件集)
然后返回问卷集也是结果集。
查问卷1次
每个查题库1次(构建查询条件,直接一次性查到)
题库里面的每个问题要查一次选项(有多少问题就得查多少次)
答题人每个问题的选项都得查一遍(问题次数,有多少个问题就得查多少次)
优化
private QuestionResult genQuestionResult(QuestionResult questionResult, Long DoInvestigateId, Long id) {
// 获取问卷信息
EvalInvestigate ei = evalInvestigateMapper.selectEvalInvestigateById(DoInvestigateId);
if (ei == null)
throw new NullPointerException("DoInvestigateId=" + DoInvestigateId + "的问卷不存在");
// 获取问卷的题库集
String[] splits = ei.getInvParperIds().split("\\.");
List<EvalPaper> papers = new ArrayList<>();
for (String s : splits) {
Long i = Long.parseLong(s);
// 获取题库集里的一份题库
EvalPaper evalPaper = evalPaperMapper.selectEvalPaperById(i);
// 一次性查询题库里面的所有问题和答案
List<EvalQu> evalQus = evalQuMapper.selectEvalQuListByPaperId(evalPaper.getId());
List<EvalQuAnswer> evalQuAnswers = evalQuAnswerMapper.selectEvalQuAnswersByQuestionIds(
evalQus.stream().map(EvalQu::getId).collect(Collectors.toList())
);
List<EvalDoQuestionDetail> evalDoQuestionDetails = evalDoQuestionDetailMapper.selectEvalDoQuestionDetailsByQuestionIds(
evalQus.stream().map(EvalQu::getId).collect(Collectors.toList()), id
);
// 将问题和答案进行匹配
//遍历问题
for (EvalQu evalQu : evalQus) {
//获得问题的答案集
List<EvalQuAnswer> answersForQuestion = evalQuAnswers.stream()
.filter(answer -> answer.getFid().equals(evalQu.getId()))
.collect(Collectors.toList());
//遍历答案集(答案集*选项个数)
for (EvalDoQuestionDetail questionDetail : evalDoQuestionDetails) {
//答案跟每个每个选项集对比
for (EvalQuAnswer quAnswer : answersForQuestion) {
if (quAnswer.getId().equals(questionDetail.getEvalDetailAnsId())) {
quAnswer.setChoose(true);
}
}
}
evalQu.setEvalQuAnswerList(answersForQuestion);
}
evalPaper.setQuestionList(evalQus);
papers.add(evalPaper);
}
questionResult.setEvalPaperList(papers);
return questionResult;
}
(在evalPaper题库里面加个存放所有问题的List)
(问题跟选项做成视图)
1、一次性查询所有题库里的问题和答案(将题库获得题库里面所有id的List<id>)
2、selectEvalQuAnswersByQuestionIds()
public interface EvalQuAnswerMapper {
List<EvalQuAnswer> selectEvalQuAnswersByQuestionIds(List<Long> questionIds);
}
<select id="selectEvalQuAnswersByQuestionIds" parameterType="java.util.List" resultType="com.example.EvalQuAnswer">
SELECT * FROM eval_qu_answer WHERE fid IN
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
3、查询答案集
public interface EvalDoQuestionDetailMapper {
List<EvalDoQuestionDetail> selectEvalDoQuestionDetailsByQuestionIds(List<Long> questionIds, Long Id);
}
<select id="selectEvalDoQuestionDetailsByQuestionIds" parameterType="java.util.Map" resultType="com.example.EvalDoQuestionDetail">
SELECT * FROM eval_do_question_detail
WHERE eval_main_que_id = #{Id}
AND eval_detail_que_id IN
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
4、遍历问题集
遍历答案集,每个答案都要跟问题的所有选项对比。
5、可以继续优化
for (EvalQu evalQu : evalQus) { Long evalQuId = evalQu.getId(); List<EvalQuAnswer> answersForQuestion = evalQuAnswers.stream() .filter(answer -> answer.getFid().equals(evalQuId)) .collect(Collectors.toList()); List<Long> evalDetailAnsIds = evalDoQuestionDetails.stream() .map(EvalDoQuestionDetail::getEvalDetailAnsId) .collect(Collectors.toList()); for (EvalQuAnswer quAnswer : answersForQuestion) { quAnswer.setChoose(evalDetailAnsIds.contains(quAnswer.getId())); } evalQu.setEvalQuAnswerList(answersForQuestion); }