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.VideoGoodsReadMapper;
import com.slodon.b2b2c.dao.write.video.VideoGoodsWriteMapper;
import com.slodon.b2b2c.video.example.VideoGoodsExample;
import com.slodon.b2b2c.video.pojo.VideoGoods;
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 VideoGoodsModel {

    @Resource
    private VideoGoodsReadMapper videoGoodsReadMapper;
    @Resource
    private VideoGoodsWriteMapper videoGoodsWriteMapper;

    /**
     * 新增短视频或直播商品表
     *
     * @param videoGoods
     * @return
     */
    public Integer saveVideoGoods(VideoGoods videoGoods) {
        int count = videoGoodsWriteMapper.insert(videoGoods);
        if (count == 0) {
            throw new MallException("添加短视频或直播商品表失败，请重试");
        }
        return count;
    }

    /**
     * 根据videoGoodsId删除短视频或直播商品表
     *
     * @param videoGoodsId videoGoodsId
     * @return
     */
    public Integer deleteVideoGoods(Integer videoGoodsId) {
        if (StringUtils.isEmpty(videoGoodsId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = videoGoodsWriteMapper.deleteByPrimaryKey(videoGoodsId);
        if (count == 0) {
            log.error("根据videoGoodsId：" + videoGoodsId + "删除短视频或直播商品表失败");
            throw new MallException("删除短视频或直播商品表失败,请重试");
        }
        return count;
    }

    /**
     * 根据videoGoodsId更新短视频或直播商品表
     *
     * @param videoGoods
     * @return
     */
    public Integer updateVideoGoods(VideoGoods videoGoods) {
        if (StringUtils.isEmpty(videoGoods.getVideoGoodsId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = videoGoodsWriteMapper.updateByPrimaryKeySelective(videoGoods);
        if (count == 0) {
            log.error("根据videoGoodsId：" + videoGoods.getVideoGoodsId() + "更新短视频或直播商品表失败");
            throw new MallException("更新短视频或直播商品表失败,请重试");
        }
        return count;
    }

    /**
     * 根据videoGoodsId获取短视频或直播商品表详情
     *
     * @param videoGoodsId videoGoodsId
     * @return
     */
    public VideoGoods getVideoGoodsByVideoGoodsId(Integer videoGoodsId) {
        return videoGoodsReadMapper.getByPrimaryKey(videoGoodsId);
    }

    /**
     * 根据条件获取短视频或直播商品表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<VideoGoods> getVideoGoodsList(VideoGoodsExample example, PagerInfo pager) {
        List<VideoGoods> videoGoodsList;
        if (pager != null) {
            pager.setRowsCount(videoGoodsReadMapper.countByExample(example));
            videoGoodsList = videoGoodsReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            videoGoodsList = videoGoodsReadMapper.listByExample(example);
        }
        return videoGoodsList;
    }

    /**
     * 根据条件获取短视频或直播商品字段列表
     *
     * @param fields  查询字段，逗号分隔
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<VideoGoods> getVideoGoodsFieldList(String fields, VideoGoodsExample example, PagerInfo pager) {
        List<VideoGoods> videoGoodsList;
        if (pager != null) {
            pager.setRowsCount(videoGoodsReadMapper.countGroupFieldsByExample(fields, example));
            videoGoodsList = videoGoodsReadMapper.listFieldsPageByExample(fields, example, pager.getStart(), pager.getPageSize());
        } else {
            videoGoodsList = videoGoodsReadMapper.listFieldsByExample(fields, example);
        }
        return videoGoodsList;
    }

    /**
     * 根据条件获取短视频或直播商品字段列表数量
     *
     * @param fields  查询字段，逗号分隔
     * @param example 查询条件信息
     * @return
     */
    public Integer getVideoGoodsFieldCount(String fields, VideoGoodsExample example) {
        return videoGoodsReadMapper.countGroupFieldsByExample(fields, example);
    }
}