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

import com.its.op.dto.its.common.MonitoringListInf;
import com.its.op.entity.its.vms.TbVmsMonitoring;
import com.its.op.entity.its.vms.TbVmsMonitoringKey;
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;

@Repository
public interface TbVmsMonitoringRepository extends JpaRepository<TbVmsMonitoring, TbVmsMonitoringKey>, JpaSpecificationExecutor<TbVmsMonitoring> {

    @Query("select p.monitoringNm as name, count(p.monitoringNm) as count " +
            "  from TbVmsMonitoring p " +
            " where p.monitoringType = :monitoringType " +
            " group by p.monitoringNm")
    List<MonitoringListInf> findAllMonitoringList(@Param("monitoringType") Integer monitoringType);

    @Query("select p from TbVmsMonitoring p inner join fetch p.vms vms inner join fetch vms.type tp inner join fetch vms.state where p.monitoringType = :monitoringType")
    List<TbVmsMonitoring> findAllMonitoring(@Param("monitoringType") Integer monitoringType);

    @Query("select p from TbVmsMonitoring p inner join fetch p.vms vms inner join fetch vms.type tp inner join fetch vms.state")
    List<TbVmsMonitoring> findAll();

    @Query("select p from TbVmsMonitoring p inner join fetch p.vms vms inner join fetch vms.type tp inner join fetch vms.state " +
           " where p.monitoringType = :monitoringType and p.monitoringNm = :monitoringNm")
    List<TbVmsMonitoring> findByName(@Param("monitoringType") Integer monitoringType, @Param("monitoringNm") String monitoringNm);

    @Transactional
    @Modifying // select 문이 아님을 나타낸다
    @Query("delete from TbVmsMonitoring p where p.monitoringType = :monitoringType and p.monitoringNm = :monitoringNm")
    void deleteByName(@Param("monitoringType") Integer monitoringType, @Param("monitoringNm") String monitoringNm);

}
