package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebNewsTagMapper;
import com.hc.gpbusiness.model.WebNewsTag;
import com.hc.gpbusiness.service.WebNewsTagService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

/**
 * 新闻标签表(NewsTag)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:11
 */

@Service("newsTagService")
public class WebNewsTagServiceImpl implements WebNewsTagService {

    private final WebNewsTagMapper newsTagMapper;

    public WebNewsTagServiceImpl(WebNewsTagMapper newsTagMapper) {
        this.newsTagMapper = newsTagMapper;
    }

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

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

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

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

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