package cn.lihongjie.coal.emDevice.repository;

import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.emDevice.entity.EmDeviceEntity;

import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface EmDeviceRepository extends BaseRepository<EmDeviceEntity> {
    @Query(
            value =
                    "select d.id\n"
                            + "from t_em_device d\n"
                            + "where (select case when max(time) is null then now() - interval '10000 minute' else max(time)  end  from t_em_device_data data where data.device_id = d.id) + (d.\"interval\" * interval '1 minute') <= :now",
            nativeQuery = true)
    List<String> findNeedToFetchDevice(LocalDateTime now);
}
