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.PurchaseReturn;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface PurchaseReturnMapper extends BaseMapper<PurchaseReturn> {

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

    @Select("SELECT * FROM purchase_return WHERE deleted = 0 AND return_no = #{returnNo} LIMIT 1")
    PurchaseReturn findByReturnNo(@Param("returnNo") String returnNo);
}
