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

import com.its.op.entity.its.vms.TbVmsRltnIfsc;
import com.its.op.entity.its.vms.TbVmsRltnIfscKey;
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 TbVmsRltnIfscRepository extends JpaRepository<TbVmsRltnIfsc, TbVmsRltnIfscKey>, JpaSpecificationExecutor<TbVmsRltnIfsc> {

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

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

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

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

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

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

}
