package com.huimv.env.dao.repo;

import com.huimv.env.dao.entity.SjHkAqEntity;
import com.huimv.env.dao.entity.SjHkSdEntity;
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 SjHkSdEntityRepository extends JpaRepository<SjHkSdEntity, Integer>, JpaSpecificationExecutor<SjHkSdEntity> {

    @Query(nativeQuery = true , value = "SELECT ID,LQID,CGQID,DQSD,CJSJ,SCSJ FROM sj_hk_sd WHERE id IN( SELECT MAX(id) FROM sj_hk_sd WHERE lqid IN(?1) AND DATE_FORMAT(CJSJ,'%Y-%m-%d')=?2  GROUP BY lqid)")
    List<SjHkSdEntity> getLastSd(List lqidList,String date);

    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqsd,CJSJ,SCSJ FROM sj_hk_sd WHERE lqid=?1 AND DATE_FORMAT(CJSJ,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY cjsj DESC LIMIT 0,1")
    SjHkSdEntity getLastSd(Integer lqid, String todayDate);

    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqsd,SCSJ,CJSJ FROM sj_hk_sd WHERE lqid=?1 AND DATE_FORMAT(CJSJ,'%Y-%m-%d %H:%i:%s')>=DATE_FORMAT(?2,'%Y-%m-%d %H:%i:%s') ORDER BY cjsj ASC ")
    List<SjHkSdEntity> getHumidityOnToday(Integer lqid, String beforeDateTime);

    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqsd,SCSJ,CJSJ FROM sj_hk_sd WHERE lqid=?1 ORDER BY cjsj DESC limit 10")
    List<SjHkSdEntity> getHumidity(Integer lqid, String beforeDateTime);
}