package com.jasamedika.medifirst2000.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.AntrianPasienRegistrasi;

/**
 * Repository class for Pasien
 * 
 * @author Askur
 */
@Repository("AntrianPasienRegistrasiDao")
public interface AntrianPasienRegistrasiDao extends PagingAndSortingRepository<AntrianPasienRegistrasi, String> {
	
	@Query("select p from AntrianPasienDiPeriksa p where p.pasienDaftar.noRegistrasi=:noRegistrasi ")
	AntrianPasienDiPeriksa findByNoRegister(@Param("noRegistrasi")  String noRegistrasi);
	public AntrianPasienRegistrasi findByNoReservasi(@Param("noReservasi") String noReservasi);
	
	@Query("select count(p.id) from AntrianPasienRegistrasi p where date(p.tglReservasi)=:dateNow and noReservasi='-'")
	Integer getCountByMonthAndYear(@Param("dateNow") Date dateNow);

	//Modify
	//Syamsu
	@Query("select count(p.id) from AntrianPasienRegistrasi p where date(p.tglReservasi)=:dateNow and noReservasi='-' and p.jenis=:jenis")
	Integer getCountByMonthAndYear(@Param("dateNow") Date dateNow, @Param("jenis") String jenis);
	
	@Query("select min(p.noAntrian) from AntrianPasienRegistrasi p where p.jenis=:jenis and p.statusPanggil='0'")
	Integer getCurrentAntrian(@Param("jenis") String jenis);
	
	
	@Modifying(clearAutomatically = true)
	@Query("Update AntrianPasienRegistrasi p set p.statusPanggil = '1' Where p.noAntrian = :noAntrian and p.jenis=:jenis")
	Integer updatePanggilNoAntrian(@Param("noAntrian") Integer noAntrian, @Param("jenis") String jenis);
	
	@Modifying(clearAutomatically = true)
	@Query("Update AntrianPasienRegistrasi p set p.statusPanggil = '2' Where p.statusPanggil = '1' and p.jenis=:jenis")
	Integer updateSudahNoAntrian(@Param("jenis") String jenis);
   //Syamsu
	
	
	@Query("select max(p.noAntrian) from AntrianPasienDiPeriksa p where date(p.tglRegistrasi)=:dateNow and  p.ruangan.id=:idRuangan")
	Integer getCountByMonthAndYearRuangan(@Param("dateNow") Date dateNow,@Param("idRuangan") Integer idRuangan);
	
	@Query("select  count(antrian.noRec) from AntrianPasienRegistrasi antrian where antrian.statusPanggil='0' and antrian.jenis = :jenis ")
	Long getCount(@Param("jenis") String jenis);
	
	
	@Query("select p from AntrianPasienDiPeriksa p where p.ruanganId=:ruanganId  ")
	Page<AntrianPasienDiPeriksa> findAllAntrianPaging(@Param("ruanganId") Integer ruanganId,Pageable pageable);
	
	@Query("select p from AntrianPasienRegistrasi p where p.noRec=:noRec  ")
	AntrianPasienRegistrasi findByNoRec(@Param("noRec")String noRec);
	@Query("select count(p.id) from AntrianPasienRegistrasi p where date(p.tglReservasi)=:date and p.ruangan.id=:ruanganId")
	Integer findByRuanganIdAndTglRegistrasi(@Param("ruanganId") Integer ruanganId, @Param("date") Date date);
	
	@Query("select antrian.noRec from AntrianPasienRegistrasi antrian "+
	"where antrian.namaPasien =:nama and to_char(antrian.tglLahir, 'yyyy-MM-dd') = :tglLahir "+
	"and antrian.ruanganId =:ruanganId  and antrian.pegawaiId =:pegawaiId "+
	"and to_char(antrian.tglReservasi, 'yyyy-MM-dd') =:tglReservasi")
	List<String> findAntrianPasienRegistrasi(@Param("nama") String nama,@Param("tglLahir") String tglLahir,@Param("ruanganId") Integer ruanganId,@Param("pegawaiId") Integer pegawaiId, @Param("tglReservasi") String tglReservasi);
	
	@Query("select antrian.noRec from AntrianPasienRegistrasi antrian left join antrian.pasien pasien "+
	"where pasien.noCm = :noCm "+
	"and antrian.ruanganId =:ruanganId  and antrian.pegawaiId =:pegawaiId "+
	"and to_char(antrian.tglReservasi, 'yyyy-MM-dd') =:tglReservasi")
	List<String> findAntrianPasienLamaRegistrasi(@Param("noCm") String noCm,@Param("ruanganId") Integer ruanganId,@Param("pegawaiId") Integer pegawaiId, @Param("tglReservasi") String tglReservasi);
}
