You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
6.9 KiB
188 lines
6.9 KiB
package cc.bnblogs.service; |
|
|
|
import cc.bnblogs.common.MailHelper; |
|
import cc.bnblogs.common.PageHelper; |
|
import cc.bnblogs.common.WebSite; |
|
import cc.bnblogs.mapper.ArticleMapper; |
|
import cc.bnblogs.mapper.CommentMapper; |
|
import cc.bnblogs.pojo.Article; |
|
import cc.bnblogs.pojo.Comment; |
|
import cc.bnblogs.utils.UpdateUtil; |
|
import org.springframework.data.domain.Page; |
|
import org.springframework.data.domain.PageRequest; |
|
import org.springframework.data.domain.Pageable; |
|
import org.springframework.data.domain.Sort; |
|
import org.springframework.stereotype.Service; |
|
|
|
import java.util.Date; |
|
import java.util.List; |
|
import java.util.Objects; |
|
import java.util.stream.Collectors; |
|
|
|
/** |
|
* @author zfp@bnblogs.cc |
|
* @createTime: 2022/10/17 |
|
*/ |
|
@Service |
|
public class CommentService { |
|
|
|
private final CommentMapper commentMapper; |
|
|
|
private final MailHelper mailHelper; |
|
private final WebSite webSite; |
|
private final ArticleMapper articleMapper; |
|
|
|
public CommentService(CommentMapper commentMapper, MailHelper mailHelper, WebSite webSite, ArticleMapper articleMapper) { |
|
this.commentMapper = commentMapper; |
|
this.mailHelper = mailHelper; |
|
this.webSite = webSite; |
|
this.articleMapper = articleMapper; |
|
} |
|
|
|
/** |
|
* 获取所有评论 |
|
* |
|
* @return 评论列表 |
|
*/ |
|
public List<Comment> list() { |
|
return commentMapper.findAll(); |
|
} |
|
|
|
/** |
|
* 根据是否view的取值返回评论数(null:表示所有) |
|
* @param view 是否已读 |
|
* @return 评论个数 |
|
*/ |
|
public long count(Boolean view) { |
|
return Objects.isNull(view) ? |
|
commentMapper.count(): commentMapper.countCommentByView(view); |
|
} |
|
|
|
/** |
|
* 根据id查询评论对象 |
|
* |
|
* @param id 评论id |
|
* @return id对应的评论对象 |
|
*/ |
|
public Comment detail(Integer id) { |
|
return commentMapper.findById(id).orElse(null); |
|
} |
|
|
|
/** |
|
* 保存评论 |
|
* |
|
* @param comment 待保存的评论对象 |
|
*/ |
|
public void save(Comment comment) { |
|
// 如果是新评论 |
|
if (Objects.isNull(comment.getId())) { |
|
// 有人在你的网站评论了 |
|
sendMailToWebsite(comment.getArticle().getId()); |
|
comment.setCreated(new Date()); |
|
comment.setView(false); |
|
if (comment.getPid() != 0) { |
|
// 有人回复了你的评论 |
|
sendMailToComment(comment.getArticle().getId(), comment.getPid(), comment.getContent()); |
|
// 展示它的父评论 |
|
Comment parent = detail(comment.getPid()); |
|
comment.setContent("@" + parent.getNickname() + ": " + comment.getContent()); |
|
comment.setPid(findCommentPid(comment.getPid())); |
|
} |
|
commentMapper.save(comment); |
|
} |
|
// 修改评论内容 |
|
else { |
|
Comment one = detail(comment.getId()); |
|
UpdateUtil.copyNullProperties(comment, one); |
|
commentMapper.save(one); |
|
} |
|
} |
|
|
|
/** |
|
* 网站有人留言了发送一封邮件给站长 |
|
* |
|
* @param id 文章id |
|
*/ |
|
public void sendMailToWebsite(Integer id) { |
|
Article article = articleMapper.findById(id).orElse(null); |
|
if (Objects.isNull(article)) { |
|
return; |
|
} |
|
String content = "<p>\n" + " 你的文章 <a href=\"" + webSite.getDomain() + "/" + id + ".html\">" + article.getTitle() |
|
+ "</a>收到了新评论," + |
|
"<a href=\"" + webSite.getDomain() + "/" + id + ".html\">" + "点此</a>" + "查看\n" + "</p>\n" + |
|
"<p style=\"text-align:right ;\">\n" + "时间:\n" + new Date() + "</p>"; |
|
mailHelper.sendMail(webSite.getMail(), webSite.getTitle() + "收到新评论", content); |
|
} |
|
|
|
/** |
|
* 异步方法: 有人回复了你的评论,需要给你发送一封邮件 |
|
* |
|
* @param id 文章id |
|
* @param cid 被评论的评论id |
|
* @param reply 回复内容 |
|
*/ |
|
public void sendMailToComment(Integer id, Integer cid, String reply) { |
|
Article article = articleMapper.findById(id).orElse(null); |
|
if (Objects.isNull(article)) { |
|
return; |
|
} |
|
Comment comment = commentMapper.findById(cid).orElse(null); |
|
if (Objects.isNull(comment)) { |
|
return; |
|
} |
|
String content = "<p>\n" + " 你在<a href=\"" + webSite.getDomain() + "\">" + webSite.getTitle() + "</a>对 <a href=\"" + webSite.getDomain() + "/" + article.getId() + ".html\">" + article.getTitle() + "</a>文章的评论收到了新回复,回复内容如下:\n" + "</p>\n" + "<p>\n" + reply + "</p>\n" + "<p style=\"text-align:right ;\">\n" + " 一一发件人:" + webSite.getTitle() + "</p>\n" + "<p style=\"text-align:right ;\">\n" + "时间:" + new Date() + "</p>\n" + "<p style=\"text-align:right ;\">\n" + " 此邮件是由" + webSite.getTitle() + "自动发送,请勿回复 \n" + " </p>"; |
|
mailHelper.sendMail(comment.getEmail(), webSite.getTitle() + "收到新回复", content); |
|
} |
|
|
|
/** |
|
* 递归查找直到找到最原始评论 |
|
* |
|
* @param id 评论id |
|
* @return 祖先评论的id |
|
*/ |
|
private Integer findCommentPid(Integer id) { |
|
Comment comment = detail(id); |
|
// 直到找到某个评论的pid为0,因为他就是最原始的评论 |
|
if (comment.getPid() == 0) { |
|
return comment.getId(); |
|
} |
|
return findCommentPid(comment.getPid()); |
|
} |
|
|
|
/** |
|
* 根据id删除评论对象 |
|
* |
|
* @param id 待删除评论的id |
|
*/ |
|
public void delete(Integer id) { |
|
commentMapper.deleteById(id); |
|
} |
|
|
|
/** |
|
* 返回文章的评论 |
|
* |
|
* @param id 文章id |
|
* @param pageNumber 初始页号 |
|
* @return 评论分页 |
|
*/ |
|
public PageHelper<Comment> show(Integer id, int pageNumber) { |
|
// 每页显示5个评论 |
|
Page<Comment> page = commentMapper.findAllByArticleAndPidOrderByCreatedDesc(Article.builder().id(id).build(), 0, PageRequest.of(pageNumber - 1, 5)); |
|
// 评论内容-当前页数-评论总数-总页数 |
|
return PageHelper.<Comment>builder().rows(page.getContent().stream().peek(x -> x.setChildren(commentMapper.findAllByPidOrderByCreatedDesc(x.getId()))).collect(Collectors.toList())).currentPage(pageNumber).total(page.getTotalElements()).totalPages(page.getTotalPages()).build(); |
|
} |
|
|
|
public void readAll() { |
|
commentMapper.readAll(); |
|
} |
|
|
|
public PageHelper<Comment> list(Boolean view, Integer pageNumber, Integer pageSize) { |
|
Pageable pageable = PageRequest.of(pageNumber-1,pageSize, Sort.by(Sort.Direction.DESC,"created")); |
|
Page<Comment> page = Objects.isNull(view) ? commentMapper.findAll(pageable) : commentMapper.findAllByView(view,pageable); |
|
return PageHelper.<Comment>builder() |
|
.rows(page.getContent()) |
|
.total(page.getTotalElements()) |
|
.build(); |
|
} |
|
}
|
|
|