自动更新优化

This commit is contained in:
xxy 2020-05-07 04:57:41 +08:00
parent 320d693238
commit 16aa925521
3 changed files with 15 additions and 10 deletions

View File

@ -58,7 +58,7 @@ public class StarterListener implements ServletContextListener {
Map<Integer, BookIndex> existBookIndexMap = bookService.queryExistBookIndexMap(needUpdateBook.getId()); Map<Integer, BookIndex> existBookIndexMap = bookService.queryExistBookIndexMap(needUpdateBook.getId());
//解析章节目录 //解析章节目录
Map<Integer, List> indexAndContentList = CrawlParser.parseBookIndexAndContent(needUpdateBook.getCrawlBookId(),book, ruleBean, existBookIndexMap); Map<Integer, List> indexAndContentList = CrawlParser.parseBookIndexAndContent(needUpdateBook.getCrawlBookId(),book, ruleBean, existBookIndexMap);
bookService.updateBookAndIndexAndContent(book, (List<BookIndex>) indexAndContentList.get(CrawlParser.BOOK_INDEX_LIST_KEY), (List<BookContent>) indexAndContentList.get(CrawlParser.BOOK_CONTENT_LIST_KEY)); bookService.updateBookAndIndexAndContent(book, (List<BookIndex>) indexAndContentList.get(CrawlParser.BOOK_INDEX_LIST_KEY), (List<BookContent>) indexAndContentList.get(CrawlParser.BOOK_CONTENT_LIST_KEY),existBookIndexMap);
}catch (Exception e){ }catch (Exception e){
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
//解析异常中断更新一下小说的最后解析时间 //解析异常中断更新一下小说的最后解析时间

View File

@ -65,8 +65,9 @@ public interface BookService {
* @param book 小说数据 * @param book 小说数据
* @param bookIndexList 目录集合 * @param bookIndexList 目录集合
* @param bookContentList 内容集合 * @param bookContentList 内容集合
* @param existBookIndexMap 已存在的章节Map
* */ * */
void updateBookAndIndexAndContent(Book book, List<BookIndex> bookIndexList, List<BookContent> bookContentList); void updateBookAndIndexAndContent(Book book, List<BookIndex> bookIndexList, List<BookContent> bookContentList, Map<Integer, BookIndex> existBookIndexMap);
/** /**
* 更新一下最后一次的抓取时间 * 更新一下最后一次的抓取时间

View File

@ -8,7 +8,6 @@ import com.java2nb.novel.mapper.*;
import com.java2nb.novel.service.BookService; import com.java2nb.novel.service.BookService;
import com.java2nb.novel.utils.Constants; import com.java2nb.novel.utils.Constants;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.mybatis.dynamic.sql.Constant;
import org.mybatis.dynamic.sql.render.RenderingStrategies; import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; 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.crawlBookId;
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.crawlSourceId; 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 com.java2nb.novel.mapper.CrawlSourceDynamicSqlSupport.id;
import static org.mybatis.dynamic.sql.SqlBuilder.*; import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static org.mybatis.dynamic.sql.select.SelectDSL.select; import static org.mybatis.dynamic.sql.select.SelectDSL.select;
@ -119,7 +117,7 @@ public class BookServiceImpl implements BookService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public void updateBookAndIndexAndContent(Book book, List<BookIndex> bookIndexList, List<BookContent> bookContentList) { public void updateBookAndIndexAndContent(Book book, List<BookIndex> bookIndexList, List<BookContent> bookContentList, Map<Integer, BookIndex> existBookIndexMap) {
Date currentDate = new Date(); Date currentDate = new Date();
for (int i = 0; i < bookIndexList.size(); i++) { for (int i = 0; i < bookIndexList.size(); i++) {
BookIndex bookIndex = bookIndexList.get(i); BookIndex bookIndex = bookIndexList.get(i);
@ -160,11 +158,17 @@ public class BookServiceImpl implements BookService {
} }
//更新小说主表 //更新小说主表
book.setWordCount(queryTotalWordCount(book.getId())); if(bookIndexList.size()>0) {
BookIndex lastIndex = queryLastIndex(book.getId()); //有更新章节才需要更新以下字段
book.setLastIndexId(lastIndex.getId()); book.setWordCount(queryTotalWordCount(book.getId()));
book.setLastIndexName(lastIndex.getIndexName()); BookIndex lastIndex = bookIndexList.get(bookIndexList.size()-1);
book.setLastIndexUpdateTime(lastIndex.getUpdateTime()); if(!existBookIndexMap.containsKey(lastIndex.getIndexNum())) {
//如果最新章节不在已存在章节中那么更新小说表最新章节信息
book.setLastIndexId(lastIndex.getId());
book.setLastIndexName(lastIndex.getIndexName());
book.setLastIndexUpdateTime(currentDate);
}
}
book.setUpdateTime(currentDate); book.setUpdateTime(currentDate);
book.setBookName(null); book.setBookName(null);
book.setAuthorName(null); book.setAuthorName(null);