package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebCourseChapterVideoMapper;
import com.hc.gpbusiness.model.WebCourseChapterVideo;
import com.hc.gpbusiness.service.WebCourseChapterVideoService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * 课程视频(WebCourseChapterVideo)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:08
 */

@Service("courseChapterVideoService")
public class WebCourseChapterVideoServiceImpl implements WebCourseChapterVideoService {

    private final WebCourseChapterVideoMapper courseChapterVideoMapper;

    public WebCourseChapterVideoServiceImpl(WebCourseChapterVideoMapper courseChapterVideoMapper) {
        this.courseChapterVideoMapper = courseChapterVideoMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param videoId 主键
     * @return 实例对象
     */
    @Override
    public WebCourseChapterVideo queryById(Integer videoId) {
        return this.courseChapterVideoMapper.selectByPrimaryKey(videoId);
    }

    /**
     * 新增数据
     *
     * @param courseChapterVideo 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourseChapterVideo insert(WebCourseChapterVideo courseChapterVideo) {
        courseChapterVideo.setCreateTime(System.currentTimeMillis());
        this.courseChapterVideoMapper.insert(courseChapterVideo);
        return courseChapterVideo;
    }

    /**
     * 修改数据
     *
     * @param courseChapterVideo 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourseChapterVideo update(WebCourseChapterVideo courseChapterVideo) {
        courseChapterVideo.setUpdateTime(System.currentTimeMillis());
        this.courseChapterVideoMapper.updateByPrimaryKeySelective(courseChapterVideo);
        return this.queryById(courseChapterVideo.getVideoId());
    }

    /**
     * 通过主键删除数据
     *
     * @param videoId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer videoId) {
        return this.courseChapterVideoMapper.deleteByPrimaryKey(videoId) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size, Integer chapterId) {
        PageHelper.startPage(page, size);
        List<WebCourseChapterVideo> courseChapterVideos
                = this.courseChapterVideoMapper.queryByChapterId(chapterId);
        return new PageInfo(courseChapterVideos);
    }
}