|
|
|
@ -2,6 +2,7 @@ package com.luoo.music.service;
|
|
|
|
|
|
|
|
|
|
import api.PageResult;
|
|
|
|
|
import api.Result;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.luoo.music.client.UserClient;
|
|
|
|
|
import com.luoo.music.dao.*;
|
|
|
|
|
import com.luoo.music.dto.request.cms.ArticleAddModel;
|
|
|
|
@ -11,6 +12,7 @@ import com.luoo.music.pojo.Article;
|
|
|
|
|
import com.luoo.music.pojo.Comment;
|
|
|
|
|
import com.luoo.music.pojo.UserInfo;
|
|
|
|
|
import com.luoo.music.util.Constants;
|
|
|
|
|
import net.oschina.j2cache.CacheChannel;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.domain.Example;
|
|
|
|
@ -55,6 +57,9 @@ public class CMSArticleService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private CommentDao commentDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private CacheChannel cacheChannel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Result add(ArticleAddModel paramAdd) {
|
|
|
|
|
String image = null;
|
|
|
|
@ -79,6 +84,7 @@ public class CMSArticleService {
|
|
|
|
|
|
|
|
|
|
if(!articleDao.exists(example)) {
|
|
|
|
|
articleDao.save(article);
|
|
|
|
|
cleanArticleCache(article.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.success();
|
|
|
|
@ -151,31 +157,32 @@ public class CMSArticleService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result deleteById(String id){
|
|
|
|
|
Article article = articleDao.findById(id).get();
|
|
|
|
|
Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
|
|
|
|
|
if (!Objects.isNull(article)) {
|
|
|
|
|
article.setIsDeleted("1");
|
|
|
|
|
articleDao.save(article);
|
|
|
|
|
cleanArticleCache(article.getId());
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
return Result.failed("文章不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result visitAdd(String id){
|
|
|
|
|
Article article = articleDao.findById(id).get();
|
|
|
|
|
Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
|
|
|
|
|
if (!Objects.isNull(article)) {
|
|
|
|
|
article.setVisits(article.getVisits()+1);
|
|
|
|
|
articleDao.save(article);
|
|
|
|
|
cleanArticleCache(id);
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
return Result.failed("文章不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result update(String id,ArticleAddModel param){
|
|
|
|
|
Optional<Article> optional = articleDao.findById(id);
|
|
|
|
|
if (!optional.isPresent()) {
|
|
|
|
|
return Result.failed("找不到期刊: " + id);
|
|
|
|
|
Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
|
|
|
|
|
if (article == null) {
|
|
|
|
|
return Result.failed("找不到文章: " + id);
|
|
|
|
|
}
|
|
|
|
|
Article article = optional.get();
|
|
|
|
|
|
|
|
|
|
//如果图片路径存在 temp/ 则为新图片
|
|
|
|
|
if (StringUtils.isNotBlank(param.getImage()) && param.getImage().contains(Constants.TEMP_KEY_PREFIX)) {
|
|
|
|
@ -207,70 +214,55 @@ public class CMSArticleService {
|
|
|
|
|
article.setPubTime(param.getPubTime());
|
|
|
|
|
|
|
|
|
|
articleDao.save(article);
|
|
|
|
|
cleanArticleCache(article.getId());
|
|
|
|
|
//TODO: 发布方式
|
|
|
|
|
|
|
|
|
|
return Result.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result<ArticleRespDTO> findOne(String id) {
|
|
|
|
|
Optional<Article> optional=articleDao.findById(id);
|
|
|
|
|
if(!optional.isPresent()) {
|
|
|
|
|
return Result.failed("无法找到文章: " + id);
|
|
|
|
|
}
|
|
|
|
|
Article article = optional.get();
|
|
|
|
|
Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
|
|
|
|
|
|
|
|
|
|
if("1".equals(article.getIsDeleted())){
|
|
|
|
|
return Result.success(null);
|
|
|
|
|
}else {
|
|
|
|
|
ArticleRespDTO response = ArticleRespDTO.convertPojo(article);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArticleRespDTO response = ArticleRespDTO.convertPojo(article);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取文章总评论数 一级评论数加上父评论的总回复数
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* 获取文章总评论数 一级评论数加上父评论的总回复数
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
String totalString = "0";
|
|
|
|
|
String totalString = "0";
|
|
|
|
|
|
|
|
|
|
totalString = "0";
|
|
|
|
|
Criteria criteria = Criteria.where("journalId").is(article.getId());
|
|
|
|
|
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
|
|
|
|
|
Aggregation.group().sum("commentCount").as("totalComment"));
|
|
|
|
|
AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
|
|
|
|
|
TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
|
|
|
|
|
totalString = "0";
|
|
|
|
|
Criteria criteria = Criteria.where("journalId").is(article.getId());
|
|
|
|
|
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
|
|
|
|
|
Aggregation.group().sum("commentCount").as("totalComment"));
|
|
|
|
|
AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
|
|
|
|
|
TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
|
|
|
|
|
// commentDTO.setTotalCommentReply("0");
|
|
|
|
|
List<Comment> list = commentDao.findByJournalId(article.getId());
|
|
|
|
|
int total = 0;
|
|
|
|
|
if (null != list && list.size() > 0) {
|
|
|
|
|
total = list.size();
|
|
|
|
|
}
|
|
|
|
|
if (null != totalCommentVo) {
|
|
|
|
|
total = total + totalCommentVo.getTotalComment();
|
|
|
|
|
totalString = total + "";
|
|
|
|
|
if (total > 99) {
|
|
|
|
|
totalString = "99+";
|
|
|
|
|
}
|
|
|
|
|
List<Comment> list = commentDao.findByJournalId(article.getId());
|
|
|
|
|
int total = 0;
|
|
|
|
|
if (null != list && list.size() > 0) {
|
|
|
|
|
total = list.size();
|
|
|
|
|
}
|
|
|
|
|
if (null != totalCommentVo) {
|
|
|
|
|
total = total + totalCommentVo.getTotalComment();
|
|
|
|
|
totalString = total + "";
|
|
|
|
|
if (total > 99) {
|
|
|
|
|
totalString = "99+";
|
|
|
|
|
}
|
|
|
|
|
response.setTotalCommentReply(Long.valueOf(total));
|
|
|
|
|
}
|
|
|
|
|
response.setTotalCommentReply(Long.valueOf(total));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Result.success(response);
|
|
|
|
|
}
|
|
|
|
|
return Result.success(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result<PageResult<ArticleRespDTO>> search(int page, int size) {
|
|
|
|
|
List<ArticleRespDTO> result = new ArrayList<>();
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size);
|
|
|
|
|
Specification<Article> articleSpecification = buildSearchSpecification();
|
|
|
|
|
Page<Article> ArticlePage = articleDao.findAll(articleSpecification,pageRequest);
|
|
|
|
|
|
|
|
|
|
long totalElements = ArticlePage.getTotalElements();
|
|
|
|
|
List<Article> content = ArticlePage.getContent();
|
|
|
|
|
if (!CollectionUtils.isEmpty(content)) {
|
|
|
|
|
for (Article item : content) {
|
|
|
|
|
ArticleRespDTO response = ArticleRespDTO.convertPojo(item);
|
|
|
|
|
result.add(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Result.success(new PageResult<>(totalElements, result));
|
|
|
|
|
List<ArticleRespDTO> pageArticleFromCache = getPageArticleFromCache(page, size);
|
|
|
|
|
return Result.success(new PageResult<>(Long.valueOf(pageArticleFromCache.size()), pageArticleFromCache));
|
|
|
|
|
}
|
|
|
|
|
private Specification<Article> buildSearchSpecification() {
|
|
|
|
|
return (Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
|
|
|
|
@ -294,4 +286,40 @@ public class CMSArticleService {
|
|
|
|
|
String cleanedString = cleanedStringBuilder.toString();
|
|
|
|
|
return cleanedString.length() >=150 ? cleanedString.substring(0,150):cleanedString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<ArticleRespDTO> getPageArticleFromCache(int page , int size){
|
|
|
|
|
|
|
|
|
|
return (List<ArticleRespDTO>) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE_PAGE, page+","+size, key -> getPageArticle(page,size), false).getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<ArticleRespDTO> getPageArticle(int page , int size){
|
|
|
|
|
|
|
|
|
|
List<ArticleRespDTO> result = new ArrayList<>();
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size);
|
|
|
|
|
Specification<Article> articleSpecification = buildSearchSpecification();
|
|
|
|
|
Page<Article> ArticlePage = articleDao.findAll(articleSpecification,pageRequest);
|
|
|
|
|
|
|
|
|
|
List<Article> content = ArticlePage.getContent();
|
|
|
|
|
if (!CollectionUtils.isEmpty(content)) {
|
|
|
|
|
for (Article item : content) {
|
|
|
|
|
ArticleRespDTO response = ArticleRespDTO.convertPojo(item);
|
|
|
|
|
result.add(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getArticleFromCache(String id){
|
|
|
|
|
return (String) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE, id, key -> getArticleById(id), false).getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getArticleById(String id){
|
|
|
|
|
return JSON.toJSONString(articleDao.findById(id).get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cleanArticleCache(String id){
|
|
|
|
|
cacheChannel.evict(constants.Constants.J2CACHE_REGION_ARTICLE,id);
|
|
|
|
|
cacheChannel.clear(constants.Constants.J2CACHE_REGION_ARTICLE_PAGE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|