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

import com.its.op.entity.its.link.TbLinkTraf;
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;
import java.util.Optional;

@Repository
public interface TbLinkTrafRepository extends JpaRepository<TbLinkTraf, Long>, JpaSpecificationExecutor<TbLinkTraf> {

    @Query("select p from TbLinkTraf p inner join fetch p.link m join fetch m.fromNode fnode join fetch m.toNode tnode")
    List<TbLinkTraf> findAll();

    @Query("select p from TbLinkTraf p")
    List<TbLinkTraf> findAllTraf();

    @Query("select p from TbLinkTraf p inner join fetch p.link m join fetch m.fromNode fnode  join fetch m.toNode tnode " +
           " where m.delYn = 'N' " +
           "   and m.areaCd = 'LAT001' " +
           "   and p.missYn = 'Y' " +
           "   and p.prcnDt >= to_char(sysdate - 10/1440, 'YYYYMMDDHH24MISS')")
    List<TbLinkTraf> findAllMissTraf();

    @Query("select p from TbLinkTraf p inner join fetch p.link m join fetch m.fromNode fnode join fetch m.toNode tnode where p.linkId = :linkId")
    Optional<TbLinkTraf> findById(@Param("linkId") Long linkId);

}
