diff --git a/src/main/java/com/peanut/common/excel/ExcelUtil.java b/src/main/java/com/peanut/common/excel/ExcelUtil.java new file mode 100644 index 00000000..fdcbbf11 --- /dev/null +++ b/src/main/java/com/peanut/common/excel/ExcelUtil.java @@ -0,0 +1,185 @@ +package com.peanut.common.excel; + +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.xssf.eventusermodel.XSSFReader; +import org.apache.poi.xssf.model.SharedStringsTable; +import org.xml.sax.ContentHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; +import java.io.InputStream; +import java.util.*; + +/** + * 数据量比较大(8万条以上)的excel文件解析,将excel文件解析为 行列坐标-值的形式存入map中,此方式速度快,内存耗损小 但只能读取excel文件 + * 提供处理单个sheet方法 processOneSheet(String filename) 以及处理多个sheet方法 processAllSheets(String filename) + * 只需传入文件路径+文件名即可 调用处理方法结束后,只需接收ExcelUtil.getRowContents()返回值即可获得解析后的数据 + */ +public class ExcelUtil { + private static LinkedHashMap rowContents = new LinkedHashMap(); + private static SheetHandler sheetHandler; + + public LinkedHashMap getRowContents() { + return rowContents; + } + + public static void setRowContents(LinkedHashMap rc) { + rowContents = rc; + } + + public SheetHandler getSheetHandler() { + return sheetHandler; + } + + public static void setSheetHandler(SheetHandler sh) { + sheetHandler = sh; + } + + public static List> processSheetByRId(InputStream in, Integer count) throws Exception { + OPCPackage pkg = null; + InputStream sheet = null; + List> results = new ArrayList>(); + try { + pkg = OPCPackage.open(in); + XSSFReader r = new XSSFReader(pkg); + SharedStringsTable sst = r.getSharedStringsTable(); + + for (int i = 0; i < count; i++) { + sheet = r.getSheet("rId" + (i + 1)); + InputSource sheetSource = new InputSource(sheet); + XMLReader parser = fetchSheetParser(sst); + parser.parse(sheetSource); + results.add(sheetHandler.getRowContents()); + } + return results; + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + if (pkg != null) { + pkg.close(); + } + if (sheet != null) { + sheet.close(); + } + } + } + + // 处理一个sheet + public static void processOneSheet(String filename) throws Exception { + InputStream sheet2 = null; + OPCPackage pkg = null; + try { + pkg = OPCPackage.open(filename); + XSSFReader r = new XSSFReader(pkg); + SharedStringsTable sst = r.getSharedStringsTable(); + XMLReader parser = fetchSheetParser(sst); + sheet2 = r.getSheet("rId1"); + InputSource sheetSource = new InputSource(sheet2); + parser.parse(sheetSource); + setRowContents(sheetHandler.getRowContents()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + if (pkg != null) { + pkg.close(); + } + if (sheet2 != null) { + sheet2.close(); + } + } + } + + // 处理一个sheet + public static void processOneSheet(InputStream is) throws Exception { + InputStream sheet2 = null; + OPCPackage pkg = null; + try { + pkg = OPCPackage.open(is); + XSSFReader r = new XSSFReader(pkg); + SharedStringsTable sst = r.getSharedStringsTable(); + XMLReader parser = fetchSheetParser(sst); + sheet2 = r.getSheet("rId1"); + InputSource sheetSource = new InputSource(sheet2); + parser.parse(sheetSource); + setRowContents(sheetHandler.getRowContents()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + if (pkg != null) { + pkg.close(); + } + if (sheet2 != null) { + sheet2.close(); + } + } + } + + // 处理多个sheet + public static void processAllSheets(String filename) throws Exception { + OPCPackage pkg = null; + InputStream sheet = null; + try { + pkg = OPCPackage.open(filename); + XSSFReader r = new XSSFReader(pkg); + SharedStringsTable sst = r.getSharedStringsTable(); + XMLReader parser = fetchSheetParser(sst); + Iterator sheets = r.getSheetsData(); + while (sheets.hasNext()) { + System.out.println("Processing new sheet:\n"); + sheet = sheets.next(); + InputSource sheetSource = new InputSource(sheet); + parser.parse(sheetSource); + } + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + if (pkg != null) { + pkg.close(); + } + if (sheet != null) { + sheet.close(); + } + } + } + + public static XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException { + XMLReader parser = XMLReaderFactory.createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser"); + setSheetHandler(new SheetHandler(sst)); + ContentHandler handler = (ContentHandler) sheetHandler; + parser.setContentHandler(handler); + return parser; + } + + /** + * See org.xml.sax.helpers.DefaultHandler + */ + // 测试 + public void test() throws Exception { + Long time = System.currentTimeMillis(); + ExcelUtil example = new ExcelUtil(); + example.processOneSheet("C:/Users/Desktop/2018041310024112.xlsx"); + Long endtime = System.currentTimeMillis(); + LinkedHashMap map = example.getRowContents(); + Iterator> it = map.entrySet().iterator(); + int count = 0; + String prePos = ""; + while (it.hasNext()) { + Map.Entry entry = (Map.Entry) it.next(); + String pos = entry.getKey(); + if (!pos.substring(1).equals(prePos)) { + prePos = pos.substring(1); + count++; + } + System.out.println(pos + ";" + entry.getValue()); + } + System.out.println("解析数据" + count + "条;耗时" + (endtime - time) / 1000 + "秒"); + } +} + + + diff --git a/src/main/java/com/peanut/common/excel/SheetHandler.java b/src/main/java/com/peanut/common/excel/SheetHandler.java new file mode 100644 index 00000000..3edcd440 --- /dev/null +++ b/src/main/java/com/peanut/common/excel/SheetHandler.java @@ -0,0 +1,68 @@ +package com.peanut.common.excel; + +import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +import java.util.LinkedHashMap; + +/** + * @description: 重写startElement() endElement() characters() 用于把Excel数据封装到 LinkedHashMap 中 + * @date: 2021/12/16 15:10 + */ +public class SheetHandler extends DefaultHandler { + private SharedStringsTable sst; + private String lastContents; + private boolean nextIsString; + private String cellPosition; + private LinkedHashMap rowContents = new LinkedHashMap(); + + public LinkedHashMap getRowContents() { + return rowContents; + } + + public void setRowContents(LinkedHashMap rowContents) { + this.rowContents = rowContents; + } + + public SheetHandler(SharedStringsTable sst) { + this.sst = sst; + } + + public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { + if (name.equals("c")) { + cellPosition = attributes.getValue("r"); + String cellType = attributes.getValue("t"); + if (cellType != null && cellType.equals("s")) { + nextIsString = true; + } else { + nextIsString = false; + } + } + // 清楚缓存内容 + lastContents = ""; + } + + public void endElement(String uri, String localName, String name) + throws SAXException { + if (nextIsString) { + int idx = Integer.parseInt(lastContents); + lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString(); + nextIsString = false; + } + if (name.equals("v")) { + // 数据读取结束后,将单元格坐标,内容存入map中 + if (!(cellPosition.length() == 2) || (cellPosition.length() == 2 && !"1".equals(cellPosition.substring(1)))) { //不保存第一行数据 + rowContents.put(cellPosition, lastContents); + } + } + } + + public void characters(char[] ch, int start, int length) throws SAXException { + lastContents += new String(ch, start, length); + } +} + + diff --git a/src/main/java/com/peanut/modules/common/controller/ClassController.java b/src/main/java/com/peanut/modules/common/controller/ClassController.java index 4a5a1036..84f74db6 100644 --- a/src/main/java/com/peanut/modules/common/controller/ClassController.java +++ b/src/main/java/com/peanut/modules/common/controller/ClassController.java @@ -33,20 +33,56 @@ public class ClassController { //新增班级模型 @RequestMapping("/addClassModel") public R addClassModel(@RequestBody ClassModel classModel){ - if (classEntityService.addClassModel(classModel)){ - return R.ok(); + int score = 0; + if (classModel.getIsQuestion()==1){ + score += classModel.getQuestionScore(); + } + if (classModel.getIsTask()==1){ + score += classModel.getTaskScore(); + } + if (classModel.getIsMedicalcase()==1){ + score += classModel.getMedicalcaseScore(); + } + if (classModel.getIsExperience()==1){ + score += classModel.getExperienceScore(); + } + if (classModel.getIsExam()==1){ + score += classModel.getExamScore(); + } + if (score==100){ + if (classEntityService.addClassModel(classModel)){ + return R.ok(); + }else { + return R.error("保存出错"); + } }else { - return R.error("保存出错"); + return R.error("请设置各模块占比总和为100"); } } //修改班级模型 @RequestMapping("/editClassModel") public R editClassModel(@RequestBody ClassModel classModel){ - if (classEntityService.editClassModel(classModel)){ - return R.ok(); + int score = 0; + if (classModel.getIsQuestion()==1){ + score += classModel.getQuestionScore(); + } + if (classModel.getIsTask()==1){ + score += classModel.getTaskScore(); + } + if (classModel.getIsMedicalcase()==1){ + score += classModel.getMedicalcaseScore(); + } + if (classModel.getIsExperience()==1){ + score += classModel.getExperienceScore(); + } + if (classModel.getIsExam()==1){ + score += classModel.getExamScore(); + } + if (score==100){ + return classEntityService.editClassModel(classModel); }else { - return R.error("已存在小班,不能修改"); + return R.error("请设置各模块占比总和为100"); } } diff --git a/src/main/java/com/peanut/modules/common/controller/ClassExamController.java b/src/main/java/com/peanut/modules/common/controller/ClassExamController.java index 8be2b117..a85c7ae7 100644 --- a/src/main/java/com/peanut/modules/common/controller/ClassExamController.java +++ b/src/main/java/com/peanut/modules/common/controller/ClassExamController.java @@ -1,6 +1,7 @@ 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; @@ -9,9 +10,9 @@ 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.transaction.interceptor.TransactionAspectSupport; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -37,93 +38,83 @@ public class ClassExamController { @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 subjectList = new ArrayList<>(); List 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); - } - } - } + Long time = System.currentTimeMillis(); + ExcelUtil example = new ExcelUtil(); + example.processOneSheet(fis); + Long endtime = System.currentTimeMillis(); + LinkedHashMap map = example.getRowContents(); + Iterator> it = map.entrySet().iterator(); + int count = 0; + while (it.hasNext()) { + Map.Entry 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); + 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("结束"+(new Date().getTime()-date.getTime())); + System.out.println("解析数据" + count + "条;耗时" + (endtime - time) / 1000 + "秒"); } catch (Exception e) { e.printStackTrace(); + TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); + return R.error(e.getMessage()); } return R.ok(); } - private static boolean isRowEmpty(Row row) { - for (Cell cell : row) { - if (cell.getCellTypeEnum() != CellType.BLANK) { - return false; + public void insertOption(List 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); } - } - 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(); - }else { - return R.error(res); } } diff --git a/src/main/java/com/peanut/modules/common/entity/ClassModel.java b/src/main/java/com/peanut/modules/common/entity/ClassModel.java index 7e74893a..2b9d7664 100644 --- a/src/main/java/com/peanut/modules/common/entity/ClassModel.java +++ b/src/main/java/com/peanut/modules/common/entity/ClassModel.java @@ -25,6 +25,17 @@ public class ClassModel { private Integer days;//教学天数 + private Integer isQuestion; + private Integer questionScore; + private Integer isTask; + private Integer taskScore; + private Integer isMedicalcase; + private Integer medicalcaseScore; + private Integer isExperience; + private Integer experienceScore; + private Integer isExam; + private Integer examScore; + @TableLogic private Integer delFlag; diff --git a/src/main/java/com/peanut/modules/common/service/ClassEntityService.java b/src/main/java/com/peanut/modules/common/service/ClassEntityService.java index 45f03739..4df99e86 100644 --- a/src/main/java/com/peanut/modules/common/service/ClassEntityService.java +++ b/src/main/java/com/peanut/modules/common/service/ClassEntityService.java @@ -13,7 +13,7 @@ public interface ClassEntityService extends IService { boolean addClassModel(ClassModel classModel); - boolean editClassModel(ClassModel classModel); + R editClassModel(ClassModel classModel); List getClassModelByUserid(Map params); diff --git a/src/main/java/com/peanut/modules/common/service/ClassExamService.java b/src/main/java/com/peanut/modules/common/service/ClassExamService.java index 21baa6c1..862c48d9 100644 --- a/src/main/java/com/peanut/modules/common/service/ClassExamService.java +++ b/src/main/java/com/peanut/modules/common/service/ClassExamService.java @@ -5,13 +5,10 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.peanut.modules.common.entity.ClassExam; import com.peanut.modules.common.entity.ClassExamOption; import com.peanut.modules.common.entity.ClassExamSubject; -import org.springframework.web.multipart.MultipartFile; import java.util.Map; public interface ClassExamService extends IService { - String importSubject(MultipartFile file, int courseId); - void addClassExamSubject(ClassExamSubject classExamSubject); void addClassExamOption(ClassExamOption classExamOption); diff --git a/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java index f9735faa..fb811c89 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java @@ -43,8 +43,6 @@ public class ClassEntityServiceImpl extends ServiceImpl params) { @@ -79,12 +77,12 @@ public class ClassEntityServiceImpl extends ServiceImpl wrapper = new LambdaQueryWrapper<>(); wrapper.eq(ClassEntity::getModelId,classModel.getId()); //模型下有小班,不能编辑 if (this.getBaseMapper().selectCount(wrapper)>0){ - return false; + return R.error("模型下有小班,不能编辑"); }else { classModelDao.updateById(classModel); //查询出所属课程,先删除,在添加 @@ -100,7 +98,7 @@ public class ClassEntityServiceImpl extends ServiceImpl params) { String state = params.get("state").toString(); ClassEntity classEntity = this.getBaseMapper().selectById(params.get("classId").toString()); + ClassModel classModel = classModelDao.selectById(classEntity.getModelId()); if ("1".equals(state)){ //开班校验 int monitorCount = classUserDao.selectCount(new LambdaQueryWrapper() @@ -232,17 +231,49 @@ public class ClassEntityServiceImpl extends ServiceImpl() - .eq(ClassTask::getClassId,classEntity.getId()).eq(ClassTask::getType,"0")); - ClassModel classModel = classModelDao.selectById(classEntity.getModelId()); - if (taskCount<(classModel.getDays()/7.0)){ - return R.error("请至少发布"+Math.ceil(classModel.getDays()/7.0)+"个任务。"); + + if (classModel.getIsTask()==1){ + int taskCount = classTaskDao.selectCount(new LambdaQueryWrapper() + .eq(ClassTask::getClassId,classEntity.getId()).eq(ClassTask::getType,"0")); + if (taskCount<(classModel.getDays()/7.0)){ + return R.error("请至少发布"+Math.ceil(classModel.getDays()/7.0)+"个任务。"); + } } + int studentCount = classUserDao.selectCount(new LambdaQueryWrapper() .eq(ClassUser::getClassId,classEntity.getId()).eq(ClassUser::getRole,"0")); if (studentCount userList = classUserDao.selectList(new LambdaQueryWrapper() + .eq(ClassUser::getClassId,classEntity.getId()).eq(ClassUser::getRole,"0")); + StringBuffer sb = new StringBuffer(); + if (userList.size() > 0) { + for (ClassUser classUser:userList){ + MyUserEntity user = myUserDao.selectById(classUser.getUserId()); + if ("0".equals(user.getVip())||"3".equals(user.getVip())){ + //不是vip查询每门课是否购买 + boolean flag = false; + List courses = classCourseDao.selectList(new LambdaQueryWrapper() + .eq(ClassCourse::getModelId,classEntity.getModelId())); + for (ClassCourse classCourse:courses){ + List ucb = userCourseBuyDao.selectList(new LambdaQueryWrapper() + .eq(UserCourseBuyEntity::getUserId,classUser.getUserId()) + .eq(UserCourseBuyEntity::getCourseId,classCourse.getCourseId())); + if (ucb.size() == 0){ + flag = true; + CourseEntity c = courseDao.selectById(classCourse.getCourseId()); + sb.append("用户"+ user.getTel()+"未购买课程"+c.getTitle()+";\n"); + } + } + if (flag) { + return R.error(sb.toString()); + } + } + } + } + classEntity.setState("1"); Date startTime = new Date(); classEntity.setStartTime(startTime); diff --git a/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java index 8f660204..57332e08 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java @@ -11,14 +11,8 @@ import com.peanut.modules.common.entity.*; import com.peanut.modules.common.service.ClassExamService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.ss.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; -import java.io.InputStream; import java.util.*; @Slf4j @@ -34,86 +28,6 @@ public class ClassExamServiceImpl extends ServiceImpl i @Autowired private ClassExamUserDao classExamUserDao; - @Override - @Transactional - public String importSubject(MultipartFile file, int courseId) { - try (InputStream fis = file.getInputStream()) { - Workbook workbook = WorkbookFactory.create(fis); - Sheet sheet = workbook.getSheetAt(0); - Date date = new Date(); - // 遍历行 - 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()); - classExamSubjectDao.insert(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); - classExamOptionDao.insert(option); - } - } - } - } - } - System.out.println("结束"+(new Date().getTime()-date.getTime())); - } catch (IOException | InvalidFormatException e) { - e.printStackTrace(); - } - return null; - } - private static boolean isRowEmpty(Row row) { - for (Cell cell : row) { - if (cell.getCellTypeEnum() != CellType.BLANK) { - return false; - } - } - return true; - } - @Override public void addClassExamSubject(ClassExamSubject classExamSubject) { classExamSubjectDao.insert(classExamSubject);