|
|
|
@ -15,18 +15,14 @@ import com.tz.platform.pc.dto.ListQuestionImportDTO;
|
|
|
|
|
import com.tz.platform.pc.dto.QuestionImportDTO;
|
|
|
|
|
import com.tz.platform.repository.CourseDao;
|
|
|
|
|
import com.tz.platform.repository.QuestionDao;
|
|
|
|
|
import org.apache.catalina.User;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class QuestionImportBiz {
|
|
|
|
@ -57,6 +53,10 @@ public class QuestionImportBiz {
|
|
|
|
|
ExcelUtil util = new ExcelUtil(file.getInputStream());
|
|
|
|
|
int lastRowNum = util.getLastRowNum();
|
|
|
|
|
List<QuestionImportDTO> list = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
Workbook workbook = new XSSFWorkbook(file.getInputStream());
|
|
|
|
|
ExcelUtil.initSheet(workbook);
|
|
|
|
|
|
|
|
|
|
for (int rowNo = 1; rowNo <= lastRowNum; ) {
|
|
|
|
|
int lineCount = util.getMergedRegionLineCount(rowNo, 1);
|
|
|
|
|
if (lineCount <= 0) {
|
|
|
|
@ -98,7 +98,7 @@ public class QuestionImportBiz {
|
|
|
|
|
question.setLevelId(levelVo.getId());
|
|
|
|
|
question.setLevelName(levelNamme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//todo 兼容填空题
|
|
|
|
|
question.setStem(stem);
|
|
|
|
|
question.setUserNo(userNo);
|
|
|
|
|
question.setCreateTime(new Date());
|
|
|
|
@ -117,6 +117,8 @@ public class QuestionImportBiz {
|
|
|
|
|
qt = QuestionTypeEnum.MULTY;
|
|
|
|
|
} else if (questType.equals(QuestionTypeEnum.JUDGE.getDesc())) {
|
|
|
|
|
qt = QuestionTypeEnum.JUDGE;
|
|
|
|
|
} else if (questType.equals(QuestionTypeEnum.GAPFILLING.getDesc())) {
|
|
|
|
|
qt = QuestionTypeEnum.GAPFILLING;
|
|
|
|
|
}
|
|
|
|
|
if (qt == null) {
|
|
|
|
|
subDto.setStatus("题型未匹配");
|
|
|
|
@ -144,8 +146,8 @@ public class QuestionImportBiz {
|
|
|
|
|
if (answerList.size() == 0) {
|
|
|
|
|
subDto.setStatus("无选项");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(answerIds.size()>0&&answerList.size()>0&&qt!=null){
|
|
|
|
|
//todo 兼容填空题
|
|
|
|
|
if (answerIds.size() > 0 && answerList.size() > 0) {
|
|
|
|
|
question.setQuestionType(qt.getCode());
|
|
|
|
|
question.setAnswerList(answerList);
|
|
|
|
|
question.setAnswerId(answerIds);
|
|
|
|
@ -169,4 +171,159 @@ public class QuestionImportBiz {
|
|
|
|
|
|
|
|
|
|
return Result.success(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Result<String> importExcel11(MultipartFile file, Long userNo, Long courseId) throws Exception {
|
|
|
|
|
//身份校验
|
|
|
|
|
if (userNo == null || userNo <= 0) {
|
|
|
|
|
return Result.error("无权操作");
|
|
|
|
|
}
|
|
|
|
|
UserVo user = feignUser.getByUserNo(userNo);
|
|
|
|
|
if (user == null || user.getUserType() < UserTypeEnum.TEACHER.getCode()) {
|
|
|
|
|
return Result.error("无权操作");
|
|
|
|
|
}
|
|
|
|
|
List<Question> questionList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Workbook workbook = WorkbookFactory.create(file.getInputStream());
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
// 迭代每一行
|
|
|
|
|
Iterator<Row> iterator = sheet.iterator();
|
|
|
|
|
iterator.next();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
Row row = iterator.next();
|
|
|
|
|
|
|
|
|
|
/*解析列,下标从0开始*/
|
|
|
|
|
Cell cell0 = row.getCell(0); //题型
|
|
|
|
|
Cell cell1 = row.getCell(1);//题干
|
|
|
|
|
Cell cell2 = row.getCell(2);//A
|
|
|
|
|
Cell cell3 = row.getCell(3);//B
|
|
|
|
|
Cell cell4 = row.getCell(4);//C
|
|
|
|
|
Cell cell5 = row.getCell(5);//D
|
|
|
|
|
Cell cell6 = row.getCell(6);//E
|
|
|
|
|
Cell cell7 = row.getCell(7);//答案
|
|
|
|
|
Cell cell8 = row.getCell(8);//分数
|
|
|
|
|
Cell cell9 = row.getCell(9);//解析
|
|
|
|
|
|
|
|
|
|
//判断题的BCDE选项和解析可能为空
|
|
|
|
|
if (cell0 == null || cell1 == null || cell2 == null || cell3 == null || cell7 == null || cell8 == null) {
|
|
|
|
|
return Result.error("必填项不能为空!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String questType = this.getCellValue(cell0);//题型
|
|
|
|
|
String a = null;
|
|
|
|
|
String b = null;
|
|
|
|
|
if (!questType.equals("填空题")) {
|
|
|
|
|
a = this.getCellValue(cell2); //选项A
|
|
|
|
|
b = this.getCellValue(cell3);//选项B
|
|
|
|
|
}
|
|
|
|
|
String stem = this.getCellValue(cell1);//题干
|
|
|
|
|
String answer = this.getCellValue(cell7);//答案
|
|
|
|
|
String score = this.getCellValue(cell8);//分值
|
|
|
|
|
String c = cell4 != null ? this.getCellValue(cell4) : null;
|
|
|
|
|
String d = cell5 != null ? this.getCellValue(cell5) : null;
|
|
|
|
|
String e = cell6 != null ? this.getCellValue(cell6) : null;
|
|
|
|
|
String analysis = cell9 != null ? this.getCellValue(cell9) : null; //答案解析
|
|
|
|
|
|
|
|
|
|
Course course = courseDao.getById(courseId);
|
|
|
|
|
if (course == null) {
|
|
|
|
|
return Result.error("课程不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Question temp = questionDao.getByStem(stem);
|
|
|
|
|
if (temp != null) {
|
|
|
|
|
return Result.error("题干已存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QuestionTypeEnum qt = null;
|
|
|
|
|
if (questType.equals(QuestionTypeEnum.SINGLE.getDesc())) {
|
|
|
|
|
qt = QuestionTypeEnum.SINGLE;
|
|
|
|
|
} else if (questType.equals(QuestionTypeEnum.MULTY.getDesc())) {
|
|
|
|
|
qt = QuestionTypeEnum.MULTY;
|
|
|
|
|
} else if (questType.equals(QuestionTypeEnum.JUDGE.getDesc())) {
|
|
|
|
|
qt = QuestionTypeEnum.JUDGE;
|
|
|
|
|
} else if (questType.equals(QuestionTypeEnum.GAPFILLING.getDesc())) {
|
|
|
|
|
qt = QuestionTypeEnum.GAPFILLING;
|
|
|
|
|
}
|
|
|
|
|
if (qt == null) {
|
|
|
|
|
return Result.error("题型未匹配");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Question question = new Question();
|
|
|
|
|
question.setStem(stem);
|
|
|
|
|
question.setUserNo(userNo);
|
|
|
|
|
question.setCreateTime(new Date());
|
|
|
|
|
question.setStatus(0);
|
|
|
|
|
question.setScore(Long.parseLong(score));
|
|
|
|
|
question.setCreatorId(userNo);
|
|
|
|
|
question.setCreator(user.getName());
|
|
|
|
|
question.setType(0);
|
|
|
|
|
question.setAnalysis(analysis);
|
|
|
|
|
question.setCourseId(course.getId());
|
|
|
|
|
question.setLevelId(0);
|
|
|
|
|
question.setLevelName("通用");
|
|
|
|
|
question.setQuestionType(qt.getCode());
|
|
|
|
|
question.setCourseName(course.getName());
|
|
|
|
|
|
|
|
|
|
List<Answer> answerList = new ArrayList<>();
|
|
|
|
|
List<Integer> answerIds = new ArrayList<>();
|
|
|
|
|
String[] split = answer.split(",");
|
|
|
|
|
|
|
|
|
|
int id = 1; // 选项的ID
|
|
|
|
|
|
|
|
|
|
if (questType.equals("填空题")) {
|
|
|
|
|
Answer answerData = new Answer();
|
|
|
|
|
answerData.setId(id);
|
|
|
|
|
answerData.setTitle(answer);
|
|
|
|
|
answerList.add(answerData);
|
|
|
|
|
} else {
|
|
|
|
|
String[] options = {a, b, c, d, e}; // 选项文本
|
|
|
|
|
for (String option : options) {
|
|
|
|
|
if (com.alibaba.cloud.commons.lang.StringUtils.isNotBlank(option)) {
|
|
|
|
|
Answer answerData = new Answer();
|
|
|
|
|
answerData.setId(id);
|
|
|
|
|
answerData.setTitle(option);
|
|
|
|
|
answerList.add(answerData);
|
|
|
|
|
}
|
|
|
|
|
id++; // 每次循环递增ID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String s : split) {
|
|
|
|
|
int answerId = 1; // 默认A选项对应ID为1
|
|
|
|
|
if (s.equals("A")) {
|
|
|
|
|
answerId = 1;
|
|
|
|
|
} else if (s.equals("B")) {
|
|
|
|
|
answerId = 2;
|
|
|
|
|
} else if (s.equals("C")) {
|
|
|
|
|
answerId = 3;
|
|
|
|
|
} else if (s.equals("D")) {
|
|
|
|
|
answerId = 4;
|
|
|
|
|
} else if (s.equals("E")) {
|
|
|
|
|
answerId = 5;
|
|
|
|
|
}
|
|
|
|
|
answerIds.add(answerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
question.setAnswerList(answerList);
|
|
|
|
|
question.setAnswerId(answerIds);
|
|
|
|
|
questionList.add(question);
|
|
|
|
|
}
|
|
|
|
|
questionDao.saveAll(questionList);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return Result.error("系统异常,请联系管理员");
|
|
|
|
|
}
|
|
|
|
|
return Result.success("导入成功!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getCellValue(Cell cell) {
|
|
|
|
|
if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
|
|
|
|
|
return String.valueOf(cell.getBooleanCellValue());
|
|
|
|
|
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
|
|
|
|
|
Double d = cell.getNumericCellValue();
|
|
|
|
|
return String.valueOf(d.intValue());
|
|
|
|
|
}
|
|
|
|
|
return String.valueOf(cell.getStringCellValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|