package com.its.api.its.repository.link;

import com.its.api.its.model.dto.analysis.TbTraf15mStatCountInf;
import com.its.api.its.model.dto.analysis.TbTraf15mStatListInf;
import com.its.api.its.model.entity.link.TbLink15mStat;
import com.its.api.its.model.entity.link.TbLink15mStatKey;
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<TbLink15mStat, TbLink15mStatKey>, JpaSpecificationExecutor<TbLink15mStat> {

    @Query("select p.statDt as statDt, count(p.linkId) as dataCnt from TbLink15mStat p where p.statDt between :fromDt and :toDt group by p.statDt")
    List<TbTraf15mStatCountInf> 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, TbLink15mStat b, TbCmtrGradClsf 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<TbTraf15mStatListInf> findAllDataByDateRange(@Param("statDt") String statDt);
}
