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

@Mapper
public interface PurchaseOrderMapper extends BaseMapper<PurchaseOrder> {

    @Select("SELECT po.*, s.name as supplier_name, w.name as warehouse_name " +
            "FROM purchase_order po " +
            "LEFT JOIN supplier s ON po.supplier_id = s.id " +
            "LEFT JOIN warehouse w ON po.warehouse_id = w.id " +
            "WHERE po.deleted = 0 " +
            "AND (#{orderNo} IS NULL OR po.order_no LIKE CONCAT('%', #{orderNo}, '%')) " +
            "AND (#{supplierId} IS NULL OR po.supplier_id = #{supplierId}) " +
            "AND (#{status} IS NULL OR po.status = #{status}) " +
            "AND (#{startDate} IS NULL OR po.order_date >= #{startDate}) " +
            "AND (#{endDate} IS NULL OR po.order_date <= #{endDate}) " +
            "ORDER BY po.create_time DESC")
    IPage<PurchaseOrder> selectPageList(Page<PurchaseOrder> page,
                                         @Param("orderNo") String orderNo,
                                         @Param("supplierId") Long supplierId,
                                         @Param("status") String status,
                                         @Param("startDate") java.time.LocalDate startDate,
                                         @Param("endDate") java.time.LocalDate endDate);

    @Select("SELECT * FROM purchase_order WHERE deleted = 0 AND order_no = #{orderNo} LIMIT 1")
    PurchaseOrder findByOrderNo(@Param("orderNo") String orderNo);
}
