123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- package com.qxgmat.service.extend;
- import com.nuliji.tools.Tools;
- import com.nuliji.tools.Transform;
- import com.qxgmat.data.constants.enums.ServiceKey;
- import com.qxgmat.data.constants.enums.TextbookSubject;
- import com.qxgmat.data.constants.enums.logic.SentenceLogic;
- import com.qxgmat.data.constants.enums.logic.TextbookLogic;
- 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.TextbookQuestionRelation;
- import com.qxgmat.service.UserServiceService;
- 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.text.SimpleDateFormat;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- public class TextbookService {
- SimpleDateFormat stringToDate = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat dateToString = new SimpleDateFormat("MM月dd日");
- SimpleDateFormat dateToDay = new SimpleDateFormat("dd日");
- @Value("${paper.textbookLength}")
- private Integer paperLength;
- @Resource
- private TextbookPaperService textbookPaperService;
- @Resource
- private QuestionService questionService;
- @Resource
- private QuestionNoService questionNoService;
- @Resource
- private TextbookQuestionService textbookQuestionService;
- @Resource
- private TextbookLibraryService textbookLibraryService;
- @Resource
- private UserTextbookEnrollService userTextbookEnrollService;
- @Resource
- private TextbookTopicService textbookTopicService;
- @Resource
- private UserServiceService userServiceService;
- /**
- * 报名
- * @param userId
- * @param date
- */
- @Transactional
- public void enroll(Integer userId, Date date){
- UserTextbookEnroll in = userTextbookEnrollService.getByUser(userId);
- if (in != null){
- return;
- }
- userTextbookEnrollService.add(UserTextbookEnroll.builder()
- .userId(userId)
- .date(date)
- .build());
- }
- /**
- * 取消报名
- * @param userId
- */
- @Transactional
- public void unEnroll(Integer userId){
- UserTextbookEnroll in = userTextbookEnrollService.getByUser(userId);
- if (in == null){
- return;
- }
- userTextbookEnrollService.delete(in.getId());
- }
- /**
- * 添加新一期的换库
- * @param entity
- * @return
- */
- @Transactional
- public TextbookLibrary replace(TextbookLibrary entity){
- TextbookLibrary last = textbookLibraryService.getLatest();
- if (last != null){
- // 将最后一期设定库尾
- last.setEndDate(entity.getStartDate());
- textbookLibraryService.edit(last);
- String prefixTitle = generatePrefixTitle(last);
- textbookPaperService.updateTitle(last.getId(), prefixTitle, paperLength);
- textbookQuestionService.updateTitle(last.getId(), prefixTitle);
- }
- return textbookLibraryService.add(entity);
- }
- /**
- * 添加机经题目:加入题库,关联题目,并关联机经paper
- * @param relation
- * @return
- */
- @Transactional
- public TextbookQuestion addQuestion(TextbookQuestionRelation relation){
- Question question = relation.getQuestion();
- question.setQuestionModule(QuestionModule.TEXTBOOK.key);
- question = questionService.add(question);
- QuestionNo questionNo = QuestionNo.builder()
- .questionId(question.getId())
- .module(QuestionNoModule.TEXTBOOK.key)
- .no(relation.getNo())
- .title(relation.getTitle())
- .build();
- questionNo = questionNoService.add(questionNo);
- // 绑定关系
- relation.setQuestionId(question.getId());
- relation.setQuestionNoId(questionNo.getId());
- TextbookLibrary library = textbookLibraryService.get(relation.getLibraryId());
- String prefixTitle = generatePrefixTitle(library);
- relation.setTitle(textbookQuestionService.generateTitle(prefixTitle, relation.getNo()));
- // 保存年份
- relation.setYear(String.valueOf(Tools.yearNumber(library.getStartDate())));
- TextbookQuestion textbookQuestion = textbookQuestionService.add(relation);
- // 绑定关系
- if (question.getQuestionType().equals(TextbookLogic.DS.key)){
- addQuestionToPaper(library, textbookQuestion, TextbookLogic.DS);
- addQuestionToPaper(library, textbookQuestion, TextbookLogic.DS_PS);
- }else if (question.getQuestionType().equals(TextbookLogic.PS.key)){
- addQuestionToPaper(library, textbookQuestion, TextbookLogic.PS);
- addQuestionToPaper(library, textbookQuestion, TextbookLogic.DS_PS);
- }
- textbookQuestionService.edit(textbookQuestion);
- return textbookQuestion;
- }
- /**
- * 更新机经题目:更新题库
- * @param relation
- * @return
- */
- @Transactional
- public TextbookQuestion editQuestion(TextbookQuestionRelation relation){
- TextbookQuestion textbookQuestion = textbookQuestionService.get(relation.getId());
- Question question = relation.getQuestion();
- question = questionService.edit(question);
- QuestionNo questionNo = questionNoService.get(textbookQuestion.getQuestionNoId());
- if (relation.getTitle() != null) questionNo.setTitle(relation.getTitle());
- if (relation.getNo() != null) questionNo.setNo(relation.getNo());
- questionNo = questionNoService.edit(questionNo);
- textbookQuestion = textbookQuestionService.edit(relation);
- return textbookQuestion;
- }
- /**
- * 更新换库表题目数
- * @param libraryId
- * @param subject
- */
- public void updateLibraryNo(Integer libraryId, String subject){
- TextbookTopic last = textbookTopicService.lastByLibrary(libraryId,subject);
- TextbookLibrary library = textbookLibraryService.get(last.getLibraryId());
- TextbookLibrary tmp = TextbookLibrary.builder()
- .id(library.getId())
- .build();
- switch(TextbookSubject.ValueOf(subject)){
- case QUANT:
- if (!last.getNo().equals(library.getQuantNumber())){
- tmp.setQuantNumber(last.getNo());
- textbookLibraryService.edit(tmp);
- }
- break;
- case IR:
- if (!last.getNo().equals(library.getIrNumber())){
- tmp.setIrNumber(last.getNo());
- textbookLibraryService.edit(tmp);
- }
- break;
- case RC:
- if (!last.getNo().equals(library.getRcNumber())){
- tmp.setRcNumber(last.getNo());
- textbookLibraryService.edit(tmp);
- }
- break;
- }
- }
- /**
- * 根据用户权限更新资源信息
- * @param user
- * @param textbookLibrary
- */
- public void refreshLibraryResource(User user, TextbookLibrary textbookLibrary){
- // 处理权限
- if (user != null){
- if (!userServiceService.hasService(user.getId(), ServiceKey.TEXTBOOK)){
- // 移除数据
- textbookLibrary.setRc("");
- textbookLibrary.setIr("");
- textbookLibrary.setQuant("");
- }else{
- // 替换为水印地址
- textbookLibrary.setRc("/api/my/download/textbook?subject=rc");
- textbookLibrary.setIr("/api/my/download/textbook?subject=ir");
- textbookLibrary.setQuant("/api/my/download/textbook?subject=quant");
- }
- }else{
- textbookLibrary.setRc("");
- textbookLibrary.setIr("");
- textbookLibrary.setQuant("");
- }
- }
- /**
- * 根据用户权限更新资源信息
- * @param user
- * @param textbookLibraryList
- */
- public void refreshLibraryResource(User user, List<TextbookLibrary> textbookLibraryList){
- // 处理权限
- if (user != null){
- if (!userServiceService.hasService(user.getId(), ServiceKey.TEXTBOOK)) {
- for (TextbookLibrary textbookLibrary : textbookLibraryList) {
- textbookLibrary.setRc("");
- textbookLibrary.setIr("");
- textbookLibrary.setQuant("");
- }
- }else{
- for (TextbookLibrary textbookLibrary : textbookLibraryList) {
- // 替换为水印地址
- textbookLibrary.setRc("/api/my/download/textbook?subject=rc");
- textbookLibrary.setIr("/api/my/download/textbook?subject=ir");
- textbookLibrary.setQuant("/api/my/download/textbook?subject=quant");
- }
- }
- }else{
- for(TextbookLibrary textbookLibrary : textbookLibraryList){
- textbookLibrary.setRc("");
- textbookLibrary.setIr("");
- textbookLibrary.setQuant("");
- }
- }
- }
- /**
- * 根据用户权限更新资源信息
- * @param user
- * @param textbookLibraryHistoryList
- */
- public void refreshHistoryResource(User user, List<TextbookLibraryHistory> textbookLibraryHistoryList){
- // 处理权限
- if (user != null){
- if (!userServiceService.hasService(user.getId(), ServiceKey.TEXTBOOK)) {
- for (TextbookLibraryHistory history : textbookLibraryHistoryList) {
- history.setRc("");
- history.setIr("");
- history.setQuant("");
- }
- }
- }else{
- for(TextbookLibraryHistory history : textbookLibraryHistoryList){
- history.setRc("");
- history.setIr("");
- history.setQuant("");
- }
- }
- }
- private void addQuestionToPaper(TextbookLibrary library, TextbookQuestion question, TextbookLogic logic){
- String prefixTitle = generatePrefixTitle(library);
- // 获取最后一个paper
- TextbookPaper paper = textbookPaperService.getLastByLibrary(logic, question.getLibraryId());
- Integer no = 0;
- if (paper == null || paper.getQuestionNumber().equals(paperLength)){
- if (paper != null){
- no = paper.getNo();
- }
- paper = textbookPaperService.add(TextbookPaper.builder()
- .logic(SentenceLogic.NO.key)
- .year(question.getYear())
- .libraryId(question.getLibraryId())
- .title(textbookPaperService.generateTitle(prefixTitle, paperLength, no, 1))
- .no(no+1)
- .questionNumber(1)
- .status(1)
- .questionNoIds(new Integer[]{question.getId()}).build()
- );
- }else{
- // 加入
- List<Integer> questionNoList = Arrays.stream(paper.getQuestionNoIds()).collect(Collectors.toList());
- questionNoList.add(question.getQuestionNoId());
- List<Integer> textbookList = Arrays.stream(paper.getQuestionTIds()).collect(Collectors.toList());
- textbookList.add(question.getId());
- paper.setQuestionNumber(paper.getQuestionNumber() + 1);
- paper.setQuestionNoIds(questionNoList.toArray(new Integer[0]));
- paper.setQuestionTIds(textbookList.toArray(new Integer[0]));
- paper.setTitle(textbookPaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber()));
- paper = textbookPaperService.edit(paper);
- }
- }
- private void removeQuestionFromPaper(TextbookQuestion question){
- List<TextbookPaper> paperList = textbookPaperService.listByQuestion(question);
- for(TextbookPaper paper : paperList){
- paper.setQuestionNumber(paper.getQuestionNumber() - 1);
- List<Integer> questionNoList = Arrays.stream(paper.getQuestionNoIds()).collect(Collectors.toList());
- questionNoList.remove(question.getQuestionNoId());
- List<Integer> textbookList = Arrays.stream(paper.getQuestionNoIds()).collect(Collectors.toList());
- textbookList.remove(question.getId());
- paper.setQuestionNoIds(questionNoList.toArray(new Integer[0]));
- paper.setQuestionTIds(textbookList.toArray(new Integer[0]));
- textbookPaperService.edit(paper);
- }
- }
- private String generatePrefixTitle(TextbookLibrary library){
- String startDate = dateToString.format(library.getStartDate());
- String endDate;
- if (library.getEndDate()==null){
- // 至今
- endDate = "Now";
- }else if(library.getStartDate().equals(library.getEndDate())){
- // 同月
- endDate = dateToString.format(library.getEndDate());
- }else{
- endDate = dateToString.format(library.getEndDate());
- }
- return String.format("%s-%s", startDate, endDate);
- }
- }
|