package com.huimv.receiver.cloud.dao.repo;

import com.huimv.receiver.cloud.dao.entity.EnvDayEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

public interface EnvDayRepo extends JpaRepository<EnvDayEntity, Integer>, JpaSpecificationExecutor<EnvDayEntity> {

    @Transactional
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Query(nativeQuery = true,value ="DELETE FROM env_day WHERE farm_id=?1 AND DATE_FORMAT(add_date,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d')")
    void removeByFarmIdAndDate(Integer farmId, String todayDateText);

    @Query(nativeQuery = true,value ="SELECT * FROM env_day WHERE farm_id=?1 AND DATE_FORMAT(add_date,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d')")
    Optional<EnvDayEntity> findByFarmIdAndDate(Integer farmId, String todayDateText);
}