package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import java.util.List;
import com.hc.business.service.NewsService;
import com.hc.business.mapper.NewsMapper;
import com.hc.business.model.News;



/**
 * 新闻表
 * (News)表服务实现类
 *
 * @author java
 * @since 2020-11-20 11:48:38
 */

@Service("newsService")
public class NewsServiceImpl implements NewsService {

    private final NewsMapper newsMapper;

    public NewsServiceImpl(NewsMapper newsMapper) {
        this.newsMapper = newsMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param news 实例对象
     * @return 实例对象
     */
    @Override
    public News update(News news) {
        this.newsMapper.updateByPrimaryKeySelective(news);
        return this.queryById(news.getNewsId());
    }

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

    @Override
    public PageInfo queryListByTagId(Integer tagId, int page, int size) {
        PageHelper.startPage(page, size);
        List<News> news = this.newsMapper.queryByTagId(tagId);
        return new PageInfo(news);
    }

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