package com.tencent.service.impl;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tencent.dao.IArticleCategoryRefDao;
import com.tencent.dao.IArticleDao;
import com.tencent.dao.IArticleTagRefDao;
import com.tencent.entity.*;
import com.tencent.enums.ArticleCommentStatus;
import com.tencent.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.elasticsearch.common.text.Text;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Service("articleService")
@Slf4j
public class ArticleServiceImpl implements IArticleService {
@Autowired(required = false)
private IArticleDao articleDao;
@Autowired(required = false)
private RestHighLevelClient highLevelClient;
@Autowired(required = false)
private IArticleCategoryRefDao articleCategoryRefDao;
@Autowired(required = false)
private IArticleTagRefDao articleTagRefDao;
@Override
@Transactional(rollbackFor = Exception.class)
public void insertArticle(Article article) {
//添加文章
article.setArticleCreateTime(new Date());
article.setArticleUpdateTime(new Date());
article.setArticleIsComment(ArticleCommentStatus.ALLOW.getValue());
article.setArticleViewCount(0);
article.setArticleLikeCount(0);
article.setArticleCommentCount(0);
article.setArticleOrder(1);
articleDao.insert(article);
//添加分类和文章关联
for(int i=0;i<article.getCategoryList().size();i++){
ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(),article.getCategoryList().get(i).getCategoryId());
articleCategoryRefDao.insert(articleCategoryRef);
}
//添加标签和文章关联
for(int i=0;i<article.getTagList().size();i++){
ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(),article.getTagList().get(i).getTagId());
articleTagRefDao.insert(articleTagRef);
}
}
@Override
public void deleteArticle(Integer articleId) {
articleDao.deleteById(articleId);
}
@Override
public void deleteArticleBatch(List<Integer> ids) {
articleDao.deleteBatch(ids);
}
@Override
public void updateArticle(Article article) {
articleDao.update(article);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateArticleDetail(Article article) {
article.setArticleUpdateTime(new Date());
articleDao.update(article);
if(article.getTagList()!=null){
//删除标签和文章关联
articleTagRefDao.deleteByArticleId(article.getArticleId());
//添加标签和文章关联
for(int i=0;i<article.getTagList().size();i++){
ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(),article.getTagList().get(i).getTagId());
articleTagRefDao.insert(articleTagRef);
}
}
if(article.getCategoryList()!=null){
//删除分类和文章关联
articleCategoryRefDao.deleteByArticleId(article.getArticleId());
//添加分类和文章关联
for(int i=0;i<article.getCategoryList().size();i++){
ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(),article.getCategoryList().get(i).getCategoryId());
articleCategoryRefDao.insert(articleCategoryRef);
}
}
}
@Override
public Integer getArticleUserId(Integer articleId) {
return articleDao.getArticleUserId(articleId);
}
@Override
public Integer countArticle(Integer status) {
Integer count = 0;
try{
count = articleDao.countArticle(status);
}catch(Exception e){
e.printStackTrace();
log.error("根据状态统计文章数,status:{},cause:{}",status,e);
}
return count;
}
@Override
public Integer updateLikeCount(Integer articleId, Integer likeCount) {
return articleDao.updateLikeCount(articleId, likeCount);
}
@Override
public Integer countArticleComment() {
Integer count = 0;
try{
count = articleDao.countArticleComment();
}catch(Exception e){
e.printStackTrace();
log.error("统计文章评论数失败,cause:{}",e);
}
return count; }
@Override
public Integer countArticleView() {
Integer count = 0;
try{
count = articleDao.countArticleView();
}catch(Exception e){
e.printStackTrace();
log.error("统计文章访问量失败,cause:{}",e);
}
return count;
}
@Override
public Integer countArticleByCategoryId(Integer categoryId) {
Integer count = 0;
try{
count = articleCategoryRefDao.countArticleByCategoryId(categoryId);
}catch (Exception e){
e.printStackTrace();
log.error("根据分类统计文章数量失败,categoryId:{},cause:{}",categoryId,e);
}
return count;
}
@Override
public PageInfo<Article> findArticleByEs(RestHighLevelClient client,HashMap<String, Object> criteria, Integer pageIndex, Integer pageSize) {
String keywords = null;
for(String key : criteria.keySet()){
keywords = criteria.get(key).toString();
System.out.println(criteria.get(key));
break;
}
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//设置搜索结果起始行数下标,默认从0开始。
sourceBuilder.from(0);
//设置搜索结果显示条数
sourceBuilder.size(10);
//设置搜索结果超时时间
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
QueryBuilder queryBuilder = null;
MatchPhraseQueryBuilder mpqBuilder1 = null;
MatchPhraseQueryBuilder mpqBuilder2 = null;
MatchPhraseQueryBuilder mpqBuilder3 = null;
String keyword = keywords;
//如果关键词中包含"+",则默认将其拆分为2部分,+的左半部分用来标识查找范围,+的右半部分是关键词
if(keywords.indexOf("+")>-1){
String[] slist = keywords.split("\\+");
String range = slist[0];
keyword = slist[1];
//
if(range.equals("articleTitle")){
mpqBuilder1 = new MatchPhraseQueryBuilder("article_title",keyword);
queryBuilder = QueryBuilders.boolQuery().must(mpqBuilder1);
}else if(range.equals("articleContent")){
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 # 项目介绍: ## 一.该项目实现了一个简单的博客系统,具体功能包括: 后台:后台管理系统,具体包括:文章管理、分类管理、标签管理、用户管理、活动通知管理等 前台:用户注册登录、搜索文章、评价文章、点赞文章、图片上传、文章分页展示、文章分享等 使用ssm(spring+springmvc+mybaits)+redis+elk作为后台技术,使用layui+jsp+ajax作为前端技术 ### 技术亮点: 1.注册登录时利用ajax对不合适的输入做出提示,并使用MD5+salt对密码进行加密; 2.使用elk替换了原来mysql的模糊查询方式,实现对文章的标题,摘要,全文的搜索功能,并高亮展示; 3.点赞功能由Redis来实现,将点赞数率先存入redis,然后再更新到mysql中;
资源推荐
资源详情
资源评论
收起资源包目录
基于javaweb+ssm+redis+elk+layui+jsp的全栈学习的博客项目源码+数据库+说明.zip (1127个子文件)
ArticleServiceImpl.class 18KB
ArticleServiceImpl.class 18KB
ArticleController.class 7KB
ArticleController.class 7KB
AdminController.class 6KB
AdminController.class 6KB
HomeIndexController.class 6KB
HomeIndexController.class 6KB
HomePageController.class 6KB
HomePageController.class 6KB
CommentServiceImpl.class 5KB
CommentServiceImpl.class 5KB
Article.class 5KB
Article.class 5KB
HomeArticleController.class 5KB
HomeArticleController.class 5KB
UserController.class 5KB
UserController.class 5KB
RedisAdpeter.class 5KB
RedisAdpeter.class 5KB
CommentController.class 5KB
CommentController.class 5KB
TagServiceImpl.class 5KB
TagServiceImpl.class 5KB
CategoryServiceImpl.class 4KB
CategoryServiceImpl.class 4KB
Options.class 4KB
Options.class 4KB
Comment.class 4KB
Comment.class 4KB
HomeResourceInterceptor.class 4KB
HomeResourceInterceptor.class 4KB
User.class 4KB
User.class 4KB
HomeCategoryController.class 4KB
HomeCategoryController.class 4KB
UploadFileController.class 4KB
UploadFileController.class 4KB
PageController.class 3KB
PageController.class 3KB
TagController.class 3KB
TagController.class 3KB
LinkController.class 3KB
LinkController.class 3KB
Link.class 3KB
Link.class 3KB
EventConsumer.class 3KB
EventConsumer.class 3KB
HomeCommentController.class 3KB
HomeCommentController.class 3KB
CategoryController.class 3KB
CategoryController.class 3KB
Category.class 3KB
Category.class 3KB
NoticeController.class 3KB
NoticeController.class 3KB
HomeTagController.class 3KB
HomeTagController.class 3KB
ArticleParam.class 3KB
ArticleParam.class 3KB
LikeHandler.class 3KB
LikeHandler.class 3KB
LikeController.class 3KB
LikeController.class 3KB
EventModel.class 3KB
EventModel.class 3KB
MenuController.class 3KB
MenuController.class 3KB
Page.class 3KB
Page.class 3KB
IArticleDao.class 3KB
IArticleDao.class 3KB
IArticleService.class 3KB
IArticleService.class 3KB
UserServiceImpl.class 3KB
UserServiceImpl.class 3KB
JsonResult.class 3KB
JsonResult.class 3KB
MyUtils.class 2KB
MyUtils.class 2KB
Response.class 2KB
Response.class 2KB
Notice.class 2KB
Notice.class 2KB
EventConsumer$1.class 2KB
EventConsumer$1.class 2KB
HomeLinkController.class 2KB
HomeLinkController.class 2KB
OptionsController.class 2KB
OptionsController.class 2KB
Menu.class 2KB
Menu.class 2KB
ResultVo.class 2KB
ResultVo.class 2KB
Tag.class 2KB
Tag.class 2KB
IMessageDao.class 2KB
IMessageDao.class 2KB
SecurityInterceptor.class 2KB
SecurityInterceptor.class 2KB
共 1127 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
土豆片片
- 粉丝: 1518
- 资源: 5643
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功