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.constants.enums.module.QuestionModule; import com.qxgmat.data.constants.enums.module.QuestionNoModule; import com.qxgmat.data.dao.entity.*; import com.qxgmat.data.relation.entity.SentenceQuestionRelation; import com.qxgmat.data.relation.entity.UserRecordStatRelation; import com.qxgmat.service.UserPaperService; import com.qxgmat.service.inline.*; 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.Date; import java.util.List; @Service public class SentenceService { @Value("${paper.sentenceLength}") private Integer paperLength; @Resource private QuestionService questionService; @Resource private QuestionNoService questionNoService; @Resource private SentenceQuestionService sentenceQuestionService; @Resource private SentencePaperService sentencePaperService; @Resource private UserReportService userReportService; @Resource private UserPaperService userPaperService; @Resource private UserSentenceRecordService userSentenceRecordService; @Resource private SentenceCodeService sentenceCodeService; /** * 添加长难句题目:加入题库,关联题目,并关联长难句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.setQuestionModule(QuestionModule.SENTENCE.key); question = questionService.add(question); QuestionNo questionNo = QuestionNo.builder() .questionId(question.getId()) .title(String.format("CNG%d", relation.getNo())) .no(relation.getNo()) .module(QuestionNoModule.SENTENCE.key) .build(); questionNo = questionNoService.add(questionNo); // 绑定关系 relation.setQuestionId(question.getId()); relation.setQuestionNoId(questionNo.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()); } QuestionNo questionNo = questionNoService.get(in.getQuestionNoId()); if(relation.getTitle() != null) questionNo.setTitle(relation.getTitle()); if(relation.getNo() != null) questionNo.setNo(relation.getNo()); questionNoService.edit(questionNo); 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 createPaper(String prefixTitle, SentenceLogic logic, List questionList){ Integer no = 0; List list = new ArrayList<>(); if (questionList == null || questionList.size() == 0) return list; List questionNoList = new ArrayList<>(paperLength); List sentenceList = new ArrayList<>(paperLength); for(SentenceQuestion question : questionList){ questionNoList.add(question.getQuestionNoId()); sentenceList.add(question.getId()); if (sentenceList.size() == paperLength){ no += 1; SentencePaper paper = SentencePaper.builder() .logic(logic.key) .no(no) .questionNumber(sentenceList.size()) .status(0) .questionNoIds(questionNoList.toArray(new Integer[0])) .questionSIds(sentenceList.toArray(new Integer[0])) .build(); paper.setTitle(sentencePaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber())); sentencePaperService.add(paper); list.add(paper); questionNoList.clear(); sentenceList.clear(); } } if (sentenceList.size() > 0){ no += 1; SentencePaper paper = SentencePaper.builder() .logic(logic.key) .no(no) .questionNumber(sentenceList.size()) .status(0) .questionNoIds(questionNoList.toArray(new Integer[0])) .questionSIds(sentenceList.toArray(new Integer[0])) .build(); paper.setTitle(sentencePaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber())); paper = sentencePaperService.add(paper); list.add(paper); } return list; } /** * 切换新组卷 * @param logic * @param ids */ @Transactional public void switchPaper(SentenceLogic logic, Collection ids){ // 先将可用卷删除 List list = sentencePaperService.listByLogic(logic); Collection oldIds = Transform.getIds(list, SentencePaper.class, "id"); sentencePaperService.updatePaper(oldIds, 0); // 将新组卷启用 sentencePaperService.updatePaper(ids, 1); } /** * 累计长难句学习时间 * @param userId * @return */ public Integer studyTime(Integer userId, Date startTime, Date endTime){ UserRecordStatRelation record = userSentenceRecordService.stat(userId, startTime != null? startTime.toString():null, endTime != null ? endTime.toString(): null); return record != null ? record.getUserTime() : 0; } /** * 全站平均时间 * @return */ public Integer studyAvgTime(Date startTime, Date endTime){ UserRecordStatRelation record = userSentenceRecordService.statAvg(startTime != null? startTime.toString():null, endTime != null ? endTime.toString(): null); return record != null ? record.getUserTime() : 0; } /** * 激活长难句 * @param userId * @param code */ public void active(Integer userId, String code){ if (sentenceCodeService.isActive(userId) == null){ sentenceCodeService.active(userId, code); } } }