package com.slodon.b2b2c.model.video;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.video.SvideoCommentLikeReadMapper;
import com.slodon.b2b2c.dao.write.video.SvideoCommentLikeWriteMapper;
import com.slodon.b2b2c.video.example.SvideoCommentLikeExample;
import com.slodon.b2b2c.video.pojo.SvideoCommentLike;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

/**
 * 短视频评论点赞表model
 */
@Component
@Slf4j
public class SvideoCommentLikeModel {
    @Resource
    private SvideoCommentLikeReadMapper svideoCommentLikeReadMapper;

    @Resource
    private SvideoCommentLikeWriteMapper svideoCommentLikeWriteMapper;

    /**
     * 新增短视频评论点赞表
     *
     * @param svideoCommentLike
     * @return
     */
    public Integer saveSvideoCommentLike(SvideoCommentLike svideoCommentLike) {
        int count = svideoCommentLikeWriteMapper.insert(svideoCommentLike);
        if (count == 0) {
            throw new MallException("添加短视频评论点赞表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据commentLikeId更新短视频评论点赞表
     *
     * @param svideoCommentLike
     * @return
     */
    public Integer updateSvideoCommentLike(SvideoCommentLike svideoCommentLike) {
        if (StringUtils.isEmpty(svideoCommentLike.getCommentLikeId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = svideoCommentLikeWriteMapper.updateByPrimaryKeySelective(svideoCommentLike);
        if (count == 0) {
            log.error("根据commentLikeId：" + svideoCommentLike.getCommentLikeId() + "更新短视频评论点赞表失败");
            throw new MallException("更新短视频评论点赞表失败,请重试");
        }
        return count;
    }

    /**
     * 根据commentLikeId获取短视频评论点赞表详情
     *
     * @param commentLikeId commentLikeId
     * @return
     */
    public SvideoCommentLike getSvideoCommentLikeByCommentLikeId(Integer commentLikeId) {
        return svideoCommentLikeReadMapper.getByPrimaryKey(commentLikeId);
    }

    /**
     * 根据条件获取短视频评论点赞表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<SvideoCommentLike> getSvideoCommentLikeList(SvideoCommentLikeExample example, PagerInfo pager) {
        List<SvideoCommentLike> svideoCommentLikeList;
        if (pager != null) {
            pager.setRowsCount(svideoCommentLikeReadMapper.countByExample(example));
            svideoCommentLikeList = svideoCommentLikeReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            svideoCommentLikeList = svideoCommentLikeReadMapper.listByExample(example);
        }
        return svideoCommentLikeList;
    }
}