package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ClassConfigSponsorMapper;
import com.hc.business.model.ClassConfigSponsor;
import com.hc.business.service.ClassConfigSponsorService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * (ClassConfigSponsor)表服务实现类
 *
 * @author java
 * @since 2021-05-28 14:19:35
 */

@Service("classConfigSponsorService")
public class ClassConfigSponsorServiceImpl implements ClassConfigSponsorService {

    private final ClassConfigSponsorMapper classConfigSponsorMapper;

    public ClassConfigSponsorServiceImpl(ClassConfigSponsorMapper classConfigSponsorMapper) {
        this.classConfigSponsorMapper = classConfigSponsorMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param classConfigSponsor 实例对象
     * @return 实例对象
     */
    @Override
    public ClassConfigSponsor update(ClassConfigSponsor classConfigSponsor) {
        this.classConfigSponsorMapper.updateByPrimaryKeySelective(classConfigSponsor);
        return this.queryById(classConfigSponsor.getSponsorId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size, Integer classId) {
        if (Objects.isNull(classId)) {
            return PageHelper.startPage(page, size).doSelectPageInfo(this.classConfigSponsorMapper::selectAll);
        }
        PageHelper.startPage(page, size);
        List<ClassConfigSponsor> classConfigSponsors = this.classConfigSponsorMapper.queryListByClassId(classId);
        return new PageInfo(classConfigSponsors);
    }

}
