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

import com.its.op.entity.its.atrd.TbAtrdRltnRoad;
import com.its.op.entity.its.atrd.TbAtrdRltnRoadKey;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface TbAtrdRoadRltnRepository extends JpaRepository<TbAtrdRltnRoad, TbAtrdRltnRoadKey>, JpaSpecificationExecutor<TbAtrdRltnRoad> {

    @Query("select p from TbAtrdRltnRoad p inner join fetch p.road m")
    List<TbAtrdRltnRoad> findAll();

    //@Query(value = "select p from TbAtrdRoadRltn p inner join fetch p.road m inner join fetch m.fromNode inner join fetch m.toNode where p.atrdId = :atrdId")
    @Query("select p from TbAtrdRltnRoad p inner join fetch p.road m where p.atrdId = :atrdId")
    List<TbAtrdRltnRoad> findRoadRltnById(@Param("atrdId") String atrdId);

    @Transactional
    @Modifying
    @Query("delete from TbAtrdRltnRoad p where p.atrdId = :atrdId")
    void deleteDataById(@Param("atrdId") String atrdId);

}
