package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.UserTestHistoryMapper;
import com.hc.business.model.UserTestHistory;
import com.hc.business.service.UserTestHistoryService;
import org.springframework.stereotype.Service;

/**
 * 用户考试记录表(UserTestHistory)表服务实现类
 *
 * @author java
 * @since 2020-12-07 11:28:21
 */

@Service("userTestHistoryService")
public class UserTestHistoryServiceImpl implements UserTestHistoryService {

    private final UserTestHistoryMapper userTestHistoryMapper;

    public UserTestHistoryServiceImpl(UserTestHistoryMapper userTestHistoryMapper) {
        this.userTestHistoryMapper = userTestHistoryMapper;
    }

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

    /**
     * 新增数据
     *
     * @param userTestHistory 实例对象
     * @return 实例对象
     */
    @Override
    public UserTestHistory insert(UserTestHistory userTestHistory) {
        this.userTestHistoryMapper.insert(userTestHistory);
        return userTestHistory;
    }

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

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

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

    @Override
    public UserTestHistory queryMaxScore(Integer userId,Integer classId) {
        return userTestHistoryMapper.queryMaxScore(userId,classId);
    }
}