package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebCourseTagMapper;
import com.hc.gpbusiness.model.WebCourseTag;
import com.hc.gpbusiness.service.WebCourseTagService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

/**
 * 课程标签表(WebCourseTag)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:09
 */

@Service("courseTagService")
public class WebCourseTagServiceImpl implements WebCourseTagService {

    private final WebCourseTagMapper courseTagMapper;

    public WebCourseTagServiceImpl(WebCourseTagMapper courseTagMapper) {
        this.courseTagMapper = courseTagMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param courseTag 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourseTag update(WebCourseTag courseTag) {
        courseTag.setUpdateTime(System.currentTimeMillis());
        this.courseTagMapper.updateByPrimaryKeySelective(courseTag);
        return this.queryById(courseTag.getTagId());
    }

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

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