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

import com.its.op.entity.its.vds.TbVdsCtlr;
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 TbVdsCtlrRepository extends JpaRepository<TbVdsCtlr, Long>, JpaSpecificationExecutor<TbVdsCtlr> {

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

    @Query("select p from TbVdsCtlr p left outer join fetch p.state state where p.vdsCtlrNmbr = :id")
    Optional<TbVdsCtlr> findOne(@Param("id") String id);

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

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

    @Query(value = "SELECT NVL(MAX(VDS_CTLR_NMBR), 1000) + 1 AS NEWID  FROM TB_VDS_CTLR", nativeQuery = true)
    Long getNewNmbr();
}
