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

import com.its.op.dto.its.analysis.TbTrafStat15mCountInf;
import com.its.op.dto.its.analysis.TbTrafStat15mListInf;
import com.its.op.entity.its.road.TbRoadStat15m;
import com.its.op.entity.its.road.TbRoadStat15mKey;
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 TbRoad15mStatRepository extends JpaRepository<TbRoadStat15m, TbRoadStat15mKey>, JpaSpecificationExecutor<TbRoadStat15m> {

    @Query("select p.statDt as statDt, count(p.roadId) as dataCnt from TbRoadStat15m 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.roadId as id, nvl(c.cmtrGradCd, 'LTC0') as cmtrGradCd, b.sped as sped " +
            "  from TbRoad a, TbRoadStat15m b, TbSectGradStup c " +
            " where a.delYn  = 'N' " +
            "   and a.roadId = b.roadId " +
            "   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);
}
