191 lines
6.1 KiB
Java
191 lines
6.1 KiB
Java
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.checkerframework.checker.units.qual.C;
|
||
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<String, Object> 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<Comment,String> hashMap = new HashMap<>();
|
||
hashMap.put(comment, name);
|
||
return JSON.toJSONString(hashMap);
|
||
}
|
||
// 删除
|
||
@Override
|
||
public String Delete(int id){
|
||
int result = commentDao.deleteByPrimaryKey(id);
|
||
HashMap<String, Object> 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<String, Long> hashMap = new HashMap<>();
|
||
hashMap.put("点赞成功",comment.getLikeCount());
|
||
return JSON.toJSONString(hashMap);
|
||
}
|
||
|
||
// 显示评论和用户信息
|
||
public String View_comment(){
|
||
List<Comment> list_comment = commentDao.selectAllComment();
|
||
return JSON.toJSONString(list_comment);
|
||
}
|
||
|
||
// 显示回复
|
||
public String View_Reply(int id){
|
||
List<Comment> list = commentDao.selectByAllReply(id);
|
||
return JSON.toJSONString(list);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 功能描述:根据博客id,查询此博客的所有评论信息
|
||
*
|
||
* @param UrlId 博客id
|
||
* @return 博客的评论信息
|
||
*/
|
||
@Override
|
||
public List<CommentNode> queryCommentByUrlId ( Integer UrlId ) {
|
||
//所有未处理的一级评论集合
|
||
List<CommentNode> firstCommentList = commentDao.queryFirstCommentList(UrlId);
|
||
//所有未处理的二级评论集合
|
||
List<CommentNode> secondCommentList = commentDao.querySecondCommentList(UrlId);
|
||
//将二级评论用链表的方式添加到一级评论
|
||
List<CommentNode> 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<CommentNode> 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<CommentNode> addAllNode ( List<CommentNode> firstList, List<CommentNode> 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;
|
||
}
|
||
public String SelectChildAndDelete(String commentId)
|
||
{
|
||
|
||
String child = commentDao.queryChildId(commentId);
|
||
String [] childrens = child.split(",");
|
||
int flag = 0;
|
||
for(String id : childrens)
|
||
{
|
||
flag = commentDao.deleteByPrimaryKey(Integer.valueOf(id));
|
||
if (String.valueOf(flag).equals("0")){
|
||
return "出现错误";
|
||
}
|
||
}
|
||
return "处理完成";
|
||
}
|
||
|
||
|
||
}
|