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

import com.its.op.entity.its.cctv.TbCctvCtlr;
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;
import java.util.Optional;

@Repository
public interface TbCctvCtlrRepository extends JpaRepository<TbCctvCtlr, Long>, JpaSpecificationExecutor<TbCctvCtlr> {

    @Query("select p from TbCctvCtlr p left outer join fetch p.state state")
    List<TbCctvCtlr> findAll();

    @Query("select p from TbCctvCtlr p left outer join fetch p.state state where p.cctvCtlrNmbr = :cctvCtlrNmbr")
    Optional<TbCctvCtlr> findOne(@Param("cctvCtlrNmbr") Long cctvCtlrNmbr);

    @Query("select p from TbCctvCtlr p left outer join fetch p.state state where p.delYn = 'N'")
    List<TbCctvCtlr> findAllList();

    @Query("select count(p.cctvCtlrNmbr) as total from TbCctvCtlr p where p.delYn = 'N'")
    Integer findAllListCount();

    @Query(value = "SELECT NVL(MAX(CCTV_CTLR_NMBR), 0) + 1 AS NEWID FROM TB_CCTV_CTLR", nativeQuery = true)
    Long getNewNmbr();

}
