package com.its.api.its.repository.cctv;

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.cctv.TbCctvSttsHs;
import com.its.api.its.model.entity.cctv.TbCctvSttsHsKey;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TbCctvSttsHsRepository extends JpaRepository<TbCctvSttsHs, TbCctvSttsHsKey>, JpaSpecificationExecutor<TbCctvSttsHs> {

    @Query("select p from TbCctvSttsHs p inner join fetch p.cctv where p.crtnDt between :fromDt and :toDt and p.cctvMngmNmbr in :ids")
    List<TbCctvSttsHs> findAllByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<Long> ids);

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

    @Query("select p from TbCctvSttsHs p inner join fetch p.cctv " +
           " where p.crtnDt between :fromDt and :toDt " +
           "   and p.cmncSttsCd <> 'CMS0' " +
           "   and p.cctvMngmNmbr in :ids")
    List<TbCctvSttsHs> 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(CMNC_STTS_CD, 'CMS0', 1, 0) AS CMS0CNT, " +
                    "               DECODE(CMNC_STTS_CD, 'CMS0', 0, 1) AS CMS1CNT  " +
                    "          FROM TB_CCTV_STTS_HS " +
                    "         WHERE CRTN_DT BETWEEN :fromDt AND :toDt" +
                    "       )", nativeQuery = true)
    ReportFcltSummaryInf findAllReportSummary(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query("select p from TbCctvSttsHs p inner join fetch p.cctv " +
            " where p.crtnDt between :fromDt and :toDt " +
            "   and p.cmncSttsCd = 'CMS1' ")
    List<TbCctvSttsHs> findAllReportError(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query(value =
            "SELECT A.CCTV_MNGM_NMBR AS fcltNmbr, A.CCTV_CTLR_ID AS fcltId, A.ISTL_LCTN_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_CCTV_CTLR A, " +
            "             (SELECT CCTV_MNGM_NMBR, " +
            "                     SUM(DECODE(CMNC_STTS_CD, 'CMS1', 1, 0)) AS CNT_CMS1, " +
            "                     SUM(DECODE(CMNC_STTS_CD, 'CMS2', 1, 0)) AS CNT_CMS2 " +
            "              FROM TB_CCTV_STTS_HS " +
            "              WHERE CRTN_DT BETWEEN :fromDt AND :toDt " +
            "                AND CMNC_STTS_CD IN ('CMS1', 'CMS2') " +
            "              GROUP BY CCTV_MNGM_NMBR) B " +
            "        WHERE A.CCTV_MNGM_NMBR = B.CCTV_MNGM_NMBR(+)"
            , nativeQuery = true)
    List<FcltSttsStatErrorInf> findAllStatisticsCommErrByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

}
