123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package com.qxgmat.service.extend;
- import com.nuliji.tools.Transform;
- import com.nuliji.tools.exception.ParameterException;
- import com.qxgmat.data.constants.enums.QuestionType;
- import com.qxgmat.data.constants.enums.logic.SentenceLogic;
- import com.qxgmat.data.dao.entity.Question;
- import com.qxgmat.data.dao.entity.SentencePaper;
- import com.qxgmat.data.dao.entity.SentenceQuestion;
- import com.qxgmat.data.relation.entity.SentenceQuestionRelation;
- import com.qxgmat.service.inline.SentencePaperService;
- import com.qxgmat.service.inline.QuestionService;
- import com.qxgmat.service.inline.SentenceQuestionService;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- @Service
- public class SentenceService {
- @Value("${paper.sentenceLength}")
- private Integer paperLength;
- @Resource
- private QuestionService questionService;
- @Resource
- private SentenceQuestionService sentenceQuestionService;
- @Resource
- private SentencePaperService sentencePaperService;
- /**
- * 添加长难句题目:加入题库,关联题目,并关联长难句paper
- * @param relation
- * @return
- */
- @Transactional
- public SentenceQuestion addQuestion(SentenceQuestionRelation relation){
- SentenceQuestion in = sentenceQuestionService.getByNo(relation.getNo());
- if (in != null){
- throw new ParameterException("序号已经存在");
- }
- Question question = relation.getQuestion();
- question.setQuestionType(QuestionType.SENTENCE.key);
- question = questionService.add(question);
- // 绑定关系
- relation.setQuestionId(question.getId());
- relation.setSubject(String.format("CNG%d", relation.getNo()));
- SentenceQuestion sentenceQuestion = sentenceQuestionService.add(relation);
- // 绑定关系:统一组卷
- // addQuestionToPaperWithNo(sentenceQuestion);
- //
- // if (sentenceQuestion.getIsTrail() > 0){
- // addQuestionToPaperWithTrail(sentenceQuestion);
- // }
- return sentenceQuestion;
- }
- /**
- * 更新长难句题目:更新题库,,变更长难句paper
- * @param relation
- * @return
- */
- @Transactional
- public SentenceQuestion editQuestion(SentenceQuestionRelation relation){
- SentenceQuestion in = sentenceQuestionService.getByNo(relation.getNo());
- if (in != null && !in.getId().equals(relation.getId())){
- throw new ParameterException("序号已经存在");
- }
- Question question = relation.getQuestion();
- question.setQuestionType(QuestionType.SENTENCE.key);
- question = questionService.edit(question);
- if(in == null){
- in = sentenceQuestionService.get(relation.getId());
- }
- relation.setSubject(String.format("CNG%d", relation.getNo()));
- // 根据序号调整分组:移出原有关系 - 绑定关系
- // if (!in.getNo().equals(relation.getNo())){
- // removeQuestionFromPaper(in, SentenceLogic.NO);
- // addQuestionToPaperWithNo(relation);
- // }
- // 根据试用调整
- // if (!in.getIsTrail().equals(relation.getIsTrail())){
- // if (relation.getIsTrail() > 0){
- // addQuestionToPaperWithTrail(relation);
- // }else{
- // removeQuestionFromPaper(in, SentenceLogic.TRAIL);
- // }
- // }
- SentenceQuestion sentenceQuestion = sentenceQuestionService.edit(relation);
- return sentenceQuestion;
- }
- /**
- * 根据题目创建组卷序列
- * @param logic
- * @param questionList
- * @return
- */
- @Transactional
- public List<SentencePaper> createPaper(String prefixTitle, SentenceLogic logic, List<SentenceQuestion> questionList){
- Integer no = 0;
- List<SentencePaper> list = new ArrayList<>();
- List<Integer> tmp = new ArrayList<>(paperLength);
- for(SentenceQuestion question : questionList){
- tmp.add(question.getId());
- if (tmp.size() == paperLength){
- no += 1;
- SentencePaper paper = sentencePaperService.add(SentencePaper.builder()
- .logic(logic.key)
- .no(no)
- .questionNumber(tmp.size())
- .status(0)
- .questionNoIds(tmp.toArray(new Integer[0])).build()
- );
- paper.setTitle(sentencePaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber()));
- list.add(paper);
- tmp.clear();
- }
- }
- if (tmp.size() > 0){
- no += 1;
- SentencePaper paper = sentencePaperService.add(SentencePaper.builder()
- .logic(logic.key)
- .no(no)
- .questionNumber(tmp.size())
- .status(0)
- .questionNoIds(tmp.toArray(new Integer[0])).build()
- );
- paper.setTitle(sentencePaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber()));
- list.add(paper);
- }
- return list;
- }
- /**
- * 切换新组卷
- * @param logic
- * @param ids
- */
- @Transactional
- public void switchPaper(SentenceLogic logic, Collection ids){
- // 先将可用卷删除
- List<SentencePaper> list = sentencePaperService.listByLogic(logic);
- Collection oldIds = Transform.getIds(list, SentencePaper.class, "id");
- sentencePaperService.updatePaper(oldIds, 0);
- // 将新组卷启用
- sentencePaperService.updatePaper(ids, 1);
- }
- }
|