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

import com.its.api.its.model.entity.vms.TbVmsIfscRltn;
import com.its.api.its.model.entity.vms.TbVmsIfscRltnKey;
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 TbVmsIfscRltnRepository extends JpaRepository<TbVmsIfscRltn, TbVmsIfscRltnKey>, JpaSpecificationExecutor<TbVmsIfscRltn> {

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

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

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

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

    @Query(value = "select p.vmsCtlrNmbr from TbVmsIfscRltn p where p.vmsIfscId in :ids")
    List<Long> findVmsByVmsIfscId(@Param("ids") List<Long> ids);

}
