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

import com.its.op.dto.its.common.SttsHsErrCntInf;
import com.its.op.dto.its.facility.FcltSttsErrorInf;
import com.its.op.entity.its.wcam.TbWcamCtlrSttsHs;
import com.its.op.entity.its.wcam.TbWcamCtrlSttsHsKey;
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 TbWcamCtlrSttsHsRepository extends JpaRepository<TbWcamCtlrSttsHs, TbWcamCtrlSttsHsKey>, JpaSpecificationExecutor<TbWcamCtlrSttsHs> {

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

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

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

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

    @Query("select p from TbWcamCtlrSttsHs p inner join fetch p.wcam " +
            " where p.crtnDt between :fromDt and :toDt " +
            "   and (p.cmncSttsCd <> 'CMS0')" +
            "   and p.wcamCtlrNmbr in :ids")
    List<TbWcamCtlrSttsHs> findAllFailByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<Long> ids);


    @Query(value =
            "SELECT 'WCAM' AS fcltType, A.WCAM_CTLR_NMBR AS fcltNmbr, A.WCAM_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_WCAM_CTLR A,                                                                                                  " +
            "       (SELECT WCAM_CTLR_NMBR, CMNC_STTS_CD AS CONN_STTS_CD,                                                            " +
            "               'CDS0' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CRTN_DT                                                          " +
            "          FROM TB_WCAM_CTLR_STTS_HS                                                                                     " +
            "         WHERE CRTN_DT BETWEEN :fromDt AND :toDt                                                                        " +
            "           AND (CMNC_STTS_CD <> 'CMS0')                                                                                 " +
            "       ) B                                                                                                              " +
            " WHERE A.WCAM_CTLR_NMBR = B.WCAM_CTLR_NMBR                                                                              "
            , nativeQuery = true)
    List<FcltSttsErrorInf> findAllSttsErrorByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

}
