package com.ycl.jxkg.listener; import com.ycl.jxkg.domain.*; import com.ycl.jxkg.domain.entity.ExamPaper; import com.ycl.jxkg.domain.entity.ExamPaperScore; import com.ycl.jxkg.domain.entity.ExamPaperScoreDetail; import com.ycl.jxkg.domain.entity.TextContent; import com.ycl.jxkg.enums.ExamPaperTypeEnum; import com.ycl.jxkg.enums.QuestionTypeEnum; import com.ycl.jxkg.event.CalculateExamPaperAnswerCompleteEvent; import com.ycl.jxkg.service.ExamPaperScoreService; import com.ycl.jxkg.service.ExamPaperScoreDetailService; import com.ycl.jxkg.service.TaskExamCustomerAnswerService; import com.ycl.jxkg.service.TextContentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; /** * @version 3.5.0 * @description: The type Calculate exam paper answer listener. * Copyright (C), 2020-2024, 武汉思维跳跃科技有限公司 * @date 2021/12/25 9:45 */ @Component public class CalculateExamPaperAnswerListener implements ApplicationListener { private final ExamPaperScoreService examPaperScoreService; private final ExamPaperScoreDetailService examPaperScoreDetailService; private final TextContentService textContentService; private final TaskExamCustomerAnswerService examCustomerAnswerService; /** * Instantiates a new Calculate exam paper answer listener. * * @param examPaperScoreService the exam paper answer service * @param examPaperScoreDetailService the exam paper question customer answer service * @param textContentService the text content service * @param examCustomerAnswerService the exam customer answer service */ @Autowired public CalculateExamPaperAnswerListener(ExamPaperScoreService examPaperScoreService, ExamPaperScoreDetailService examPaperScoreDetailService, TextContentService textContentService, TaskExamCustomerAnswerService examCustomerAnswerService) { this.examPaperScoreService = examPaperScoreService; this.examPaperScoreDetailService = examPaperScoreDetailService; this.textContentService = textContentService; this.examCustomerAnswerService = examCustomerAnswerService; } @Override @Transactional public void onApplicationEvent(CalculateExamPaperAnswerCompleteEvent calculateExamPaperAnswerCompleteEvent) { Date now = new Date(); ExamPaperAnswerInfo examPaperAnswerInfo = (ExamPaperAnswerInfo) calculateExamPaperAnswerCompleteEvent.getSource(); ExamPaper examPaper = examPaperAnswerInfo.getExamPaper(); ExamPaperScore examPaperScore = examPaperAnswerInfo.getExamPaperScore(); List examPaperScoreDetails = examPaperAnswerInfo.getExamPaperScoreDetails(); examPaperScoreService.save(examPaperScore); examPaperScoreDetails.stream().filter(a -> QuestionTypeEnum.needSaveTextContent(a.getQuestionType())).forEach(d -> { TextContent textContent = new TextContent(); textContent.setContent(d.getAnswer()); textContent.setCreateTime(now); textContentService.save(textContent); d.setTextContentId(textContent.getId()); d.setAnswer(null); }); examPaperScoreDetails.forEach(d -> { d.setExamPaperAnswerId(examPaperScore.getId()); }); examPaperScoreDetailService.insertList(examPaperScoreDetails); switch (ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) { case RandomOrder: { examCustomerAnswerService.insertOrUpdate(examPaper, examPaperScore, now); break; } default: break; } } }