master
小萌新 4 months ago
parent 97c823b15c
commit 6a57683f50
  1. 1
      src/main/java/com/teaching/backend/mapper/chapter/TemporaryChapterMapper.java
  2. 6
      src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java
  3. 29
      src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.teaching.backend.model.entity.chapter.Chapter;
import com.teaching.backend.model.entity.chapter.TemporaryChapter;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@ -10,6 +10,7 @@ package com.teaching.backend.utils.Chapter;
* @Version 1.0
*/
import com.teaching.backend.mapper.chapter.TemporaryChapterMapper;
import com.teaching.backend.service.chapter.IChapterService;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
@ -31,7 +32,8 @@ import java.util.List;
*/
@Component
public class ExcelParser {
@Autowired
TemporaryChapterMapper temporaryChapterMapper;
@Autowired
IChapterService chapterService;
@ -44,7 +46,7 @@ public class ExcelParser {
StylesTable styles = reader.getStylesTable();
XMLReader parser = XMLReaderFactory.createXMLReader();
SheetHandler sheetHandler = new SheetHandler(chapterService, courseId);
SheetHandler sheetHandler = new SheetHandler(temporaryChapterMapper, chapterService, courseId);
parser.setContentHandler(new XSSFSheetXMLHandler(styles, sst, sheetHandler, false));
XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) reader.getSheetsData();

@ -1,12 +1,13 @@
package com.teaching.backend.utils.Chapter;
import com.teaching.backend.common.ErrorCode;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.exception.BusinessException;
import com.teaching.backend.mapper.chapter.TemporaryChapterMapper;
import com.teaching.backend.model.dto.chapter.ChapterDTO;
import com.teaching.backend.service.chapter.IChapterService;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@ -22,6 +23,9 @@ import java.util.*;
public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {
private final TemporaryChapterMapper temporaryChapterMapper;
private final IChapterService chapterService;
private final String courseId;
private ChapterDTO chapterDTO;
@ -30,7 +34,8 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {
private boolean hasErrors = false;
private List<ChapterDTO> collectedChapters = new ArrayList<>();
public SheetHandler(IChapterService chapterService, String courseId) {
public SheetHandler(TemporaryChapterMapper temporaryChapterMapper, IChapterService chapterService, String courseId) {
this.temporaryChapterMapper = temporaryChapterMapper;
this.chapterService = chapterService;
this.courseId = courseId;
}
@ -58,6 +63,7 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {
collectedChapters.add(chapterDTO);
}
}
@Transactional(rollbackFor = Exception.class)
public List<String> finalizeProcess() {
if (hasErrors) {
@ -70,25 +76,30 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {
}
}
for (ChapterDTO chapter : collectedChapters) {
if (chapter.getParentExcelId() != null && chapter.getParentExcelId() != 0) {
saveToTemporaryTable(chapter);
for (ChapterDTO chapter : collectedChapters) {
if (chapter.getParentExcelId() != null && chapter.getParentExcelId() != 0) {
saveToTemporaryTable(chapter);
}
}
}
chapterService.moveDataFromTemporaryToFinal();
return Collections.emptyList();
}
private void saveToTemporaryTable(ChapterDTO chapterDTO) {
private void saveToTemporaryTable(ChapterDTO chapterDTO) {
Long pid = chapterDTO.getParentExcelId();
if (pid != null && pid != 0) {
Long parentId = excelIdToDatabaseIdMap.get(pid);
if (parentId != null) {
chapterDTO.setPid(parentId);
} else {
temporaryChapterMapper.deleteAll();
throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,Excel ID 为: " + pid);
}
}
@ -105,12 +116,16 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {
if (chapterDTO.getName() == null || chapterDTO.getName().isEmpty()) {
errors.add("编号:"+chapterDTO.getExcelId()+"的章节名字不能为空");
}
if (chapterDTO.getExcelId() == null || chapterDTO.getExcelId() <= 0) {
errors.add("编号不能小于0");
}
if (chapterDTO.getParentExcelId() == null || chapterDTO.getParentExcelId() < 0) {
errors.add("编号:"+chapterDTO.getExcelId()+"的上级编号不能小于0");
}
if (chapterDTO.getParentExcelId().equals(chapterDTO.getExcelId())) {
errors.add("编号:"+chapterDTO.getExcelId()+"的 上级编号不能等于其自身");
}
return errors;
}

Loading…
Cancel
Save