package com.lovenav.service.serviceImpl; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.lovenav.dao.CommentDao; import com.lovenav.dao.UserDao; import com.lovenav.entity.Comment; import com.lovenav.entity.CommentNode; import com.lovenav.entity.CommentUser; import com.lovenav.entity.User; import com.lovenav.service.CommentService; import io.swagger.models.auth.In; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; @Service public class CommentServiceImpl implements CommentService { @Autowired private CommentDao commentDao; @Autowired private UserDao userDao; // 评论功能 @Override public String Reply1(Comment comment){ int result = commentDao.insert(comment); HashMap hashMap = new HashMap<>(); if(result > 0){ return JSON.toJSONString(comment); } else{ hashMap.put("msg", "评论失败"); return JSON.toJSONString(hashMap); } } // 回复功能 @Override public String Reply2(Comment comment,int id){ String name = (userDao.selectByPrimaryKey(id)).getNickname(); HashMap hashMap = new HashMap<>(); hashMap.put(comment, name); return JSON.toJSONString(hashMap); } // 删除 @Override public String Delete(int id){ int result = commentDao.deleteByPrimaryKey(id); HashMap hashMap = new HashMap<>(); if (result > 0){ hashMap.put("code", 200); hashMap.put("msg", "删除成功"); String jsonString = JSONObject.toJSONString(hashMap); return JSON.toJSONString(hashMap); }else{ hashMap.put("msg","删除失败"); return JSON.toJSONString(hashMap); } } // 点赞++ @Override public String AddLikeCount(int id){ Comment comment = commentDao.selectByPrimaryKey(id); comment.setLikeCount(comment.getLikeCount()+1); commentDao.updateByPrimaryKeySelective(comment); HashMap hashMap = new HashMap<>(); hashMap.put("点赞成功",comment.getLikeCount()); return JSON.toJSONString(hashMap); } // 显示评论和用户信息 public String View_comment(){ List list_comment = commentDao.selectAllComment(); return JSON.toJSONString(list_comment); } // 显示回复 public String View_Reply(int id){ List list = commentDao.selectByAllReply(id); return JSON.toJSONString(list); } /** * 功能描述:根据博客id,查询此博客的所有评论信息 * * @param UrlId 博客id * @return 博客的评论信息 */ @Override public List queryCommentByUrlId ( Integer UrlId ) { //所有未处理的一级评论集合 List firstCommentList = commentDao.queryFirstCommentList(UrlId); //所有未处理的二级评论集合 List secondCommentList = commentDao.querySecondCommentList(UrlId); //将二级评论用链表的方式添加到一级评论 List list = addAllNode(firstCommentList, secondCommentList); return list; } /** * 功能描述:根据评论id查询用户信息 * * @param id 评论id * @return 评论信息,携带用户信息 */ @Override public CommentUser queryCommentUserById ( Integer id ) { Comment comment = commentDao.selectByPrimaryKey(id); User user = userDao.selectByPrimaryKey(comment.getUserId()); CommentUser commentUser = new CommentUser(); commentUser.setComment(comment); commentUser.setUser(user); return commentUser; } /** * 功能描述:将单个node添加到链表中 * * @param firstList 第一层评论集合(链表) * @param commentNode 非第一层评论的回复信息 * @return 是否添加 */ private boolean addNode (List firstList, CommentNode commentNode ) { //循环添加 for (CommentNode node : firstList) { //判断留言的上一段是否是这条留言(判断这条回复,是否是当前评论的回复) if (node.getId().equals(commentNode.getRootCommentId())) { //是,添加,返回true node.getNextNodes().add(commentNode); return true; } else { //否则递归继续判断 if (node.getNextNodes().size() != 0) { if (addNode(node.getNextNodes(), commentNode)) { return true; } } } } return false; } /** * 功能描述:将查出来的rootId不为null的回复都添加到第一层Node集合中 * * @param firstList 第一层评论集合(链表) * @param thenList 非第一层评论集合(链表) * @return 所有评论集合(非第一层评论集合对应添加到第一层评论集合,返回) */ private List addAllNode ( List firstList, List thenList ) { while (thenList.size() != 0) { int size = thenList.size(); for (int i = 0; i < size; i++) { if (addNode(firstList, new CommentNode(thenList.get(i)))) { thenList.remove(i); i--; size--; } } } return firstList; } }