package com.erp.waybill.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.waybill.entity.Waybill;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Optional;

/**
 * 运单 Mapper
 */
@Mapper
public interface WaybillRepository extends BaseMapper<Waybill> {

    @Select("SELECT * FROM waybills WHERE waybill_no = #{waybillNo} AND deleted_at IS NULL LIMIT 1")
    Optional<Waybill> findByWaybillNo(@Param("waybillNo") String waybillNo);

    @Select("SELECT * FROM waybills WHERE order_id = #{orderId} AND deleted_at IS NULL")
    List<Waybill> findByOrderId(@Param("orderId") Long orderId);

    @Select("SELECT * FROM waybills WHERE order_id = #{orderId} AND status = #{status} AND deleted_at IS NULL")
    List<Waybill> findByOrderIdAndStatus(@Param("orderId") Long orderId, @Param("status") String status);

    @Select("SELECT * FROM waybills WHERE express_code = #{expressCode} AND status = 'unused' AND deleted_at IS NULL ORDER BY created_at DESC")
    List<Waybill> findUnusedByExpressCode(@Param("expressCode") String expressCode);
}
