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.service.CommentService;
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 = commentDao.selectAllComment();
        return JSON.toJSONString(list);
    }

//    显示回复
    public String View_Reply(int id){
        List<Comment> list = commentDao.selectByAllReply(id);
        return JSON.toJSONString(list);
    }
}
