198 lines
8.2 KiB
Java
198 lines
8.2 KiB
Java
package com.peanut.modules.common.controller;
|
||
|
||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
import com.peanut.common.excel.ExcelUtil;
|
||
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.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||
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.io.InputStream;
|
||
import java.util.*;
|
||
|
||
@Slf4j
|
||
@RestController("commonClassExam")
|
||
@RequestMapping("common/classExam")
|
||
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) {
|
||
int num = 0;
|
||
try (InputStream fis = file.getInputStream()) {
|
||
List<ClassExamOption> optionList = new ArrayList<>();
|
||
Long time = System.currentTimeMillis();
|
||
ExcelUtil example = new ExcelUtil();
|
||
example.processOneSheet(fis);
|
||
Long endtime = System.currentTimeMillis();
|
||
LinkedHashMap<String, String> map = example.getRowContents();
|
||
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
|
||
int count = 0;
|
||
while (it.hasNext()) {
|
||
Map.Entry<String, String> entry = it.next();
|
||
String pos = entry.getKey();
|
||
String rowNo = pos.substring(1);
|
||
count = Integer.parseInt(rowNo);
|
||
// System.out.println(pos + ";" + entry.getValue());
|
||
}
|
||
for (int i=2;i<=count;i++){
|
||
ClassExamSubject subject = new ClassExamSubject();
|
||
subject.setCourseId(courseId);
|
||
//类型
|
||
String type = map.containsKey("A"+i)?map.get("A"+i):"";
|
||
if (type.contains("单选")){
|
||
subject.setType(0);
|
||
}else if (type.contains("多选")){
|
||
subject.setType(1);
|
||
}else {
|
||
throw new Exception("第"+i+"题无题目类型");
|
||
}
|
||
//题目
|
||
subject.setContent(map.containsKey("B"+i)?map.get("B"+i):"");
|
||
//所属章节
|
||
subject.setChapter(map.containsKey("N"+i)?map.get("N"+i):"");
|
||
//音视频序号
|
||
subject.setMedia(map.containsKey("O"+i)?map.get("O"+i):"");
|
||
//音视频时间
|
||
subject.setMediaTime(map.containsKey("P"+i)?map.get("P"+i):"");
|
||
//出题人
|
||
subject.setCreateUser(map.containsKey("Q"+i)?map.get("Q"+i):"");
|
||
classExamSubjectService.save(subject);
|
||
num++;
|
||
insertOption(optionList,subject.getId(),map.containsKey("C"+i)?map.get("C"+i):"",map.containsKey("I"+i)?map.get("I"+i):"");
|
||
insertOption(optionList,subject.getId(),map.containsKey("D"+i)?map.get("D"+i):"",map.containsKey("I"+i)?map.get("I"+i):"");
|
||
insertOption(optionList,subject.getId(),map.containsKey("E"+i)?map.get("E"+i):"",map.containsKey("I"+i)?map.get("I"+i):"");
|
||
insertOption(optionList,subject.getId(),map.containsKey("F"+i)?map.get("F"+i):"",map.containsKey("I"+i)?map.get("I"+i):"");
|
||
insertOption(optionList,subject.getId(),map.containsKey("G"+i)?map.get("G"+i):"",map.containsKey("I"+i)?map.get("I"+i):"");
|
||
insertOption(optionList,subject.getId(),map.containsKey("H"+i)?map.get("H"+i):"",map.containsKey("I"+i)?map.get("I"+i):"");
|
||
}
|
||
classExamOptionService.saveBatch(optionList);
|
||
System.out.println("解析数据" + count + "条;耗时" + (endtime - time) / 1000 + "秒");
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return R.error(e.getMessage());
|
||
}
|
||
return R.ok("导入成功"+num+"条");
|
||
}
|
||
public void insertOption(List<ClassExamOption> optionList,int subjectId,String content,String rightAnswer){
|
||
if (StringUtils.isNotEmpty(content)){
|
||
ClassExamOption option = new ClassExamOption();
|
||
option.setSubjectId(subjectId);
|
||
if (rightAnswer.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);
|
||
}
|
||
}
|
||
}
|
||
|
||
//考试题目列表
|
||
@RequestMapping("/getClassExamSubjectList")
|
||
public R getClassExamSubjectList(@RequestBody Map<String,Object> params){
|
||
Page classExamSubjectList = classExamService.getClassExamSubjectList(params);
|
||
return R.ok().put("page",classExamSubjectList);
|
||
}
|
||
|
||
//考试题目列表
|
||
@RequestMapping("/generateExamPaper")
|
||
public R generateExamPaper(@RequestBody Map<String,Object> params){
|
||
Object examPaper = classExamService.generateExamPaper(params);
|
||
return R.ok().put("examPaper",examPaper);
|
||
}
|
||
|
||
//提交考卷
|
||
@RequestMapping("/submitExamPaper")
|
||
public R submitExamPaper(@RequestBody Map<String,Object> params){
|
||
Object examPaper = classExamService.submitExamPaper(params);
|
||
return R.ok().put("examPaper",examPaper);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
//添加题目
|
||
@RequestMapping("/addClassExamSubject")
|
||
public R addClassExamSubject(@RequestBody ClassExamSubject classExamSubject){
|
||
classExamService.addClassExamSubject(classExamSubject);
|
||
return R.ok();
|
||
}
|
||
|
||
//添加选项
|
||
@RequestMapping("/addClassExamOption")
|
||
public R addClassExamOption(@RequestBody ClassExamOption classExamOption){
|
||
classExamService.addClassExamOption(classExamOption);
|
||
return R.ok();
|
||
}
|
||
|
||
//题目详情
|
||
@RequestMapping("/classExamSubjectInfo")
|
||
public R classExamSubjectInfo(@RequestBody Map<String,Object> params){
|
||
return R.ok().put("classExamSubjectInfo",classExamService.classExamSubjectInfo(params));
|
||
}
|
||
|
||
//修改题目
|
||
@RequestMapping("/updateClassExamSubject")
|
||
public R updateClassExamSubject(@RequestBody ClassExamSubject classExamSubject){
|
||
classExamService.updateClassExamSubject(classExamSubject);
|
||
return R.ok();
|
||
}
|
||
|
||
//修改选项
|
||
@RequestMapping("/updateClassExamOption")
|
||
public R updateClassExamOption(@RequestBody ClassExamOption classExamOption){
|
||
classExamService.updateClassExamOption(classExamOption);
|
||
return R.ok();
|
||
}
|
||
|
||
//删除题目
|
||
@RequestMapping("/delClassExamSubject")
|
||
public R delClassExamSubject(@RequestBody Map<String,Object> params){
|
||
classExamService.delClassExamSubject(params);
|
||
return R.ok();
|
||
}
|
||
|
||
//删除选项
|
||
@RequestMapping("/delClassExamOption")
|
||
public R delClassExamOption(@RequestBody Map<String,Object> params){
|
||
classExamService.delClassExamOption(params);
|
||
return R.ok();
|
||
}
|
||
|
||
}
|