package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmQuestionMapper;
import com.hc.business.model.ZccmQuestion;
import com.hc.business.service.ZccmQuestionService;
import org.springframework.stereotype.Service;

/**
 * 习题表(ZccmQuestion)表服务实现类
 *
 * @author java
 * @since 2020-12-07 11:35:57
 */

@Service("questionService")
public class ZccmQuestionServiceImpl implements ZccmQuestionService {

    private final ZccmQuestionMapper questionMapper;

    public ZccmQuestionServiceImpl(ZccmQuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

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

    /**
     * 新增数据
     *
     * @param question 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmQuestion insert(ZccmQuestion question) {
        this.questionMapper.insert(question);
        return question;
    }

    /**
     * 修改数据
     *
     * @param question 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmQuestion update(ZccmQuestion question) {
        this.questionMapper.updateByPrimaryKeySelective(question);
        return this.queryById(question.getQuestionId());
    }

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

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


    /**
     * 分页查询多条数据
     *
     * @param tagId 题库id
     * @param page  页码
     * @param size  查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryByTagLimit(int tagId, int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> questionMapper.queryTagId(tagId));
    }
}