package com.its.api.its.repository.vds;

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.vds.TbVdsCtlrSttsHs;
import com.its.api.its.model.entity.vds.TbVdsCtlrSttsHsKey;
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 TbVdsCtlrSttsHsRepository extends JpaRepository<TbVdsCtlrSttsHs, TbVdsCtlrSttsHsKey>, JpaSpecificationExecutor<TbVdsCtlrSttsHs> {

    @Query("select p from TbVdsCtlrSttsHs p inner join fetch p.vds where p.ocrrDt between :fromDt and :toDt and p.ctlrMngmNmbr in :ids")
    List<TbVdsCtlrSttsHs> findAllByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<String> ids);

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

    @Query("select p from TbVdsCtlrSttsHs p inner join fetch p.vds " +
           " where p.ocrrDt between :fromDt and :toDt " +
           "   and (p.cmncSttsCd <> 'CMS0' or p.cboxDoorSttsCd <> 'CDS0' or p.cboxTmpr > 40)" +
           "   and p.ctlrMngmNmbr in :ids")
    List<TbVdsCtlrSttsHs> findAllFailByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<String> 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_VDS_CTLR_STTS_RAW " +
                    "         WHERE OCRR_DT BETWEEN :fromDt AND :toDt" +
                    "       )", nativeQuery = true)
    ReportFcltSummaryInf findAllReportSummary(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

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

    @Query(value =
            "SELECT A.CTLR_MNGM_NMBR AS fcltNmbr, A.VDS_CTLR_ID AS fcltId, A.LCTN 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_VDS_CTLR A, " +
            "             (SELECT CTLR_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_VDS_CTLR_STTS_RAW " +
            "              WHERE OCRR_DT BETWEEN :fromDt AND :toDt " +
            "                AND CMNC_STTS_CD IN ('CMS1', 'CMS2') " +
            "              GROUP BY CTLR_MNGM_NMBR) B " +
            "        WHERE A.CTLR_MNGM_NMBR = B.CTLR_MNGM_NMBR(+)"
            , nativeQuery = true)
    List<FcltSttsStatErrorInf> findAllStatisticsCommErrByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

}
