package com.its.api.its.repository.vms;

import com.its.api.its.model.dto.common.SttsHsErrCntInf;
import com.its.api.its.model.dto.report.ReportFcltSummaryInf;
import com.its.api.its.model.dto.statistics.FcltSttsStatErrorInf;
import com.its.api.its.model.entity.vms.TbVmsSttsHs;
import com.its.api.its.model.entity.vms.TbVmsSttsHsKey;
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 TbVmsSttsHsRepository extends JpaRepository<TbVmsSttsHs, TbVmsSttsHsKey>, JpaSpecificationExecutor<TbVmsSttsHs> {

    @Query("select p from TbVmsSttsHs p inner join fetch p.vms where p.rgstDt between :fromDt and :toDt and p.vmsCtlrNmbr in :ids")
    List<TbVmsSttsHs> findAllByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<Long> ids);

    @Query("select p.vmsCtlrNmbr as nmbr, count(p.vmsCtlrNmbr) as errCnt " +
            "  from TbVmsSttsHs p " +
            " where p.rgstDt     >= :fromDt " +
            "   and p.connSttsCd <> 'CMS0' " +
            " group by p.vmsCtlrNmbr")
    List<SttsHsErrCntInf> findAllCommErrorCount(@Param("fromDt") String fromDt);

    @Query("select p from TbVmsSttsHs p inner join fetch p.vms " +
           " where p.rgstDt between :fromDt and :toDt " +
           "   and (p.connSttsCd <> 'CMS0' or p.cboxdoorOpenSttsCd <> 'CDS0' or p.cboxTmpr > 40)" +
           "   and p.vmsCtlrNmbr in :ids")
    List<TbVmsSttsHs> findAllFailByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<Long> ids);

    @Query(value =
            "SELECT NVL(SUM(CMS0CNT), 0) AS cms0Cnt, NVL(SUM(CMS1CNT), 0) AS cms1Cnt " +
            "  FROM (SELECT DECODE(CONN_STTS_CD, 'CMS0', 1, 0) AS CMS0CNT, " +
            "               DECODE(CONN_STTS_CD, 'CMS0', 0, 1) AS CMS1CNT  " +
            "          FROM TB_VMS_STTS_HS " +
            "         WHERE RGST_DT BETWEEN :fromDt AND :toDt" +
            "       )", nativeQuery = true)
    ReportFcltSummaryInf findAllReportSummary(@Param("fromDt") String fromDt, @Param("toDt") String toDt);


    @Query("select p from TbVmsSttsHs p inner join fetch p.vms " +
            " where p.rgstDt between :fromDt and :toDt " +
            "   and p.connSttsCd = 'CMS1' ")
    List<TbVmsSttsHs> findAllReportError(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query(value =
            "SELECT A.VMS_CTLR_NMBR AS fcltNmbr, A.VMS_CTLR_ID AS fcltId, A.VMS_NM AS fcltNm, " +
            "               NVL(B.CNT_CMS1, 0) + NVL(B.CNT_CMS2, 0) AS totCnt, " +
            "               NVL(B.CNT_CMS1, 0) AS cms1Cnt, NVL(B.CNT_CMS2, 0) AS cms2Cnt " +
            "        FROM TB_VMS_CTLR A, " +
            "             (SELECT VMS_CTLR_NMBR, " +
            "                     SUM(DECODE(CONN_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1, " +
            "                     SUM(DECODE(CONN_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2 " +
            "              FROM TB_VMS_STTS_HS " +
            "              WHERE RGST_DT BETWEEN :fromDt AND :toDt " +
            "                AND CONN_STTS_CD IN ('CMS1', 'CMS2') " +
            "              GROUP BY VMS_CTLR_NMBR) B " +
            "        WHERE A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR(+)"
            , nativeQuery = true)
    List<FcltSttsStatErrorInf> findAllStatisticsCommErrByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);


}
