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

import com.its.op.entity.its.facility.TbFcltSubjPart;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
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 TbFcltSubjPartRepository extends JpaRepository<TbFcltSubjPart, Long>, JpaSpecificationExecutor<TbFcltSubjPart> {

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

    @Query("select p from TbFcltSubjPart p where p.delYn = 'N'")
    List<TbFcltSubjPart> findAllList();

    @Query("select p from TbFcltSubjPart p where p.subjNmbr = :subjNmbr")
    List<TbFcltSubjPart> findAllBySubject(@Param("subjNmbr") Long subjNmbr);

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

    @Query(value = "SELECT NVL(MAX(PART_ID), 0) + 1 AS NEWID FROM TB_FCLT_SUBJ_PART WHERE SUBJ_NMBR = :subjNmbr", nativeQuery = true)
    Long getNewPartId(@Param("subjNmbr") Long subjNmbr);

    @Modifying
    @Query(value = "UPDATE TB_FCLT_SUBJ_PART SET VALID_CNT = :validCnt WHERE PART_NMBR = :partNmbr", nativeQuery = true)
    Integer updateValidCount(@Param("partNmbr") Long partNmbr, @Param("validCnt") Integer validCnt);

}
