package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebNewsHistoryMapper;
import com.hc.gpbusiness.model.WebNewsHistory;
import com.hc.gpbusiness.service.WebNewsHistoryService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

/**
 * 新闻操作记录表，比如转发，点评(WebNewsHistory)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:11
 */

@Service("newsHistoryService")
public class WebNewsHistoryServiceImpl implements WebNewsHistoryService {

    private final WebNewsHistoryMapper newsHistoryMapper;

    public WebNewsHistoryServiceImpl(WebNewsHistoryMapper newsHistoryMapper) {
        this.newsHistoryMapper = newsHistoryMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param newsHistory 实例对象
     * @return 实例对象
     */
    @Override
    public WebNewsHistory update(WebNewsHistory newsHistory) {
        newsHistory.setUpdateTime(System.currentTimeMillis());
        this.newsHistoryMapper.updateByPrimaryKeySelective(newsHistory);
        return this.queryById(newsHistory.getHistoryId());
    }

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

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