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

import com.its.op.dto.its.analysis.TbTrafStat15mCountInf;
import com.its.op.dto.its.analysis.TbTrafStat15mListInf;
import com.its.op.entity.its.ifsc.TbIfscStat15m;
import com.its.op.entity.its.ifsc.TbIfscStat15mKey;
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 TbIfsc15mStatRepository extends JpaRepository<TbIfscStat15m, TbIfscStat15mKey>, JpaSpecificationExecutor<TbIfscStat15m> {

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

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