package com.ruoyi.mall.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.StaticLog;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.Bo.CommentArticleRequest;
import com.ruoyi.mall.domain.Bo.CommentDelRequest;
import com.ruoyi.mall.domain.Bo.CommentReplyRequest;
import com.ruoyi.mall.domain.Vo.BusinessArticleVo;
import com.ruoyi.mall.domain.Vo.QueryArtilceCommentListResponse;
import com.ruoyi.mall.domain.Vo.QueryArtilceReplyListResponse;
import com.ruoyi.mall.mapper.BusinessArticleMapper;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.mapper.MemberMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.BusinessArticleCommentMapper;
import com.ruoyi.mall.service.IBusinessArticleCommentService;

import javax.annotation.Resource;

/**
 * 帖子评论Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-18
 */
@Service
public class BusinessArticleCommentServiceImpl implements IBusinessArticleCommentService 
{
    @Resource
    private BusinessArticleCommentMapper businessArticleCommentMapper;
    @Resource
    private BusinessArticleMapper businessArticleMapper;
    @Resource
    private MallMemberUserMapper memberUserMapper;
    @Resource
    private MemberMessageMapper memberMessageMapper;

    /**
     * 查询帖子评论列表 API
     * @param artilceId
     * @return
     */
    @Override
    public List<QueryArtilceCommentListResponse> queryArtilceCommentList(Long artilceId) {
        List<QueryArtilceCommentListResponse> list = businessArticleCommentMapper.queryArtilceCommentOrReplyList(artilceId);
        return list;

    }

    /**
     * 查询帖子 子级评论列表 API
     * @param commentId
     * @return
     */
    @Override
    public List<QueryArtilceReplyListResponse>  queryArtilceReplyList(Long commentId) {
        List<QueryArtilceReplyListResponse> list = businessArticleCommentMapper.queryArtilceReplyList(commentId);
        return list;

    }




    /**删除帖子评论API*/
    @Override
    public String delCommentArticle(CommentDelRequest commentDelRequest) {
        //是不是当前用户的 是才能删
        BusinessArticleComment businessArticleComment = businessArticleCommentMapper.selectBusinessArticleCommentByCommentId(commentDelRequest.getCommentId());
        if(null != businessArticleComment && businessArticleComment.getUserId() != commentDelRequest.getUserId()){
            return "非当前评论的创建者，没有删除权限";
        }else{
            // 真删评论
            int i = businessArticleCommentMapper.deleteBusinessArticleCommentByCommentId(commentDelRequest.getCommentId());
            // 更新 评论目标帖子的表评论数量 -1
            updateArticleCommentNum(commentDelRequest.getArticleId(),"2");
            // 新增删除评论消息 删除评论4
            if(businessArticleComment.getCommentType().equals("1")){//删除回复评论
                BusinessArticleComment to_businessArticleCommentVo = businessArticleCommentMapper.selectBusinessArticleCommentByCommentId(businessArticleComment.getReplyId());
                addReplyArticleMessage(businessArticleComment.getReplyId(),commentDelRequest.getUserId(), to_businessArticleCommentVo,"4");
            }else if(businessArticleComment.getCommentType().equals("0")){//删除评论
                addArticleMessage(commentDelRequest.getArticleId(),commentDelRequest.getUserId(),"4");
            }
            if(i>0){
                return "删除成功";
            }else{
                return "删除失败";
            }
        }
    }
    /**
     * 评论帖子 API 一级评论
     * @param commentArticleRequest
     * @return
     */
    @Override
    public Boolean commentArticle(CommentArticleRequest commentArticleRequest) {//uid 帖子id 评论内容
        //
        BusinessArticleComment businessArticleComment = BeanUtil.toBean(commentArticleRequest, BusinessArticleComment.class);

        //评论的目标帖子
        BusinessArticleVo businessArticleVo = businessArticleMapper.selectBusinessArticleByArticleId(businessArticleComment.getArticleId());
        //设置 评论目标帖子的发布者名字 to_uid
        businessArticleComment.setToUserId(businessArticleVo.getUserId());
        //设置 评论类型为评论帖子（父级评论，一级评论 '0'）
        businessArticleComment.setCommentType("0");

        // 更新 目标帖子的表评论数量
        updateArticleCommentNum(commentArticleRequest.getArticleId(),"1");

        // 添加评论消息 ‘2‘新增评论
        addArticleMessage(commentArticleRequest.getArticleId(),commentArticleRequest.getUserId(),"2");
        // 新建评论
        businessArticleComment.setCreateTime(DateUtils.getNowDate());
        return businessArticleCommentMapper.insertBusinessArticleComment(businessArticleComment) >0;

    }

    /**
     * 回复评论 APi
     * @param commentReplyRequest
     * @return
     */
    @Override
    public Boolean replyComment(CommentReplyRequest commentReplyRequest) {
        //用户提交的回复评论
        BusinessArticleComment businessArticleComment = BeanUtil.toBean(commentReplyRequest, BusinessArticleComment.class);
        //被回复的评论
        BusinessArticleComment to_businessArticleCommentVo = businessArticleCommentMapper.selectBusinessArticleCommentByCommentId(businessArticleComment.getReplyId());
        // articleid tocommentid touserid commenttype"1"回复
        //目标评论所属用户
        MallMemberUser toUser = memberUserMapper.selectMallMemberUserById(to_businessArticleCommentVo.getUserId());
        //设置 提交的回复评论 所属的帖子id
        businessArticleComment.setArticleId(to_businessArticleCommentVo.getArticleId());
        //CommentType是评论,就是businessArticleCommentVo的commentId 是回复-就是就是businessArticleCommentVo的toCommentId
        //设置 提交的回复评论 目标评论id
        businessArticleComment.setToCommentId(Integer.parseInt(to_businessArticleCommentVo.getCommentType())<=0?to_businessArticleCommentVo.getCommentId():to_businessArticleCommentVo.getToCommentId());
        //设置 提交的回复评论 评论类型为子级评论(回复评论)  1回复
        businessArticleComment.setCommentType("1");
        //设置 提交的回复评论 目标评论用户id
        businessArticleComment.setToUserId(toUser.getId());


        // 更新 帖子评论数 "1"//+1
        updateArticleCommentNum(to_businessArticleCommentVo.getArticleId(),"1");
        // 新增 回复评论 消息表
        addReplyArticleMessage(commentReplyRequest.getReplyId(),commentReplyRequest.getUserId(),to_businessArticleCommentVo,"5");
        // 新增 评论
        businessArticleComment.setCreateTime(DateUtils.getNowDate());
        return businessArticleCommentMapper.insertBusinessArticleComment(businessArticleComment)>0;
    }
    @Async
    public void addReplyArticleMessage(Long commentId,Long userId, BusinessArticleComment to_businessArticleCommentVo,String messageType ) {//2评论 4删除评论
        if(messageType.equals("5")){//回复评论
            StaticLog.info("异步插入消息表数据,用户"+userId+"回复"+commentId+"评论");
        }else if(messageType.equals("4")){//删除评论
            StaticLog.info("异步插入消息表数据,用户"+userId+"删除自己对"+commentId+"评论"+"的回复");
        }
        MemberMessage message = new MemberMessage();
        message.setArticleId(to_businessArticleCommentVo.getArticleId());
        message.setUserId(to_businessArticleCommentVo.getUserId());//消息属于 被回复评论的创作者
        //评论的目标帖子
        BusinessArticleVo articleVo = businessArticleMapper.selectBusinessArticleByArticleId(to_businessArticleCommentVo.getArticleId());
        if(StringUtils.isNotBlank(articleVo.getArticleImgs())) {
            message.setArticleImg(articleVo.getArticleImgs());
        }
        message.setReadStatus("0");
        message.setMessageType(messageType);//回复评论类型的消息
        message.setCreateTime(DateUtils.getNowDate());
        memberMessageMapper.insertMemberMessage(message);
    }

    @Async
    public void addArticleMessage(Long articleId,Long userId,String messageType ) {//2评论 4删除评论
        if(messageType.equals("2")){
            StaticLog.info("异步插入消息表数据,用户"+userId+"评论"+articleId+"帖子");
        }else if(messageType.equals("4")){
            StaticLog.info("异步插入消息表数据,用户"+userId+"删除自己对"+articleId+"帖子"+"的评论");
        }
        BusinessArticleVo articleVo = businessArticleMapper.selectBusinessArticleByArticleId(articleId);
        MemberMessage message = new MemberMessage();
        message.setArticleId(articleVo.getArticleId());
        if(StringUtils.isNotBlank(articleVo.getArticleImgs())) {
            message.setArticleImg(articleVo.getArticleImgs());
        }
        message.setUserId(articleVo.getUserId());//消息属于 帖子创作者
        message.setReadStatus("0");
        message.setMessageType(messageType);//点赞类型的消息
        message.setCreateTime(DateUtils.getNowDate());
        memberMessageMapper.insertMemberMessage(message);
    }
    @Async
    public void updateArticleCommentNum(Long articleId,String flag) {
        if(flag.equals("1")){
            StaticLog.info("异步更新评论表数据"+articleId+"帖子,评论数+1");
        }else if(flag.equals("2")){
            StaticLog.info("异步更新评论表数据"+articleId+"帖子,评论数-1");
        }
        BusinessArticleVo businessArticleVo = businessArticleMapper.selectBusinessArticleByArticleId(articleId);
        if(StringUtils.isNotBlank(flag) && flag.equals("1")){//+1
            BusinessArticle ba = BeanUtil.toBean(businessArticleVo,BusinessArticle.class);
            ba.setArticleComments(ba.getArticleComments()+1);
            ba.setUpdateTime(DateUtils.getNowDate());
            businessArticleMapper.updateBusinessArticle(ba);
        }else if(StringUtils.isNotBlank(flag) && flag.equals("2")){//-1
            BusinessArticle ba = BeanUtil.toBean(businessArticleVo,BusinessArticle.class);
            ba.setArticleComments(ba.getArticleComments()-1);
            ba.setUpdateTime(DateUtils.getNowDate());
            businessArticleMapper.updateBusinessArticle(ba);
        }
    }


    /**
     * 查询帖子评论
     * 
     * @param commentId 帖子评论主键
     * @return 帖子评论
     */
    @Override
    public BusinessArticleComment selectBusinessArticleCommentByCommentId(Long commentId)
    {
        return businessArticleCommentMapper.selectBusinessArticleCommentByCommentId(commentId);
    }

    /**
     * 查询帖子评论列表
     * 
     * @param businessArticleComment 帖子评论
     * @return 帖子评论
     */
    @Override
    public List<BusinessArticleComment> selectBusinessArticleCommentList(BusinessArticleComment businessArticleComment)
    {
        return businessArticleCommentMapper.selectBusinessArticleCommentList(businessArticleComment);
    }

    /**
     * 新增帖子评论
     * 
     * @param businessArticleComment 帖子评论
     * @return 结果
     */
    @Override
    public int insertBusinessArticleComment(BusinessArticleComment businessArticleComment)
    {
        businessArticleComment.setCreateTime(DateUtils.getNowDate());
        return businessArticleCommentMapper.insertBusinessArticleComment(businessArticleComment);
    }

    /**
     * 修改帖子评论
     * 
     * @param businessArticleComment 帖子评论
     * @return 结果
     */
    @Override
    public int updateBusinessArticleComment(BusinessArticleComment businessArticleComment)
    {
        businessArticleComment.setUpdateTime(DateUtils.getNowDate());
        return businessArticleCommentMapper.updateBusinessArticleComment(businessArticleComment);
    }

    /**
     * 批量删除帖子评论
     * 
     * @param commentIds 需要删除的帖子评论主键
     * @return 结果
     */
    @Override
    public int deleteBusinessArticleCommentByCommentIds(Long[] commentIds)
    {
        return businessArticleCommentMapper.deleteBusinessArticleCommentByCommentIds(commentIds);
    }

    /**
     * 删除帖子评论信息
     * 
     * @param commentId 帖子评论主键
     * @return 结果
     */
    @Override
    public int deleteBusinessArticleCommentByCommentId(Long commentId)
    {
        return businessArticleCommentMapper.deleteBusinessArticleCommentByCommentId(commentId);
    }
}
