package com.huimv.env.dao.repo;

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

    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqaq,CJSJ,SCSJ FROM sj_hk_aq WHERE id IN(SELECT MAX(id) FROM sj_hk_aq WHERE lqid IN(?1) AND DATE_FORMAT(CJSJ,'%Y-%m-%d')=?2 GROUP BY lqid)")
    List<SjHkAqEntity> getLastAq(List<Integer> lqids,String todayDate);

//    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqaq,CJSJ,SCSJ FROM sj_hk_aq WHERE lqid=?1 AND function(\"date_format\",CJSJ,\"%Y-%m-%d\")=?2 ORDER BY cjsj DESC LIMIT 0,1)")
    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqaq,CJSJ,SCSJ FROM sj_hk_aq WHERE lqid=?1 AND date_format(CJSJ,'%Y-%m-%d')= DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY cjsj DESC LIMIT 0,1")
    SjHkAqEntity getLastAq(Integer lqid,String todayDate);

    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqaq,SCSJ,CJSJ FROM sj_hk_aq 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<SjHkAqEntity> getAmmoniaOnToday(Integer lqid, String beforeDateTime);

    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqaq,SCSJ,CJSJ FROM sj_hk_aq WHERE lqid=?1 ORDER BY cjsj DESC LIMIT 10")
    List<SjHkAqEntity> getAmmonia(Integer lqid, String beforeDateTime);
}