package com.huimv.eartag.dao.repo;

import cn.hutool.core.date.DateTime;
import com.huimv.eartag.dao.entity.EartagDataEntity;
import com.huimv.eartag.entity.EartagData;
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 EartagDataRepo extends JpaRepository<EartagDataEntity, Integer>, JpaSpecificationExecutor<EartagDataEntity> {
    //--查询最新到耳标记录
    @Query(nativeQuery = true,value = "SELECT * FROM eartag_data WHERE device=?1 ORDER BY id DESC LIMIT 1")
    EartagDataEntity getLastEartagData(String deviceCode);

    //
    @Query(nativeQuery = true,value = "SELECT * FROM eartag_data WHERE device=?1 AND earmark=?2 AND DATE_FORMAT(add_time,'%Y-%m-%d')=DATE_FORMAT(?3,'%Y-%m-%d') ORDER BY id DESC")
    List<EartagDataEntity> getEartagFlowByDeviceCodeAndEarmarkAndToday(String deviceCode, String earmark, String todayDateText);

    //
    @Query(nativeQuery = true,value = "SELECT * FROM eartag_data WHERE earmark=?1 AND DATE_FORMAT(add_time,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY id DESC")
    List<EartagDataEntity> getEartagFlowByEarmarkAndToday(String earmark, String todayDateText);


    @Query(nativeQuery = true,value = "select * from eartag_data where ask_time >= ?1")
    List<EartagData> getAllTem(DateTime dateTime);
}