利用Map解决复杂业务
遍历出题库表的题库名称和题库id,根据题目id即questionBankId获取
分组,即该题库题目总数,该题库题目正确数,该题库已回答题目数。
<sqltemplate id="countAnswerdQuestion"> <![CDATA[ SELECT question_bank_id , (SELECT COUNT(1) FROM edu_question WHERE question_bank_id = t.question_bank_id) totalCount , (select count(id) from edu_question_exercise where question_bank_id = t.question_bank_id and answer_status ='1' AND labour_id=:labourId) correctCount, COUNT(1) answerdCount FROM edu_question_exercise t WHERE 1=1 <#if labourId?? && labourId != '' > AND labour_id = :labourId </#if> GROUP BY question_bank_id ]]> </sqltemplate>
利用三个Map分别接收题目总数、题目正确数、已回答题目数
public Map getDetails(EduQuestionExercise eduQuestionExercise){ Map map = new HashMap(); // 岗位对应的总题库 List<EduQuestionBank> eduQuestionBanks = eduQuestionExerciseDao.queryEduQuestionBankTitle(eduQuestionExercise); // 题库已答统计 Map param = new HashMap(); param.put("labourId",eduQuestionExercise.getLabourId()); List<Map<String,Object>> answerdQuestions = eduQuestionExerciseDao.countAnswerdQuestion(param); Map<Integer, Integer> answeredMap = new HashMap<>(); // 题库id 为key , 题库已答数量为value for(Map<String,Object> answerdQuestion : answerdQuestions){ Integer questionBankId = MapUtils.getInteger(answerdQuestion, "question_bank_id"); Integer answerdCount = MapUtils.getInteger(answerdQuestion, "answerdCount"); answeredMap.put(questionBankId, answerdCount); } // 题库id 为key , 题库总题目量为value Map<Integer, Integer> totalMap = new HashMap<>(); for(Map<String,Object> answerdQuestion : answerdQuestions){ Integer questionBankId = MapUtils.getInteger(answerdQuestion, "question_bank_id"); Integer totalCount = MapUtils.getInteger(answerdQuestion, "totalCount"); totalMap.put(questionBankId, totalCount); } // 题库id 为key , 题库正确题目量为value Map<Integer, Integer> correctMap = new HashMap<>(); for(Map<String,Object> answerdQuestion : answerdQuestions){ Integer questionBankId = MapUtils.getInteger(answerdQuestion, "question_bank_id"); Integer correctCount = MapUtils.getInteger(answerdQuestion, "correctCount"); correctMap.put(questionBankId, correctCount); } // 设置题库已答数量 for(EduQuestionBank eduQuestionBank : eduQuestionBanks) { eduQuestionBank.setAnsweredCount(MapUtils.getInteger(answeredMap, eduQuestionBank.getId())!=null?MapUtils.getInteger(answeredMap, eduQuestionBank.getId()):0); eduQuestionBank.setTotalCount(MapUtils.getInteger(totalMap,eduQuestionBank.getId())!=null?MapUtils.getInteger(totalMap,eduQuestionBank.getId()):0); eduQuestionBank.setCorrectCount(MapUtils.getInteger(correctMap,eduQuestionBank.getId())!=null?MapUtils.getInteger(correctMap,eduQuestionBank.getId()):0); if(eduQuestionBank.getTotalCount()!=0&&eduQuestionBank.getTotalCount()!=null&& eduQuestionBank.getCorrectCount()!=0&&eduQuestionBank.getCorrectCount()!=null ) { Integer correctCount = eduQuestionBank.getCorrectCount(); Integer totalCount = eduQuestionBank.getTotalCount(); Long correctCount1 = Long.valueOf(correctCount); Long totalCount1 = Long.valueOf(totalCount); BigDecimal correctCount2 = BigDecimal.valueOf(correctCount1); BigDecimal totalCount2 = BigDecimal.valueOf(totalCount1); BigDecimal accuracy = correctCount2.divide(totalCount2,2,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)); eduQuestionBank.setAccuracy(accuracy.intValue()); }else{ eduQuestionBank.setAccuracy(0); } } map.put("list",eduQuestionBanks); return map; }