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

import com.its.op.entity.its.link.TbLinkParaStup;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Repository
public interface TbLinkParaStupRepository extends JpaRepository<TbLinkParaStup, Long>, JpaSpecificationExecutor<TbLinkParaStup> {

    @Query("select p from TbLinkParaStup p inner join fetch p.detl m")
    List<TbLinkParaStup> findAll();

    @Query("select p from TbLinkParaStup p inner join fetch p.detl m where p.paraId = :paramId")
    Optional<TbLinkParaStup> findById(@Param("paramId") Long paramId);

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

    @Query("select count(p.paraId) from TbLink p where p.paraId = :paraId")
    Integer findUsageId(@Param("paraId") Long paraId);

    @Transactional
    @Modifying
    @Query("delete from TbLinkParaStup p where p.paraId = :paraId")
    void deleteDataById(@Param("paraId") Long paraId);

}
