package com.light.hexo.core.admin.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.light.hexo.common.base.BaseServiceImpl;
import com.light.hexo.core.admin.component.BaiDuPushService;
import com.light.hexo.common.constant.ConfigEnum;
import com.light.hexo.common.constant.HexoExceptionEnum;
import com.light.hexo.mapper.mapper.PostMapper;
import com.light.hexo.mapper.base.BaseMapper;
import com.light.hexo.mapper.model.*;
import com.light.hexo.common.event.PostEvent;
import com.light.hexo.core.admin.service.*;
import com.light.hexo.core.portal.constant.PageConstant;
import com.light.hexo.core.portal.model.HexoPageInfo;
import com.light.hexo.common.base.BaseRequest;
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.request.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.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)
.notSelect("content", "contentHtml").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(this,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 (isMarkdown) {
post.setContentHtml(MarkdownUtil.md2html(post.getContent()));
} else {
post.setContentHtml(this.transformHtml(post.getContent()));
}
LocalDateTime now = LocalDateTime.now();
LocalDateTime jobTime = post.getJ
byg_qlh
- 粉丝: 1078
- 资源: 144
最新资源
- 西门子S7-1200流水线贴膜机项目程序,有KTP700触摸屏组态操作界面 程序包含.上下气缸控制,夹紧气缸控制,输送带电机控制,贴膜伺服控制旋转电机控制等类容 适合简单控制工艺及运动控制初学
- comsol电力变压器电磁场计算模型,通过简化高低压绕组,铁心,结构件和绝缘油的几何模型,得到变压器内部电磁场分布
- 西门子PLC1500大型程序 西门子PLC1500大型程序fanuc机器人焊装 包括1台西门子1500PLC程序,2台触摸屏TP1500程序 9个智能远程终端ET200SP Profinet连接 15
- Task-113-出租车计价
- HttpUrlConnectionUtilDemo-大炮打蚊子
- fishing-game-大炮打蚊子
- 有限信息,多智能体系统,鲁棒无碰撞编队控制,有文献参考 符合要求请放心联系,MATLAB,保证能够运行 simulink,复现
- MATLAB全桥或者半桥LLC谐振DC DC变器的设计与仿真 内含开环仿真、电压闭环仿真两个仿真文件 并含有电路参数仿真计算过程 支持最高版本2018b
- 基于模型参考自适应的永磁同步电机无感FOC 1.采用模型参考自适应MRAS来估计转速和转子位置,实现中高速下PMSM的无感运行,估计精度较高; 2.提供算法对应的参考文献和仿真模型,支持技术解答;拿后
- hustoj-重庆大学python题库答案
- 基于阻抗的微电网功率分配策略研究(文章复现),关键词:下垂控制,并联逆变器,阻抗
- Util-大炮打蚊子c++
- 一种微电网分布式电源的下垂控制策略(文章复现),关键词:下垂控制,并联逆变器,对等控制
- 汽车手动变速器sw18可编辑全套设计资料100%好用.zip
- 基于PreScan与Carsim的车道保持联合仿真 其中车道线识别算法通过采集单目摄像头数据,Ransac算法提取车道线;采用MPC控制器实现车道保持与循迹控制 内附
- 西门子1200博图程序冷却油泵PID控制系统,和多台油泵及水泵G120西门子变频器Modbud RTU通讯,画面采用西门子KTP700触摸屏,内有变频器参数 Modbus通讯报文详细讲解,PID带手动
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈