package com.huimv.eartag.dao.repo;

import com.huimv.eartag.dao.entity.EartagDeviceOnlineEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface EartagDeviceOnlineRepo extends JpaRepository<EartagDeviceOnlineEntity, Integer>, JpaSpecificationExecutor<EartagDeviceOnlineEntity> {

    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_online WHERE device_code=?1 AND DATE_FORMAT(add_date,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d') limit 1")
    EartagDeviceOnlineEntity getDeviceOnline(String deviceCode, String todayText);

    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_online WHERE farm_id=?1 AND DATE_FORMAT(add_date,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY add_date DESC")
    List<EartagDeviceOnlineEntity> getDeviceOnlineByFarmIdAndCreateDate(String farmId, String todayDate);

    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_online WHERE device_code=?1 AND add_date BETWEEN DATE_FORMAT(?2,'%Y-%m-%d') AND DATE_FORMAT(?3,'%Y-%m-%d') ORDER BY add_date ASC")
    List<EartagDeviceOnlineEntity> getDeviceOnlineByDeviceCode(String deviceCode, String pastDate, String todayDate);
}