|
@@ -0,0 +1,94 @@
|
|
|
+package com.sf.handler;
|
|
|
+
|
|
|
+import com.sf.mapper.BookChapterMapper;
|
|
|
+import com.sf.mapper.BookContentMapper;
|
|
|
+import com.sf.mapper.BookInfoMapper;
|
|
|
+import com.sf.po.BookChapter;
|
|
|
+import com.sf.po.BookContent;
|
|
|
+import com.sf.po.BookInfo;
|
|
|
+import com.sf.util.EpubUtils;
|
|
|
+import com.sf.util.vo.ChapterVo;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据处理器
|
|
|
+ * 编写导入数据的逻辑
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class DataHandler {
|
|
|
+
|
|
|
+ // book_info book_chapter book_content
|
|
|
+ // CRUD
|
|
|
+ // 可以在类别表中新增一行数据 代表一个新的类别book_category
|
|
|
+ // INSERT INTO `book_info` (`id`, `work_direction`, `category_id`, `category_name`, `pic_url`, `book_name`, `author_id`, `author_name`, `book_desc`, `score`, `book_status`, `visit_count`, `word_count`, `comment_count`, `last_chapter_id`, `last_chapter_name`, `last_chapter_update_time`, `is_vip`, `create_time`, `update_time`)
|
|
|
+ // VALUES
|
|
|
+ // (1431630596354977792, 0, 3, '都市言情', '/images/2023/11/30/1cfaa56c4e32bbef.jpg', '重生之夫妻', 0, '木易秋', ' 一生一世哪够,生生世世才够。一个是军人世家出身,冷酷无情。一个是医学怪才,从不心软。这样的两个人撞到了一起一个褪下了冷酷唯爱温柔,一个娇俏可爱,夫妻相互扶持一生到老。原以为俩人的缘分就这么一辈子,约<br/>', 7, 0, 1734, 364638, 0, 1432076089790877696, '第一百八十五章 智灭毒藤', '2021-08-30 04:21:59', 0, NULL, NULL);
|
|
|
+ // 参考原始数据来构造新的数据
|
|
|
+ // work_direction 0-男频 1-女频
|
|
|
+ // book_status 书籍状态;0-连载中 1-已完结
|
|
|
+ // is_vip 是否收费;1-收费 0-免费
|
|
|
+ // author_id 是0
|
|
|
+ // 重要信息有 book_name author_name book_desc
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookInfoMapper bookInfoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookChapterMapper bookChapterMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BookContentMapper bookContentMapper;
|
|
|
+
|
|
|
+ public void saveBookInfo() throws Exception{
|
|
|
+ BookInfo bookInfo = BookInfo.builder()
|
|
|
+ .bookName("长安的荔枝")
|
|
|
+ .authorId(0L)
|
|
|
+ .authorName("马伯庸")
|
|
|
+ .bookDesc("大唐天宝十四年,长安城的小吏李善德突然接到一个任务:要在贵妃诞日之前,从岭南运来新鲜荔枝。荔枝“一日色变,两日香变,三日味变”,而岭南距长安五千余里,山水迢迢,这是个不可能完成的任务,可为了家人,李善德决心放手一搏:“就算失败,我也想知道,自己倒在距离终点多远的地方。”")
|
|
|
+ .workDirection((byte) 1)
|
|
|
+ .categoryId(4L)
|
|
|
+ .categoryName("历史军事")
|
|
|
+ .picUrl("")
|
|
|
+ .score((byte) 0)
|
|
|
+ .bookStatus((byte) 1)
|
|
|
+ .visitCount(0L)
|
|
|
+ .wordCount(0)
|
|
|
+ .commentCount(0)
|
|
|
+ .isVip((byte) 0)
|
|
|
+ .createTime(LocalDateTime.now())
|
|
|
+ .updateTime(LocalDateTime.now())
|
|
|
+ .build();
|
|
|
+ bookInfoMapper.insert(bookInfo);
|
|
|
+
|
|
|
+ // 根据生成的bookId 继续存储章节内容 章节表中的章节名字和其他 生成章节id 再去章节内容表中存储
|
|
|
+ Long bookId = bookInfo.getId();
|
|
|
+ List<ChapterVo> chapterVos = EpubUtils.getChapterInfo("长安的荔枝 - 马伯庸");
|
|
|
+ for (int i = 0; i < chapterVos.size(); i++) {
|
|
|
+ ChapterVo chapterVo = chapterVos.get(i);
|
|
|
+ BookChapter bookChapter = BookChapter.builder()
|
|
|
+ .bookId(bookId)
|
|
|
+ .chapterName(chapterVo.getTitle())
|
|
|
+ .chapterNum(i)
|
|
|
+ .wordCount(chapterVo.getContent().length())
|
|
|
+ .isVip(0)
|
|
|
+ .createTime(LocalDateTime.now())
|
|
|
+ .updateTime(LocalDateTime.now())
|
|
|
+ .build();
|
|
|
+ bookChapterMapper.insert(bookChapter);
|
|
|
+
|
|
|
+ BookContent bookContent = BookContent.builder()
|
|
|
+ .chapterId(bookChapter.getId())
|
|
|
+ .content(chapterVo.getContent())
|
|
|
+ .createTime(LocalDateTime.now())
|
|
|
+ .updateTime(LocalDateTime.now())
|
|
|
+ .build();
|
|
|
+ bookContentMapper.insert(bookContent);
|
|
|
+
|
|
|
+ }
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+}
|