123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package com.qxgmat.service.inline;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- import com.nuliji.tools.Transform;
- import com.nuliji.tools.exception.ParameterException;
- import com.nuliji.tools.exception.SystemException;
- import com.qxgmat.data.dao.QuestionMapper;
- import com.qxgmat.data.dao.entity.Question;
- import com.qxgmat.data.dao.entity.UserCollectQuestion;
- import com.qxgmat.data.dao.entity.UserQuestion;
- import com.qxgmat.data.inline.PaperStat;
- import com.qxgmat.data.relation.QuestionRelationMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- public class QuestionService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(QuestionService.class);
- @Resource
- private QuestionMapper questionMapper;
- @Resource
- private QuestionRelationMapper questionRelationMapper;
- /**
- * 根据题目获取总试卷统计信息
- * @param questionNoList
- * @return
- */
- public PaperStat statPaper(List<Question> questionNoList){
- PaperStat stat = new PaperStat();
- Integer totalTime = 0;
- Integer totalNumber = 0;
- Integer totalCorrect = 0;
- for(Question question : questionNoList){
- totalTime += question.getTotalTime();
- totalNumber += question.getTotalNumber();
- totalCorrect += question.getTotalCorrect();
- }
- stat.setTotalCorrect(totalCorrect);
- stat.setTotalNumber(totalNumber);
- stat.setTotalTime(totalTime);
- return stat;
- }
- /**
- * 根据试卷分组获取统计信息
- * @param questionIdsMap
- * @return
- */
- public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionIdsMap){
- Map<Integer, PaperStat> relationMap = new HashMap<>();
- List<Integer> ids = new ArrayList<>();
- for(Integer[] questionIds : questionIdsMap.values()){
- ids.addAll(Arrays.stream(questionIds).collect(Collectors.toList()));
- }
- List<Question> questionList = select(ids);
- Map questionMap = Transform.getMap(questionList, Question.class, "id");
- List<Question> l = new ArrayList<>();
- for(Integer k: questionIdsMap.keySet()){
- l.clear();
- for (Integer questionNoId : questionIdsMap.get(k)){
- l.add((Question)questionMap.get(questionNoId));
- }
- relationMap.put(k, statPaper(l));
- }
- return relationMap;
- }
- /**
- * 累加做题记录到questionNo
- * @param question
- */
- public void accumulation(UserQuestion question){
- questionRelationMapper.accumulation(question.getQuestionId(), 1, question.getUserTime(), question.getIsCorrect());
- }
- /**
- * 累加做题记录到questionNo
- * @param question
- */
- public void accumulationCollect(UserCollectQuestion question, int collect){
- questionRelationMapper.accumulationCollect(question.getQuestionId(), collect);
- }
- public Question add(Question question){
- // 按实际更新更改更新时间
- if (question.getQxContent() != null && !question.getQxContent().isEmpty()){
- question.setQxTime(new Date());
- }else{
- question.setQxContent("");
- }
- if (question.getOfficialContent() != null && !question.getOfficialContent().isEmpty()){
- question.setOfficialTime(new Date());
- }else{
- question.setOfficialContent("");
- }
- if (question.getAssociationContent() != null && question.getAssociationContent().length > 0){
- question.setAssociationTime(new Date());
- }
- int result = insert(questionMapper, question);
- question = one(questionMapper, question.getId());
- if(question == null){
- throw new SystemException("题目添加失败");
- }
- return question;
- }
- public Question edit(Question question){
- Question in = one(questionMapper, question.getId());
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- // 按实际更新更改更新时间
- if (question.getQxContent() != null && (in.getQxContent() == null || !in.getQxContent().equals(question.getQxContent()))){
- question.setQxTime(new Date());
- }
- if (question.getOfficialContent() != null && (in.getOfficialContent() == null || !in.getOfficialContent().equals(question.getOfficialContent()))){
- question.setOfficialTime(new Date());
- }
- if (question.getAssociationContent() != null && (in.getAssociationContent() == null || in.getAssociationContent().length != question.getAssociationContent().length)){
- question.setAssociationTime(new Date());
- }
- int result = update(questionMapper, question);
- return question;
- }
- public boolean delete(Number id){
- Question in = one(questionMapper, id);
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- int result = delete(questionMapper, id);
- return result > 0;
- }
- public Question get(Number id){
- Question in = one(questionMapper, id);
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- return in;
- }
- public Page<Question> select(int page, int pageSize){
- return select(questionMapper, page, pageSize);
- }
- public Page<Question> select(Integer[] ids){
- return page(()->select(questionMapper, ids), 1, ids.length);
- }
- public List<Question> select(Collection ids){
- return select(questionMapper, ids);
- }
- }
|