package com.huimv.environ.env.dao.repo;

import com.huimv.environ.env.dao.entity.BaseWarningInfoEntity;
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 BaseWarningInfoRepo extends JpaRepository<BaseWarningInfoEntity, Integer>, JpaSpecificationExecutor<BaseWarningInfoEntity> {

    //查询今天的N条记录列表
    @Query(nativeQuery = true,value ="SELECT level_name,DATE_FORMAT(warning_time,'%T') warning_time,msg FROM base_warning_info where farm_id=?1 AND DATE_FORMAT(warning_time,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY warning_time DESC LIMIT ?3")
    List<Object[]> findTodayAlarm(Integer farmId, String today, Integer quantity);

    //查询最近7天警报数据
    @Query(nativeQuery = true,value ="SELECT * FROM base_warning_info where farm_id=?1 AND DATE_FORMAT(warning_time,'%Y-%m-%d')>=DATE_FORMAT(?2,'%Y-%m-%d') AND DATE_FORMAT(warning_time,'%Y-%m-%d')<=DATE_FORMAT(?3,'%Y-%m-%d')")
    List<BaseWarningInfoEntity> findLastSevenDaysAlarm(Integer farmId, String startDate, String endDate);

    //查询今天所有报警记录
    @Query(nativeQuery = true,value ="SELECT * FROM base_warning_info where farm_id=?1 AND DATE_FORMAT(warning_time,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY warning_time DESC")
    List<BaseWarningInfoEntity> findTodayAllAlarm(Integer farmId, String todayText);
}