package com.light.hexo.business.admin.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.light.hexo.business.admin.component.BaiDuPushService;
import com.light.hexo.business.admin.constant.ConfigEnum;
import com.light.hexo.business.admin.constant.HexoExceptionEnum;
import com.light.hexo.business.admin.mapper.PostMapper;
import com.light.hexo.business.admin.model.*;
import com.light.hexo.business.admin.model.event.PostEvent;
import com.light.hexo.business.admin.service.*;
import com.light.hexo.business.portal.constant.PageConstant;
import com.light.hexo.business.portal.model.HexoPageInfo;
import com.light.hexo.common.base.BaseMapper;
import com.light.hexo.common.base.BaseRequest;
import com.light.hexo.common.base.BaseServiceImpl;
import com.light.hexo.common.component.event.BaseEvent;
import com.light.hexo.common.component.event.EventEnum;
import com.light.hexo.common.component.event.EventPublisher;
import com.light.hexo.common.constant.HexoConstant;
import com.light.hexo.common.exception.GlobalException;
import com.light.hexo.common.model.PostRequest;
import com.light.hexo.common.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;
import javax.servlet.ServletContext;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @Author MoonlightL
* @ClassName: PostServiceImpl
* @ProjectName hexo-boot
* @Description: 文章 Service 实现
* @DateTime 2020/7/31 17:28
*/
@CacheConfig(cacheNames = "postCache")
@Service
@Slf4j
public class PostServiceImpl extends BaseServiceImpl<Post> implements PostService {
@Autowired
private PostMapper postMapper;
@Autowired
private CategoryService categoryService;
@Autowired
private TagService tagService;
@Autowired
private PostTagService postTagService;
@Autowired
private ConfigService configService;
@Autowired
private BaiDuPushService baiDuPushService;
@Autowired
private PostTaskService postTaskService;
@Autowired
@Lazy
private EventPublisher eventPublisher;
private static final Integer COVER_NUM = 20;
@Override
public BaseMapper<Post> getBaseMapper() {
return this.postMapper;
}
@Override
protected Example getExample(BaseRequest request) {
// 获取查询参数
PostRequest postRequest = (PostRequest) request;
Example example = Example.builder(Post.class)
.select("id", "title", "link", "customLink", "customLink", "coverUrl", "categoryId",
"readNum", "commentNum", "praiseNum", "authCode",
"publishDate","publish", "comment", "top").orderByDesc("createTime").build();
Example.Criteria criteria = example.createCriteria();
Boolean publish = postRequest.getPublish();
if (publish != null) {
criteria.andEqualTo("publish", publish);
}
Boolean top = postRequest.getTop();
if (top != null) {
criteria.andEqualTo("top", top);
}
String title = postRequest.getTitle();
if (StringUtils.isNotBlank(title)) {
criteria.andLike("title", "%" + title.trim() + "%");
}
Integer categoryId = postRequest.getCategoryId();
if (categoryId != null) {
criteria.andEqualTo("categoryId", categoryId);
}
String publishDate = postRequest.getPublishDate();
if (StringUtils.isNotBlank(publishDate)) {
criteria.andEqualTo("publishDate", publishDate.trim());
}
String authCode = postRequest.getAuthCode();
if (StringUtils.isNotBlank(authCode)) {
if ("1".equals(authCode)) {
criteria.andNotEqualTo("authCode", "");
} else {
criteria.andEqualTo("authCode", "");
}
}
return example;
}
@Override
public PageInfo<Post> findPage(BaseRequest<Post> request) throws GlobalException {
PageInfo<Post> pageInfo = super.findPage(request);
List<Post> list = pageInfo.getList();
if (CollectionUtils.isEmpty(list)) {
return new PageInfo<>();
}
List<Category> categoryList = this.categoryService.findAll();
Map<Integer, Category> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId, Function.identity(), (k1, k2)->k1));
for (Post post : list) {
Category category = categoryMap.get(post.getCategoryId());
post.setCategoryName(category == null ? "默认" : category.getName());
if (StringUtils.isNotBlank(post.getCustomLink())) {
post.setLink(post.getCustomLink() + ".html");
}
}
return pageInfo;
}
@Override
public void removePostBatch(List<String> idStrList) throws GlobalException {
List<Long> idList = idStrList.stream().map(Long::valueOf).collect(Collectors.toList());
Example example = new Example(Post.class);
example.createCriteria().andIn("id", idList);
int num = this.getBaseMapper().deleteByExample(example);
if (num > 0) {
this.eventPublisher.emit(new PostEvent(null, PostEvent.Type.POST_NUM));
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void savePost(Post post) throws GlobalException {
Category category = this.categoryService.findById(post.getCategoryId());
if (category == null || !category.getState()) {
ExceptionUtil.throwEx(HexoExceptionEnum.ERROR_CATEGORY_NOT_EXIST);
}
// 前端上传的参数可能带空格,影响条件查询
post.setTitle(post.getTitle().trim());
Example example = new Example(Post.class);
example.createCriteria().andEqualTo("title", post.getTitle());
int titleCount = this.getBaseMapper().selectCountByExample(example);
if (titleCount > 0) {
ExceptionUtil.throwEx(HexoExceptionEnum.ERROR_POST_TITLE_REPEAT);
}
if (StringUtils.isBlank(post.getAuthor())) {
post.setAuthor(this.configService.getConfigValue(ConfigEnum.BLOG_AUTHOR.getName()));
}
String coverUrl = post.getCoverUrl();
if(StringUtils.isBlank(coverUrl)) {
int num = new Random().nextInt(COVER_NUM);
coverUrl = HexoConstant.DEFAULT_IMG_DIR + "/post/post_cover_" + num + ".jpg";
post.setCoverUrl(coverUrl);
}
boolean isMarkdown = this.configService.getConfigValue(ConfigEnum.EDITOR_TYPE.getName()).equals("markdown");
String content = post.getContent();
// 摘要
post.setSummary(this.interceptContent(content, isMarkdown))
.setSummaryHtml(this.interceptContentHtml(content, isMarkdown));
if (i
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码
资源推荐
资源详情
资源评论
收起资源包目录
基于 Spring Boot + MySQL 开发的博客系统源码.zip (1835个子文件)
mvnw.cmd 6KB
codebase.min.css 351KB
bootstrap.css 143KB
bootstrap.min.css 118KB
bootstrap.min.css 118KB
bootstrap.min.css 118KB
iziModal.min.css 84KB
iziModal.min.css 84KB
editormd.css 76KB
animate.css 76KB
material-design-iconic-font.min.css 69KB
editormd.min.css 60KB
editormd.preview.css 55KB
app.css 47KB
editormd.preview.min.css 44KB
font-awesome.min.css 30KB
sweetalert2.min.css 26KB
ambiance.css 26KB
ambiance.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
bootstrap-datepicker3.min.css 21KB
summernote.min.css 19KB
flatpickr.min.css 16KB
layer.css 14KB
layer.css 14KB
jquery.fancybox.min.css 12KB
jquery.fancybox.min.css 12KB
APlayer.min.css 12KB
dropzone.min.css 9KB
codemirror.css 9KB
hb-comment.css 8KB
hb-comment.css 8KB
awesome.css 8KB
codemirror.css 8KB
magnific-popup.css 7KB
metroStyle.css 6KB
zTreeStyle.css 6KB
fonts.googleapis.css 6KB
solarized.css 5KB
codemirror.min.css 5KB
emoji.css 5KB
layer.css 5KB
layer.css 5KB
mdn-like.css 5KB
mdn-like.css 5KB
solarized.css 5KB
share.min.css 4KB
liquibyte.css 4KB
waves.min.css 4KB
merge.css 3KB
merge.css 3KB
yonce.css 3KB
xq-dark.css 3KB
lint.css 3KB
lint.css 3KB
jquery.search.css 3KB
xq-dark.css 3KB
material-palenight.css 3KB
duotone-light.css 3KB
darcula.css 3KB
lesser-dark.css 3KB
duotone-dark.css 3KB
material-darker.css 3KB
material-ocean.css 3KB
icecoder.css 2KB
pastel-on-dark.css 2KB
ttcn.css 2KB
shadowfox.css 2KB
tomorrow-night-eighties.css 2KB
moxer.css 2KB
ayu-mirage.css 2KB
lesser-dark.css 2KB
material.css 2KB
erlang-dark.css 2KB
oceanic-next.css 2KB
xq-light.css 2KB
ayu-dark.css 2KB
pastel-on-dark.css 2KB
xq-light.css 2KB
monokai.css 2KB
twilight.css 2KB
vibrant-ink.css 2KB
tomorrow-night-eighties.css 2KB
base16-light.css 2KB
base16-dark.css 2KB
mbo.css 2KB
nord.css 2KB
paraiso-dark.css 2KB
paraiso-light.css 2KB
3024-night.css 2KB
editormd.logo.css 2KB
dracula.css 2KB
erlang-dark.css 2KB
seti.css 2KB
zenburn.css 2KB
zenburn.css 2KB
3024-day.css 2KB
abcdef.css 2KB
共 1835 条
- 1
- 2
- 3
- 4
- 5
- 6
- 19
「已注销」
- 粉丝: 792
- 资源: 3613
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页