package com.mindskip.xzs.service.impl; import com.mindskip.xzs.domain.*; import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum; import com.mindskip.xzs.domain.exam.ExamClassShortInfo; import com.mindskip.xzs.domain.exam.ExamPaperQuestionItemObject; import com.mindskip.xzs.domain.exam.ExamPaperTitleItemObject; import com.mindskip.xzs.domain.other.KeyValue; import com.mindskip.xzs.repository.ClassesMapper; import com.mindskip.xzs.repository.ExamPaperClassesMapper; import com.mindskip.xzs.repository.ExamPaperMapper; import com.mindskip.xzs.repository.QuestionMapper; import com.mindskip.xzs.service.ExamPaperService; import com.mindskip.xzs.service.QuestionService; import com.mindskip.xzs.service.SubjectService; import com.mindskip.xzs.service.TextContentService; import com.mindskip.xzs.service.enums.ActionEnum; import com.mindskip.xzs.utility.DateTimeUtil; import com.mindskip.xzs.utility.JsonUtil; import com.mindskip.xzs.utility.ModelMapperSingle; import com.mindskip.xzs.utility.ExamUtil; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM; import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM; import com.mindskip.xzs.viewmodel.student.dashboard.PaperFilter; import com.mindskip.xzs.viewmodel.student.dashboard.PaperInfo; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @Service public class ExamPaperServiceImpl extends BaseServiceImpl implements ExamPaperService { protected final static ModelMapper modelMapper = ModelMapperSingle.Instance(); private final ExamPaperMapper examPaperMapper; private final QuestionMapper questionMapper; private final TextContentService textContentService; private final QuestionService questionService; private final SubjectService subjectService; private final ExamPaperClassesMapper examPaperClassesMapper; private final ClassesMapper classesMapper; @Autowired public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperClassesMapper examPaperClassesMapper, ClassesMapper classesMapper) { super(examPaperMapper); this.examPaperMapper = examPaperMapper; this.questionMapper = questionMapper; this.textContentService = textContentService; this.questionService = questionService; this.subjectService = subjectService; this.examPaperClassesMapper = examPaperClassesMapper; this.classesMapper = classesMapper; } @Override public PageInfo page(ExamPaperPageRequestVM requestVM) { return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> examPaperMapper.page(requestVM)); } @Override public PageInfo adminPage(ExamPaperPageRequestVM requestVM) { return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> examPaperMapper.adminPage(requestVM)); } @Override public PageInfo taskExamPage(ExamPaperPageRequestVM requestVM) { return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> examPaperMapper.taskExamPage(requestVM)); } @Override public PageInfo studentPage(ExamPaperPageVM requestVM) { return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> examPaperMapper.studentPage(requestVM)); } @Override public PageInfo studentClassesPage(ExamPaperPageVM requestVM) { return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() -> examPaperMapper.studentClassesPage(requestVM)); } @Override @Transactional public ExamPaper savePaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, User user) { ActionEnum actionEnum = (examPaperEditRequestVM.getId() == null) ? ActionEnum.ADD : ActionEnum.UPDATE; Date now = new Date(); List titleItemsVM = examPaperEditRequestVM.getTitleItems(); List frameTextContentList = frameTextContentFromVM(titleItemsVM); String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList); ExamPaper examPaper; if (actionEnum == ActionEnum.ADD) { examPaper = modelMapper.map(examPaperEditRequestVM, ExamPaper.class); TextContent frameTextContent = new TextContent(frameTextContentStr, now); textContentService.insertByFilter(frameTextContent); examPaper.setFrameTextContentId(frameTextContent.getId()); examPaper.setCreateTime(now); examPaper.setCreateUser(user.getId()); examPaper.setDeleted(false); examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM); examPaperMapper.insertSelective(examPaper); if (ExamPaperTypeEnum.Classes == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) { examPaperEditRequestVM.getClasses().forEach(c -> { examPaperClassInsert(examPaper, user, c, now); }); } } else { examPaper = examPaperMapper.selectByPrimaryKey(examPaperEditRequestVM.getId()); TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId()); frameTextContent.setContent(frameTextContentStr); textContentService.updateByIdFilter(frameTextContent); modelMapper.map(examPaperEditRequestVM, examPaper); examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM); examPaperMapper.updateByPrimaryKeySelective(examPaper); if (ExamPaperTypeEnum.Classes == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) { List existExamPaperClasses = examPaperClassesMapper.getExamPaperClasses(examPaper.getId()); examPaperEditRequestVM.getClasses().stream() .filter(nc -> !existExamPaperClasses.stream().anyMatch(ec -> ec.getClassesId().equals(nc))) .forEach(c -> { examPaperClassInsert(examPaper, user, c, now); }); existExamPaperClasses.stream() .filter(ec -> !examPaperEditRequestVM.getClasses().stream().anyMatch(nc -> nc.equals(ec.getClassesId()))) .forEach(ec -> { ec.setDeleted(true); examPaperClassesMapper.updateByPrimaryKeySelective(ec); }); } } return examPaper; } @Override public ExamPaperEditRequestVM examPaperToVM(Integer id) { ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(id); ExamPaperEditRequestVM vm = modelMapper.map(examPaper, ExamPaperEditRequestVM.class); vm.setLevel(examPaper.getGradeLevel()); TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId()); List examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), ExamPaperTitleItemObject.class); List questionIds = examPaperTitleItemObjects.stream() .flatMap(t -> t.getQuestionItems().stream() .map(q -> q.getId())) .collect(Collectors.toList()); List questions = questionMapper.selectByIds(questionIds); List examPaperTitleItemVMS = examPaperTitleItemObjects.stream().map(t -> { ExamPaperTitleItemVM tTitleVM = modelMapper.map(t, ExamPaperTitleItemVM.class); List questionItemsVM = t.getQuestionItems().stream().map(i -> { Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get(); QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(question); questionEditRequestVM.setItemOrder(i.getItemOrder()); return questionEditRequestVM; }).collect(Collectors.toList()); tTitleVM.setQuestionItems(questionItemsVM); return tTitleVM; }).collect(Collectors.toList()); vm.setTitleItems(examPaperTitleItemVMS); vm.setScore(ExamUtil.scoreToVM(examPaper.getScore())); ExamPaperTypeEnum examPaperTypeEnum = ExamPaperTypeEnum.fromCode(examPaper.getPaperType()); switch (examPaperTypeEnum) { case TimeLimit: List limitDateTime = Arrays.asList(DateTimeUtil.dateFormat(examPaper.getLimitStartTime()), DateTimeUtil.dateFormat(examPaper.getLimitEndTime())); vm.setLimitDateTime(limitDateTime); break; case Classes: List classes = examPaperClassesMapper.getExamPaperClasses(examPaper.getId()).stream().map(d -> d.getClassesId()).collect(Collectors.toList()); vm.setClasses(classes); break; } return vm; } @Override public List indexPaper(PaperFilter paperFilter) { return examPaperMapper.indexPaper(paperFilter); } @Override public List classesPaper(List cIds) { return examPaperMapper.classesPaper(cIds); } @Override public Integer selectAllCount() { return examPaperMapper.selectAllCount(); } @Override public List selectMothCount() { Date startTime = DateTimeUtil.getMonthStartDay(); Date endTime = DateTimeUtil.getMonthEndDay(); List mouthCount = examPaperMapper.selectCountByDate(startTime, endTime); List mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat(); return mothStartToNowFormat.stream().map(md -> { KeyValue keyValue = mouthCount.stream().filter(kv -> kv.getName().equals(md)).findAny().orElse(null); return null == keyValue ? 0 : keyValue.getValue(); }).collect(Collectors.toList()); } @Override public List getExamPaperClassesByPaperId(Integer paperId) { return examPaperClassesMapper.getExamPaperClasses(paperId); } @Override public List getPaperClassCount(List classIds) { return examPaperClassesMapper.getPaperClassCount(classIds); } @Override public Integer getRandomPaperCount(Integer userId) { return examPaperMapper.getRandomPaperCount(userId); } private void examPaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper, List titleItemsVM) { Integer gradeLevel = subjectService.levelBySubjectId(examPaperEditRequestVM.getSubjectId()); Integer questionCount = titleItemsVM.stream() .mapToInt(t -> t.getQuestionItems().size()).sum(); Integer score = titleItemsVM.stream(). flatMapToInt(t -> t.getQuestionItems().stream() .mapToInt(q -> ExamUtil.scoreFromVM(q.getScore())) ).sum(); examPaper.setQuestionCount(questionCount); examPaper.setScore(score); examPaper.setGradeLevel(gradeLevel); List dateTimes = examPaperEditRequestVM.getLimitDateTime(); if (ExamPaperTypeEnum.TimeLimit == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) { examPaper.setLimitStartTime(DateTimeUtil.parse(dateTimes.get(0), DateTimeUtil.STANDER_FORMAT)); examPaper.setLimitEndTime(DateTimeUtil.parse(dateTimes.get(1), DateTimeUtil.STANDER_FORMAT)); } } private List frameTextContentFromVM(List titleItems) { AtomicInteger index = new AtomicInteger(1); return titleItems.stream().map(t -> { ExamPaperTitleItemObject titleItem = modelMapper.map(t, ExamPaperTitleItemObject.class); List questionItems = t.getQuestionItems().stream() .map(q -> { ExamPaperQuestionItemObject examPaperQuestionItemObject = modelMapper.map(q, ExamPaperQuestionItemObject.class); examPaperQuestionItemObject.setItemOrder(index.getAndIncrement()); return examPaperQuestionItemObject; }) .collect(Collectors.toList()); titleItem.setQuestionItems(questionItems); return titleItem; }).collect(Collectors.toList()); } private void examPaperClassInsert(ExamPaper examPaper, User user, Integer cId, Date now) { ExamPaperClasses examPaperClasses = new ExamPaperClasses(); examPaperClasses.setCreateUser(user.getId()); examPaperClasses.setClassesId(cId); examPaperClasses.setExamPaperId(examPaper.getId()); examPaperClasses.setDeleted(false); examPaperClasses.setCreateTime(now); Classes classes = classesMapper.selectByPrimaryKey(cId); examPaperClasses.setClassesName(classes.getName()); examPaperClassesMapper.insertSelective(examPaperClasses); } }