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;
|
}
|
|
}
|