|
@@ -0,0 +1,154 @@
|
|
|
+package cn.com.qmth.examcloud.core.questions.service.impl;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLog;
|
|
|
+import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLogFactory;
|
|
|
+import cn.com.qmth.examcloud.commons.web.security.bean.User;
|
|
|
+import cn.com.qmth.examcloud.core.questions.api.bean.PaperK12Bean;
|
|
|
+import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
|
|
|
+import cn.com.qmth.examcloud.core.questions.base.enums.PaperStatus;
|
|
|
+import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.PaperDetailRepo;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.PaperDetailUnitRepo;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.PaperRepo;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.QuesRepo;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.entity.Course;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetail;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
|
|
|
+import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
|
|
|
+import cn.com.qmth.examcloud.core.questions.service.PaperProviderService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author weiwenhai
|
|
|
+ * @date 2018.10.8
|
|
|
+ * @company qmth
|
|
|
+ * @description paperProviderServiceImpl
|
|
|
+ * @code 014
|
|
|
+ */
|
|
|
+@Service("paperProviderServiceImpl")
|
|
|
+public class PaperProviderServiceImpl implements PaperProviderService{
|
|
|
+
|
|
|
+ protected ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QuesRepo quesRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ PaperRepo paperRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ PaperDetailRepo paperDetailRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ PaperDetailUnitRepo paperDetailUnitRepo;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String genPaper(Set<String> questionIds,Map<String, PaperK12Bean> map,String paperName,User user) {
|
|
|
+ if(questionIds == null || questionIds.size()<1){
|
|
|
+ throw new StatusException("Q-014038", "questionIds is null");
|
|
|
+ }
|
|
|
+ if(map == null || map.size()<1){
|
|
|
+ throw new StatusException("Q-014041", "map is null");
|
|
|
+ }
|
|
|
+ //查询所有试题集合
|
|
|
+ List<Question> questions = quesRepo.findByIdIn(questionIds);
|
|
|
+ if(CollectionUtils.isEmpty(questions)){
|
|
|
+ log.error("根据试题id的集合没有查询到试题结合");
|
|
|
+ throw new StatusException("Q-014041", "根据试题id的集合没有查询到试题结合");
|
|
|
+ }
|
|
|
+ //构建试卷对象
|
|
|
+ Paper paper = initPaper(paperName,user);
|
|
|
+ //构建大题对象,未设置
|
|
|
+ PaperDetail paperDetail = initPaperDetail(paper);
|
|
|
+ //构建包装试题对象
|
|
|
+ List<PaperDetailUnit> paperDetailUnits = buildUnits(paper,paperDetail,questions,map);
|
|
|
+ paper = paperRepo.save(paper);
|
|
|
+ paperDetailRepo.save(paperDetail);
|
|
|
+ quesRepo.save(questions);
|
|
|
+ paperDetailUnitRepo.save(paperDetailUnits);
|
|
|
+ return paper.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建大题对象
|
|
|
+ * @param paper
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private PaperDetail initPaperDetail(Paper paper) {
|
|
|
+ PaperDetail paperDetail = new PaperDetail();
|
|
|
+ paperDetail.setPaper(paper);
|
|
|
+ paperDetail.setName("默认大题");
|
|
|
+ paperDetail.setNumber(1);
|
|
|
+ paperDetail.setCreator(paper.getCreator());
|
|
|
+ return paperDetail;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建试卷对象
|
|
|
+ * @param paperName
|
|
|
+ * @param user
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Paper initPaper(String paperName, User user) {
|
|
|
+ Paper paper = new Paper();
|
|
|
+ paper.setName(paperName);
|
|
|
+ paper.setTitle(paperName);
|
|
|
+ paper.setPaperType(PaperType.GENERATE);
|
|
|
+ paper.setOrgId(user.getRootOrgId().toString());
|
|
|
+ paper.setCreator(user.getDisplayName());
|
|
|
+ paper.setCreateTime(CommonUtils.getCurDateTime());
|
|
|
+ return paper;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建题包装器
|
|
|
+ * @param questions
|
|
|
+ * @param map
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<PaperDetailUnit> buildUnits(Paper paper, PaperDetail paperDetail,List<Question> questions, Map<String, PaperK12Bean> map) {
|
|
|
+ List<PaperDetailUnit> paperDetailUnits = new ArrayList<PaperDetailUnit>();
|
|
|
+ int j = 0;
|
|
|
+ double score = 0;
|
|
|
+ double sum = 0;
|
|
|
+ for(int i=0;i<questions.size();i++){
|
|
|
+ PaperDetailUnit paperDetailUnit = new PaperDetailUnit();
|
|
|
+ paperDetailUnit.setNumber(i+1);
|
|
|
+ paperDetailUnit.setQuestion(questions.get(i));
|
|
|
+ paperDetailUnit.setPaperDetail(paperDetail);
|
|
|
+ paperDetailUnit.setQuestionType(questions.get(i).getQuestionType());
|
|
|
+ paperDetailUnit.setCreator(paper.getCreator());
|
|
|
+ paperDetailUnit.setPaper(paper);
|
|
|
+ paperDetailUnit.setPaperType(PaperType.GENERATE);
|
|
|
+ if(!StringUtils.isBlank(map.get(questions.get(i).getId()).getScore()+"")){
|
|
|
+ paperDetailUnit.setScore(map.get(questions.get(i).getId()).getScore());
|
|
|
+ }
|
|
|
+ if(!StringUtils.isBlank(map.get(questions.get(i).getId()).getTimeLimit()+"")){
|
|
|
+ paperDetailUnit.setTimeLimit(map.get(questions.get(i).getId()).getTimeLimit());
|
|
|
+ }
|
|
|
+ j++;
|
|
|
+ score = map.get(questions.get(i).getId()).getScore() + score;
|
|
|
+ sum = paperDetailUnit.getScore() * paperDetailUnit.getQuestion().getDifficultyDegree()/10 + sum;
|
|
|
+ }
|
|
|
+ paperDetail.setUnitCount(j);
|
|
|
+ paperDetail.setScore(score);
|
|
|
+ BigDecimal b = new BigDecimal(sum / score);
|
|
|
+ Double difficulty = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
+ paper.setTotalScore(score);
|
|
|
+ paper.setDifficultyDegree(difficulty);
|
|
|
+ return paperDetailUnits;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|