package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.PaperQuestionMapper;
import com.hc.business.model.PaperQuestion;
import com.hc.business.service.PaperQuestionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 随机试卷问题表(PaperQuestion)表服务实现类
 *
 * @author java
 * @since 2020-12-07 11:35:14
 */

@Service("paperQuestionService")
public class PaperQuestionServiceImpl implements PaperQuestionService {

    private final PaperQuestionMapper paperQuestionMapper;

    public PaperQuestionServiceImpl(PaperQuestionMapper paperQuestionMapper) {
        this.paperQuestionMapper = paperQuestionMapper;
    }

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

    /**
     * 新增数据
     *
     * @param paperQuestion 实例对象
     * @return 实例对象
     */
    @Override
    public PaperQuestion insert(PaperQuestion paperQuestion) {
        this.paperQuestionMapper.insert(paperQuestion);
        return paperQuestion;
    }

    /**
     * 修改数据
     *
     * @param paperQuestion 实例对象
     * @return 实例对象
     */
    @Override
    public PaperQuestion update(PaperQuestion paperQuestion) {
        this.paperQuestionMapper.updateByPrimaryKeySelective(paperQuestion);
        return this.queryById(paperQuestion.getPaperRuleId());
    }

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

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

    @Override
    public List<PaperQuestion> queryByPaperId(Integer paperId) {
        return this.paperQuestionMapper.queryByPaperId(paperId);
    }
}