guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.business.dto.MentalTestQuestionOptionDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestTopicPageDTO;
import cn.stylefeng.guns.modular.business.dto.request.MentalTestTopicAddUpdateRequest;
import cn.stylefeng.guns.modular.business.entity.MentalTestOption;
import cn.stylefeng.guns.modular.business.entity.MentalTestQuestion;
import cn.stylefeng.guns.modular.business.entity.MentalTestResultSet;
import cn.stylefeng.guns.modular.business.entity.MentalTestTopic;
import cn.stylefeng.guns.modular.business.mapper.MentalTestTopicMapper;
import cn.stylefeng.guns.modular.business.service.IMentalTestOptionService;
import cn.stylefeng.guns.modular.business.service.IMentalTestQuestionService;
import cn.stylefeng.guns.modular.business.service.IMentalTestResultSetService;
import cn.stylefeng.guns.modular.business.service.IMentalTestTopicService;
import cn.stylefeng.roses.kernel.rule.enums.DeleteEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * <p>
 * 心理测试题库 服务实现类
 * </p>
 *
 * @author goupan
 * @since 2024-01-01
 */
@Service
public class MentalTestTopicServiceImpl extends ServiceImpl<MentalTestTopicMapper, MentalTestTopic> implements IMentalTestTopicService {
 
    @Resource
    IMentalTestQuestionService mentalTestQuestionService;
 
    @Resource
    IMentalTestOptionService mentalTestOptionService;
 
    @Resource
    IMentalTestResultSetService mentalTestResultSetService;
 
    @Override
    public Page<MentalTestTopicPageDTO> topicPage(Page page, String classIds, String title, Integer testType, Integer testFlag, Long userId, Integer statusFlag) {
        List<String> classIdList = StrUtil.isNotBlank(classIds) ? Arrays.asList(classIds.split(",")) : null;
        return this.baseMapper.topicPage(page, classIdList, title, testType, testFlag, userId, statusFlag);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTopic(MentalTestTopicAddUpdateRequest req) {
        // 保存题库信息
        MentalTestTopic topic = BeanUtil.toBean(req, MentalTestTopic.class);
        this.save(topic);
        Long topicId = topic.getId();
 
        for (MentalTestTopicAddUpdateRequest.QuestionRequest qu : req.getQuestionList()) {
            // 保存问题
            MentalTestQuestion question = BeanUtil.toBean(qu, MentalTestQuestion.class);
            question.setTopicId(topicId);
            mentalTestQuestionService.save(question);
            Long questionId = question.getId();
 
            // 保存问题选项
            for (MentalTestTopicAddUpdateRequest.QuestionRequest.OptionRequest op : qu.getOptionList()) {
                MentalTestOption option = BeanUtil.toBean(op, MentalTestOption.class);
                option.setTopicId(topicId);
                option.setQuestionId(questionId);
                mentalTestOptionService.save(option);
            }
        }
 
        // 保存结果区间配置
        for (MentalTestTopicAddUpdateRequest.ResultSetRequest rs : req.getResultSetList()) {
            MentalTestResultSet resultSet = BeanUtil.toBean(rs, MentalTestResultSet.class);
            resultSet.setTopicId(topicId);
            resultSet.setResultCalculateMode(req.getResultCalculateMode());
            mentalTestResultSetService.save(resultSet);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editTopic(MentalTestTopicAddUpdateRequest req) {
        // 保存题库信息
        MentalTestTopic topic = BeanUtil.toBean(req, MentalTestTopic.class);
        this.saveOrUpdate(topic);
        Long topicId = topic.getId();
 
        // 清空问题、问题选项
        mentalTestQuestionService.remove(
                Wrappers.<MentalTestQuestion>lambdaQuery()
                        .eq(MentalTestQuestion::getTopicId, topicId)
        );
        mentalTestOptionService.remove(
                Wrappers.<MentalTestOption>lambdaQuery()
                        .eq(MentalTestOption::getTopicId, topicId)
        );
        for (MentalTestTopicAddUpdateRequest.QuestionRequest qu : req.getQuestionList()) {
            // 保存问题
            MentalTestQuestion question = BeanUtil.toBean(qu, MentalTestQuestion.class);
            question.setTopicId(topicId);
            mentalTestQuestionService.saveOrUpdate(question);
            Long questionId = question.getId();
 
            // 保存问题选项
            for (MentalTestTopicAddUpdateRequest.QuestionRequest.OptionRequest op : qu.getOptionList()) {
                MentalTestOption option = BeanUtil.toBean(op, MentalTestOption.class);
                option.setTopicId(topicId);
                option.setQuestionId(questionId);
                mentalTestOptionService.saveOrUpdate(option);
            }
        }
 
        // 清空结果区间配置
        mentalTestResultSetService.remove(
                Wrappers.<MentalTestResultSet>lambdaQuery()
                        .eq(MentalTestResultSet::getTopicId, topicId)
        );
        // 保存结果区间配置
        for (MentalTestTopicAddUpdateRequest.ResultSetRequest rs : req.getResultSetList()) {
            MentalTestResultSet resultSet = BeanUtil.toBean(rs, MentalTestResultSet.class);
            resultSet.setTopicId(topicId);
            resultSet.setResultCalculateMode(req.getResultCalculateMode());
            mentalTestResultSetService.saveOrUpdate(resultSet);
        }
    }
 
    @Override
    public List<MentalTestQuestionOptionDTO> getQuestionOptionByTopicId(Long topicId) {
        // 心理测试问题
        List<MentalTestQuestion> questionList = mentalTestQuestionService.list(
                Wrappers.<MentalTestQuestion>lambdaQuery()
                        .eq(MentalTestQuestion::getTopicId, topicId)
                        .eq(MentalTestQuestion::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
        );
        // 心理测试问题选项
        List<MentalTestOption> optionList = mentalTestOptionService.list(
                Wrappers.<MentalTestOption>lambdaQuery()
                        .eq(MentalTestOption::getTopicId, topicId)
                        .eq(MentalTestOption::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
        );
        // 封装问题、选项信息
        List<MentalTestQuestionOptionDTO> dtoList = questionList.stream()
                .map(o -> {
                    MentalTestQuestionOptionDTO questionDto = BeanUtil.toBean(o, MentalTestQuestionOptionDTO.class);
                    List<MentalTestOption> options = optionList.stream()
                            .filter(op -> op.getQuestionId().equals(o.getId()))
                            .collect(Collectors.toList());
                    questionDto.setOptionList(options);
                    return questionDto;
                })
                .collect(Collectors.toList());
        return dtoList;
    }
 
}