qirong
2023-07-28 fbdd6af3039a83cd4727a03cecb7c5914277371f
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.mindskip.xzs.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.*;
import com.mindskip.xzs.domain.vo.ExamTemplatesVO;
import com.mindskip.xzs.repository.BaseMapper;
import com.mindskip.xzs.repository.ExamTemplatesMapper;
import com.mindskip.xzs.repository.ExamTemplatesQuestionMapper;
import com.mindskip.xzs.repository.ExamTemplatesSubjectMapper;
import com.mindskip.xzs.service.DepartmentService;
import com.mindskip.xzs.service.ExamPaperService;
import com.mindskip.xzs.service.ExamTemplatesService;
import com.mindskip.xzs.service.ExamTemplatesUserCountService;
import com.mindskip.xzs.utility.convert.ExamTemplatesClassConvert;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.mindskip.xzs.viewmodel.admin.exam.QuestionTypeVM;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class ExamTemplatesServiceImpl extends BaseServiceImpl<ExamTemplates> implements ExamTemplatesService {
 
    private final ExamTemplatesMapper examTemplatesMapper;
    private final ExamTemplatesQuestionMapper examTemplatesQuestionMapper;
    private final ExamTemplatesSubjectMapper examTemplatesSubjectMapper;
    private final DepartmentService departmentService;
    private final ExamPaperService examPaperService;
    private final ExamTemplatesUserCountService examTemplatesUserCountService;
 
    public ExamTemplatesServiceImpl(BaseMapper<ExamTemplates> baseMapper, ExamTemplatesMapper examTemplatesMapper, ExamTemplatesQuestionMapper examTemplatesQuestionMapper, ExamTemplatesSubjectMapper examTemplatesSubjectMapper, DepartmentService departmentService, ExamPaperService examPaperService, ExamTemplatesUserCountService examTemplatesUserCountService) {
        super(baseMapper);
        this.examTemplatesMapper = examTemplatesMapper;
        this.examTemplatesQuestionMapper = examTemplatesQuestionMapper;
        this.examTemplatesSubjectMapper = examTemplatesSubjectMapper;
        this.departmentService = departmentService;
        this.examPaperService = examPaperService;
        this.examTemplatesUserCountService = examTemplatesUserCountService;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ExamPaperEditRequestVM model) {
        if (model.getId() != null) {
            removes(model.getId());
        }
 
        ExamTemplates examTemplates = ExamTemplatesClassConvert.INSTANCE.ExamPaperEditRequestVMToExamTemplates(model);
        examTemplates.setCtime(new Date());
        examTemplates.setTitleName(model.getTitleItems().get(0).getName());
        examTemplatesMapper.add(examTemplates);
 
        List<ExamTemplatesQuestion> examTemplatesQuestions = ExamTemplatesClassConvert.INSTANCE.QuestionTypeVMListToExamTemplatesQuestionList(model.getQuestionTypeVMS())
                .stream().map(e -> {
                    e.setTemplatesId(examTemplates.getId());
                    return e;
                }).collect(Collectors.toList());
        examTemplatesQuestionMapper.saves(examTemplatesQuestions);
 
        List<ExamTemplatesSubject> subjects = new ArrayList<>();
        for (Integer e : model.getSubjectId()) {
            ExamTemplatesSubject examTemplatesSubject = new ExamTemplatesSubject();
            examTemplatesSubject.setSubjectId(e);
            examTemplatesSubject.setTemplatesId(examTemplates.getId());
            subjects.add(examTemplatesSubject);
        }
        examTemplatesSubjectMapper.saves(subjects);
    }
 
    @Override
    public List<ExamTemplatesVO> list(ExamTemplatesVO examTemplatesVO) {
        return null;
    }
 
    @Override
    public PageInfo<ExamTemplates> gets(ExamTemplatesVO templatesVO) {
        return PageHelper.startPage(templatesVO.getPageIndex(), templatesVO.getPageSize(), "id desc").doSelectPageInfo(() ->
                examTemplatesMapper.gets());
    }
 
    @Override
    public ExamPaperEditRequestVM getById(Integer id) {
        return restore(id);
    }
 
    @Override
    public ExamTemplates getByName(String name) {
        return examTemplatesMapper.getByName(name);
    }
 
    @Override
    public Integer randomExam(User user) throws Exception {
 
        ExamTemplatesUserCount count = new ExamTemplatesUserCount();
 
        ExamPaperEditRequestVM vm = restore(null);
        count.setExamTemplatesId(vm.getId());
        Integer[] id = {user.getId()};
        vm.setUserIds(id);
        vm.setType("1");
        vm.setId(null);
        ExamPaper examPaper = examPaperService.savePaperFromVM(vm, user);
        ExamPaperEditRequestVM newVM = examPaperService.examPaperToVM(examPaper.getId());
        count.setExamPaperId(examPaper.getId());
        count.setUserId(user.getId());
        examTemplatesUserCountService.add(count);
        return count.getExamPaperId();
    }
 
    @Override
    public void remove(Integer id) {
        removes(id);
    }
 
    public ExamPaperEditRequestVM restore(Integer id){
        if(id == null){
            ExamTemplates examTemplates = examTemplatesMapper.getTime();
            id = examTemplates.getId();
        }
        ExamTemplates examTemplates = examTemplatesMapper.getById(id);
        ExamPaperEditRequestVM vm = ExamTemplatesClassConvert.INSTANCE.ExamTemplatesToExamPaperEditRequestVM(examTemplates);
        Integer[] ids = examTemplatesSubjectMapper.getTemplatesId(id)
                .stream().map(ExamTemplatesSubject::getSubjectId).toArray(Integer[]::new);
        vm.setSubjectId(ids);
        List<ExamTemplatesQuestion> questions = examTemplatesQuestionMapper.getByTemplatesId(id);
        List<QuestionTypeVM> questionTypeVMList = ExamTemplatesClassConvert.INSTANCE.ExamTemplatesQuestionListToQuestionTypeVMList(questions);
        vm.setQuestionTypeVMS(questionTypeVMList);
        ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM();
        examPaperTitleItemVM.setName(examTemplates.getTitleName());
        List<ExamPaperTitleItemVM> list = new ArrayList<>();
        list.add(examPaperTitleItemVM);
        vm.setTitleItems(list);
        vm.setAggregateSource(100);
        return vm;
    }
    public void removes(Integer id){
        examTemplatesMapper.removeById(id);
        examTemplatesQuestionMapper.removeByTemplatesId(id);
        examTemplatesSubjectMapper.removeByTemplatesId(id);
    }
 
}