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.VideoSettingReadMapper;
import com.slodon.b2b2c.dao.write.video.VideoSettingWriteMapper;
import com.slodon.b2b2c.video.example.VideoSettingExample;
import com.slodon.b2b2c.video.pojo.VideoSetting;
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 VideoSettingModel {

    @Resource
    private VideoSettingReadMapper videoSettingReadMapper;
    @Resource
    private VideoSettingWriteMapper videoSettingWriteMapper;

    /**
     * 新增直播和短视频设置表
     *
     * @param videoSetting
     * @return
     */
    public Integer saveVideoSetting(VideoSetting videoSetting) {
        int count = videoSettingWriteMapper.insert(videoSetting);
        if (count == 0) {
            throw new MallException("添加直播和短视频设置表失败，请重试");
        }
        return count;
    }

    /**
     * 根据settingName删除直播和短视频设置表
     *
     * @param settingName settingName
     * @return
     */
    public Integer deleteVideoSetting(String settingName) {
        if (StringUtils.isEmpty(settingName)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = videoSettingWriteMapper.deleteByPrimaryKey(settingName);
        if (count == 0) {
            log.error("根据settingName：" + settingName + "删除直播和短视频设置表失败");
            throw new MallException("删除直播和短视频设置表失败,请重试");
        }
        return count;
    }

    /**
     * 根据settingName更新直播和短视频设置表
     *
     * @param videoSetting
     * @return
     */
    public Integer updateVideoSetting(VideoSetting videoSetting) {
        if (StringUtils.isEmpty(videoSetting.getName())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = videoSettingWriteMapper.updateByPrimaryKeySelective(videoSetting);
        if (count == 0) {
            log.error("根据settingName：" + videoSetting.getName() + "更新直播和短视频设置表失败");
            throw new MallException("更新直播和短视频设置表失败,请重试");
        }
        return count;
    }

    /**
     * 根据settingName获取直播和短视频设置表详情
     *
     * @param settingName settingName
     * @return
     */
    public VideoSetting getVideoSettingBySettingName(String settingName) {
        return videoSettingReadMapper.getByPrimaryKey(settingName);
    }

    /**
     * 根据条件获取直播和短视频设置表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<VideoSetting> getVideoSettingList(VideoSettingExample example, PagerInfo pager) {
        List<VideoSetting> videoSettingList;
        if (pager != null) {
            pager.setRowsCount(videoSettingReadMapper.countByExample(example));
            videoSettingList = videoSettingReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            videoSettingList = videoSettingReadMapper.listByExample(example);
        }
        return videoSettingList;
    }
}