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

import com.its.op.dto.its.analysis.TbTrafStat15mCountInf;
import com.its.op.dto.its.analysis.TbTrafStat15mListInf;
import com.its.op.entity.its.link.TbLinkStat15m;
import com.its.op.entity.its.link.TbLinkStat15mKey;
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 TbLink15mStatRepository extends JpaRepository<TbLinkStat15m, TbLinkStat15mKey>, JpaSpecificationExecutor<TbLinkStat15m> {

    @Query("select p from TbLinkStat15m p")
    List<TbLinkStat15m> findAll();

    @Query("select p.statDt as statDt, count(p.linkId) as dataCnt from TbLinkStat15m p where p.statDt between :fromDt and :toDt group by p.statDt")
    List<TbTrafStat15mCountInf> findAllCountByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query( "select b.linkId as id, nvl(c.cmtrGradCd, 'LTC0') as cmtrGradCd, b.sped as sped " +
            "  from TbLink a, TbLinkStat15m b, TbSectGradStup c " +
            " where a.delYn  = 'N' " +
            "   and a.linkId = b.linkId " +
            "   and a.sectGradCd = c.sectGradCd " +
            "   and (b.sped >= c.lwstTrvlSped and b.sped <= c.hghsTrvlSped) " +
            "   and b.statDt = :statDt"
    )
    List<TbTrafStat15mListInf> findAllDataByDateRange(@Param("statDt") String statDt);
}
