小班相关

This commit is contained in:
wuchunlei
2024-08-28 10:11:41 +08:00
parent 8ac3fc29f4
commit 40f192a8bc
14 changed files with 197 additions and 17 deletions

View File

@@ -4,15 +4,21 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.utils.R;
import com.peanut.modules.common.entity.ClassExamOption;
import com.peanut.modules.common.entity.ClassExamSubject;
import com.peanut.modules.common.service.ClassExamOptionService;
import com.peanut.modules.common.service.ClassExamService;
import com.peanut.modules.common.service.ClassExamSubjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
import java.io.InputStream;
import java.util.*;
@Slf4j
@RestController("commonClassExam")
@@ -21,10 +27,98 @@ public class ClassExamController {
@Autowired
private ClassExamService classExamService;
@Autowired
private ClassExamSubjectService classExamSubjectService;
@Autowired
private ClassExamOptionService classExamOptionService;
//导入考试题
@RequestMapping("/importSubject")
@Transactional
public R importSubject(@RequestParam("file") MultipartFile file, @RequestParam("courseId") int courseId) {
try (InputStream fis = file.getInputStream()) {
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
Date date = new Date();
List<ClassExamSubject> subjectList = new ArrayList<>();
List<ClassExamOption> optionList = new ArrayList<>();
// 遍历行
for (Row row : sheet) {
if (!isRowEmpty(row)){
if (row.getRowNum()==0){
continue;
}
ClassExamSubject subject = new ClassExamSubject();
subject.setCourseId(courseId);
//类型
String type = row.getCell(0)==null?"":row.getCell(0).toString();
if (type.contains("单选")){
subject.setType(0);
}else if (type.contains("多选")){
subject.setType(1);
}else {
System.out.println(row.getRowNum()+"此题无题目类型");
}
//题目
subject.setContent(row.getCell(1)==null?"":row.getCell(1).toString());
//所属章节
subject.setChapter(row.getCell(13)==null?"":row.getCell(13).toString());
//音视频序号
subject.setMedia(row.getCell(14)==null?"":row.getCell(14).toString());
//音视频时间
subject.setMediaTime(row.getCell(15)==null?"":row.getCell(15).toString());
//出题人
subject.setCreateUser(row.getCell(16)==null?"":row.getCell(16).toString());
classExamSubjectService.save(subject);
//选项1-6
for (int i=2; i<=7;i++) {
if (row.getCell(i)!=null&&!"".equals(row.getCell(i).toString())){
ClassExamOption option = new ClassExamOption();
option.setSubjectId(subject.getId());
String content = row.getCell(i).toString();
String raw = row.getCell(8)==null?"":row.getCell(8).toString();
if (raw.contains(content.substring(0,1))){
option.setRightWrong(1);
}else {
option.setRightWrong(0);
}
char[] c = content.toCharArray();
if (c.length>1){
if (c[1]=='、'||c[1]==''||c[1]==''||c[1]==':'||c[1]==';'||c[1]==' '||c[1]=='.'){
content = content.substring(2);
}else {
content = content.substring(1);
}
}else {
content = "";
}
if (StringUtils.isNotBlank(content)){
option.setContent(content);
optionList.add(option);
}
}
}
}
}
classExamOptionService.saveBatch(optionList);
System.out.println("结束"+(new Date().getTime()-date.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return R.ok();
}
private static boolean isRowEmpty(Row row) {
for (Cell cell : row) {
if (cell.getCellTypeEnum() != CellType.BLANK) {
return false;
}
}
return true;
}
//导入考试题
@RequestMapping("/importSubject1")
public R importSubject1(@RequestParam("file") MultipartFile file, @RequestParam("courseId") int courseId) {
String res = classExamService.importSubject(file,courseId);
if (res==null){
return R.ok();