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

import com.its.api.its.model.dto.link.TbLinkTrafClctHsAnalInf;
import com.its.api.its.model.dto.link.TbLinkTrafClctHsHistInf;
import com.its.api.its.model.entity.link.TbLinkTrafClctHs;
import com.its.api.its.model.entity.link.TbLinkTrafClctHsKey;
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 TbLinkTrafClctHsRepository extends JpaRepository<TbLinkTrafClctHs, TbLinkTrafClctHsKey>, JpaSpecificationExecutor<TbLinkTrafClctHs> {

    /**
    SELECT *
FROM (
   SELECT
       A.LINK_ID AS linkId,
       A.PRCN_DT as prcnDt,
       CLCT_SYST_CD, SPED
   FROM TB_LINK_TRAF_CLCT_HS A
   WHERE
       A.PRCN_DT BETWEEN '20210112152000' AND '20221112152000'
   AND A.LINK_ID = '2280212200'
)
PIVOT(
   max(SPED)
   FOR CLCT_SYST_CD IN (
       'OPER01' AS "OPER01",
       'UTIS01' AS "UTIS01",
       'UTIS02' AS "UTIS02",
       'VDS01'  AS "VDS01",
       'MOCT01' AS "MOCT01")
);
*/

    @Query( "SELECT p.linkId AS linkId, p.prcnDt AS prcnDt, " +
            "      nvl(max(case when p.clctSystCd = 'OPER01' then p.sped end), 0) AS oper01, " +
            "      nvl(max(case when p.clctSystCd = 'UTIS01' then p.sped end), 0) AS utis01, " +
            "      nvl(max(case when p.clctSystCd = 'UTIS02' then p.sped end), 0) AS utis02, " +
            "      nvl(max(case when p.clctSystCd = 'VDS01'  then p.sped end), 0) AS vds01,  " +
            "      nvl(max(case when p.clctSystCd = 'MOCT01' then p.sped end), 0) AS moct01  " +
            "  FROM TbLinkTrafClctHs p " +
            " WHERE p.prcnDt BETWEEN :fromDt AND :toDt " +
            "   AND p.linkId = :linkId " +
            " GROUP BY p.linkId, p.prcnDt")
    List<TbLinkTrafClctHsHistInf> findAllHistByDateRange(@Param("linkId") Long linkId, @Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query( "SELECT max(l.roadName) as roadName, max(fn.nodeName) as fromNodeInfo, max(tn.nodeName) as toNode, " +
            "      p.linkId AS linkId, p.prcnDt AS prcnDt, " +
            "      nvl(max(case when p.clctSystCd = 'OPER01' then p.sped end), 0) AS oper01, " +
            "      nvl(max(case when p.clctSystCd = 'UTIS01' then p.sped end), 0) AS utis01, " +
            "      nvl(max(case when p.clctSystCd = 'UTIS02' then p.sped end), 0) AS utis02, " +
            "      nvl(max(case when p.clctSystCd = 'VDS01'  then p.sped end), 0) AS vds01,  " +
            "      nvl(max(case when p.clctSystCd = 'MOCT01' then p.sped end), 0) AS moct01  " +
            "  FROM TbLinkTrafClctHs p inner join p.link l inner join l.fromNode fn inner join l.toNode tn " +
            " WHERE p.prcnDt BETWEEN :fromDt AND :toDt " +
            "   AND p.linkId in :ids " +
            " GROUP BY p.linkId, p.prcnDt")
    List<TbLinkTrafClctHsAnalInf> findAllAnalByDateRangeByIds(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<Long> ids);

    @Query("SELECT max(l.roadName) as roadName, max(fn.nodeName) as fromNodeInfo, max(tn.nodeName) as toNode, " +
            "      p.linkId AS linkId, p.prcnDt AS prcnDt, " +
            "      nvl(max(case when p.clctSystCd = 'OPER01' then p.sped end), 0) AS oper01, " +
            "      nvl(max(case when p.clctSystCd = 'UTIS01' then p.sped end), 0) AS utis01, " +
            "      nvl(max(case when p.clctSystCd = 'UTIS02' then p.sped end), 0) AS utis02, " +
            "      nvl(max(case when p.clctSystCd = 'VDS01'  then p.sped end), 0) AS vds01,  " +
            "      nvl(max(case when p.clctSystCd = 'MOCT01' then p.sped end), 0) AS moct01  " +
            "  FROM TbLinkTrafClctHs p inner join p.link l inner join l.fromNode fn inner join l.toNode tn " +
            " WHERE p.prcnDt BETWEEN :fromDt AND :toDt " +
            " GROUP BY p.linkId, p.prcnDt")
    List<TbLinkTrafClctHsAnalInf> findAllAnalByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);
}
