package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.CourseChapterVideoMapper;
import com.hc.business.model.CourseChapterVideo;
import com.hc.business.service.CourseChapterVideoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程视频(CourseChapterVideo)表服务实现类
 *
 * @author java
 * @since 2020-11-09 10:04:56
 */

@Service("courseChapterVideoService")
public class CourseChapterVideoServiceImpl implements CourseChapterVideoService {

    private final CourseChapterVideoMapper courseChapterVideoMapper;

    public CourseChapterVideoServiceImpl(CourseChapterVideoMapper courseChapterVideoMapper) {
        this.courseChapterVideoMapper = courseChapterVideoMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param courseChapterVideo 实例对象
     * @return 实例对象
     */
    @Override
    public CourseChapterVideo update(CourseChapterVideo courseChapterVideo) {
        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) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.courseChapterVideoMapper::selectAll);
    }


    /**
     * 通过courseId查询单条数据
     *
     * @param courseId 课程ID
     * @return 实例对象
     */
    @Override
    public List<CourseChapterVideo> queryByCourseId(Integer courseId){
        return courseChapterVideoMapper.selectVideoByCourseId(courseId);
    }

}