package com.erp.purchase.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.purchase.entity.PurchaseInbound;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface PurchaseInboundMapper extends BaseMapper<PurchaseInbound> {

    @Select("SELECT pi.*, po.supplier_name, w.name as warehouse_name " +
            "FROM purchase_inbound pi " +
            "LEFT JOIN purchase_order po ON pi.purchase_order_id = po.id " +
            "LEFT JOIN warehouse w ON pi.warehouse_id = w.id " +
            "WHERE pi.deleted = 0 " +
            "AND (#{inboundNo} IS NULL OR pi.inbound_no LIKE CONCAT('%', #{inboundNo}, '%')) " +
            "AND (#{purchaseOrderId} IS NULL OR pi.purchase_order_id = #{purchaseOrderId}) " +
            "AND (#{supplierId} IS NULL OR pi.supplier_id = #{supplierId}) " +
            "AND (#{warehouseId} IS NULL OR pi.warehouse_id = #{warehouseId}) " +
            "AND (#{status} IS NULL OR pi.status = #{status}) " +
            "ORDER BY pi.create_time DESC")
    IPage<PurchaseInbound> selectPageList(Page<PurchaseInbound> page,
                                           @Param("inboundNo") String inboundNo,
                                           @Param("purchaseOrderId") Long purchaseOrderId,
                                           @Param("supplierId") Long supplierId,
                                           @Param("warehouseId") Long warehouseId,
                                           @Param("status") String status);

    @Select("SELECT * FROM purchase_inbound WHERE deleted = 0 AND inbound_no = #{inboundNo} LIMIT 1")
    PurchaseInbound findByInboundNo(@Param("inboundNo") String inboundNo);
}
