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.SvideoVideoLikeReadMapper;
import com.slodon.b2b2c.dao.write.video.SvideoVideoLikeWriteMapper;
import com.slodon.b2b2c.video.example.SvideoVideoLikeExample;
import com.slodon.b2b2c.video.pojo.SvideoVideoLike;
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 SvideoVideoLikeModel {
    @Resource
    private SvideoVideoLikeReadMapper svideoVideoLikeReadMapper;

    @Resource
    private SvideoVideoLikeWriteMapper svideoVideoLikeWriteMapper;

    /**
     * 新增短视频点赞表
     *
     * @param svideoVideoLike
     * @return
     */
    public Integer saveSvideoVideoLike(SvideoVideoLike svideoVideoLike) {
        int count = svideoVideoLikeWriteMapper.insert(svideoVideoLike);
        if (count == 0) {
            throw new MallException("添加短视频点赞表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据videoLikeId更新短视频点赞表
     *
     * @param svideoVideoLike
     * @return
     */
    public Integer updateSvideoVideoLike(SvideoVideoLike svideoVideoLike) {
        if (StringUtils.isEmpty(svideoVideoLike.getVideoLikeId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = svideoVideoLikeWriteMapper.updateByPrimaryKeySelective(svideoVideoLike);
        if (count == 0) {
            log.error("根据videoLikeId：" + svideoVideoLike.getVideoLikeId() + "更新短视频点赞表失败");
            throw new MallException("更新短视频点赞表失败,请重试");
        }
        return count;
    }

    /**
     * 根据videoLikeId获取短视频点赞表详情
     *
     * @param videoLikeId videoLikeId
     * @return
     */
    public SvideoVideoLike getSvideoVideoLikeByVideoLikeId(Integer videoLikeId) {
        return svideoVideoLikeReadMapper.getByPrimaryKey(videoLikeId);
    }

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