package com.ming.biz.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.biz.component.StringAndArray;
import com.ming.biz.constant.SiteOwner;
import com.ming.biz.mapper.ArticleMapper;
import com.ming.biz.model.Article;
import com.ming.biz.service.*;
import com.ming.biz.service.*;
import com.ming.biz.utils.TimeUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author: wellfuture
* @Date: 2018/6/20 21:42
* Describe:
*/
@Service
public class ArticleServiceImpl implements ArticleService {
private Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);
@Autowired
private ArticleMapper articleMapper;
@Autowired
private ArticleService articleService;
@Autowired
private ArticleLikesRecordService articleLikesRecordService;
@Autowired
private VisitorService visitorService;
@Autowired
private ArchiveService archiveService;
@Autowired
private CommentService commentService;
@Autowired
private CommentLikesRecordService commentLikesRecordService;
@Override
public JSONObject insertArticle(Article article) {
JSONObject articleReturn = new JSONObject();
try {
if("".equals(article.getOriginalAuthor())){
article.setOriginalAuthor(article.getAuthor());
}
if("".equals(article.getArticleUrl())){
//保存文章的url
String url = SiteOwner.SITE_OWNER_URL + "/article/" + article.getArticleId();
article.setArticleUrl(url);
}
Article endArticleId = articleMapper.findEndArticleId();
//设置文章的上一篇文章id
if(endArticleId != null){
article.setLastArticleId(endArticleId.getArticleId());
}
articleMapper.insertArticle(article);
//判断发表文章的归档日期是否存在,不存在则插入归档日期
TimeUtil timeUtil = new TimeUtil();
String archiveName = timeUtil.timeWhippletreeToYear(article.getPublishDate().substring(0, 7));
archiveService.addArchiveName(archiveName);
//新文章加入访客量
visitorService.insertVisitorArticlePage("article/" + article.getArticleId());
//设置上一篇文章的下一篇文章id
if(endArticleId != null){
articleService.updateArticleLastOrNextId("nextArticleId", article.getArticleId(), endArticleId.getArticleId());
}
articleReturn.put("status",200);
articleReturn.put("articleTitle",article.getArticleTitle());
articleReturn.put("updateDate",article.getUpdateDate());
articleReturn.put("author",article.getOriginalAuthor());
//本博客中的URL
articleReturn.put("articleUrl","/article/" + article.getArticleId());
return articleReturn;
} catch (Exception e){
articleReturn.put("status",500);
logger.error("用户 " + article.getAuthor() + " 保存文章 " + article.getArticleTitle() + " 失败");
e.printStackTrace();
return articleReturn;
}
}
@Override
public JSONObject updateArticleById(Article article) {
Article a = articleMapper.getArticleUrlById(article.getId());
if("原创".equals(article.getArticleType())){
article.setOriginalAuthor(article.getAuthor());
String url = SiteOwner.SITE_OWNER_URL + "/article/" + a.getArticleId();
article.setArticleUrl(url);
}
articleMapper.updateArticleById(article);
JSONObject articleReturn = new JSONObject();
articleReturn.put("status",200);
articleReturn.put("articleTitle",article.getArticleTitle());
articleReturn.put("updateDate",article.getUpdateDate());
articleReturn.put("author",article.getOriginalAuthor());
//本博客中的URL
articleReturn.put("articleUrl","/article/" + a.getArticleId());
return articleReturn;
}
@Override
public JSONObject getArticleByArticleId(long articleId, String username) {
Article article = articleMapper.getArticleByArticleId(articleId);
JSONObject jsonObject = new JSONObject();
if(article != null){
Article lastArticle = articleMapper.findArticleByArticleId(article.getLastArticleId());
Article nextArticle = articleMapper.findArticleByArticleId(article.getNextArticleId());
jsonObject.put("status","200");
jsonObject.put("author",article.getAuthor());
jsonObject.put("articleId",articleId);
jsonObject.put("originalAuthor",article.getOriginalAuthor());
jsonObject.put("articleTitle",article.getArticleTitle());
jsonObject.put("publishDate",article.getPublishDate());
jsonObject.put("updateDate",article.getUpdateDate());
jsonObject.put("articleContent",article.getArticleContent());
jsonObject.put("articleTags", StringAndArray.stringToArray(article.getArticleTags()));
jsonObject.put("articleType",article.getArticleType());
jsonObject.put("articleCategories",article.getArticleCategories());
jsonObject.put("articleUrl",article.getArticleUrl());
jsonObject.put("likes",article.getLikes());
if(username == null){
jsonObject.put("isLiked",0);
}else {
if(articleLikesRecordService.isLiked(articleId,username)){
jsonObject.put("isLiked",1);
}else {
jsonObject.put("isLiked",0);
}
}
if(lastArticle != null){
jsonObject.put("lastStatus","200");
jsonObject.put("lastArticleTitle",lastArticle.getArticleTitle());
jsonObject.put("lastArticleUrl","/article/" + lastArticle.getArticleId());
} else {
jsonObject.put("lastStatus","500");
jsonObject.put("lastInfo","无");
}
if(nextArticle != null){
jsonObject.put("nextStatus","200");
jsonObject.put("nextArticleTitle",nextArticle.getArticleTitle());
jsonObject.put("nextArticleUrl","/article/" + nextArticle.getArticleId());
} else {
jsonObject.put("nextStatus","500");
jsonObject.put("nextInfo","无");
}
return jsonObject;
} else {
jsonObject.put("status","500");
jsonObject.put("errorInfo","获取文章信息失败");
logger.error("获取文章id " + articleId + " 失败");
return jsonObject;
}
}
@Override
public Map<String,String> findArticleTitleByArticleId(long articleId) {
Article articleInfo = articleMapper.findArticleTitleByArticleId(articleId);
Map<String, String> articleMap = new HashMap<>();
if(articleInfo != null){
articleMap.put("articleTitle", articleInfo.getArticleTitle());
articleMap.put("articleTabloid", articleInfo.getArticleTabloid());
}
return articleMap;
}
@Override
public JSONArray findAllArticles(String rows, String pageNo) {
int pageNum = Integer.parseInt(pageNo);
int pageSize = Integer.parseInt(rows);
PageHelper.startPage(pageNum,pageSize);
List<Article> articles = articleMapper.findAllArticles();
PageInfo<Article> pageInfo = new PageInfo<>(articles);
List<Map<String, Object>> newArticles = new ArrayL