package com.blog.personalblog.service.Impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.blog.personalblog.bo.ArticleBO;
import com.blog.personalblog.bo.ArticleInsertBO;
import com.blog.personalblog.common.ArticleArtStatusEnum;
import com.blog.personalblog.config.mail.MailInfo;
import com.blog.personalblog.config.mail.SendMailConfig;
import com.blog.personalblog.entity.Article;
import com.blog.personalblog.entity.ArticleTag;
import com.blog.personalblog.entity.Category;
import com.blog.personalblog.entity.Tag;
import com.blog.personalblog.entity.User;
import com.blog.personalblog.mapper.ArticleMapper;
import com.blog.personalblog.service.ArticleService;
import com.blog.personalblog.service.ArticleTagService;
import com.blog.personalblog.service.CategoryService;
import com.blog.personalblog.service.TagService;
import com.blog.personalblog.service.UserService;
import com.blog.personalblog.util.FileUtils;
import com.blog.personalblog.util.WordCountUtil;
import com.blog.personalblog.vo.ArticleVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: SuperMan
* @create: 2021-12-01
*/
@Log4j2
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
ArticleMapper articleMapper;
@Resource
ArticleTagService articleTagService;
@Resource
TagService tagService;
@Resource
private CategoryService categoryService;
@Resource
private UserService userService;
/**
* 本地路径
*/
@Value("${upload.local.path}")
private String localPath;
/**
* 访问url
*/
@Value("${upload.local.url}")
private String localUrl;
private static final String ARTICLE = "articles/";
/**
* key:文章id
* value: 文章
*/
Map<Integer, Article> articleMap = new LinkedHashMap<>();
@Override
@PostConstruct
public void init() {
List<Article> articleList = articleMapper.findAll();
try {
getTagsOrCategory(articleList);
for(Article article : articleList) {
articleMap.put(article.getId(), article);
}
log.info("文章缓存数据加载完成");
} catch (Exception e) {
log.error("文章缓存数据加载失败!", e.getMessage());
}
}
@Override
public List<Article> getArticlePage(ArticleBO articleBO) {
int pageNum = articleBO.getPageNum();
int pageSize = articleBO.getPageSize();
PageHelper.startPage(pageNum,pageSize, "id desc");
List<Article> articleList = articleMapper.getArticlePage(articleBO);
getTagsOrCategory(articleList);
return articleList;
}
public void getTagsOrCategory(List<Article> list) {
if (list != null) {
for (Article article : list) {
//查询分类
Category category = categoryService.findById(article.getCategoryId());
if (category == null) {
article.setCategoryName("无分类");
} else {
article.setCategoryName(category.getCategoryName());
}
List<Tag> tagList = new ArrayList<>();
List<ArticleTag> articleTags = articleTagService.findArticleTagById(article.getId());
if (articleTags != null) {
for (ArticleTag articleTag : articleTags) {
Tag tag = tagService.findTagById(articleTag.getTagId());
tagList.add(tag);
}
}
article.setTagList(tagList);
}
}
}
@Override
public void insertOrUpdateArticle(ArticleInsertBO bo) {
//分类添加
Category category = saveCategory(bo);
Article article = BeanUtil.copyProperties(bo, Article.class);
if (category != null) {
article.setCategoryId(category.getCategoryId());
}
String username = (String) SecurityUtils.getSubject().getPrincipal();
User user = userService.getUserByUserName(username);
article.setUserId(user.getId());
article.setAuthor(user.getUserName());
article.setViews(0L);
article.setTotalWords(WordCountUtil.wordCount(bo.getContent()));
if (bo.getId() != null) {
articleMapper.updateArticle(article);
} else {
articleMapper.createArticle(article);
}
articleMap.put(article.getId(), article);
//添加文章标签
saveTags(bo, article.getId());
this.init();
//添加文章发送邮箱提醒
try {
String content = "【{0}】您好:\n" +
"您已成功发布了标题为: {1} 的文章 \n" +
"请注意查收!\n";
MailInfo build = MailInfo.builder()
.receiveMail(user.getEmail())
.content(MessageFormat.format(content, user.getUserName(), article.getTitle()))
.title("文章发布")
.build();
SendMailConfig.sendMail(build);
} catch (Exception e) {
log.error("邮件发送失败{}", e.getMessage());
}
}
private Category saveCategory(ArticleInsertBO bo) {
if (StrUtil.isEmpty(bo.getCategoryName())) {
return null;
}
Category cat = new Category();
Category category = categoryService.getCategoryByName(bo.getCategoryName());
if (category == null && !ArticleArtStatusEnum.DRAFT.getStatus().equals(bo.getArtStatus())) {
cat.setCategoryName(bo.getCategoryName());
categoryService.saveCategory(cat);
}
return cat;
}
private void saveTags(ArticleInsertBO bo, Integer articleId) {
//首先判断是不是更新文章
if (bo.getId() == null) {
articleTagService.deleteTag(bo.getId());
}
//添加文章标签
List<String> tagNameList = bo.getTagNameList();
List<Integer> tagIdList = new ArrayList<>();
if (CollUtil.isNotEmpty(tagNameList)) {
//先查看添加的标签数据库里有没有
for (String tagName : tagNameList) {
Tag one = tagService.findByTagName(tagName);
if (one == null) {
Tag tag = new Tag();
tag.setTagName(tagName);
tagService.saveTag(tag);
tagIdList.add(tag.getId());
} else {
tagIdList.add(one.getId());
}
}
}
articleTagService.deleteTag(articleId);
if (tagIdList != null) {
List<ArticleTag> articleTagList = tagIdList.stream().map(tagId -> ArticleTag.builder()
.tagId(tagId)
.articleId(articleId)
.build()).collect(Collectors.toList());
articleTagService.insertBatch(articleTagList);
}
}
@Override
public void updateArticle(Article article) {
articleMapper.updateArticle(article);
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于Spring Boot和vue-element开发个人博客项目是一个非常实用的毕业设计和课设主题。满足大多数人的需求。 项目以个人网站为主题,采用前后端分离进行项目的搭建。管理平台则使用框架vue-element-admin进行搭建快速开发,总的来说,这是一个前后端分离的小项目,采用的都是最基础的知识点,几乎没有太多的难点。我的初心是让大家大概了解做项目的流程,以及编写代码的流程和做项目的思想等。本项目有完整的教学文档,可以在我的主页查看相关专题的教程。 适合毕业设计、课程设计作业,该源码均经过严格测试,可以直接运行,可以放心下载使用。在运行的过程中,有任何使用问题欢迎随时与我联系,可以私信我,我看到后会在第一时间进行解答! 适合毕业设计、课程设计作业,该源码均经过严格测试,可以直接运行,可以放心下载使用。在运行的过程中,有任何使用问题欢迎随时与我联系,可以私信我,我看到后会在第一时间进行解答! 适合毕业设计、课程设计作业,该源码均经过严格测试,可以直接运行,可以放心下载使用。在运行的过程中,有任何使用问题欢迎随时与我联系,可以私信我,我看到后会在第一时间进行解答!
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计&课设-基于Spring Boot + vue-element 开发个人博客项目.zip (313个子文件)
.babelrc 229B
ArticleServiceImpl.class 15KB
StatisticsServiceImpl.class 11KB
UserController.class 10KB
ArticleVO.class 10KB
Article.class 10KB
TagController.class 6KB
ArticleController.class 6KB
NoticeController.class 6KB
ShiroConfiguration.class 5KB
OperationLog.class 5KB
ArticleInsertBO.class 5KB
User.class 5KB
Notice.class 5KB
LoginOperationLog.class 5KB
CategoryController.class 5KB
OptLogAspect.class 5KB
TagServiceImpl.class 4KB
IpUtil.class 4KB
OperationLogController.class 3KB
Category.class 3KB
IndexController.class 3KB
Tag.class 3KB
ArticleBO.class 3KB
StatisticsTopCountVO.class 3KB
CategoryServiceImpl.class 3KB
PageResult.class 3KB
JsonResult.class 3KB
StatisticsWordCloudVO.class 3KB
MailInfo.class 3KB
MyShiroRealm.class 3KB
SendMailConfig.class 3KB
Swagger3Config.class 2KB
NoticeServiceImpl.class 2KB
UserServiceImpl.class 2KB
ArticleTag.class 2KB
ArticleTagServiceImpl.class 2KB
StatisticsBaseCountVO.class 2KB
FileUtils.class 2KB
StatisticsTagCountVO.class 2KB
ServletUtils.class 2KB
LoginModel.class 2KB
LoginOperationLogServiceImpl.class 2KB
OperationLogServiceImpl.class 2KB
ArticleArtStatusEnum.class 2KB
MD5Util.class 2KB
ResourcesConfig.class 2KB
ErrorCode.class 2KB
MySessionManager.class 2KB
OperationType.class 2KB
PageRequest.class 2KB
CategoryBO.class 1KB
PageRequestApi.class 1KB
TagBO.class 1KB
MailInfo$MailInfoBuilder.class 1KB
PageUtil.class 1KB
ArticleTag$ArticleTagBuilder.class 1KB
WebAppConfigurer.class 1KB
TagMapper.class 1KB
TagService.class 1KB
PhoneUtil.class 1KB
CategoryService.class 1KB
WordCountUtil.class 968B
ArticleService.class 966B
CategoryMapper.class 962B
ArticleMapper.class 954B
PersonalBlogApplication.class 930B
NoticeService.class 800B
ArticleTagMapper.class 789B
UserMapper.class 734B
StatisticsService.class 727B
UserService.class 697B
NoticeMapper.class 679B
OperationLogSys.class 674B
PersonalBlogApplicationTests.class 565B
ArticleTagService.class 560B
LoginOperationLogService.class 556B
LoginOperationLogMapper.class 527B
OperationLogService.class 526B
OperationLogMapper.class 492B
StatisticsMapper.class 435B
.editorconfig 243B
.eslintignore 38B
.gitignore 171B
.gitkeep 0B
index.html 280B
favicon.ico 66KB
personal-blog.iml 14KB
ArticleServiceImpl.java 11KB
UserController.java 9KB
ShiroConfiguration.java 6KB
StatisticsServiceImpl.java 6KB
TagController.java 4KB
ArticleController.java 4KB
NoticeController.java 4KB
IndexController.java 4KB
OptLogAspect.java 3KB
CategoryController.java 3KB
IpUtil.java 3KB
TagServiceImpl.java 3KB
共 313 条
- 1
- 2
- 3
- 4
资源评论
码上言
- 粉丝: 1w+
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功