package com.erp.waybill.repository;

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

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 运单状态跟踪 Mapper
 */
@Mapper
public interface WaybillStatusRepository extends BaseMapper<WaybillStatus> {

    @Select("SELECT * FROM waybill_status WHERE waybill_no = #{waybillNo} LIMIT 1")
    Optional<WaybillStatus> findByWaybillNo(@Param("waybillNo") String waybillNo);

    @Select("SELECT * FROM waybill_status WHERE carrier = #{carrier} AND status NOT IN ('signed','rejected','returned') ORDER BY updated_at ASC")
    List<WaybillStatus> findPendingByCarrier(@Param("carrier") String carrier);

    @Select("SELECT * FROM waybill_status WHERE updated_at < #{before} AND status NOT IN ('signed','rejected','returned') ORDER BY updated_at ASC")
    List<WaybillStatus> findNeedSync(@Param("before") LocalDateTime before);
}
