package com.its.op.dao.repository.its.link;

import com.its.op.entity.its.link.TbLinkTrafHs;
import com.its.op.entity.its.link.TbLinkTrafHsKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TbLinkTrafHsRepository extends JpaRepository<TbLinkTrafHs, TbLinkTrafHsKey>, JpaSpecificationExecutor<TbLinkTrafHs> {

    @Query("select p from TbLinkTrafHs p where p.linkId = :linkId and p.prcnDt between :fromDt and :toDt")
    List<TbLinkTrafHs> findAllByDateRange(@Param("linkId") Long linkId, @Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query("select p from TbLinkTrafHs p " +
           " where (p.linkId, p.prcnDt) in (" +
           "        select x.linkId, max(x.prcnDt) " +
           "          from TbLinkTrafHs x " +
           "         where x.prcnDt between :fromDt and :toDt " +
           "           and x.cmtrGradCd <> 'LTC1' " +
           "         group by x.linkId)")
    List<TbLinkTrafHs> findAllCongestByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);
}
