mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
事物优化
This commit is contained in:
parent
cca73526fb
commit
005f47a039
@ -45,4 +45,6 @@ public interface BookService {
|
|||||||
int indexRemove(Long id, Long bookId);
|
int indexRemove(Long id, Long bookId);
|
||||||
|
|
||||||
int batchIndexRemove(Long[] ids, Long[] bookIds);
|
int batchIndexRemove(Long[] ids, Long[] bookIds);
|
||||||
|
|
||||||
|
void saveBookAndIndexAndContent(BookDO book, List<BookIndexDO> bookIndex, List<BookContentDO> bookContent);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import com.java2nb.books.dao.BookIndexDao;
|
|||||||
import com.java2nb.books.domain.BookContentDO;
|
import com.java2nb.books.domain.BookContentDO;
|
||||||
import com.java2nb.books.domain.BookDO;
|
import com.java2nb.books.domain.BookDO;
|
||||||
import com.java2nb.books.domain.BookIndexDO;
|
import com.java2nb.books.domain.BookIndexDO;
|
||||||
|
import com.java2nb.books.service.BookService;
|
||||||
import com.java2nb.books.util.RestTemplateUtil;
|
import com.java2nb.books.util.RestTemplateUtil;
|
||||||
import com.java2nb.common.utils.DateUtils;
|
import com.java2nb.common.utils.DateUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -50,7 +51,7 @@ public class BookCrawlServiceImpl implements BookCrawlService {
|
|||||||
private BookIndexDao bookIndexDao;
|
private BookIndexDao bookIndexDao;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BookContentDao bookContentDao;
|
private BookService bookService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BookCrawlDO get(Long id) {
|
public BookCrawlDO get(Long id) {
|
||||||
@ -592,7 +593,7 @@ public class BookCrawlServiceImpl implements BookCrawlService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (indexList.size() == contentList.size() && indexList.size() > 0) {
|
if (indexList.size() == contentList.size() && indexList.size() > 0) {
|
||||||
saveBookAndIndexAndContent(book, indexList, contentList);
|
bookService.saveBookAndIndexAndContent(book, indexList, contentList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -820,7 +821,7 @@ public class BookCrawlServiceImpl implements BookCrawlService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (indexList.size() == contentList.size() && indexList.size() > 0) {
|
if (indexList.size() == contentList.size() && indexList.size() > 0) {
|
||||||
saveBookAndIndexAndContent(book, indexList, contentList);
|
bookService.saveBookAndIndexAndContent(book, indexList, contentList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -855,70 +856,9 @@ public class BookCrawlServiceImpl implements BookCrawlService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public void saveBookAndIndexAndContent(BookDO book, List<BookIndexDO> bookIndex, List<BookContentDO> bookContent) {
|
|
||||||
Long bookId = -1L;
|
|
||||||
book.setBookName(book.getBookName().trim());
|
|
||||||
book.setAuthor(book.getAuthor().trim());
|
|
||||||
Map<String, Object> bookExample = new HashMap<>();
|
|
||||||
bookExample.put("bookName", book.getBookName());
|
|
||||||
bookExample.put("author", book.getAuthor());
|
|
||||||
List<BookDO> books = bookDao.list(bookExample);
|
|
||||||
if (books.size() > 0) {
|
|
||||||
//更新
|
|
||||||
bookId = books.get(0).getId();
|
|
||||||
book.setId(bookId);
|
|
||||||
bookDao.update(book);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if (book.getVisitCount() == null) {
|
|
||||||
Long visitCount = generateVisiteCount(book.getScore());
|
|
||||||
book.setVisitCount(visitCount);
|
|
||||||
}
|
|
||||||
//插入
|
|
||||||
int rows = bookDao.save(book);
|
|
||||||
if (rows > 0) {
|
|
||||||
bookId = book.getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bookId >= 0) {
|
|
||||||
|
|
||||||
|
|
||||||
List<BookIndexDO> newBookIndexList = new ArrayList<>();
|
|
||||||
List<BookContentDO> newContentList = new ArrayList<>();
|
|
||||||
for (int i = 0; i < bookIndex.size(); i++) {
|
|
||||||
BookContentDO bookContentItem = bookContent.get(i);
|
|
||||||
if (!bookContentItem.getContent().contains("正在手打中,请稍等片刻,内容更新后,需要重新刷新页面,才能获取最新更新")) {
|
|
||||||
|
|
||||||
|
|
||||||
BookIndexDO bookIndexItem = bookIndex.get(i);
|
|
||||||
bookIndexItem.setBookId(bookId);
|
|
||||||
bookContentItem.setBookId(bookId);
|
|
||||||
bookContentItem.setIndexNum(bookIndexItem.getIndexNum());
|
|
||||||
|
|
||||||
newBookIndexList.add(bookIndexItem);
|
|
||||||
newContentList.add(bookContentItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newBookIndexList.size() > 0) {
|
|
||||||
bookIndexDao.insertBatch(newBookIndexList);
|
|
||||||
|
|
||||||
bookContentDao.insertBatch(newContentList);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Long generateVisiteCount(Float score) {
|
|
||||||
return Long.parseLong((int)(score*10000) + new Random().nextInt(1000) + "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getByTemplate(String catBookListUrl) {
|
private String getByTemplate(String catBookListUrl) {
|
||||||
try {
|
try {
|
||||||
|
@ -11,9 +11,7 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.java2nb.books.dao.BookDao;
|
import com.java2nb.books.dao.BookDao;
|
||||||
import com.java2nb.books.domain.BookDO;
|
import com.java2nb.books.domain.BookDO;
|
||||||
@ -131,4 +129,71 @@ public class BookServiceImpl implements BookService {
|
|||||||
return bookIndexDao.batchRemove(ids);
|
return bookIndexDao.batchRemove(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void saveBookAndIndexAndContent(BookDO book, List<BookIndexDO> bookIndex, List<BookContentDO> bookContent) {
|
||||||
|
Long bookId = -1L;
|
||||||
|
book.setBookName(book.getBookName().trim());
|
||||||
|
book.setAuthor(book.getAuthor().trim());
|
||||||
|
Map<String, Object> bookExample = new HashMap<>();
|
||||||
|
bookExample.put("bookName", book.getBookName());
|
||||||
|
bookExample.put("author", book.getAuthor());
|
||||||
|
List<BookDO> books = bookDao.list(bookExample);
|
||||||
|
if (books.size() > 0) {
|
||||||
|
//更新
|
||||||
|
bookId = books.get(0).getId();
|
||||||
|
book.setId(bookId);
|
||||||
|
bookDao.update(book);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if (book.getVisitCount() == null) {
|
||||||
|
Long visitCount = generateVisiteCount(book.getScore());
|
||||||
|
book.setVisitCount(visitCount);
|
||||||
|
}
|
||||||
|
//插入
|
||||||
|
int rows = bookDao.save(book);
|
||||||
|
if (rows > 0) {
|
||||||
|
bookId = book.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bookId >= 0) {
|
||||||
|
|
||||||
|
|
||||||
|
List<BookIndexDO> newBookIndexList = new ArrayList<>();
|
||||||
|
List<BookContentDO> newContentList = new ArrayList<>();
|
||||||
|
for (int i = 0; i < bookIndex.size(); i++) {
|
||||||
|
BookContentDO bookContentItem = bookContent.get(i);
|
||||||
|
if (!bookContentItem.getContent().contains("正在手打中,请稍等片刻,内容更新后,需要重新刷新页面,才能获取最新更新")) {
|
||||||
|
|
||||||
|
|
||||||
|
BookIndexDO bookIndexItem = bookIndex.get(i);
|
||||||
|
bookIndexItem.setBookId(bookId);
|
||||||
|
bookContentItem.setBookId(bookId);
|
||||||
|
bookContentItem.setIndexNum(bookIndexItem.getIndexNum());
|
||||||
|
|
||||||
|
newBookIndexList.add(bookIndexItem);
|
||||||
|
newContentList.add(bookContentItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newBookIndexList.size() > 0) {
|
||||||
|
bookIndexDao.insertBatch(newBookIndexList);
|
||||||
|
|
||||||
|
bookContentDao.insertBatch(newContentList);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long generateVisiteCount(Float score) {
|
||||||
|
return Long.parseLong((int)(score*10000) + new Random().nextInt(1000) + "");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import xyz.zinglizingli.books.core.enums.PicSaveType;
|
|||||||
import xyz.zinglizingli.books.mapper.*;
|
import xyz.zinglizingli.books.mapper.*;
|
||||||
import xyz.zinglizingli.books.po.*;
|
import xyz.zinglizingli.books.po.*;
|
||||||
import xyz.zinglizingli.books.core.utils.Constants;
|
import xyz.zinglizingli.books.core.utils.Constants;
|
||||||
|
import xyz.zinglizingli.common.utils.SpringUtil;
|
||||||
import xyz.zinglizingli.common.utils.UUIDUtils;
|
import xyz.zinglizingli.common.utils.UUIDUtils;
|
||||||
import xyz.zinglizingli.common.cache.CommonCacheUtil;
|
import xyz.zinglizingli.common.cache.CommonCacheUtil;
|
||||||
import xyz.zinglizingli.common.utils.RestTemplateUtil;
|
import xyz.zinglizingli.common.utils.RestTemplateUtil;
|
||||||
@ -51,6 +52,7 @@ public class BookService {
|
|||||||
|
|
||||||
private final CommonCacheUtil cacheUtil;
|
private final CommonCacheUtil cacheUtil;
|
||||||
|
|
||||||
|
|
||||||
@Value("${pic.save.type}")
|
@Value("${pic.save.type}")
|
||||||
private Integer picSaveType;
|
private Integer picSaveType;
|
||||||
|
|
||||||
@ -63,6 +65,8 @@ public class BookService {
|
|||||||
* 保存章节目录和内容
|
* 保存章节目录和内容
|
||||||
* */
|
* */
|
||||||
public void saveBookAndIndexAndContent(Book book, List<BookIndex> bookIndex, List<BookContent> bookContent){
|
public void saveBookAndIndexAndContent(Book book, List<BookIndex> bookIndex, List<BookContent> bookContent){
|
||||||
|
//解决内部调用事物不生效的问题
|
||||||
|
BookService bookService = SpringUtil.getBean(BookService.class);
|
||||||
|
|
||||||
boolean isUpdate = false;
|
boolean isUpdate = false;
|
||||||
Long bookId = -1L;
|
Long bookId = -1L;
|
||||||
@ -107,7 +111,7 @@ public class BookService {
|
|||||||
}
|
}
|
||||||
//一次最多只允许插入20条记录,否则影响服务器响应
|
//一次最多只允许插入20条记录,否则影响服务器响应
|
||||||
if (isUpdate && i % 20 == 0 && newBookIndexList.size() > 0) {
|
if (isUpdate && i % 20 == 0 && newBookIndexList.size() > 0) {
|
||||||
insertIndexListAndContentList(newBookIndexList, newContentList);
|
bookService.insertIndexListAndContentList(newBookIndexList, newContentList);
|
||||||
newBookIndexList = new ArrayList<>();
|
newBookIndexList = new ArrayList<>();
|
||||||
newContentList = new ArrayList<>();
|
newContentList = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
@ -121,7 +125,7 @@ public class BookService {
|
|||||||
|
|
||||||
|
|
||||||
if (newBookIndexList.size() > 0) {
|
if (newBookIndexList.size() > 0) {
|
||||||
insertIndexListAndContentList(newBookIndexList, newContentList);
|
bookService.insertIndexListAndContentList(newBookIndexList, newContentList);
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheUtil.del(CacheKeyConstans.NEWST_BOOK_LIST_KEY);
|
cacheUtil.del(CacheKeyConstans.NEWST_BOOK_LIST_KEY);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user