package com.dacrt.SBIABackend.repository;

import java.util.Date;
import java.util.Optional;

import javax.transaction.Transactional;

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.stereotype.Repository;

import com.dacrt.SBIABackend.entity.Coverageapps;
import com.dacrt.SBIABackend.entity.Coverageappshistory;

@Repository
public interface CoverageappshistoryRepository extends JpaRepository<Coverageappshistory,Integer>,JpaSpecificationExecutor<Coverageappshistory>{

	
	
	@Query("SELECT e FROM Coverageappshistory e WHERE e.id =?1")
	Optional<Coverageappshistory> findByCoverageappid(int id);
	
	@Query(value=" SELECT u FROM Coverageappshistory u WHERE u.id= ?1 ")
	Coverageappshistory getById(int id);
	
	boolean existsById(Integer id);
	
	@Query("SELECT count(e) FROM Coverageappshistory e WHERE e.coverageappid =?1 ")
	int countByCoverageappid(Integer coverageAppid);
	
	@Query(value=" SELECT max(u.id) FROM Coverageappshistory u")
	int getMaxRecord();
	
	
	@Transactional
	@Modifying
	@Query(" DELETE FROM Coverageappshistory e where e.id = ?1 ")
	void deleteCoverageHistorybyid(Integer id);
	
	@Query("SELECT count(e) FROM Coverageappshistory e WHERE e.coverageappid =?1 and date > ?2")
	int findByCoverageapphistoryFecha(int coverageAppid, Date fecha);
	
}
