package com.its.api.its.repository.vms;

import com.its.api.its.model.entity.vms.TbVmsLinkIfsc;
import com.its.api.its.model.entity.vms.TbVmsLinkIfscKey;
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 TbVmsLinkIfscRepository extends JpaRepository<TbVmsLinkIfsc, TbVmsLinkIfscKey>, JpaSpecificationExecutor<TbVmsLinkIfsc> {

    @Query("select p from TbVmsLinkIfsc p inner join fetch p.ifsc")
    List<TbVmsLinkIfsc> findDataAll();

    @Query("select p from TbVmsLinkIfsc p inner join fetch p.ifsc where p.vmsIfscId = :vmsIfscId")
    List<TbVmsLinkIfsc> findDataById(@Param("vmsIfscId") Long vmsIfscId);

    @Query("select count(p.vmsIfscId) from TbVmsLinkIfsc p where p.vmsIfscId = :vmsIfscId")
    Integer findUsageCountByVmsIfscId(@Param("vmsIfscId") Long vmsIfscId);

    @Modifying
    @Query("delete from TbVmsLinkIfsc p where p.vmsIfscId = :vmsIfscId")
    void deleteDataById(@Param("vmsIfscId") Long vmsIfscId);

    @Query(value = "select p.vmsIfscId from TbVmsLinkIfsc p where p.ifscId in :ids")
    List<Long> findVmsIfscIdByIfscId(@Param("ids") List<Long> ids);

}
