123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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.nuliji.tools.mybatis.Example;
- import com.qxgmat.data.dao.SentenceQuestionMapper;
- import com.qxgmat.data.dao.entity.Manager;
- import com.qxgmat.data.dao.entity.Question;
- import com.qxgmat.data.dao.entity.SentenceQuestion;
- import com.qxgmat.data.dao.entity.UserQuestion;
- import com.qxgmat.data.inline.PaperStat;
- import com.qxgmat.data.relation.SentenceQuestionRelationMapper;
- import com.qxgmat.data.relation.entity.SentenceQuestionRelation;
- 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 SentenceQuestionService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(SentenceQuestionService.class);
- @Resource
- private SentenceQuestionMapper sentenceQuestionMapper;
- @Resource
- private QuestionService questionService;
- @Resource
- private SentenceQuestionRelationMapper sentenceQuestionRelationMapper;
-
- public Page<SentenceQuestion> searchAdmin(int page, int size, String keyword){
- Example example = new Example(SentenceQuestion.class);
- if(keyword != null)
- example.and(
- example.createCriteria()
- .orLike("title", "%"+keyword+"%")
- .orLike("subject", "%"+keyword+"%")
- );
- return page(() -> select(sentenceQuestionMapper, example), page, size);
- }
-
- public List<SentenceQuestionRelation> listWithRelationByIds(Number[] ids){
- List<SentenceQuestion> p = select(sentenceQuestionMapper, ids);
- return relation(p);
- }
-
- public Map<Number, SentenceQuestionRelation> mapWithRelationByIds(Number[] ids){
- List<SentenceQuestion> p = select(sentenceQuestionMapper, ids);
- List<SentenceQuestionRelation> list = relation(p);
- Map<Number, SentenceQuestionRelation> map = new HashMap<>();
- for(SentenceQuestionRelation relation : list){
- map.put(relation.getId(), relation);
- }
- return map;
- }
-
- public void accumulation(UserQuestion question){
- sentenceQuestionRelationMapper.accumulation(question.getQuestionNoId(), 1, question.getTime(), question.getIsCorrect());
- }
-
- public PaperStat statPaper(List<SentenceQuestion> questionNoList){
- PaperStat stat = new PaperStat();
- Integer totalTime = 0;
- Integer totalNumber = 0;
- Integer totalCorrect = 0;
- for(SentenceQuestion questionNo : questionNoList){
- totalTime += questionNo.getTotalTime();
- totalNumber += questionNo.getTotalNumber();
- totalCorrect += questionNo.getTotalCorrect();
- }
- stat.setTotalCorrect(totalCorrect);
- stat.setTotalNumber(totalNumber);
- stat.setTotalTime(totalTime);
- return stat;
- }
-
- public Map<Integer, PaperStat> statPaperMap(Map<Integer, Integer[]> questionNoIdsMap){
- Map<Integer, PaperStat> relationMap = new HashMap<>();
- List<Integer> ids = new ArrayList<>();
- for(Integer[] questionNoIds : questionNoIdsMap.values()){
- ids.addAll(Arrays.stream(questionNoIds).collect(Collectors.toList()));
- }
- List<SentenceQuestion> questionNoList = select(ids);
- Map questionNoMap = Transform.getMap(questionNoList, SentenceQuestion.class, "id");
- List<SentenceQuestion> l = new ArrayList<>();
- for(Integer k: questionNoIdsMap.keySet()){
- l.clear();
- for (Integer questionNoId : questionNoIdsMap.get(k)){
- l.add((SentenceQuestion)questionNoMap.get(questionNoId));
- }
- relationMap.put(k, statPaper(l));
- }
- return relationMap;
- }
-
- public List<SentenceQuestionRelation> relation(List<SentenceQuestion> p){
- List<SentenceQuestionRelation> relationList = Transform.convert(p, SentenceQuestionRelation.class);
- Collection questionIds = Transform.getIds(p, SentenceQuestion.class, "questionId");
- List<Question> questions = questionService.select(questionIds);
- Transform.combine(relationList, questions, SentenceQuestionRelation.class, "questionId", "question", Question.class, "id");
- return relationList;
- }
-
- public SentenceQuestionRelation relation(SentenceQuestion p){
- SentenceQuestionRelation relation = Transform.convert(p, SentenceQuestionRelation.class);
- Question question = questionService.get(p.getQuestionId());
- relation.setQuestion(question);
- return relation;
- }
-
- public SentenceQuestion getByNo(Integer no){
- Example example = new Example(SentenceQuestion.class);
- example.and(
- example.createCriteria()
- .andEqualTo("no", no)
- );
- return one(sentenceQuestionMapper, example);
- }
-
- public List<SentenceQuestion> allPaper(){
- Example example = new Example(SentenceQuestion.class);
- example.and(
- example.createCriteria()
- .andEqualTo("isPaper", 1)
- );
- example.orderBy("no").asc();
- return select(sentenceQuestionMapper, example);
- }
- public SentenceQuestion add(SentenceQuestion question){
- int result = insert(sentenceQuestionMapper, question);
- question = one(sentenceQuestionMapper, question.getId());
- if(question == null){
- throw new SystemException("题目添加失败");
- }
- return question;
- }
- public SentenceQuestion edit(SentenceQuestion question){
- SentenceQuestion in = one(sentenceQuestionMapper, question.getId());
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- int result = update(sentenceQuestionMapper, question);
- return question;
- }
- public boolean delete(Number id){
- SentenceQuestion in = one(sentenceQuestionMapper, id);
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- int result = delete(sentenceQuestionMapper, id);
- return result > 0;
- }
- public SentenceQuestion get(Number id){
- SentenceQuestion in = one(sentenceQuestionMapper, id);
- if(in == null){
- throw new ParameterException("题目不存在");
- }
- return in;
- }
- public Page<SentenceQuestion> select(int page, int pageSize){
- return select(sentenceQuestionMapper, page, pageSize);
- }
- public Page<SentenceQuestion> select(Integer[] ids){
- return page(()-> select(sentenceQuestionMapper, ids), 1, ids.length);
- }
- public List<SentenceQuestion> select(Collection ids){
- return select(sentenceQuestionMapper, ids);
- }
- }
|