package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.communityNews.CommentContent;
import com.ruoyi.system.domain.communityNews.CommentIndex;
import com.ruoyi.system.domain.communityNews.CommunityNews;
import com.ruoyi.system.domain.communityNews.vo.CommentInteractionVo;
import com.ruoyi.system.mapper.CommentContentMapper;
import com.ruoyi.system.mapper.CommentIndexMapper;
import com.ruoyi.system.mapper.CommunityNewsMapper;
import com.ruoyi.system.service.ICommentContentService;
import com.ruoyi.system.service.ICommunityNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ruoyi.common.constant.Constants.*;

/**
 * 社区资讯评论内容Service业务层处理
 *
 * @author boman
 * @date 2025-02-25
 */
@Service
public class CommentContentServiceImpl implements ICommentContentService {
    @Autowired
    private CommentContentMapper commentContentMapper;
    @Autowired
    private CommentIndexMapper commentIndexMapper;
    @Autowired
    private CommunityNewsMapper communityNewsMapper;
    @Autowired
    private ICommunityNewsService communityNewsService;

    /**
     * 查询社区资讯评论内容
     *
     * @param contentId 社区资讯评论内容主键
     * @return 社区资讯评论内容
     */
    @Override
    public CommentContent selectCommentContentByContentId(Long contentId) {
        return commentContentMapper.selectCommentContentByContentId(contentId);
    }

    /**
     * 查询社区资讯评论内容列表
     *
     * @param commentContent 社区资讯评论内容
     * @return 社区资讯评论内容
     */
    @Override
    public List<CommentContent> selectCommentContentList(CommentContent commentContent) {
        return commentContentMapper.selectCommentContentList(commentContent);
    }

    /**
     * 新增社区资讯评论内容
     *
     * @param commentContent 社区资讯评论内容
     * @return 结果
     */
    @Override
    public int insertCommentContent(CommentContent commentContent) {
        commentContent.setCreateTime(DateUtils.getNowDate());
        return commentContentMapper.insertCommentContent(commentContent);
    }

    /**
     * 修改社区资讯评论内容
     *
     * @param commentContent 社区资讯评论内容
     * @return 结果
     */
    @Override
    public int updateCommentContent(CommentContent commentContent) {
        commentContent.setUpdateTime(DateUtils.getNowDate());
        int i = commentContentMapper.updateCommentContent(commentContent);
        if (TWO.equals(commentContent.getStatus())) {
            CommentIndex commentIndex = commentIndexMapper.selectCommentIndexByCommentId(commentContent.getCommentId());
            Long communityId = commentIndex.getCommunityId();
            Long userId = commentIndex.getUserId();
            //评论审核通过
            //------设置未读互动，评论的谁就给谁在redis中新增一条未读的互动--------
            CommentInteractionVo commentInteractionVo = new CommentInteractionVo();
            Integer isRoot = commentIndex.getIsRoot();
            //默认是子评论
            commentInteractionVo.setTargetType(FOR);
            commentInteractionVo.setTargetUserId(commentIndex.getToUserId());
            //默认标题是子评论的评论
            commentInteractionVo.setTargetTitle(commentIndex.getCommentContent());
            if (1 == isRoot) {
                //说明是根评论
                //查询该资讯是谁发的
                CommunityNews communityNews = communityNewsMapper.selectCommunityNewsByCommunityId(communityId);
                commentInteractionVo.setTargetUserId(communityNews.getUserId());
                commentInteractionVo.setTargetType(ONE);
                //社区资讯标题
                commentInteractionVo.setTargetTitle(commentIndex.getCommunityTitle());
            }
            commentInteractionVo.setUserId(userId);
            commentInteractionVo.setAvatar(commentIndex.getAvatar());
            commentInteractionVo.setNickName(commentIndex.getNickName());
            commentInteractionVo.setCreateTime(DateUtils.getNowDate());
            commentInteractionVo.setTargetId(commentIndex.getCommentId());
            //新增
            commentInteractionVo.setType(ONE);
            communityNewsService.setCommentInteraction(commentInteractionVo);
            //----------------------设置未读互动完成--------
        }
        return i;
    }

    /**
     * 批量删除社区资讯评论内容
     *
     * @param contentIds 需要删除的社区资讯评论内容主键
     * @return 结果
     */
    @Override
    public int deleteCommentContentByContentIds(Long[] contentIds) {
        return commentContentMapper.deleteCommentContentByContentIds(contentIds);
    }

    /**
     * 删除社区资讯评论内容信息
     *
     * @param contentId 社区资讯评论内容主键
     * @return 结果
     */
    @Override
    public int deleteCommentContentByContentId(Long contentId) {
        return commentContentMapper.deleteCommentContentByContentId(contentId);
    }
}
