package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebCourseChapterTestMapper;
import com.hc.gpbusiness.model.WebCourseChapterTest;
import com.hc.gpbusiness.service.WebCourseChapterTestService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

/**
 * 章节测试表/随堂练习表(WebCourseChapterTest)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:08
 */

@Service("courseChapterTestService")
public class WebCourseChapterTestServiceImpl implements WebCourseChapterTestService {

    private final WebCourseChapterTestMapper courseChapterTestMapper;

    public WebCourseChapterTestServiceImpl(WebCourseChapterTestMapper courseChapterTestMapper) {
        this.courseChapterTestMapper = courseChapterTestMapper;
    }

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

    /**
     * 新增数据
     *
     * @param courseChapterTest 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourseChapterTest insert(WebCourseChapterTest courseChapterTest) {
        this.courseChapterTestMapper.insert(courseChapterTest);
        return courseChapterTest;
    }

    /**
     * 修改数据
     *
     * @param courseChapterTest 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourseChapterTest update(WebCourseChapterTest courseChapterTest) {
        this.courseChapterTestMapper.updateByPrimaryKeySelective(courseChapterTest);
        return this.queryById(courseChapterTest.getTestId());
    }

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

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