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

import com.its.op.dto.its.facility.FcltSttsErrorInf;
import com.its.op.entity.its.scrs.TbScIxrCmraSttsHs;
import com.its.op.entity.its.scrs.TbScIxrCmraSttsHsKey;
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 TbScIxrCmraSttsHsRepository extends JpaRepository<TbScIxrCmraSttsHs, TbScIxrCmraSttsHsKey>, JpaSpecificationExecutor<TbScIxrCmraSttsHs> {

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

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

    @Query(value =
            "SELECT 'CCAM' AS fcltType, A.CMRA_ID AS fcltNmbr, A.CMRA_ID AS fcltId, A.ISTL_LCTN 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_SC_IXR_CMRA_MNGM A,                                                                                           " +
            "       (SELECT IXR_ID, CMRA_ID, DECODE(CMRA_STTS, 0, 'CMS0', 'CMS1') AS CONN_STTS_CD,                                   " +
            "               'CDS0' AS DOOR_STTS_CD, 0 AS CBOX_TMPR, CLCT_DT AS CRTN_DT                                               " +
            "          FROM TB_SC_IXR_CMRA_STTS                                                                                      " +
            "         WHERE CLCT_DT BETWEEN :fromDt AND :toDt                                                                        " +
            "           AND (CMRA_STTS <> 1)                                                                                         " +
            "       ) B                                                                                                              " +
            " WHERE A.IXR_ID = B.IXR_ID AND A.CMRA_ID = B.CMRA_ID                                                                    "
            , nativeQuery = true)
    List<FcltSttsErrorInf> findAllSttsErrorByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query("select p from TbScIxrCmraSttsHs p inner join fetch p.cmra cmra where p.clctDt between :fromDt and :toDt and p.cmraId in :ids")
    List<TbScIxrCmraSttsHs> findAllByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<String> ids);

    @Query("select p from TbScIxrCmraSttsHs p inner join fetch p.cmra cmra " +
            " where p.clctDt between :fromDt and :toDt " +
            "   and p.cmraStts <> 1 " +
            "   and p.cmraId in :ids")
    List<TbScIxrCmraSttsHs> findAllFailByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<String> ids);
}
