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.SvideoReplyLikeReadMapper;
import com.slodon.b2b2c.dao.write.video.SvideoReplyLikeWriteMapper;
import com.slodon.b2b2c.video.example.SvideoReplyLikeExample;
import com.slodon.b2b2c.video.pojo.SvideoReplyLike;
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 SvideoReplyLikeModel {
    @Resource
    private SvideoReplyLikeReadMapper svideoReplyLikeReadMapper;

    @Resource
    private SvideoReplyLikeWriteMapper svideoReplyLikeWriteMapper;

    /**
     * 新增短视频回复点赞表
     *
     * @param svideoReplyLike
     * @return
     */
    public Integer saveSvideoReplyLike(SvideoReplyLike svideoReplyLike) {
        int count = svideoReplyLikeWriteMapper.insert(svideoReplyLike);
        if (count == 0) {
            throw new MallException("添加短视频回复点赞表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据replyLikeId更新短视频回复点赞表
     *
     * @param svideoReplyLike
     * @return
     */
    public Integer updateSvideoReplyLike(SvideoReplyLike svideoReplyLike) {
        if (StringUtils.isEmpty(svideoReplyLike.getReplyLikeId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = svideoReplyLikeWriteMapper.updateByPrimaryKeySelective(svideoReplyLike);
        if (count == 0) {
            log.error("根据replyLikeId：" + svideoReplyLike.getReplyLikeId() + "更新短视频回复点赞表失败");
            throw new MallException("更新短视频回复点赞表失败,请重试");
        }
        return count;
    }

    /**
     * 根据replyLikeId获取短视频回复点赞表详情
     *
     * @param replyLikeId replyLikeId
     * @return
     */
    public SvideoReplyLike getSvideoReplyLikeByReplyLikeId(Integer replyLikeId) {
        return svideoReplyLikeReadMapper.getByPrimaryKey(replyLikeId);
    }

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