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

import com.its.op.dto.its.common.SttsHsErrCntInf;
import com.its.op.dto.its.facility.FcltSttsErrorInf;
import com.its.op.dto.its.report.ReportFcltSummaryInf;
import com.its.op.dto.its.statistics.FcltSttsStatErrorInf;
import com.its.op.entity.its.rse.TbRseCtlrSttsHs;
import com.its.op.entity.its.rse.TbRseCtlrSttsHsKey;
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 TbRseCtlrSttsHsRepository extends JpaRepository<TbRseCtlrSttsHs, TbRseCtlrSttsHsKey>, JpaSpecificationExecutor<TbRseCtlrSttsHs> {

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

    @Query("select p from TbRseCtlrSttsHs p")
    List<TbRseCtlrSttsHs> findAllList();

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

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

    @Query("select p from TbRseCtlrSttsHs p inner join fetch p.rse " +
            " where p.crtnDt between :fromDt and :toDt " +
            "   and (p.cmncSttsCd <> 'CMS0' or p.cboxDoorSttsCd <> 'CDS0' or p.cboxTmpr > 40)" +
            "   and p.rseCtlrNmbr in :ids")
    List<TbRseCtlrSttsHs> 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_RSE_CTLR_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 TbRseCtlrSttsHs p inner join fetch p.rse rse " +
            " where p.crtnDt between :fromDt and :toDt " +
            "   and p.cmncSttsCd = 'CMS1' ")
    List<TbRseCtlrSttsHs> findAllReportError(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query(value =
            "SELECT A.RSE_CTLR_NMBR AS fcltNmbr, A.RSE_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_RSE_CTLR A, " +
            "             (SELECT RSE_CTLR_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_RSE_CTLR_STTS_HS " +
            "              WHERE CRTN_DT BETWEEN :fromDt AND :toDt " +
            "                AND CMNC_STTS_CD IN ('CMS1', 'CMS2') " +
            "              GROUP BY RSE_CTLR_NMBR) B " +
            "        WHERE A.RSE_CTLR_NMBR = B.RSE_CTLR_NMBR(+)"
            , nativeQuery = true)
    List<FcltSttsStatErrorInf> findAllStatisticsCommErrByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query(value =
            "SELECT 'DSRC' AS fcltType, A.RSE_CTLR_NMBR AS fcltNmbr, A.RSE_CTLR_ID AS fcltId, A.ISTL_LCTN_NM AS fcltNm,              " +
            "       B.CONN_STTS_CD AS commSttsCd, B.DOOR_STTS_CD AS doorSttsCd, NVL(B.CBOX_TMPR, 0) AS cboxTmpr, B.CRTN_DT AS crtnDt " +
            "  FROM TB_RSE_CTLR A,                                                                                                   " +
            "       (SELECT RSE_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                             " +
            "               CBOX_DOOR_STTS_CD AS DOOR_STTS_CD, CBOX_TMPR, CRTN_DT                                                    " +
            "          FROM TB_RSE_CTLR_STTS_HS                                                                                      " +
            "         WHERE CRTN_DT BETWEEN :fromDt AND :toDt                                                                        " +
            "           AND (CMNC_STTS_CD      <> 'CMS0' OR                                                                          " +
            "                CBOX_DOOR_STTS_CD <> 'CDS0' OR                                                                          " +
            "                CBOX_TMPR > 40)                                                                                         " +
            "       ) B                                                                                                              " +
            " WHERE A.RSE_CTLR_NMBR = B.RSE_CTLR_NMBR                                                                                "
            , nativeQuery = true)
    List<FcltSttsErrorInf> findAllSttsErrorByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

}
