package com.slodon.b2b2c.model.video;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.dao.read.video.SvideoCommentReadMapper;
import com.slodon.b2b2c.dao.read.video.SvideoReplyLikeReadMapper;
import com.slodon.b2b2c.dao.read.video.SvideoReplyReadMapper;
import com.slodon.b2b2c.dao.write.video.SvideoReplyLikeWriteMapper;
import com.slodon.b2b2c.dao.write.video.SvideoReplyWriteMapper;
import com.slodon.b2b2c.video.example.SvideoReplyExample;
import com.slodon.b2b2c.video.example.SvideoReplyLikeExample;
import com.slodon.b2b2c.video.pojo.Author;
import com.slodon.b2b2c.video.pojo.SvideoComment;
import com.slodon.b2b2c.video.pojo.SvideoReply;
import com.slodon.b2b2c.video.pojo.SvideoReplyLike;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 短视频回复表model
 */
@Component
@Slf4j
public class SvideoReplyModel {

    @Resource
    private SvideoReplyReadMapper svideoReplyReadMapper;
    @Resource
    private SvideoReplyWriteMapper svideoReplyWriteMapper;
    @Resource
    private SvideoReplyLikeReadMapper svideoReplyLikeReadMapper;
    @Resource
    private SvideoReplyLikeWriteMapper svideoReplyLikeWriteMapper;
    @Resource
    private SvideoCommentReadMapper svideoCommentReadMapper;

    /**
     * 新增短视频回复表
     *
     * @param svideoReply
     * @return
     */
    public SvideoReply saveSvideoReply(SvideoReply svideoReply) {
        if (StringUtil.isNullOrZero(svideoReply.getParentReplyId())) {
            //如果父级回复id为0，说明是针对评论的回复，查评论表
            SvideoComment svideoComment = svideoCommentReadMapper.getByPrimaryKey(svideoReply.getCommentId());
            AssertUtil.notNull(svideoComment, "获取评论信息为空");
            svideoReply.setToAuthorId(svideoComment.getFromAuthorId());
            svideoReply.setToAuthorName(svideoComment.getFromAuthorName());
            svideoReply.setVideoId(svideoComment.getVideoId());
            svideoReply.setVideoImage(svideoComment.getVideoImage());
        } else {
            SvideoReply parentReply = svideoReplyReadMapper.getByPrimaryKey(svideoReply.getParentReplyId());
            AssertUtil.notNull(parentReply, "获取评论信息为空");
            svideoReply.setToAuthorId(parentReply.getFromAuthorId());
            svideoReply.setToAuthorName(parentReply.getFromAuthorName());
            svideoReply.setVideoId(parentReply.getVideoId());
            svideoReply.setVideoImage(parentReply.getVideoImage());
        }
        svideoReply.setLikeNum(0);
        svideoReply.setIsRead(0);
        int count = svideoReplyWriteMapper.insert(svideoReply);
        AssertUtil.isTrue(count == 0, "回复失败，请重试");
        return svideoReply;
    }

    /**
     * 根据replyId删除短视频回复表
     *
     * @param replyId replyId
     * @return
     */
    public Integer deleteSvideoReply(Integer replyId) {
        if (StringUtils.isEmpty(replyId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = svideoReplyWriteMapper.deleteByPrimaryKey(replyId);
        if (count == 0) {
            log.error("根据replyId：" + replyId + "删除短视频回复表失败");
            throw new MallException("删除短视频回复表失败,请重试");
        }
        return count;
    }

    /**
     * 根据replyId更新短视频回复表
     *
     * @param svideoReply
     * @return
     */
    public Integer updateSvideoReply(SvideoReply svideoReply) {
        if (StringUtils.isEmpty(svideoReply.getReplyId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = svideoReplyWriteMapper.updateByPrimaryKeySelective(svideoReply);
        if (count == 0) {
            log.error("根据replyId：" + svideoReply.getReplyId() + "更新短视频回复表失败");
            throw new MallException("更新短视频回复表失败,请重试");
        }
        return count;
    }

    /**
     * 根据条件更新短视频评论表
     *
     * @param svideoReply
     * @param example
     * @return
     */
    public Integer updateReply(SvideoReply svideoReply, SvideoReplyExample example) {
        return svideoReplyWriteMapper.updateByExampleSelective(svideoReply, example);
    }

    /**
     * 根据replyId获取短视频回复表详情
     *
     * @param replyId replyId
     * @return
     */
    public SvideoReply getSvideoReplyByReplyId(Integer replyId) {
        return svideoReplyReadMapper.getByPrimaryKey(replyId);
    }

    /**
     * 根据条件获取短视频回复表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<SvideoReply> getSvideoReplyList(SvideoReplyExample example, PagerInfo pager) {
        List<SvideoReply> svideoReplyList;
        if (pager != null) {
            pager.setRowsCount(svideoReplyReadMapper.countByExample(example));
            svideoReplyList = svideoReplyReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            svideoReplyList = svideoReplyReadMapper.listByExample(example);
        }
        return svideoReplyList;
    }

    /**
     * 获取符合条件的数量
     *
     * @param example 查询条件
     * @return
     */
    public Integer getSvideoReplyCount(SvideoReplyExample example) {
        return svideoReplyReadMapper.countByExample(example);
    }

    /**
     * 回复点赞
     *
     * @param replyId
     * @param author
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer clickPraise(Integer replyId, Author author) {
        //查询是否有历史点赞记录
        SvideoReplyLikeExample example = new SvideoReplyLikeExample();
        example.setAuthorId(author.getAuthorId());
        example.setReplyId(replyId);
        List<SvideoReplyLike> replyLikeList = svideoReplyLikeReadMapper.listByExample(example);
        //根据回复id查询回复信息
        SvideoReply svideoReply = this.getSvideoReplyByReplyId(replyId);
        AssertUtil.notNull(svideoReply, "获取回复信息为空，请重试！");
        Integer count = 0;
        if (CollectionUtils.isEmpty(replyLikeList)) {
            //没有点赞历史 直接插入
            SvideoReplyLike record = new SvideoReplyLike();
            record.setAuthorId(author.getAuthorId());
            record.setReplyId(replyId);
            count = svideoReplyLikeWriteMapper.insert(record);
            AssertUtil.isTrue(count == 0, "点赞失败，请重试！");

            //更新回复点赞数量
            SvideoReply reply = new SvideoReply();
            reply.setReplyId(replyId);
            reply.setLikeNum(svideoReply.getLikeNum() + 1);
            count = svideoReplyWriteMapper.updateByPrimaryKeySelective(reply);
            AssertUtil.isTrue(count == 0, "更新回复点赞数量失败，请重试！");
        } else {
            SvideoReplyLike like = replyLikeList.get(0);
            //点赞两次相当于直接取消
            count = svideoReplyLikeWriteMapper.deleteByPrimaryKey(like.getReplyLikeId());
            AssertUtil.isTrue(count == 0, "取消点赞失败，请重试！");

            //更新回复点赞数量
            SvideoReply reply = new SvideoReply();
            reply.setReplyId(replyId);
            reply.setLikeNum(svideoReply.getLikeNum() - 1);
            count = svideoReplyWriteMapper.updateByPrimaryKeySelective(reply);
            AssertUtil.isTrue(count == 0, "更新回复点赞数量失败，请重试！");
        }
        example.setAuthorId(null);
        return svideoReplyLikeReadMapper.countByExample(example);
    }
}