package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebCourseChapterMapper;
import com.hc.gpbusiness.model.WebCourseChapter;
import com.hc.gpbusiness.service.WebCourseChapterService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * 课程章节表(CourseChapter)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:06
 */

@Service("courseChapterService")
public class WebCourseChapterServiceImpl implements WebCourseChapterService {

    private final WebCourseChapterMapper courseChapterMapper;

    public WebCourseChapterServiceImpl(WebCourseChapterMapper courseChapterMapper) {
        this.courseChapterMapper = courseChapterMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param courseChapter 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourseChapter update(WebCourseChapter courseChapter) {
        courseChapter.setUpdateTime(System.currentTimeMillis());
        this.courseChapterMapper.updateByPrimaryKeySelective(courseChapter);
        return this.queryById(courseChapter.getCourseChapterId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @param courseId 课程id
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size, Integer courseId) {
        PageHelper.startPage(page, size);
        List<WebCourseChapter> courseChapters =
                this.courseChapterMapper.queryByCourseId(courseId);
        return new PageInfo(courseChapters);
    }
}