package com.slodon.b2b2c.model.video;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.dao.read.video.SvideoThemeReadMapper;
import com.slodon.b2b2c.dao.read.video.SvideoThemeVideoReadMapper;
import com.slodon.b2b2c.dao.write.video.SvideoThemeVideoWriteMapper;
import com.slodon.b2b2c.dao.write.video.SvideoThemeWriteMapper;
import com.slodon.b2b2c.video.example.SvideoThemeVideoExample;
import com.slodon.b2b2c.video.pojo.SvideoTheme;
import com.slodon.b2b2c.video.pojo.SvideoThemeVideo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 短视频主题视频表model
 */
@Component
@Slf4j
public class SvideoThemeVideoModel {

    @Resource
    private SvideoThemeVideoReadMapper svideoThemeVideoReadMapper;
    @Resource
    private SvideoThemeVideoWriteMapper svideoThemeVideoWriteMapper;
    @Resource
    private SvideoThemeReadMapper svideoThemeReadMapper;
    @Resource
    private SvideoThemeWriteMapper svideoThemeWriteMapper;

    /**
     * 新增短视频主题视频表
     *
     * @param svideoThemeVideo
     * @return
     */
    public Integer saveSvideoThemeVideo(SvideoThemeVideo svideoThemeVideo) {
        int count = svideoThemeVideoWriteMapper.insert(svideoThemeVideo);
        if (count == 0) {
            throw new MallException("添加短视频主题视频表失败，请重试");
        }
        return count;
    }

    /**
     * 根据themeVideoId删除短视频主题视频表
     *
     * @param themeVideoId themeVideoId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteSvideoThemeVideo(Integer themeVideoId) {
        if (StringUtils.isEmpty(themeVideoId)) {
            throw new MallException("请选择要删除的数据");
        }
        SvideoThemeVideo themeVideo = this.getSvideoThemeVideoByThemeVideoId(themeVideoId);
        AssertUtil.notNull(themeVideo, "主题视频不存在");
        int count = svideoThemeVideoWriteMapper.deleteByPrimaryKey(themeVideoId);
        if (count == 0) {
            log.error("根据themeVideoId：" + themeVideoId + "删除主题视频表失败");
            throw new MallException("删除主题视频表失败,请重试");
        }
        SvideoTheme svideoTheme = svideoThemeReadMapper.getByPrimaryKey(themeVideo.getThemeId());
        AssertUtil.notNull(svideoTheme, "主题不存在");
        SvideoTheme theme = new SvideoTheme();
        theme.setThemeId(svideoTheme.getThemeId());
        theme.setVideoNum(svideoTheme.getVideoNum() - 1);
        theme.setUpdateTime(new Date());
        count = svideoThemeWriteMapper.updateByPrimaryKeySelective(theme);
        AssertUtil.isTrue(count == 0, "修改主题视频数量失败,请重试");
        return count;
    }

    /**
     * 根据themeVideoId更新短视频主题视频表
     *
     * @param svideoThemeVideo
     * @return
     */
    public Integer updateSvideoThemeVideo(SvideoThemeVideo svideoThemeVideo) {
        if (StringUtils.isEmpty(svideoThemeVideo.getThemeVideoId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = svideoThemeVideoWriteMapper.updateByPrimaryKeySelective(svideoThemeVideo);
        if (count == 0) {
            log.error("根据themeVideoId：" + svideoThemeVideo.getThemeVideoId() + "更新短视频主题视频表失败");
            throw new MallException("更新短视频主题视频表失败,请重试");
        }
        return count;
    }

    /**
     * 根据themeVideoId获取短视频主题视频表详情
     *
     * @param themeVideoId themeVideoId
     * @return
     */
    public SvideoThemeVideo getSvideoThemeVideoByThemeVideoId(Integer themeVideoId) {
        return svideoThemeVideoReadMapper.getByPrimaryKey(themeVideoId);
    }

    /**
     * 根据条件获取短视频主题视频表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<SvideoThemeVideo> getSvideoThemeVideoList(SvideoThemeVideoExample example, PagerInfo pager) {
        List<SvideoThemeVideo> svideoThemeVideoList;
        if (pager != null) {
            pager.setRowsCount(svideoThemeVideoReadMapper.countByExample(example));
            svideoThemeVideoList = svideoThemeVideoReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            svideoThemeVideoList = svideoThemeVideoReadMapper.listByExample(example);
        }
        return svideoThemeVideoList;
    }
}