图书管理章节拆分

This commit is contained in:
wuchunlei
2024-01-15 11:42:41 +08:00
parent 1313f78c4a
commit f0c464ab1a

View File

@@ -17,6 +17,7 @@ import com.peanut.common.utils.Query;
import com.peanut.modules.book.dao.BookDao;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.entity.SysDictDataEntity;
import com.peanut.modules.book.service.BookChapterContentService;
import com.peanut.modules.book.service.BookChapterService;
import com.peanut.modules.book.service.BookService;
import com.peanut.modules.book.service.PublisherService;
@@ -52,6 +53,8 @@ public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements
private BookChapterService bookChapterService;
@Autowired
ConstantPropertiesUtils constantPropertiesUtils;
@Autowired
private BookChapterContentService bookChapterContentService;
@Override
@@ -452,86 +455,43 @@ public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements
String novel = bookEntity.getNovel();
InputStream inputStream = FileDownloadUtil.getInputStream(novel);
Document doc = new Document(inputStream);
String title2Text = "";
Map<String, List<String[]>> paraMap = new HashMap<>();
Map<String, String> headMap = new HashMap<>();
List<String> title_list = new ArrayList<>();
int index = 0;
int x1 = 0;
int x2 = 0;
String chapterName = "";
int chapterId = 0;
for (int i = 0; i < doc.getSections().getCount(); i++) {
String currentTitle = "";
Section section = doc.getSections().get(i);
for (int j = 0; j < section.getParagraphs().getCount() - 1; j++) {
Paragraph paragraph = section.getParagraphs().get(j);
if (paragraph.getStyleName().equals("Heading1")) {
String textq = paragraph.getText();
currentTitle = textq;
title_list.add(currentTitle);
List<String[]> secondTitleList = new ArrayList<>();
paraMap.put(currentTitle, secondTitleList);
if (!section.getParagraphs().get(j + 1).getStyleName().matches("Heading2")) {
int head_index = j;
StringBuilder builder = new StringBuilder();
do {
builder.append(section.getParagraphs().get(head_index).getText());
head_index++;
//防止index超出异常发生
if (head_index >= section.getParagraphs().getCount()) {
break;
}
} while (!section.getParagraphs().get(head_index).getStyleName().equals("Heading2"));
String head_text = builder.toString();
headMap.put(currentTitle, head_text);
BookChapterEntity chapter = new BookChapterEntity();
chapter.setBookId(bookId);
chapter.setNumber(index++);
chapter.setSort(j);
if (paragraph.getStyleName().equals("Heading1")&&StringUtils.isNotBlank(paragraph.getText())) {
x1 = j;
chapterName = paragraph.getText();
}else if (paragraph.getStyleName().equals("Heading2")&&StringUtils.isNotBlank(paragraph.getText())){
x2 = j;
chapter.setChapter(chapterName);
chapter.setContent(paragraph.getText());
System.out.println(paragraph.getText());
bookChapterService.save(chapter);
chapterId = chapter.getId();
}else if (StringUtils.isNotBlank(paragraph.getText())){
if (x1 > x2){
chapter.setChapter(chapterName);
chapter.setContent(paragraph.getText());
System.out.println(paragraph.getText());
bookChapterService.save(chapter);
}else {
BookChapterContentEntity content = new BookChapterContentEntity();
content.setBookId(bookId);
content.setBookChatperId(chapterId);
content.setNumber(index);
content.setContent(paragraph.getText());
bookChapterContentService.save(content);
}
} else if (paragraph.getStyleName().equals("Heading2") && StringUtils.isNotBlank(paragraph.getText().trim())) {
StringBuilder builder = new StringBuilder();
boolean isRun = false;
do {
if (isRun) {
builder.append(section.getParagraphs().get(j).getText());
}
isRun = true;
j++;
//防止index超出异常发生
if (j >= section.getParagraphs().getCount()) {
break;
}
} while (!section.getParagraphs().get(j).getStyleName().equals("Heading2") && !section.getParagraphs().get(j).getStyleName().equals("Heading1"));
j--;
String text = builder.toString();
title2Text = paragraph.getText(); // 获取标题二的文本内容
if (null != title2Text && !"".equals(title2Text)) {
String[] s = new String[]{title2Text, text};
paraMap.get(currentTitle).add(s);
}
}
}
}
int index = 0;
for (String title : title_list) {
if (null == paraMap.get(title) || paraMap.get(title).size() == 0) {
index++;
BookChapterEntity bookChapterEntity = new BookChapterEntity();
bookChapterEntity.setBookId(bookId);
bookChapterEntity.setNumber(index);
bookChapterEntity.setChapter(title);
bookChapterEntity.setSort(1);
if (null != headMap.get(title)) {
bookChapterEntity.setContent(headMap.get(title));
} else {
bookChapterEntity.setContent("");
}
bookChapterService.save(bookChapterEntity);
} else {
for (String[] t : paraMap.get(title)) {
index++;
BookChapterEntity bookChapterEntity = new BookChapterEntity();
bookChapterEntity.setBookId(bookId);
bookChapterEntity.setSort(2);
bookChapterEntity.setNumber(index);
bookChapterEntity.setChapter(title + "," + t[0]);
bookChapterEntity.setContent(t[1]);
bookChapterService.save(bookChapterEntity);
}
}
}