package com.liuyanzhao.ssm.blog.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuyanzhao.ssm.blog.enums.ArticleCommentStatus;
import com.liuyanzhao.ssm.blog.service.ArticleService;
import com.liuyanzhao.ssm.blog.entity.*;
import com.liuyanzhao.ssm.blog.mapper.ArticleCategoryRefMapper;
import com.liuyanzhao.ssm.blog.mapper.ArticleMapper;
import com.liuyanzhao.ssm.blog.mapper.ArticleTagRefMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* 文章Servie实现
*
* @author 言曌
* @date 2017/8/24
*/
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
@Autowired(required = false)
private ArticleMapper articleMapper;
@Autowired(required = false)
private ArticleCategoryRefMapper articleCategoryRefMapper;
@Autowired(required = false)
private ArticleTagRefMapper articleTagRefMapper;
@Override
public Integer countArticle(Integer status) {
Integer count = 0;
try {
count = articleMapper.countArticle(status);
} catch (Exception e) {
e.printStackTrace();
log.error("根据状态统计文章数, status:{}, cause:{}", status, e);
}
return count;
}
@Override
public Integer countArticleComment() {
Integer count = 0;
try {
count = articleMapper.countArticleComment();
} catch (Exception e) {
e.printStackTrace();
log.error("统计文章评论数失败, cause:{}", e);
}
return count;
}
@Override
public Integer countArticleView() {
Integer count = 0;
try {
count = articleMapper.countArticleView();
} catch (Exception e) {
e.printStackTrace();
log.error("统计文章访问量失败, cause:{}", e);
}
return count;
}
@Override
public Integer countArticleByCategoryId(Integer categoryId) {
Integer count = 0;
try {
count = articleCategoryRefMapper.countArticleByCategoryId(categoryId);
} catch (Exception e) {
e.printStackTrace();
log.error("根据分类统计文章数量失败, categoryId:{}, cause:{}", categoryId, e);
}
return count;
}
@Override
public Integer countArticleByTagId(Integer tagId) {
return articleTagRefMapper.countArticleByTagId(tagId);
}
@Override
public List<Article> listArticle(HashMap<String, Object> criteria) {
return articleMapper.findAll(criteria);
}
@Override
public List<Article> listRecentArticle(Integer limit) {
return articleMapper.listArticleByLimit(limit);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateArticleDetail(Article article) {
article.setArticleUpdateTime(new Date());
articleMapper.update(article);
if (article.getTagList() != null) {
//删除标签和文章关联
articleTagRefMapper.deleteByArticleId(article.getArticleId());
//添加标签和文章关联
for (int i = 0; i < article.getTagList().size(); i++) {
ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId());
articleTagRefMapper.insert(articleTagRef);
}
}
if (article.getCategoryList() != null) {
//添加分类和文章关联
articleCategoryRefMapper.deleteByArticleId(article.getArticleId());
//删除分类和文章关联
for (int i = 0; i < article.getCategoryList().size(); i++) {
ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId());
articleCategoryRefMapper.insert(articleCategoryRef);
}
}
}
@Override
public void updateArticle(Article article) {
articleMapper.update(article);
}
@Override
public void deleteArticleBatch(List<Integer> ids) {
articleMapper.deleteBatch(ids);
}
@Override
public void deleteArticle(Integer id) {
articleMapper.deleteById(id);
}
@Override
public PageInfo<Article> pageArticle(Integer pageIndex,
Integer pageSize,
HashMap<String, Object> criteria) {
PageHelper.startPage(pageIndex, pageSize);
List<Article> articleList = articleMapper.findAll(criteria);
for (int i = 0; i < articleList.size(); i++) {
//封装CategoryList
List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(articleList.get(i).getArticleId());
if (categoryList == null || categoryList.size() == 0) {
categoryList = new ArrayList<>();
categoryList.add(Category.Default());
}
articleList.get(i).setCategoryList(categoryList);
// //封装TagList
// List<Tag> tagList = articleTagRefMapper.listTagByArticleId(articleList.get(i).getArticleId());
// articleList.get(i).setTagList(tagList);
}
return new PageInfo<>(articleList);
}
@Override
public Article getArticleByStatusAndId(Integer status, Integer id) {
Article article = articleMapper.getArticleByStatusAndId(status, id);
if (article != null) {
List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(article.getArticleId());
List<Tag> tagList = articleTagRefMapper.listTagByArticleId(article.getArticleId());
article.setCategoryList(categoryList);
article.setTagList(tagList);
}
return article;
}
@Override
public List<Article> listArticleByViewCount(Integer limit) {
return articleMapper.listArticleByViewCount(limit);
}
@Override
public Article getAfterArticle(Integer id) {
return articleMapper.getAfterArticle(id);
}
@Override
public Article getPreArticle(Integer id) {
return articleMapper.getPreArticle(id);
}
@Override
public List<Article> listRandomArticle(Integer limit) {
return articleMapper.listRandomArticle(limit);
}
@Override
public List<Article> listArticleByCommentCount(Integer limit) {
return articleMapper.listArticleByCommentCount(limit);
}
@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);
articleMapper.insert(article);
//添加分类和文章关联
for (int i = 0; i < article.getCategoryList().size(); i++) {
ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId());
articleCategoryRefMapper.insert(articleCategoryRef);
}
//添加标签和文章关联
for (int i = 0; i < article.getTagList().size(); i++) {
ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId());
articleTagRefMapper.insert(articleTagRef);
}
}
@Override
public void upd