👽 代码优化

pull/21/head
ruibaby 2018-07-16 20:29:12 +08:00
parent 67418fd0cf
commit 8e9c4b14c3
10 changed files with 70 additions and 16 deletions

View File

@ -72,4 +72,11 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
@Query(value = "SELECT * FROM halo_comment ORDER BY comment_date DESC LIMIT 5", nativeQuery = true)
List<Comment> findTopFive();
/**
*
*
* @param status
* @return
*/
Integer countAllByCommentStatus(Integer status);
}

View File

@ -204,4 +204,13 @@ public interface PostRepository extends JpaRepository<Post, Long> {
*/
@Query(value = "select sum(post_views) from halo_post", nativeQuery = true)
Long getPostViewsSum();
/**
*
*
* @param status
* @param postType
* @return
*/
Integer countAllByPostStatusAndPostType(Integer status, String postType);
}

View File

@ -104,4 +104,12 @@ public interface CommentService {
* @return List
*/
List<Comment> findCommentsLatest();
/**
*
*
* @param status
* @return
*/
Integer getCountByStatus(Integer status);
}

View File

@ -239,6 +239,14 @@ public interface PostService {
*/
Long getPostViews();
/**
*
*
* @param status
* @return
*/
Integer getCountByStatus(Integer status);
/**
* rss
*

View File

@ -159,4 +159,15 @@ public class CommentServiceImpl implements CommentService {
public List<Comment> findCommentsLatest() {
return commentRepository.findTopFive();
}
/**
*
*
* @param status
* @return
*/
@Override
public Integer getCountByStatus(Integer status) {
return commentRepository.countAllByCommentStatus(status);
}
}

View File

@ -408,6 +408,17 @@ public class PostServiceImpl implements PostService {
return postRepository.getPostViewsSum();
}
/**
*
*
* @param status
* @return
*/
@Override
public Integer getCountByStatus(Integer status) {
return postRepository.countAllByPostStatusAndPostType(status, PostType.POST_TYPE_POST.getDesc());
}
/**
* rss
*

View File

@ -20,9 +20,9 @@ public class CommentUtil {
public static List<Comment> getComments(List<Comment> commentsRoot) {
List<Comment> commentsResult = new ArrayList<>();
for (int i = 0; i < commentsRoot.size(); i++) {
if (commentsRoot.get(i).getCommentParent() == 0) {
commentsResult.add(commentsRoot.get(i));
for (Comment comment : commentsRoot) {
if (comment.getCommentParent() == 0) {
commentsResult.add(comment);
}
}

View File

@ -77,9 +77,9 @@ public class CommentController extends BaseController {
Pageable pageable = PageRequest.of(page, size, sort);
Page<Comment> comments = commentService.findAllComments(status, pageable);
model.addAttribute("comments", comments);
model.addAttribute("publicCount", commentService.findAllComments(CommentStatus.PUBLISHED.getCode(), pageable).getTotalElements());
model.addAttribute("checkCount", commentService.findAllComments(CommentStatus.CHECKING.getCode(), pageable).getTotalElements());
model.addAttribute("trashCount", commentService.findAllComments(CommentStatus.RECYCLE.getCode(), pageable).getTotalElements());
model.addAttribute("publicCount", commentService.getCountByStatus(CommentStatus.PUBLISHED.getCode()));
model.addAttribute("checkCount", commentService.getCountByStatus(CommentStatus.CHECKING.getCode()));
model.addAttribute("trashCount", commentService.getCountByStatus(CommentStatus.RECYCLE.getCode()));
model.addAttribute("status", status);
return "admin/admin_comment";
}

View File

@ -92,9 +92,9 @@ public class PostController extends BaseController {
Pageable pageable = PageRequest.of(page, size, sort);
Page<Post> posts = postService.findPostByStatus(status, PostType.POST_TYPE_POST.getDesc(), pageable);
model.addAttribute("posts", posts);
model.addAttribute("publishCount", postService.findPostByStatus(PostStatus.PUBLISHED.getCode(), PostType.POST_TYPE_POST.getDesc(), pageable).getTotalElements());
model.addAttribute("draftCount", postService.findPostByStatus(PostStatus.DRAFT.getCode(), PostType.POST_TYPE_POST.getDesc(), pageable).getTotalElements());
model.addAttribute("trashCount", postService.findPostByStatus(PostStatus.RECYCLE.getCode(), PostType.POST_TYPE_POST.getDesc(), pageable).getTotalElements());
model.addAttribute("publishCount", postService.getCountByStatus(PostStatus.PUBLISHED.getCode()));
model.addAttribute("draftCount", postService.getCountByStatus(PostStatus.DRAFT.getCode()));
model.addAttribute("trashCount", postService.getCountByStatus(PostStatus.RECYCLE.getCode()));
model.addAttribute("status", status);
return "admin/admin_post";
}

View File

@ -68,9 +68,6 @@ public class FrontCommentController {
Sort sort = new Sort(Sort.Direction.DESC, "commentDate");
Pageable pageable = PageRequest.of(0, 999, sort);
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post.get(), pageable, CommentStatus.PUBLISHED.getCode()).getContent();
if (null == comments) {
return null;
}
return CommentUtil.getComments(comments);
}
@ -133,6 +130,7 @@ public class FrontCommentController {
commentService.saveByComment(comment);
if(comment.getCommentParent()>0){
new EmailToParent(comment,lastComment,post).start();
new EmailToAdmin(comment, post).start();
}else{
new EmailToAdmin(comment,post).start();
}
@ -148,13 +146,14 @@ public class FrontCommentController {
class EmailToAdmin extends Thread{
private Comment comment;
private Post post;
public EmailToAdmin(Comment comment,Post post){
private EmailToAdmin(Comment comment, Post post) {
this.comment = comment;
this.post = post;
}
@Override
public void run(){
if (StringUtils.equals(HaloConst.OPTIONS.get("smtp_email_enable"), "true") && StringUtils.equals(HaloConst.OPTIONS.get("new_comment_notice"), "true")) {
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), "true") && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), "true")) {
try {
//发送邮件到博主
Map<String, Object> map = new HashMap<>();
@ -182,7 +181,8 @@ public class FrontCommentController {
private Comment comment;
private Comment lastComment;
private Post post;
public EmailToParent(Comment comment,Comment lastComment,Post post){
private EmailToParent(Comment comment, Comment lastComment, Post post) {
this.comment = comment;
this.lastComment = lastComment;
this.post = post;
@ -191,7 +191,7 @@ public class FrontCommentController {
@Override
public void run() {
//发送通知给对方
if(StringUtils.equals(HaloConst.OPTIONS.get("smtp_email_enable"),"true") && StringUtils.equals(HaloConst.OPTIONS.get("comment_reply_notice"),"true")) {
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), "true") && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), "true")) {
if(Validator.isEmail(lastComment.getCommentAuthorEmail())){
Map<String, Object> map = new HashMap<>();
map.put("blogTitle",HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp()));