package com.huimv.env.dao.repo;

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

    @Query(nativeQuery = true , value = "SELECT id,lqid,cgqid,dqwd,cjsj,scsj FROM sj_hk_wd WHERE id IN(SELECT MAX(id) FROM sj_hk_wd WHERE lqid IN(?1) AND DATE_FORMAT(CJSJ,'%Y-%m-%d')=?2  GROUP BY lqid)")
    List<SjHkWdEntity> getLastWd(List lqidList,String date);

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

    /**
     * @Method      : getTemperatureOnToday
     * @Description : 获取今天的温度
     * @Params      : [lqid, todayDate]
     * @Return      : com.huimv.env.dao.entity.SjHkWdEntity
     * 
     * @Author      : ZhuoNing
     * @Date        : 2020/12/29       
     * @Time        : 13:31
     */
//    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqwd,SCSJ,CJSJ FROM sj_hk_wd WHERE lqid=?1 AND DATE_FORMAT(CJSJ,'%Y-%m-%d')=?2 ORDER BY cjsj DESC ")
    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqwd,SCSJ,CJSJ FROM sj_hk_wd 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<SjHkWdEntity> getTemperatureOnToday(Integer lqid, String todayDate);

    @Query(nativeQuery = true , value = "SELECT id,CGQID,lqid,dqwd,SCSJ,CJSJ FROM sj_hk_wd WHERE lqid=?1 ORDER BY cjsj DESC LIMIT 30")
    List<SjHkWdEntity> getTemperature(Integer lqid, String beforeDateTime);
}