package com.erp.warehouse.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.warehouse.entity.OutboundOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 出库单Mapper
 */
@Mapper
public interface OutboundOrderMapper extends BaseMapper<OutboundOrder> {

    @Select("SELECT oo.*, w.name as warehouse_name, tw.name as target_warehouse_name " +
            "FROM outbound_order oo " +
            "LEFT JOIN warehouse w ON oo.warehouse_id = w.id " +
            "LEFT JOIN warehouse tw ON oo.target_warehouse_id = tw.id " +
            "WHERE (#{outboundType} IS NULL OR oo.outbound_type = #{outboundType}) " +
            "AND (#{warehouseId} IS NULL OR oo.warehouse_id = #{warehouseId}) " +
            "AND (#{status} IS NULL OR oo.status = #{status}) " +
            "ORDER BY oo.create_time DESC")
    IPage<OutboundOrder> selectPageList(Page<OutboundOrder> page,
                                         @Param("outboundType") String outboundType,
                                         @Param("warehouseId") Long warehouseId,
                                         @Param("status") String status);

    @Select("SELECT * FROM outbound_order WHERE outbound_no = #{outboundNo} LIMIT 1")
    OutboundOrder findByOutboundNo(@Param("outboundNo") String outboundNo);
}
