package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.model.OrderAttachment;
import com.hc.business.mapper.OrderAttachmentMapper;
import com.hc.business.service.OrderAttachmentService;
import org.springframework.stereotype.Service;

import java.util.List;

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

@Service("orderAttachmentService")
public class OrderAttachmentServiceImpl implements OrderAttachmentService {

    private final OrderAttachmentMapper orderAttachmentMapper;

    public OrderAttachmentServiceImpl(OrderAttachmentMapper orderAttachmentMapper) {
        this.orderAttachmentMapper = orderAttachmentMapper;
    }

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

    /**
     * 新增数据
     *
     * @param orderAttachment 实例对象
     * @return 实例对象
     */
    @Override
    public OrderAttachment insert(OrderAttachment orderAttachment) {
        this.orderAttachmentMapper.insert(orderAttachment);
        return orderAttachment;
    }

    /**
     * 修改数据
     *
     * @param orderAttachment 实例对象
     * @return 实例对象
     */
    @Override
    public OrderAttachment update(OrderAttachment orderAttachment) {
        this.orderAttachmentMapper.updateByPrimaryKeySelective(orderAttachment);
        return this.queryById(orderAttachment.getAttchementId());
    }

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

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

    /**
     * 通过订单查询相关附件
     * */
    @Override
    public List<OrderAttachment> queryByOrderId(Integer orderId) {
        List<OrderAttachment> list = this.orderAttachmentMapper.queryByOrderId(orderId);
        return list;
    }
}
