package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebCourseChapterAnnexMapper;
import com.hc.gpbusiness.model.WebCourseChapterAnnex;
import com.hc.gpbusiness.service.WebCourseChapterAnnexService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

/**
 * 章节附件表(CourseChapterAnnex)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:07
 */

@Service("courseChapterAnnexService")
public class WebCourseChapterAnnexServiceImpl implements WebCourseChapterAnnexService {

    private final WebCourseChapterAnnexMapper courseChapterAnnexMapper;

    public WebCourseChapterAnnexServiceImpl(WebCourseChapterAnnexMapper courseChapterAnnexMapper) {
        this.courseChapterAnnexMapper = courseChapterAnnexMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param courseChapterAnnex 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourseChapterAnnex update(WebCourseChapterAnnex courseChapterAnnex) {
        courseChapterAnnex.setUpdateTime(System.currentTimeMillis());
        this.courseChapterAnnexMapper.updateByPrimaryKeySelective(courseChapterAnnex);
        return this.queryById(courseChapterAnnex.getCourseChapterAnnexId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.courseChapterAnnexMapper::selectAll);
    }
}