Compare commits

...

7 Commits

20 changed files with 699 additions and 114 deletions

View File

@ -40,7 +40,7 @@ novel-plus -- 父工程
``` ```
#### 技术选型 #### 技术选型
Springboot+Mybatis+Mysql+Ehcache+Thymeleaf+Layui Springboot+Mybatis+Mysql+ElasticSearch+Ehcache+Thymeleaf+Layui
#### PC站截图 #### PC站截图
@ -52,35 +52,43 @@ Springboot+Mybatis+Mysql+Ehcache+Thymeleaf+Layui
![img](https://oscimg.oschina.net/oscnet/up-d0b2e03129bfae47b8bb96a491b73d383c5.png) ![img](https://oscimg.oschina.net/oscnet/up-d0b2e03129bfae47b8bb96a491b73d383c5.png)
3. 排行榜 3. 搜索页
![img](./assets/QQ20200520-215756.png)
4. 排行榜
![img](https://oscimg.oschina.net/oscnet/up-78d5a68586cd92a86c669311f414508f922.png) ![img](https://oscimg.oschina.net/oscnet/up-78d5a68586cd92a86c669311f414508f922.png)
4. 详情页 5. 详情页
![img](https://oscimg.oschina.net/oscnet/up-8be2495a2869f93626b0c9c1df6f329747a.png) ![img](https://oscimg.oschina.net/oscnet/up-8be2495a2869f93626b0c9c1df6f329747a.png)
5. 阅读页 6. 阅读页
![img](https://oscimg.oschina.net/oscnet/up-517c84148d2db8e11717a8bbecc57fa1be7.png) ![img](https://oscimg.oschina.net/oscnet/up-517c84148d2db8e11717a8bbecc57fa1be7.png)
6. 用户中心 7. 用户中心
![img](https://oscimg.oschina.net/oscnet/up-805a30e7a663a3fd5cb39a7ea26bc132a01.png) ![img](https://oscimg.oschina.net/oscnet/up-805a30e7a663a3fd5cb39a7ea26bc132a01.png)
7. 充值 8. 充值
![img](https://oscimg.oschina.net/oscnet/up-5a601b0b3af3224d0bebcfe12fc15075d34.png) ![img](https://oscimg.oschina.net/oscnet/up-5a601b0b3af3224d0bebcfe12fc15075d34.png)
![img](https://oscimg.oschina.net/oscnet/up-face25d02c07b05b2ce954cc4bf4ee6a0cc.png) ![img](https://oscimg.oschina.net/oscnet/up-face25d02c07b05b2ce954cc4bf4ee6a0cc.png)
8.作家专区 9. 作家专区
![img](https://oscimg.oschina.net/oscnet/up-30766372cc7f56480ff1d7d55198204f6ea.png) ![img](https://oscimg.oschina.net/oscnet/up-30766372cc7f56480ff1d7d55198204f6ea.png)
![img](https://oscimg.oschina.net/oscnet/up-9737995e465b86f3bee3211221f6c3b8a56.png) ![img](https://oscimg.oschina.net/oscnet/up-9737995e465b86f3bee3211221f6c3b8a56.png)
9.购买 10. 购买
![img](https://oscimg.oschina.net/oscnet/up-ce0f585efd82a9670335f118cf5897c85e9.png) ![img](https://oscimg.oschina.net/oscnet/up-ce0f585efd82a9670335f118cf5897c85e9.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

82
es/index_create.txt Normal file
View File

@ -0,0 +1,82 @@
PUT /novel
{
"mappings" : {
"book" : {
"properties" : {
"id" : {
"type" : "long"
},
"authorId" : {
"type" : "long"
},
"authorName" : {
"type" : "text",
"analyzer": "ik_smart",
"boost": 1.9
},
"bookName" : {
"type" : "text",
"analyzer": "ik_smart",
"boost": 2
},
"bookDesc" : {
"type" : "text",
"analyzer": "ik_smart",
"boost": 0.1
},
"bookStatus" : {
"type" : "short"
},
"catId" : {
"type" : "integer"
},
"catName" : {
"type" : "text",
"analyzer": "ik_smart",
"boost": 1.0
},
"lastIndexId" : {
"type" : "long"
},
"lastIndexName" : {
"type" : "text",
"analyzer": "ik_smart",
"boost": 0.1
},
"lastIndexUpdateTime" : {
"type": "keyword"
},
"picUrl" : {
"type" : "keyword"
},
"score" : {
"type" : "float"
},
"wordCount" : {
"type" : "integer"
},
"workDirection" : {
"type" : "short"
},
"visitCount" : {
"type": "long"
}
}
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>novel</artifactId> <artifactId>novel</artifactId>
<groupId>com.java2nb</groupId> <groupId>com.java2nb</groupId>
<version>2.0.2</version> <version>2.1.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -46,4 +46,8 @@ public interface CacheKey {
* */ * */
String RUNNING_CRAWL_THREAD_KEY_PREFIX = "runningCrawlTreadDataKeyPrefix"; String RUNNING_CRAWL_THREAD_KEY_PREFIX = "runningCrawlTreadDataKeyPrefix";
/**
* 上一次搜索引擎更新的时间
* */
String ES_LAST_UPDATE_TIME = "esLastUpdateTime";
} }

View File

@ -1,6 +1,7 @@
package com.java2nb.novel.core.utils; package com.java2nb.novel.core.utils;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets; import org.apache.commons.codec.Charsets;
import org.apache.http.client.utils.DateUtils; import org.apache.http.client.utils.DateUtils;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -17,18 +18,21 @@ import java.util.Objects;
* 文件操作工具类 * 文件操作工具类
* @author 11797 * @author 11797
*/ */
@Slf4j
public class FileUtil { public class FileUtil {
/** /**
* 网络图片转本地 * 网络图片转本地
* */ * */
@SneakyThrows
public static String network2Local(String picSrc,String picSavePath,String visitPrefix) { public static String network2Local(String picSrc,String picSavePath,String visitPrefix) {
InputStream input = null;
OutputStream out = null;
try {
//本地图片保存 //本地图片保存
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers); HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
ResponseEntity<Resource> resEntity = RestTemplateUtil.getInstance(Charsets.ISO_8859_1.name()).exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class); ResponseEntity<Resource> resEntity = RestTemplateUtil.getInstance(Charsets.ISO_8859_1.name()).exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class);
InputStream input = Objects.requireNonNull(resEntity.getBody()).getInputStream(); input = Objects.requireNonNull(resEntity.getBody()).getInputStream();
Date currentDate = new Date(); Date currentDate = new Date();
picSrc = visitPrefix + DateUtils.formatDate(currentDate, "yyyy") + "/" + DateUtils.formatDate(currentDate, "MM") + "/" + DateUtils.formatDate(currentDate, "dd") + "/" picSrc = visitPrefix + DateUtils.formatDate(currentDate, "yyyy") + "/" + DateUtils.formatDate(currentDate, "MM") + "/" + DateUtils.formatDate(currentDate, "dd") + "/"
+ UUIDUtil.getUUID32() + UUIDUtil.getUUID32()
@ -38,13 +42,36 @@ public class FileUtil {
if (!parentFile.exists()) { if (!parentFile.exists()) {
parentFile.mkdirs(); parentFile.mkdirs();
} }
OutputStream out = new FileOutputStream(picFile); out = new FileOutputStream(picFile);
byte[] b = new byte[4096]; byte[] b = new byte[4096];
for (int n; (n = input.read(b)) != -1; ) { for (int n; (n = input.read(b)) != -1; ) {
out.write(b, 0, n); out.write(b, 0, n);
} }
out.close();
}catch (Exception e){
log.error(e.getMessage(),e);
picSrc = "/images/default.gif";
}finally {
if(input != null){
try {
input.close(); input.close();
} catch (IOException e) {
log.error(e.getMessage(),e);
}finally {
if(out != null){
try {
out.close();
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
}
}
}
return picSrc; return picSrc;
} }

View File

@ -0,0 +1,73 @@
package com.java2nb.novel.core.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
/**
* @author xiongxiaoyang
*/
public class StringUtil {
/**
* 将驼峰式命名的字符串转换为下划线大写方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。</br>
* 例如HelloWorld->HELLO_WORLD
* @param name 转换前的驼峰式命名的字符串
* @return 转换后下划线大写方式命名的字符串
*/
public static String underscoreName(String name) {
StringBuilder result = new StringBuilder();
if (name != null && name.length() > 0) {
// 将第一个字符处理成大写
result.append(name.substring(0, 1).toUpperCase());
// 循环处理其余字符
for (int i = 1; i < name.length(); i++) {
String s = name.substring(i, i + 1);
// 在大写字母前添加下划线
if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
result.append("_");
}
// 其他字符直接转成大写
result.append(s.toUpperCase());
}
}
return result.toString();
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
* 例如HELLO_WORLD->HelloWorld
* @param name 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
*/
public static String camelName(String name) {
StringBuilder result = new StringBuilder();
// 快速检查
if (name == null || name.isEmpty()) {
// 没必要转换
return "";
} else if (!name.contains("_")) {
// 不含下划线,仅将首字母小写
return name.substring(0, 1).toLowerCase() + name.substring(1);
}
// 用下划线将原始字符串分割
String camels[] = name.split("_");
for (String camel : camels) {
// 跳过原始字符串中开头、结尾的下换线或双重下划线
if (camel.isEmpty()) {
continue;
}
// 处理真正的驼峰片段
if (result.length() == 0) {
// 第一个驼峰片段,全部字母都小写
result.append(camel.toLowerCase());
} else {
// 其他的驼峰片段,首字母大写
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
}
return result.toString();
}
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>novel</artifactId> <artifactId>novel</artifactId>
<groupId>com.java2nb</groupId> <groupId>com.java2nb</groupId>
<version>2.0.2</version> <version>2.1.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>novel</artifactId> <artifactId>novel</artifactId>
<groupId>com.java2nb</groupId> <groupId>com.java2nb</groupId>
<version>2.0.2</version> <version>2.1.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -28,6 +28,18 @@
</dependency> </dependency>
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>${jest.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.alipay.sdk</groupId> <groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId> <artifactId>alipay-sdk-java</artifactId>

View File

@ -79,8 +79,8 @@ public class BookController extends BaseController{
* */ * */
@PostMapping("searchByPage") @PostMapping("searchByPage")
public ResultBean searchByPage(BookSP bookSP, @RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize){ public ResultBean searchByPage(BookSP bookSP, @RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize){
List<BookVO> books = bookService.searchByPage(bookSP,page,pageSize); PageInfo<BookVO> pageInfo = bookService.searchByPage(bookSP,page,pageSize);
return ResultBean.ok(new PageInfo<>(books)); return ResultBean.ok(pageInfo);
} }
/** /**

View File

@ -0,0 +1,96 @@
package com.java2nb.novel.core.schedule;
import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.utils.BeanUtil;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.vo.EsBookVO;
import io.searchbox.client.JestClient;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* 小说导入搜索引擎
*
* @author Administrator
*/
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "1")
@Service
@RequiredArgsConstructor
@Slf4j
public class BookToEsSchedule {
private final BookService bookService;
private final CacheService cacheService;
private final JestClient jestClient;
private boolean lock = false;
/**
* 2分钟导入一次
*/
@Scheduled(fixedRate = 1000 * 60 * 2)
public void saveToEs() {
if (!lock) {
lock = true;
try {
//查询需要更新的小说
Date lastDate = (Date) cacheService.getObject(CacheKey.ES_LAST_UPDATE_TIME);
if (lastDate == null) {
lastDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
}
long count ;
do {
List<Book> books = bookService.queryBookByUpdateTimeByPage(lastDate,100);
for(Book book : books) {
//导入到ES
EsBookVO esBookVO = new EsBookVO();
BeanUtils.copyProperties(book,esBookVO,"lastIndexUpdateTime");
esBookVO.setLastIndexUpdateTime(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(book.getLastIndexUpdateTime()));
Index action = new Index.Builder(esBookVO).index("novel").type("book").id(book.getId().toString()).build();
jestClient.execute(action);
lastDate = book.getUpdateTime();
//1秒钟导入一本书1分钟导入60本
Thread.sleep(1000);
}
count = books.size();
}while (count == 100);
cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate);
} catch (Exception e) {
log.error(e.getMessage(),e);
}
lock = false;
}
}
}

View File

@ -3,6 +3,7 @@ package com.java2nb.novel.core.schedule;
import com.java2nb.novel.entity.Book; import com.java2nb.novel.entity.Book;
import com.java2nb.novel.service.BookService; import com.java2nb.novel.service.BookService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -13,6 +14,7 @@ import java.util.List;
/** /**
* 网络图片转存本地任务 * 网络图片转存本地任务
*
* @author Administrator * @author Administrator
*/ */
@ConditionalOnProperty(prefix = "pic.save", name = "type", havingValue = "2") @ConditionalOnProperty(prefix = "pic.save", name = "type", havingValue = "2")
@ -33,20 +35,18 @@ public class Network2LocalPicSchedule {
* 10分钟转一次 * 10分钟转一次
*/ */
@Scheduled(fixedRate = 1000 * 60 * 10) @Scheduled(fixedRate = 1000 * 60 * 10)
@SneakyThrows
public void trans() { public void trans() {
log.info("Network2LocalPicSchedule。。。。。。。。。。。。"); log.info("Network2LocalPicSchedule。。。。。。。。。。。。");
Integer offset = 0, limit = 100; List<Book> networkPicBooks = bookService.queryNetworkPicBooks(100);
List<Book> networkPicBooks;
do {
networkPicBooks = bookService.queryNetworkPicBooks(limit, offset);
for (Book book : networkPicBooks) { for (Book book : networkPicBooks) {
bookService.updateBookPicToLocal(book.getPicUrl(), book.getId()); bookService.updateBookPicToLocal(book.getPicUrl(), book.getId());
//3秒钟转化一张图片10分钟转化200张
Thread.sleep(3000);
} }
offset += limit;
} while (networkPicBooks.size() > 0);
} }

View File

@ -5,6 +5,7 @@ import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.vo.BookVO; import com.java2nb.novel.vo.BookVO;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -15,13 +16,13 @@ public interface FrontBookMapper extends BookMapper {
List<BookVO> searchByPage(BookSP params); List<BookVO> searchByPage(BookSP params);
void addVisitCount(@Param("bookId") Long bookId); void addVisitCount(@Param("bookId") Long bookId, @Param("date") Date date);
List<Book> listRecBookByCatId(@Param("catId") Integer catId); List<Book> listRecBookByCatId(@Param("catId") Integer catId);
void addCommentCount(@Param("bookId") Long bookId); void addCommentCount(@Param("bookId") Long bookId);
List<Book> queryNetworkPicBooks(@Param("limit") Integer limit,@Param("offset") Integer offset); List<Book> queryNetworkPicBooks(@Param("limit") Integer limit);
/** /**
* 按评分随机查询小说集合 * 按评分随机查询小说集合

View File

@ -1,12 +1,14 @@
package com.java2nb.novel.service; package com.java2nb.novel.service;
import com.github.pagehelper.PageInfo;
import com.java2nb.novel.search.BookSP; import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.vo.BookCommentVO; import com.java2nb.novel.vo.BookCommentVO;
import com.java2nb.novel.vo.BookSettingVO; import com.java2nb.novel.vo.BookSettingVO;
import com.java2nb.novel.entity.*; import com.java2nb.novel.entity.*;
import com.java2nb.novel.vo.BookVO; import com.java2nb.novel.vo.BookVO;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -44,9 +46,9 @@ public interface BookService {
* @param params 搜索参数 * @param params 搜索参数
* @param page 页码 * @param page 页码
* @param pageSize 分页大小 * @param pageSize 分页大小
* @return 小说集合 * @return 小说集合分页信息
* */ * */
List<BookVO> searchByPage(BookSP params, int page, int pageSize); PageInfo searchByPage(BookSP params, int page, int pageSize);
/** /**
* 查询小说分类列表 * 查询小说分类列表
@ -179,10 +181,9 @@ public interface BookService {
/** /**
* 查询网络图片的小说 * 查询网络图片的小说
* @param limit 查询条数 * @param limit 查询条数
* @param offset 开始行数
* @return 返回小说集合 * @return 返回小说集合
* */ * */
List<Book> queryNetworkPicBooks(Integer limit, Integer offset); List<Book> queryNetworkPicBooks(Integer limit);
/** /**
@ -224,4 +225,13 @@ public interface BookService {
* @param authorId 作者ID * @param authorId 作者ID
* */ * */
void addBookContent(Long bookId, String indexName, String content, Long authorId); void addBookContent(Long bookId, String indexName, String content, Long authorId);
/**
* 根据更新时间分页查询书籍列表
* @param startDate 开始时间,包括该时间
* @param limit 查询数量
* @return 书籍列表
* */
List<Book> queryBookByUpdateTimeByPage(Date startDate, int limit);
} }

View File

@ -1,15 +1,14 @@
package com.java2nb.novel.service.impl; package com.java2nb.novel.service.impl;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2nb.novel.core.cache.CacheKey; import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService; import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.enums.ResponseStatus; import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.exception.BusinessException; import com.java2nb.novel.core.exception.BusinessException;
import com.java2nb.novel.core.utils.BeanUtil; import com.java2nb.novel.core.utils.*;
import com.java2nb.novel.core.utils.Constants;
import com.java2nb.novel.core.utils.FileUtil;
import com.java2nb.novel.core.utils.IdWorker;
import com.java2nb.novel.entity.*; import com.java2nb.novel.entity.*;
import com.java2nb.novel.entity.Book; import com.java2nb.novel.entity.Book;
import com.java2nb.novel.mapper.*; import com.java2nb.novel.mapper.*;
@ -19,9 +18,18 @@ import com.java2nb.novel.service.BookService;
import com.java2nb.novel.vo.BookCommentVO; import com.java2nb.novel.vo.BookCommentVO;
import com.java2nb.novel.vo.BookSettingVO; import com.java2nb.novel.vo.BookSettingVO;
import com.java2nb.novel.vo.BookVO; import com.java2nb.novel.vo.BookVO;
import com.java2nb.novel.vo.EsBookVO;
import io.searchbox.client.JestClient;
import io.searchbox.core.*;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.mybatis.dynamic.sql.SortSpecification; import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.render.RenderingStrategies; import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
@ -31,6 +39,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.orderbyhelper.OrderByHelper; import tk.mybatis.orderbyhelper.OrderByHelper;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -50,14 +59,18 @@ import static org.mybatis.dynamic.sql.select.SelectDSL.select;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j
public class BookServiceImpl implements BookService { public class BookServiceImpl implements BookService {
/** /**
* 本地图片保存路径 * 本地图片保存路径
* */ */
@Value("${pic.save.path}") @Value("${pic.save.path}")
private String picSavePath; private String picSavePath;
@Value("${spring.elasticsearch.enable}")
private Integer esEnable;
private final FrontBookSettingMapper bookSettingMapper; private final FrontBookSettingMapper bookSettingMapper;
private final FrontBookMapper bookMapper; private final FrontBookMapper bookMapper;
@ -76,6 +89,8 @@ public class BookServiceImpl implements BookService {
private final AuthorService authorService; private final AuthorService authorService;
private final JestClient jestClient;
@SneakyThrows @SneakyThrows
@Override @Override
@ -96,7 +111,7 @@ public class BookServiceImpl implements BookService {
/** /**
* 初始化首页小说设置 * 初始化首页小说设置
* */ */
private List<BookSettingVO> initIndexBookSetting() { private List<BookSettingVO> initIndexBookSetting() {
Date currentDate = new Date(); Date currentDate = new Date();
List<Book> books = bookMapper.selectIdsByScoreAndRandom(Constants.INDEX_BOOK_SETTING_NUM); List<Book> books = bookMapper.selectIdsByScoreAndRandom(Constants.INDEX_BOOK_SETTING_NUM);
@ -171,18 +186,167 @@ public class BookServiceImpl implements BookService {
} }
@Override @Override
public List<BookVO> searchByPage(BookSP params, int page, int pageSize) { public PageInfo searchByPage(BookSP params, int page, int pageSize) {
PageHelper.startPage(page, pageSize);
if (params.getUpdatePeriod() != null) { if (params.getUpdatePeriod() != null) {
long cur = System.currentTimeMillis(); long cur = System.currentTimeMillis();
long period = params.getUpdatePeriod() * 24 * 3600 * 1000; long period = params.getUpdatePeriod() * 24 * 3600 * 1000;
long time = cur - period; long time = cur - period;
params.setUpdateTimeMin(new Date(time)); params.setUpdateTimeMin(new Date(time));
} }
if (esEnable == 1) {
try {
List<EsBookVO> bookList = new ArrayList<>(0);
//使用搜索引擎搜索
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 构造查询哪个字段
if (StringUtils.isNoneBlank(params.getKeyword())) {
boolQueryBuilder = boolQueryBuilder.must(QueryBuilders.queryStringQuery(params.getKeyword()));
}
// 作品方向
if (params.getWorkDirection() != null) {
boolQueryBuilder.filter(QueryBuilders.termQuery("workDirection", params.getWorkDirection()));
}
// 分类
if (params.getCatId() != null) {
boolQueryBuilder.filter(QueryBuilders.termQuery("catId", params.getCatId()));
}
if (params.getBookStatus() != null) {
boolQueryBuilder.filter(QueryBuilders.termQuery("bookStatus", params.getBookStatus()));
}
if (params.getWordCountMin() == null) {
params.setWordCountMin(0);
}
if (params.getWordCountMax() == null) {
params.setWordCountMax(Integer.MAX_VALUE);
}
boolQueryBuilder.filter(QueryBuilders.rangeQuery("wordCount").gte(params.getWordCountMin()).lte(params.getWordCountMax()));
if (params.getUpdateTimeMin() != null) {
boolQueryBuilder.filter(QueryBuilders.rangeQuery("lastIndexUpdateTime").gte(params.getUpdateTimeMin()));
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);
Count count = new Count.Builder().addIndex("novel").addType("book")
.query(searchSourceBuilder.toString()).build();
CountResult results = jestClient.execute(count);
Double total = results.getCount();
// 高亮字段
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("authorName");
highlightBuilder.field("bookName");
highlightBuilder.field("bookDesc");
highlightBuilder.field("lastIndexName");
highlightBuilder.field("catName");
highlightBuilder.preTags("<span style='color:red'>").postTags("</span>");
highlightBuilder.fragmentSize(20000);
searchSourceBuilder.highlighter(highlightBuilder);
//设置排序
if (params.getSort() != null) {
searchSourceBuilder.sort(StringUtil.camelName(params.getSort()), SortOrder.DESC);
}
// 设置分页
searchSourceBuilder.from((page - 1) * pageSize);
searchSourceBuilder.size(pageSize);
// 构建Search对象
Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex("novel").addType("book").build();
log.debug(search.toString());
SearchResult result;
result = jestClient.execute(search);
if (result.isSucceeded()) {
log.debug(result.getJsonString());
Map resultMap = new ObjectMapper().readValue(result.getJsonString(), Map.class);
if (resultMap.get("hits") != null) {
Map hitsMap = (Map) resultMap.get("hits");
if (hitsMap.size() > 0 && hitsMap.get("hits") != null) {
List hitsList = (List) hitsMap.get("hits");
if (hitsList.size() > 0 && result.getSourceAsString() != null) {
JavaType jt = new ObjectMapper().getTypeFactory().constructParametricType(ArrayList.class, EsBookVO.class);
bookList = new ObjectMapper().readValue("[" + result.getSourceAsString() + "]", jt);
if (bookList != null) {
for (int i = 0; i < bookList.size(); i++) {
hitsMap = (Map) hitsList.get(i);
Map highlightMap = (Map) hitsMap.get("highlight");
if (highlightMap != null && highlightMap.size() > 0) {
List<String> authorNameList = (List<String>) highlightMap.get("authorName");
if (authorNameList != null && authorNameList.size() > 0) {
bookList.get(i).setAuthorName(authorNameList.get(0));
}
List<String> bookNameList = (List<String>) highlightMap.get("bookName");
if (bookNameList != null && bookNameList.size() > 0) {
bookList.get(i).setBookName(bookNameList.get(0));
}
List<String> bookDescList = (List<String>) highlightMap.get("bookDesc");
if (bookDescList != null && bookDescList.size() > 0) {
bookList.get(i).setBookDesc(bookDescList.get(0));
}
List<String> lastIndexNameList = (List<String>) highlightMap.get("lastIndexName");
if (lastIndexNameList != null && lastIndexNameList.size() > 0) {
bookList.get(i).setLastIndexName(lastIndexNameList.get(0));
}
List<String> catNameList = (List<String>) highlightMap.get("catName");
if (catNameList != null && catNameList.size() > 0) {
bookList.get(i).setCatName(catNameList.get(0));
}
}
}
}
}
}
}
PageInfo<EsBookVO> pageInfo = new PageInfo<>(bookList);
pageInfo.setTotal(total.longValue());
pageInfo.setPageNum(page);
pageInfo.setPageSize(pageSize);
return pageInfo;
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
}
PageHelper.startPage(page, pageSize);
if (StringUtils.isNotBlank(params.getSort())) { if (StringUtils.isNotBlank(params.getSort())) {
OrderByHelper.orderBy(params.getSort() + " desc"); OrderByHelper.orderBy(params.getSort() + " desc");
} }
return bookMapper.searchByPage(params); return new PageInfo<>(bookMapper.searchByPage(params));
} }
@Override @Override
@ -197,7 +361,7 @@ public class BookServiceImpl implements BookService {
@Override @Override
public Book queryBookDetail(Long bookId) { public Book queryBookDetail(Long bookId) {
SelectStatementProvider selectStatement = select(id, catName, catId, picUrl, bookName, authorId, authorName, bookDesc, bookStatus, visitCount, wordCount, lastIndexId, lastIndexName, lastIndexUpdateTime,score,status) SelectStatementProvider selectStatement = select(book.allColumns())
.from(book) .from(book)
.where(id, isEqualTo(bookId)) .where(id, isEqualTo(bookId))
.build() .build()
@ -316,8 +480,7 @@ public class BookServiceImpl implements BookService {
@Override @Override
public void addVisitCount(Long bookId) { public void addVisitCount(Long bookId) {
bookMapper.addVisitCount(bookId); bookMapper.addVisitCount(bookId, new Date());
} }
@Override @Override
@ -409,7 +572,6 @@ public class BookServiceImpl implements BookService {
} }
@Override @Override
public Long queryIdByNameAndAuthor(String bookName, String author) { public Long queryIdByNameAndAuthor(String bookName, String author) {
//查询小说ID //查询小说ID
@ -438,8 +600,8 @@ public class BookServiceImpl implements BookService {
} }
@Override @Override
public List<Book> queryNetworkPicBooks(Integer limit, Integer offset) { public List<Book> queryNetworkPicBooks(Integer limit) {
return bookMapper.queryNetworkPicBooks(limit,offset); return bookMapper.queryNetworkPicBooks(limit);
} }
@Override @Override
@ -479,7 +641,8 @@ public class BookServiceImpl implements BookService {
if (queryIdByNameAndAuthor(book.getBookName(), penName) != null) { if (queryIdByNameAndAuthor(book.getBookName(), penName) != null) {
//该作者发布过此书名的小说 //该作者发布过此书名的小说
throw new BusinessException(ResponseStatus.BOOKNAME_EXISTS); throw new BusinessException(ResponseStatus.BOOKNAME_EXISTS);
}; }
;
book.setAuthorName(penName); book.setAuthorName(penName);
book.setAuthorId(authorId); book.setAuthorId(authorId);
book.setVisitCount(0L); book.setVisitCount(0L);
@ -503,7 +666,7 @@ public class BookServiceImpl implements BookService {
.render(RenderingStrategies.MYBATIS3)); .render(RenderingStrategies.MYBATIS3));
} }
@Transactional @Transactional(rollbackFor = Exception.class)
@Override @Override
public void addBookContent(Long bookId, String indexName, String content, Long authorId) { public void addBookContent(Long bookId, String indexName, String content, Long authorId) {
@ -553,7 +716,19 @@ public class BookServiceImpl implements BookService {
bookContentMapper.insertSelective(bookContent); bookContentMapper.insertSelective(bookContent);
}
@Override
public List<Book> queryBookByUpdateTimeByPage(Date startDate, int limit) {
return bookMapper.selectMany(select(book.allColumns())
.from(book)
.where(updateTime, isGreaterThan(startDate))
.orderBy(updateTime)
.limit(limit)
.build()
.render(RenderingStrategies.MYBATIS3));
} }

View File

@ -0,0 +1,85 @@
package com.java2nb.novel.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.java2nb.novel.entity.Book;
import lombok.Data;
import javax.annotation.Generated;
import java.io.Serializable;
import java.util.Date;
/**
* @author Administrator
*/
@Data
public class EsBookVO {
private Long id;
private Byte workDirection;
private Integer catId;
private String catName;
private String picUrl;
private String bookName;
private Long authorId;
private String authorName;
private String bookDesc;
private Float score;
private Byte bookStatus;
private Long visitCount;
private Integer wordCount;
private Integer commentCount;
private Long lastIndexId;
private String lastIndexName;
private String lastIndexUpdateTime;
private Byte isVip;
private Byte status;
private Integer crawlSourceId;
private String crawlBookId;
private Byte crawlIsStop;
}

View File

@ -6,6 +6,15 @@ spring:
active: dev active: dev
include: alipay include: alipay
elasticsearch:
#是否开启搜索引擎1开启0不开启
enable: 0
jest:
uris: http://127.0.0.1:9200
jwt: jwt:
secret: novel!#20191230 secret: novel!#20191230
expiration: 604800 expiration: 604800

View File

@ -34,8 +34,9 @@
</select> </select>
<update id="addVisitCount" parameterType="long"> <update id="addVisitCount" >
update book set visit_count = visit_count + 1 where id = #{bookId} update book set visit_count = visit_count + 1 , update_time = #{date}
where id = #{bookId}
</update> </update>
<select id="listRecBookByCatId" parameterType="int" resultType="com.java2nb.novel.entity.Book"> <select id="listRecBookByCatId" parameterType="int" resultType="com.java2nb.novel.entity.Book">
@ -54,8 +55,8 @@
<select id="queryNetworkPicBooks" resultType="com.java2nb.novel.entity.Book"> <select id="queryNetworkPicBooks" resultType="com.java2nb.novel.entity.Book">
select select
id,pic_url from book id,pic_url from book
where pic_url like 'http://%' or pic_url like 'https://%' where pic_url like 'http%'
limit #{offset},#{limit} limit #{limit}
</select> </select>
<select id="selectIdsByScoreAndRandom" parameterType="int" resultType="com.java2nb.novel.entity.Book"> <select id="selectIdsByScoreAndRandom" parameterType="int" resultType="com.java2nb.novel.entity.Book">

View File

@ -5,7 +5,7 @@
<groupId>com.java2nb</groupId> <groupId>com.java2nb</groupId>
<artifactId>novel</artifactId> <artifactId>novel</artifactId>
<version>2.0.2</version> <version>2.1.0</version>
<modules> <modules>
<module>novel-common</module> <module>novel-common</module>
<module>novel-front</module> <module>novel-front</module>
@ -36,6 +36,8 @@
<orderbyhelper.version>1.0.2</orderbyhelper.version> <orderbyhelper.version>1.0.2</orderbyhelper.version>
<commons-lang3.version>3.4</commons-lang3.version> <commons-lang3.version>3.4</commons-lang3.version>
<jjwt.version>0.9.0</jjwt.version> <jjwt.version>0.9.0</jjwt.version>
<elasticsearch.version>6.2.2</elasticsearch.version>
<jest.version>6.3.1</jest.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>