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

import com.its.op.dto.its.common.MonitoringListInf;
import com.its.op.entity.its.cctv.TbCctvMonitoring;
import com.its.op.entity.its.cctv.TbCctvMonitoringKey;
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 TbCctvMonitoringRepository extends JpaRepository<TbCctvMonitoring, TbCctvMonitoringKey>, JpaSpecificationExecutor<TbCctvMonitoring> {

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

    @Query("select p from TbCctvMonitoring p inner join fetch p.cctv cctv where p.monitoringType = :monitoringType")
    List<TbCctvMonitoring> findAllMonitoring(@Param("monitoringType") Integer monitoringType);

    @Query("select p from TbCctvMonitoring p inner join fetch p.cctv cctv where p.monitoringType = :monitoringType and p.monitoringNm = :monitoringNm")
    List<TbCctvMonitoring> findByName(@Param("monitoringType") Integer monitoringType, @Param("monitoringNm") String monitoringNm);

    @Transactional
    @Modifying
    @Query("delete from TbCctvMonitoring p where p.monitoringType = :monitoringType and p.monitoringNm = :monitoringNm")
    void deleteByName(@Param("monitoringType") Integer monitoringType, @Param("monitoringNm") String monitoringNm);

    /*@Transactional
    @Modifying
    @Query(value = "insert into TB_CCTV_MONITORING values(:mtype, :name, :seq, :nmbr, :mode)", nativeQuery = true)
    void insertValue(@Param("mtype") Integer mtype, @Param("name") String name, @Param("seq") Integer seq, @Param("nmbr") Long nmbr, @Param("mode") Integer mode);*/

}
