package com.ruoyi.mall.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.StaticLog;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.Bo.CommentArticleRequest;
import com.ruoyi.mall.domain.BusinessArticle;
import com.ruoyi.mall.domain.BusinessArticleComment;
import com.ruoyi.mall.domain.MemberMessage;
import com.ruoyi.mall.domain.Vo.BusinessArticleVo;
import com.ruoyi.mall.mapper.BusinessArticleMapper;
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.BusinessArticleLikeMapper;
import com.ruoyi.mall.domain.BusinessArticleLike;
import com.ruoyi.mall.service.IBusinessArticleLikeService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 帖子点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-18
 */
@Service
public class BusinessArticleLikeServiceImpl implements IBusinessArticleLikeService 
{
    @Resource
    private BusinessArticleLikeMapper businessArticleLikeMapper;
    @Resource
    private BusinessArticleMapper businessArticleMapper;
    @Resource
    private MemberMessageMapper memberMessageMapper;

    /**点赞 和 取消点赞 */


    @Override
    public Boolean likeAndCancelLikes(Long articleId,Long userId) {
        //获取用户对帖子 点赞的操作 动作
        BusinessArticleLike businessArticleLike = businessArticleLikeMapper.selectOneByArticleIdUserId(userId,articleId);
        //取消点赞
        if(BeanUtil.isNotEmpty(businessArticleLike)){
            //真删除点赞
            int i = businessArticleLikeMapper.deleteBusinessArticleLikeByLikeId(businessArticleLike.getLikeId());
            //新增取消点赞消息 3
            BusinessArticleVo articleVo = businessArticleMapper.selectBusinessArticleByArticleId(articleId);
            addArticleMessage(articleId,userId,articleVo,"3");
            //更新帖子点赞数
            //type="2" //取消点赞 数-1
            updateArticleLikeNum(articleVo,"2");
            return i > 0;
        }else{//没有动作---点赞
            // 新增点赞
                BusinessArticleLike like = new BusinessArticleLike();
                like.setArticleId(articleId);
                like.setUserId(userId);
                like.setCreateTime(DateUtils.getNowDate());
                int i = businessArticleLikeMapper.insertBusinessArticleLike(like);

            BusinessArticleVo articleVo = businessArticleMapper.selectBusinessArticleByArticleId(articleId);

            //新增消息 点赞1
            addArticleMessage(articleId,userId,articleVo,"1");
            //更新帖子点赞数 type ="1" //新增点赞 数+1
            updateArticleLikeNum(articleVo,"1");
            return i>0;
        }
    }

    @Async
    public void addArticleMessage(Long articleId,Long userId,BusinessArticleVo articleVo,String messageType) {
        if(messageType.equals("1")) {//点赞
            StaticLog.info("异步插入消息表数据,用户" + userId + "点赞" + articleId + "帖子");
        }else if(messageType.equals("3")) {//取消点赞
            StaticLog.info("异步插入消息表数据,用户" + userId + "取消点赞" + articleId + "帖子");
        }
        MemberMessage message = new MemberMessage();
        message.setArticleId(articleVo.getArticleId());
        message.setUserId(articleVo.getUserId());//消息属于 帖子创作者
        if(StringUtils.isNotBlank(articleVo.getArticleImgs())) {
            message.setArticleImg(articleVo.getArticleImgs());
        }
        message.setReadStatus("0");
        message.setMessageType(messageType);//点赞类型的消息
        //BusinessArticleLike like = businessArticleLikeMapper.selectOneByArticleIdUId(articleId,userId);
        message.setCreateTime(DateUtils.getNowDate());
        memberMessageMapper.insertMemberMessage(message);
    }


    @Async
    public void updateArticleLikeNum(BusinessArticleVo articleVo,String type) {
        StaticLog.info("异步修改信息表数据"+articleVo);
        //修改帖子点赞数
        BusinessArticle article = BeanUtil.toBean(articleVo,BusinessArticle.class);
        if(type.equals("1")){
            article.setArticleLikes(article.getArticleLikes()+1);
        }else if(type.equals("2")){
            article.setArticleLikes(article.getArticleLikes()-1);
        }
        article.setUpdateTime(DateUtils.getNowDate());
        businessArticleMapper.updateBusinessArticle(article);
    }




    /**
     * 查询帖子点赞
     * 
     * @param likeId 帖子点赞主键
     * @return 帖子点赞
     */
    @Override
    public BusinessArticleLike selectBusinessArticleLikeByLikeId(Long likeId)
    {
        return businessArticleLikeMapper.selectBusinessArticleLikeByLikeId(likeId);
    }

    /**
     * 查询帖子点赞列表
     * 
     * @param businessArticleLike 帖子点赞
     * @return 帖子点赞
     */
    @Override
    public List<BusinessArticleLike> selectBusinessArticleLikeList(BusinessArticleLike businessArticleLike)
    {
        return businessArticleLikeMapper.selectBusinessArticleLikeList(businessArticleLike);
    }

    /**
     * 新增帖子点赞
     * 
     * @param businessArticleLike 帖子点赞
     * @return 结果
     */
    @Override
    public int insertBusinessArticleLike(BusinessArticleLike businessArticleLike)
    {
        businessArticleLike.setCreateTime(DateUtils.getNowDate());
        return businessArticleLikeMapper.insertBusinessArticleLike(businessArticleLike);
    }

    /**
     * 修改帖子点赞
     * 
     * @param businessArticleLike 帖子点赞
     * @return 结果
     */
    @Override
    public int updateBusinessArticleLike(BusinessArticleLike businessArticleLike)
    {
        businessArticleLike.setUpdateTime(DateUtils.getNowDate());
        return businessArticleLikeMapper.updateBusinessArticleLike(businessArticleLike);
    }

    /**
     * 批量删除帖子点赞
     * 
     * @param likeIds 需要删除的帖子点赞主键
     * @return 结果
     */
    @Override
    public int deleteBusinessArticleLikeByLikeIds(Long[] likeIds)
    {
        return businessArticleLikeMapper.deleteBusinessArticleLikeByLikeIds(likeIds);
    }

    /**
     * 删除帖子点赞信息
     * 
     * @param likeId 帖子点赞主键
     * @return 结果
     */
    @Override
    public int deleteBusinessArticleLikeByLikeId(Long likeId)
    {
        return businessArticleLikeMapper.deleteBusinessArticleLikeByLikeId(likeId);
    }
}
