package com.its.api.its.repository.ifsc;

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.ifsc.TbIfsc15mStat;
import com.its.api.its.model.entity.ifsc.TbIfsc15mStatKey;
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<TbIfsc15mStat, TbIfsc15mStatKey>, JpaSpecificationExecutor<TbIfsc15mStat> {

    @Query("select p.statDt as statDt, count(p.ifscId) as dataCnt from TbIfsc15mStat 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.ifscId as id, nvl(c.cmtrGradCd, 'LTC0') as cmtrGradCd, b.sped as sped " +
            "  from TbIfsc a, TbIfsc15mStat b, TbCmtrGradClsf 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<TbTraf15mStatListInf> findAllDataByDateRange(@Param("statDt") String statDt);
}
