diff --git a/novel-crawl/src/main/java/com/java2nb/novel/core/listener/StarterListener.java b/novel-crawl/src/main/java/com/java2nb/novel/core/listener/StarterListener.java index 711ed77..c6bbd13 100644 --- a/novel-crawl/src/main/java/com/java2nb/novel/core/listener/StarterListener.java +++ b/novel-crawl/src/main/java/com/java2nb/novel/core/listener/StarterListener.java @@ -58,7 +58,7 @@ public class StarterListener implements ServletContextListener { Map existBookIndexMap = bookService.queryExistBookIndexMap(needUpdateBook.getId()); //解析章节目录 Map indexAndContentList = CrawlParser.parseBookIndexAndContent(needUpdateBook.getCrawlBookId(),book, ruleBean, existBookIndexMap); - bookService.updateBookAndIndexAndContent(book, (List) indexAndContentList.get(CrawlParser.BOOK_INDEX_LIST_KEY), (List) indexAndContentList.get(CrawlParser.BOOK_CONTENT_LIST_KEY)); + bookService.updateBookAndIndexAndContent(book, (List) indexAndContentList.get(CrawlParser.BOOK_INDEX_LIST_KEY), (List) indexAndContentList.get(CrawlParser.BOOK_CONTENT_LIST_KEY),existBookIndexMap); }catch (Exception e){ log.error(e.getMessage(), e); //解析异常中断,更新一下小说的最后解析时间 diff --git a/novel-crawl/src/main/java/com/java2nb/novel/service/BookService.java b/novel-crawl/src/main/java/com/java2nb/novel/service/BookService.java index a606b34..8585fd1 100644 --- a/novel-crawl/src/main/java/com/java2nb/novel/service/BookService.java +++ b/novel-crawl/src/main/java/com/java2nb/novel/service/BookService.java @@ -65,8 +65,9 @@ public interface BookService { * @param book 小说数据 * @param bookIndexList 目录集合 * @param bookContentList 内容集合 + * @param existBookIndexMap 已存在的章节Map * */ - void updateBookAndIndexAndContent(Book book, List bookIndexList, List bookContentList); + void updateBookAndIndexAndContent(Book book, List bookIndexList, List bookContentList, Map existBookIndexMap); /** * 更新一下最后一次的抓取时间 diff --git a/novel-crawl/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java b/novel-crawl/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java index 399e772..0badbec 100644 --- a/novel-crawl/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java +++ b/novel-crawl/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java @@ -8,7 +8,6 @@ import com.java2nb.novel.mapper.*; import com.java2nb.novel.service.BookService; import com.java2nb.novel.utils.Constants; import lombok.RequiredArgsConstructor; -import org.mybatis.dynamic.sql.Constant; import org.mybatis.dynamic.sql.render.RenderingStrategies; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,7 +21,6 @@ import java.util.stream.Collectors; import static com.java2nb.novel.mapper.BookDynamicSqlSupport.crawlBookId; import static com.java2nb.novel.mapper.BookDynamicSqlSupport.crawlSourceId; -import static com.java2nb.novel.mapper.BookDynamicSqlSupport.picUrl; import static com.java2nb.novel.mapper.CrawlSourceDynamicSqlSupport.id; import static org.mybatis.dynamic.sql.SqlBuilder.*; import static org.mybatis.dynamic.sql.select.SelectDSL.select; @@ -119,7 +117,7 @@ public class BookServiceImpl implements BookService { @Transactional(rollbackFor = Exception.class) @Override - public void updateBookAndIndexAndContent(Book book, List bookIndexList, List bookContentList) { + public void updateBookAndIndexAndContent(Book book, List bookIndexList, List bookContentList, Map existBookIndexMap) { Date currentDate = new Date(); for (int i = 0; i < bookIndexList.size(); i++) { BookIndex bookIndex = bookIndexList.get(i); @@ -160,11 +158,17 @@ public class BookServiceImpl implements BookService { } //更新小说主表 - book.setWordCount(queryTotalWordCount(book.getId())); - BookIndex lastIndex = queryLastIndex(book.getId()); - book.setLastIndexId(lastIndex.getId()); - book.setLastIndexName(lastIndex.getIndexName()); - book.setLastIndexUpdateTime(lastIndex.getUpdateTime()); + if(bookIndexList.size()>0) { + //有更新章节,才需要更新以下字段 + book.setWordCount(queryTotalWordCount(book.getId())); + BookIndex lastIndex = bookIndexList.get(bookIndexList.size()-1); + if(!existBookIndexMap.containsKey(lastIndex.getIndexNum())) { + //如果最新章节不在已存在章节中,那么更新小说表最新章节信息 + book.setLastIndexId(lastIndex.getId()); + book.setLastIndexName(lastIndex.getIndexName()); + book.setLastIndexUpdateTime(currentDate); + } + } book.setUpdateTime(currentDate); book.setBookName(null); book.setAuthorName(null);