linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.ots.project.exam.listener;
 
import com.ots.common.enums.ExamPaperTypeEnum;
import com.ots.common.enums.QuestionTypeEnum;
import com.ots.project.exam.domain.ExamPaperAnswer;
import com.ots.project.exam.domain.ExamPaperQuestionCustomerAnswer;
import com.ots.project.exam.domain.TExamPaper;
import com.ots.project.exam.domain.TTextContent;
import com.ots.project.exam.dto.ExamPaperAnswerInfo;
import com.ots.project.exam.service.ExamPaperAnswerService;
import com.ots.project.exam.service.ExamPaperQuestionCustomerAnswerService;
import com.ots.project.exam.service.ITTextContentService;
import com.ots.project.exam.service.TaskExamCustomerAnswerService;
import lombok.AllArgsConstructor;
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;
 
 
/**
 * @author OTS
 */
@Component
@AllArgsConstructor
public class CalculateExamPaperAnswerListener implements ApplicationListener<CalculateExamPaperAnswerCompleteEvent> {
 
    @Autowired
    private ExamPaperAnswerService examPaperAnswerService;
 
    @Autowired
    private ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService;
 
    @Autowired
    private ITTextContentService textContentService;
 
    @Autowired
    private TaskExamCustomerAnswerService examCustomerAnswerService;
 
    @Override
    @Transactional
    public void onApplicationEvent(CalculateExamPaperAnswerCompleteEvent calculateExamPaperAnswerCompleteEvent) {
        Date now = new Date();
 
        ExamPaperAnswerInfo examPaperAnswerInfo = (ExamPaperAnswerInfo) calculateExamPaperAnswerCompleteEvent.getSource();
        TExamPaper examPaper = examPaperAnswerInfo.getExamPaper();
        ExamPaperAnswer examPaperAnswer = examPaperAnswerInfo.getExamPaperAnswer();
        List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers = examPaperAnswerInfo.getExamPaperQuestionCustomerAnswers();
 
        examPaperAnswerService.insertByFilter(examPaperAnswer);
        examPaperQuestionCustomerAnswers.stream().filter(a -> QuestionTypeEnum.needSaveTextContent(a.getQuestionType())).forEach(d -> {
            TTextContent textContent = new TTextContent(d.getAnswer(), now);
            textContentService.insertTTextContent(textContent);
            d.setTextContentId(textContent.getId().intValue());
            d.setAnswer(null);
        });
        examPaperQuestionCustomerAnswers.forEach(d -> {
            d.setExamPaperAnswerId(examPaperAnswer.getId());
        });
        examPaperQuestionCustomerAnswerService.insertList(examPaperQuestionCustomerAnswers);
 
        switch (ExamPaperTypeEnum.fromCode(examPaper.getPaperType().intValue())) {
            case Task: {
                examCustomerAnswerService.insertOrUpdate(examPaper, examPaperAnswer, now);
                break;
            }
            default:
                break;
        }
    }
}