package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.gpbusiness.mapper.WebNewsAttachmentMapper;
import com.hc.gpbusiness.model.WebNewsAttachment;
import com.hc.gpbusiness.service.WebNewsAttachmentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 新闻附件表(NewsAttachment)表服务实现类
 *
 * @author makejava
 * @since 2024-01-20 10:54:50
 */

@Service("newsAttachmentService")
public class WebNewsAttachmentServiceImpl implements WebNewsAttachmentService {

    private final WebNewsAttachmentMapper newsAttachmentMapper;

    public WebNewsAttachmentServiceImpl(WebNewsAttachmentMapper newsAttachmentMapper) {
        this.newsAttachmentMapper = newsAttachmentMapper;
    }

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

    /**
     * 新增数据
     *
     * @param newsAttachment 实例对象
     * @return 实例对象
     */
    @Override
    public WebNewsAttachment insert(WebNewsAttachment newsAttachment) {
        this.newsAttachmentMapper.insert(newsAttachment);
        return newsAttachment;
    }

    /**
     * 修改数据
     *
     * @param newsAttachment 实例对象
     * @return 实例对象
     */
    @Override
    public WebNewsAttachment update(WebNewsAttachment newsAttachment) {
        this.newsAttachmentMapper.updateByPrimaryKeySelective(newsAttachment);
        return this.queryById(newsAttachment.getAttachmentId());
    }

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

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


    /**
     * 通过直播查询她关联的培训班
     * */
    @Override
    public List<WebNewsAttachment> queryByNewsId(Integer newsId) {
        List<WebNewsAttachment> list = this.newsAttachmentMapper.queryByNewsId(newsId);
        return list;
    }
}
