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.SvideoLabelReadMapper;
import com.slodon.b2b2c.dao.write.video.SvideoLabelWriteMapper;
import com.slodon.b2b2c.video.example.SvideoLabelExample;
import com.slodon.b2b2c.video.pojo.SvideoLabel;
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 SvideoLabelModel {

    @Resource
    private SvideoLabelReadMapper svideoLabelReadMapper;
    @Resource
    private SvideoLabelWriteMapper svideoLabelWriteMapper;

    /**
     * 新增短视频标签表
     *
     * @param svideoLabel
     * @return
     */
    public Integer saveSvideoLabel(SvideoLabel svideoLabel) {
        int count = svideoLabelWriteMapper.insert(svideoLabel);
        if (count == 0) {
            throw new MallException("添加短视频标签表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据labelId更新短视频标签表
     *
     * @param svideoLabel
     * @return
     */
    public Integer updateSvideoLabel(SvideoLabel svideoLabel) {
        if (StringUtils.isEmpty(svideoLabel.getLabelId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = svideoLabelWriteMapper.updateByPrimaryKeySelective(svideoLabel);
        if (count == 0) {
            log.error("根据labelId：" + svideoLabel.getLabelId() + "更新短视频标签表失败");
            throw new MallException("更新短视频标签表失败,请重试");
        }
        return count;
    }

    /**
     * 根据labelId获取短视频标签表详情
     *
     * @param labelId labelId
     * @return
     */
    public SvideoLabel getSvideoLabelByLabelId(Integer labelId) {
        return svideoLabelReadMapper.getByPrimaryKey(labelId);
    }

    /**
     * 根据条件获取短视频标签表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<SvideoLabel> getSvideoLabelList(SvideoLabelExample example, PagerInfo pager) {
        List<SvideoLabel> svideoLabelList;
        if (pager != null) {
            pager.setRowsCount(svideoLabelReadMapper.countByExample(example));
            svideoLabelList = svideoLabelReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            svideoLabelList = svideoLabelReadMapper.listByExample(example);
        }
        return svideoLabelList;
    }
}