package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.CourseAttachmentMapper;
import com.hc.business.model.CourseAttachment;
import com.hc.business.service.CourseAttachmentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 线下汇款订单附件表(CourseAttachment)表服务实现类
 *
 * @author makejava
 * @since 2024-01-25 20:48:58
 */

@Service
public class CourseAttachmentServiceImpl implements CourseAttachmentService {

    private final CourseAttachmentMapper courseAttachmentMapper;

    public CourseAttachmentServiceImpl(CourseAttachmentMapper courseAttachmentMapper) {
        this.courseAttachmentMapper = courseAttachmentMapper;
    }

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

    /**
     * 新增数据
     *
     * @param courseAttachment 实例对象
     * @return 实例对象
     */
    @Override
    public CourseAttachment insert(CourseAttachment courseAttachment) {
        this.courseAttachmentMapper.insert(courseAttachment);
        return courseAttachment;
    }

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

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

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

    /**
     * 通过订单查询相关附件
     * */
    @Override
    public List<CourseAttachment> queryByCourseId(Integer courseId) {
        List<CourseAttachment> list = this.courseAttachmentMapper.queryByCourseId(courseId);
        return list;
    }
}
