package com.quanxiaoha.weblog.web.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.quanxiaoha.weblog.common.PageResponse;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.domain.dos.*;
import com.quanxiaoha.weblog.common.enums.EventEnum;
import com.quanxiaoha.weblog.common.eventbus.ArticleEvent;
import com.quanxiaoha.weblog.common.exception.ResourceNotFoundException;
import com.quanxiaoha.weblog.web.convert.ArticleConvert;
import com.quanxiaoha.weblog.web.dao.*;
import com.quanxiaoha.weblog.web.model.vo.article.*;
import com.quanxiaoha.weblog.web.model.vo.category.QueryCategoryListItemRspVO;
import com.quanxiaoha.weblog.web.model.vo.tag.QueryTagListItemRspVO;
import com.quanxiaoha.weblog.web.service.ArticleService;
import com.quanxiaoha.weblog.web.utils.MarkdownUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author: 犬小哈
* @url: www.quanxiaoha.com
* @date: 2023-04-17 12:08
* @description: TODO
**/
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleDao articleDao;
@Autowired
private ArticleContentDao articleContentDao;
@Autowired
private CategoryDao categoryDao;
@Autowired
private ArticleCategoryRelDao articleCategoryRelDao;
@Autowired
private TagDao tagDao;
@Autowired
private ArticleTagRelDao articleTagRelDao;
@Autowired
private EventBus eventBus;
@Autowired
private ArticleConvert articleConvert;
@Override
public PageResponse queryIndexArticlePageList(QueryIndexArticlePageListReqVO queryIndexArticlePageListReqVO) {
Long current = queryIndexArticlePageListReqVO.getCurrent();
Long size = queryIndexArticlePageListReqVO.getSize();
IPage<ArticleDO> articleDOIPage = articleDao.queryArticlePageList(current, size);
List<ArticleDO> records = articleDOIPage.getRecords();
List<QueryIndexArticlePageItemRspVO> list = null;
if (!CollectionUtils.isEmpty(records)) {
list = records.stream()
.map(articleDO -> articleConvert.convert(articleDO))
.collect(Collectors.toList());
List<Long> articleIds = list.stream().map(p -> p.getId()).collect(Collectors.toList());
// 设置分类信息
List<CategoryDO> categoryDOS = categoryDao.selectAllCategory();
Map<Long, String> categoryIdNameMap = categoryDOS.stream().collect(Collectors.toMap(CategoryDO::getId, CategoryDO::getName));
List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelDao.selectByArticleIds(articleIds);
list = list.stream().map(p -> {
Long articleId = p.getId();
Optional<ArticleCategoryRelDO> optional = articleCategoryRelDOS.stream().filter(rel -> Objects.equals(rel.getArticleId(), articleId)).findFirst();
if (optional.isPresent()) {
ArticleCategoryRelDO articleCategoryRelDO = optional.get();
Long categoryId = articleCategoryRelDO.getCategoryId();
String categoryName = categoryIdNameMap.get(categoryId);
QueryCategoryListItemRspVO queryCategoryListItemRspVO = QueryCategoryListItemRspVO.builder()
.id(categoryId)
.name(categoryName)
.build();
p.setCategory(queryCategoryListItemRspVO);
}
return p;
}).collect(Collectors.toList());
// 设置标签信息
List<TagDO> tagDOS = tagDao.selectAllTag();
Map<Long, String> tagIdNameMap = tagDOS.stream().collect(Collectors.toMap(TagDO::getId, TagDO::getName));
List<ArticleTagRelDO> articleTagRelDOS = articleTagRelDao.selectByArticleIds(articleIds);
list = list.stream().map(p -> {
Long articleId = p.getId();
List<ArticleTagRelDO> articleTagRelDOList = articleTagRelDOS.stream().filter(rel -> Objects.equals(rel.getArticleId(), articleId)).collect(Collectors.toList());
List<QueryTagListItemRspVO> queryTagListItemRspVOS = Lists.newArrayList();
articleTagRelDOList.forEach(rel -> {
Long tagId = rel.getTagId();
String tagName = tagIdNameMap.get(tagId);
QueryTagListItemRspVO queryTagListItemRspVO = QueryTagListItemRspVO.builder()
.id(tagId)
.name(tagName)
.build();
queryTagListItemRspVOS.add(queryTagListItemRspVO);
});
p.setTags(queryTagListItemRspVOS);
return p;
}).collect(Collectors.toList());
}
return PageResponse.success(articleDOIPage, list);
}
@Override
public PageResponse queryCategoryArticlePageList(QueryCategoryArticlePageListReqVO queryCategoryArticlePageListReqVO) {
Long current = queryCategoryArticlePageListReqVO.getCurrent();
Long size = queryCategoryArticlePageListReqVO.getSize();
Long queryCategoryId = queryCategoryArticlePageListReqVO.getCategoryId();
List<ArticleCategoryRelDO> articleCategoryRelDOList = articleCategoryRelDao.selectByCategoryId(queryCategoryId);
// 判断该分类下是否存在文章
if (CollectionUtils.isEmpty(articleCategoryRelDOList)) {
return PageResponse.success(null, null);
}
List<Long> categoryArticleIds = articleCategoryRelDOList.stream().map(p -> p.getArticleId()).collect(Collectors.toList());
IPage<ArticleDO> articleDOIPage = articleDao.queryArticlePageListByArticleIds(current, size, categoryArticleIds);
List<ArticleDO> records = articleDOIPage.getRecords();
List<QueryIndexArticlePageItemRspVO> list = null;
if (!CollectionUtils.isEmpty(records)) {
list = records.stream()
.map(articleDO -> articleConvert.convert(articleDO))
.collect(Collectors.toList());
List<Long> articleIds = list.stream().map(p -> p.getId()).collect(Collectors.toList());
// 设置分类信息
List<CategoryDO> categoryDOS = categoryDao.selectAllCategory();
Map<Long, String> categoryIdNameMap = categoryDOS.stream().collect(Collectors.toMap(CategoryDO::getId, CategoryDO::getName));
List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelDao.selectByArticleIds(articleIds);
list = list.stream().map(p -> {
Long articleId = p.getId();
Optional<ArticleCategoryRelDO> optional = articleCategoryRelDOS.stream().filter(rel -> Objects.equals(rel.getArticleId(), articleId)).findFirst();
if (optional.isPresent()) {
ArticleCategoryRelDO articleCategoryRelDO = optional.get();
Long categoryId = articleCategoryRelDO.getCategoryId();
String categoryName = categoryIdNameMap.get(categoryId);
QueryCategoryListItemRspVO queryCategoryListItemRspVO = QueryCategoryListItemRspVO.builder()
.id(categoryId)
.name(categoryName)
.build();
p.setCategory(queryCategoryListItemRspVO);
}
return p;
}).collect(Collectors.toList());
// 设置标签信息
List<TagDO> tagDOS =