|
|
@ -1,15 +1,43 @@ |
|
|
|
package com.ccsens.braintraining.service; |
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.lang.Snowflake; |
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.ccsens.braintraining.bean.dto.QuestionDto; |
|
|
|
import com.ccsens.braintraining.bean.po.*; |
|
|
|
import com.ccsens.braintraining.persist.mapper.*; |
|
|
|
import com.ccsens.braintraining.util.BrainTrainingConstant; |
|
|
|
import com.ccsens.util.PoiUtil; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.File; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author 逗 |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) |
|
|
|
public class ImportService implements IImportService { |
|
|
|
@Resource |
|
|
|
private TrainClassifyMapper trainClassifyMapper; |
|
|
|
@Resource |
|
|
|
private Snowflake snowflake; |
|
|
|
@Resource |
|
|
|
private TrainQuestionMapper trainQuestionMapper; |
|
|
|
@Resource |
|
|
|
private TrainQuestionOptionMapper questionOptionMapper; |
|
|
|
@Resource |
|
|
|
private TrainQuestionContentMapper questionContentMapper; |
|
|
|
@Resource |
|
|
|
private TrainQuestionAnswerMapper questionAnswerMapper; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void importQuestion(File excelFile) throws Exception { |
|
|
@ -21,7 +49,171 @@ public class ImportService implements IImportService { |
|
|
|
} |
|
|
|
|
|
|
|
private void saveQuestions(List<Object[]> questions, String sheetName) { |
|
|
|
System.out.println(questions); |
|
|
|
System.out.println(sheetName); |
|
|
|
List<QuestionDto.ExcelQuestionInfo> questionInfos = initData(questions); |
|
|
|
Long classifyId = null; |
|
|
|
Long questionId = null; |
|
|
|
for (QuestionDto.ExcelQuestionInfo questionInfo : questionInfos) { |
|
|
|
//根据code查找训练计划
|
|
|
|
if(StrUtil.isNotBlank(questionInfo.getCode())){ |
|
|
|
classifyId = getClassifyIdByCode(questionInfo.getCode()); |
|
|
|
} |
|
|
|
//1. 添加题目
|
|
|
|
if(ObjectUtil.isNotNull(classifyId) && StrUtil.isNotBlank(questionInfo.getSort())){ |
|
|
|
questionId = saveQuestion(classifyId, questionInfo); |
|
|
|
} |
|
|
|
//2. 给题目添加题干
|
|
|
|
if(ObjectUtil.isNotNull(questionInfo.getType()) |
|
|
|
&& questionInfo.getType().equals(BrainTrainingConstant.QUESTION_CONTENT) |
|
|
|
&& StrUtil.isNotBlank(questionInfo.getContent())){ |
|
|
|
saveQuestionContent(questionId, questionInfo); |
|
|
|
} |
|
|
|
//3. 给题目添加选项
|
|
|
|
String optionId = null; |
|
|
|
if(ObjectUtil.isNotNull(questionInfo.getType()) |
|
|
|
&& questionInfo.getType().equals(BrainTrainingConstant.QUESTION_OPTION) |
|
|
|
&& StrUtil.isNotBlank(questionInfo.getContent())){ |
|
|
|
optionId = saveOption(questionId, questionInfo); |
|
|
|
} |
|
|
|
//4. 根据选项是否正确添加答案信息,答案关联选项id
|
|
|
|
if(StrUtil.isNotBlank(questionInfo.getCorrect()) && "1".equals(questionInfo.getCorrect())){ |
|
|
|
saveAnswer(questionId, optionId, (byte)0); |
|
|
|
} |
|
|
|
} |
|
|
|
// switch (sheetName){
|
|
|
|
// case "DSHY":
|
|
|
|
// saveQuestionAll(questionInfos);
|
|
|
|
// break;
|
|
|
|
// case "JTPX":
|
|
|
|
// saveQuestionAll(questionInfos);
|
|
|
|
// break;
|
|
|
|
// case "CXNL":
|
|
|
|
// saveQuestionAll(questionInfos);
|
|
|
|
// break;
|
|
|
|
// case "ZSYS":
|
|
|
|
// case "SJGN":
|
|
|
|
// case "KJDW":
|
|
|
|
// case "SZZC":
|
|
|
|
// case "LXJF":
|
|
|
|
// default:
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
} |
|
|
|
//
|
|
|
|
// private void saveQuestionAll(List<QuestionDto.ExcelQuestionInfo> questionInfos) {
|
|
|
|
// Long classifyId = null;
|
|
|
|
// Long questionId = null;
|
|
|
|
// for (QuestionDto.ExcelQuestionInfo questionInfo : questionInfos) {
|
|
|
|
// //根据code查找训练计划
|
|
|
|
// if(StrUtil.isNotBlank(questionInfo.getCode())){
|
|
|
|
// classifyId = getClassifyIdByCode(questionInfo.getCode());
|
|
|
|
// }
|
|
|
|
// //1. 添加题目
|
|
|
|
// if(ObjectUtil.isNotNull(classifyId) && StrUtil.isNotBlank(questionInfo.getSort())){
|
|
|
|
// questionId = saveQuestion(classifyId, questionInfo);
|
|
|
|
// }
|
|
|
|
// //2. 给题目添加题干
|
|
|
|
// if(ObjectUtil.isNotNull(questionInfo.getType())
|
|
|
|
// && questionInfo.getType().equals(BrainTrainingConstant.QUESTION_CONTENT)
|
|
|
|
// && StrUtil.isNotBlank(questionInfo.getContent())){
|
|
|
|
// saveQuestionContent(questionId, questionInfo);
|
|
|
|
// }
|
|
|
|
// //3. 给题目添加选项
|
|
|
|
// String optionId = null;
|
|
|
|
// if(ObjectUtil.isNotNull(questionInfo.getType())
|
|
|
|
// && questionInfo.getType().equals(BrainTrainingConstant.QUESTION_OPTION)
|
|
|
|
// && StrUtil.isNotBlank(questionInfo.getContent())){
|
|
|
|
// optionId = saveOption(questionId, questionInfo);
|
|
|
|
// }
|
|
|
|
// //4. 根据选项是否正确添加答案信息,答案关联选项id
|
|
|
|
// if(StrUtil.isNotBlank(questionInfo.getCorrect()) && "1".equals(questionInfo.getCorrect())){
|
|
|
|
// saveAnswer(questionId, optionId, (byte)0);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
private void saveAnswer(Long questionId, String optionId, byte optionType) { |
|
|
|
TrainQuestionAnswer trainQuestionAnswer = new TrainQuestionAnswer(); |
|
|
|
trainQuestionAnswer.setId(snowflake.nextId()); |
|
|
|
trainQuestionAnswer.setQuestionId(questionId); |
|
|
|
trainQuestionAnswer.setContent(optionId); |
|
|
|
trainQuestionAnswer.setOptionType(optionType); |
|
|
|
questionAnswerMapper.insertSelective(trainQuestionAnswer); |
|
|
|
} |
|
|
|
|
|
|
|
private String saveOption(Long questionId, QuestionDto.ExcelQuestionInfo questionInfo) { |
|
|
|
String optionId; |
|
|
|
TrainQuestionOption questionOption = new TrainQuestionOption(); |
|
|
|
questionOption.setId(snowflake.nextId()); |
|
|
|
questionOption.setQuestionId(questionId); |
|
|
|
questionOption.setContent(questionInfo.getContent()); |
|
|
|
questionOption.setShowType(Byte.parseByte(questionInfo.getQuestionType())); |
|
|
|
questionOption.setSort(Integer.parseInt(questionInfo.getQuestionSort())); |
|
|
|
questionOptionMapper.insertSelective(questionOption); |
|
|
|
optionId = questionOption.getId().toString(); |
|
|
|
return optionId; |
|
|
|
} |
|
|
|
|
|
|
|
private void saveQuestionContent(Long questionId, QuestionDto.ExcelQuestionInfo questionInfo) { |
|
|
|
TrainQuestionContent questionContent = new TrainQuestionContent(); |
|
|
|
questionContent.setId(snowflake.nextId()); |
|
|
|
questionContent.setQuestionId(questionId); |
|
|
|
questionContent.setContent(questionInfo.getContent()); |
|
|
|
questionContent.setShowType(Byte.parseByte(questionInfo.getQuestionType())); |
|
|
|
questionContent.setSort(Integer.parseInt(questionInfo.getQuestionSort())); |
|
|
|
questionContentMapper.insertSelective(questionContent); |
|
|
|
} |
|
|
|
|
|
|
|
private Long saveQuestion(Long classifyId2, QuestionDto.ExcelQuestionInfo questionInfo) { |
|
|
|
Long questionId2; |
|
|
|
TrainQuestion trainQuestion = new TrainQuestion(); |
|
|
|
trainQuestion.setId(snowflake.nextId()); |
|
|
|
trainQuestion.setClassifyId(classifyId2); |
|
|
|
trainQuestion.setSort(Integer.parseInt(questionInfo.getSort())); |
|
|
|
trainQuestion.setGrade(StrUtil.isBlank(questionInfo.getGrade()) ? null : Byte.parseByte(questionInfo.getGrade())); |
|
|
|
trainQuestionMapper.insertSelective(trainQuestion); |
|
|
|
questionId2 = trainQuestion.getId(); |
|
|
|
return questionId2; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 个根据code获取训练类型信息 |
|
|
|
* @param code code |
|
|
|
* @return 训练类型id |
|
|
|
*/ |
|
|
|
private Long getClassifyIdByCode(String code) { |
|
|
|
//根据code查找训练计划
|
|
|
|
Long classifyId = null; |
|
|
|
TrainClassifyExample classifyExample = new TrainClassifyExample(); |
|
|
|
classifyExample.createCriteria().andCodeEqualTo(code); |
|
|
|
List<TrainClassify> trainClassifies = trainClassifyMapper.selectByExample(classifyExample); |
|
|
|
if(CollectionUtil.isNotEmpty(trainClassifies)){ |
|
|
|
classifyId = trainClassifies.get(0).getId(); |
|
|
|
} |
|
|
|
return classifyId; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 将excel内的数据转换成对象 |
|
|
|
* @param questions object数组 |
|
|
|
* @return 返回题目信息 |
|
|
|
*/ |
|
|
|
private List<QuestionDto.ExcelQuestionInfo> initData(List<Object[]> questions) { |
|
|
|
List<QuestionDto.ExcelQuestionInfo> questionInfos = new ArrayList<>(); |
|
|
|
for (Object[] obj : questions) { |
|
|
|
if(obj.length < 8){ |
|
|
|
continue; |
|
|
|
} |
|
|
|
QuestionDto.ExcelQuestionInfo questionInfo = new QuestionDto.ExcelQuestionInfo(); |
|
|
|
questionInfo.setCode(obj[0] == null ? null : (String) obj[0]); |
|
|
|
questionInfo.setSort(obj[1] == null ? null : (String) obj[1]); |
|
|
|
questionInfo.setType(obj[2] == null ? null : (String) obj[2]); |
|
|
|
questionInfo.setQuestionSort(obj[3] == null ? null : (String) obj[3]); |
|
|
|
questionInfo.setQuestionType(obj[4] == null ? null : (String) obj[4]); |
|
|
|
questionInfo.setContent(obj[5] == null ? null : (String) obj[5]); |
|
|
|
questionInfo.setCorrect(obj[6] == null ? null : (String) obj[6]); |
|
|
|
questionInfo.setGrade(obj[7] == null ? null : (String) obj[7]); |
|
|
|
questionInfos.add(questionInfo); |
|
|
|
} |
|
|
|
return questionInfos; |
|
|
|
} |
|
|
|
} |
|
|
|