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.core.util.StringUtil;
import com.slodon.b2b2c.dao.read.video.SvideoLabelReadMapper;
import com.slodon.b2b2c.dao.read.video.SvideoThemeReadMapper;
import com.slodon.b2b2c.dao.read.video.SvideoVideoReadMapper;
import com.slodon.b2b2c.dao.write.video.SvideoThemeVideoWriteMapper;
import com.slodon.b2b2c.dao.write.video.SvideoThemeWriteMapper;
import com.slodon.b2b2c.video.example.SvideoThemeExample;
import com.slodon.b2b2c.video.example.SvideoThemeVideoExample;
import com.slodon.b2b2c.video.pojo.SvideoLabel;
import com.slodon.b2b2c.video.pojo.SvideoTheme;
import com.slodon.b2b2c.video.pojo.SvideoThemeVideo;
import com.slodon.b2b2c.video.pojo.SvideoVideo;
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.List;

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

    @Resource
    private SvideoThemeReadMapper svideoThemeReadMapper;
    @Resource
    private SvideoThemeWriteMapper svideoThemeWriteMapper;
    @Resource
    private SvideoThemeVideoWriteMapper svideoThemeVideoWriteMapper;
    @Resource
    private SvideoVideoReadMapper svideoVideoReadMapper;
    @Resource
    private SvideoLabelReadMapper svideoLabelReadMapper;

    /**
     * 新增短视频主题表
     *
     * @param svideoTheme
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer saveSvideoTheme(SvideoTheme svideoTheme, String videoIds) {
        AssertUtil.isTrue(StringUtil.isNullOrZero(svideoTheme.getLabelId()), "标签id不能为空，请重试！");

        SvideoLabel svideoLabel = svideoLabelReadMapper.getByPrimaryKey(svideoTheme.getLabelId());
        AssertUtil.notNull(svideoLabel, "标签不存在");
        svideoTheme.setLabelName(svideoLabel.getLabelName());
        int count = svideoThemeWriteMapper.insert(svideoTheme);
        if (count == 0) {
            throw new MallException("添加主题表失败，请重试");
        }
        if (!StringUtil.isEmpty(videoIds)) {
            String[] videoIdArray = videoIds.split(",");
            for (String videoId : videoIdArray) {
                if (StringUtils.isEmpty(videoId)) {
                    continue;
                }
                //查询视频信息
                SvideoVideo video = svideoVideoReadMapper.getByPrimaryKey(videoId.trim());
                AssertUtil.notNull(video, "视频不存在");
                SvideoThemeVideo themeVideo = new SvideoThemeVideo();
                themeVideo.setThemeId(svideoTheme.getThemeId());
                themeVideo.setVideoId(video.getVideoId());
                themeVideo.setVideoName(video.getVideoName());
                themeVideo.setVideoImage(video.getVideoThemeImage());
                themeVideo.setVideoPath(video.getVideoPath());
                themeVideo.setIntroduction(video.getIntroduction());
                themeVideo.setVideoType(video.getVideoType());
                count = svideoThemeVideoWriteMapper.insert(themeVideo);
                AssertUtil.isTrue(count == 0, "保存推荐主题视频信息失败，请重试！");
            }
        }
        return svideoTheme.getThemeId();
    }

    /**
     * 根据themeId删除短视频主题表
     *
     * @param themeId themeId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteSvideoTheme(Integer themeId) {
        if (StringUtils.isEmpty(themeId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = svideoThemeWriteMapper.deleteByPrimaryKey(themeId);
        if (count == 0) {
            log.error("根据themeId：" + themeId + "删除短视频主题表失败");
            throw new MallException("删除短视频主题表失败,请重试");
        }
        //删除推荐主题视频
        SvideoThemeVideoExample example = new SvideoThemeVideoExample();
        example.setThemeId(themeId);
        svideoThemeVideoWriteMapper.deleteByExample(example);
        return count;
    }

    /**
     * 根据themeId更新短视频主题表
     *
     * @param svideoTheme
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer updateSvideoTheme(SvideoTheme svideoTheme, String videoIds) {
        AssertUtil.isTrue(StringUtil.isNullOrZero(svideoTheme.getThemeId()), "请选择要修改的数据");

        SvideoTheme svideoThemeDb = this.getSvideoThemeByThemeId(svideoTheme.getThemeId());
        AssertUtil.notNull(svideoThemeDb, "推荐主题不存在");
        if (!StringUtil.isNullOrZero(svideoTheme.getLabelId()) && !svideoThemeDb.getLabelId().equals(svideoTheme.getLabelId())) {
            SvideoLabel svideoLabel = svideoLabelReadMapper.getByPrimaryKey(svideoTheme.getLabelId());
            AssertUtil.notNull(svideoLabel, "标签不存在");
            svideoTheme.setLabelName(svideoLabel.getLabelName());
        }
        int count = svideoThemeWriteMapper.updateByPrimaryKeySelective(svideoTheme);
        if (count == 0) {
            log.error("根据themeId：" + svideoTheme.getThemeId() + "更新主题表失败");
            throw new MallException("更新主题表失败,请重试");
        }

        if (!StringUtil.isEmpty(videoIds)) {
            //删除推荐主题视频,重新保存
            SvideoThemeVideoExample example = new SvideoThemeVideoExample();
            example.setThemeId(svideoTheme.getThemeId());
            svideoThemeVideoWriteMapper.deleteByExample(example);

            String[] videoIdArray = videoIds.split(",");
            for (String videoId : videoIdArray) {
                if (StringUtils.isEmpty(videoId)) {
                    continue;
                }
                //查询视频信息
                SvideoVideo video = svideoVideoReadMapper.getByPrimaryKey(videoId.trim());
                AssertUtil.notNull(video, "视频不存在");
                SvideoThemeVideo themeVideo = new SvideoThemeVideo();
                themeVideo.setThemeId(svideoTheme.getThemeId());
                themeVideo.setVideoId(video.getVideoId());
                themeVideo.setVideoName(video.getVideoName());
                themeVideo.setVideoImage(video.getVideoThemeImage());
                themeVideo.setVideoPath(video.getVideoPath());
                themeVideo.setIntroduction(video.getIntroduction());
                themeVideo.setVideoType(video.getVideoType());
                count = svideoThemeVideoWriteMapper.insert(themeVideo);
                AssertUtil.isTrue(count == 0, "保存推荐主题视频信息失败，请重试！");
            }
        }
        return count;
    }

    /**
     * 根据themeId获取短视频主题表详情
     *
     * @param themeId themeId
     * @return
     */
    public SvideoTheme getSvideoThemeByThemeId(Integer themeId) {
        return svideoThemeReadMapper.getByPrimaryKey(themeId);
    }

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

    /**
     * 根据条件获取短视频主题数量
     *
     * @param example 查询条件信息
     * @return
     */
    public Integer getSvideoThemeCount(SvideoThemeExample example) {
        return svideoThemeReadMapper.countByExample(example);
    }
}