|
|
|
@ -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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|