package com.jasamedika.medifirst2000.dao;

import java.util.List;
import java.util.Map;

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.RencanaOperasi;

/**
 * Repository class for RencanaOperasi
 * 
 * @author Generator
 */
@Repository("RencanaOperasiDao")
public interface RencanaOperasiDao extends PagingAndSortingRepository<RencanaOperasi, String> {	
	
	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi,  "+
				"rencanaOperasi.strukOrderId as strukOrderId, "+
				"rencanaOperasi.tglRencana as tglRencana, "+
				"rencanaOperasi.tglOperasi as tglOperasi, "+
				"kamar.namaKamar as namaKamar, "+
				"kamar.id as idKamar, "+
				"pasienDaftar.noRec as pasienDaftar, "+
				"jenisOperasi.name as namaJenisOperasi, "+
				"jenisOperasi.id as idJenisOperasi, "+
				"antrian.noRec as noRecAntrian, "+
				"asalRujukan.id as asalRujukanId, " +
				"jenisPelaksana.jenisPetugasPe as jenisPetugas, "+
				"pegawai.namaLengkap as namaPegawai, "+
				"pegawai.id as idPegawai, "+
				"ruangan.id as idRuangan, "+
				"ruangan.namaRuangan as namaRuangan) "+
				"from RencanaOperasi rencanaOperasi  "+
				"left join rencanaOperasi.strukOrder strukOrder  "+
				"left join strukOrder.noRegistrasi pasienDaftar  "+
				"left join rencanaOperasi.detailPelaksana detailPelaksana "+
				"left join detailPelaksana.jenisPelaksana jenisPelaksana "+
				"left join detailPelaksana.pegawai pegawai "+
				"left join strukOrder.ruangan ruangan "+
				"left join rencanaOperasi.jenisOperasi jenisOperasi "+
				"left join rencanaOperasi.kamar kamar "+
				"left join rencanaOperasi.noCm antrian "+
				"left join antrian.asalRujukan as asalRujukan " +
				"where strukOrder.noRec =:noRec")	
		List<Map<String,Object>> findRencanaOperasi(@Param("noRec") String noRec);
	   
	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi, "+
				"rencanaOperasi.strukOrderId as strukOrderId ) "+
				"from RencanaOperasi rencanaOperasi "+
				"left join rencanaOperasi.strukOrder strukOrder "+
				"left join strukOrder.noRegistrasi pasienDaftar "+
				"where strukOrder.noRec = :noRec ")
		Map<String,Object> findRencanaOperasiByNorecStrukOrder(@Param("noRec") String noRec);
	   
	   @Query("select rencanaOperasi " +
				"from RencanaOperasi rencanaOperasi " +
				"left join rencanaOperasi.strukOrder strukOrder " +
				"left join strukOrder.noRegistrasi pasienDaftar " +
				"where strukOrder.noRec=:noRec ")
		RencanaOperasi findRencanaOperasiByNorec(@Param("noRec") String noRec);
	   	   
	   @Query("select rencanaOperasi "+
				"from RencanaOperasi rencanaOperasi "+
				"where rencanaOperasi.noRec = :noRec and rencanaOperasi.tglOperasi is null")
		RencanaOperasi findByNorec(@Param("noRec") String noRec);	   
	   
	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi,  "+
				"rencanaOperasi.strukOrderId as strukOrderId, "+
				"rencanaOperasi.tglRencana as tglRencana, "+
				"rencanaOperasi.tglOperasi as tglOperasi, "+
				"kamar.namaKamar as namaKamar, "+
				"kamar.id as idKamar, "+
				"pasienDaftar.noRec as pasienDaftar, "+
				"jenisOperasi.name as namaJenisOperasi, "+
				"jenisOperasi.id as idJenisOperasi, "+
				"antrian.noRec as noRecAntrian, "+
				"jenisPelaksana.jenisPetugasPe as jenisPetugas, "+
				"pegawai.namaLengkap as namaPegawai, "+
				"pegawai.id as idPegawai, "+
				"ruangan.id as idRuangan, "+
				"pasien.namaPasien as namaPasien, "+
				"pasien.noCm as noCm, "+
				"ruangan.namaRuangan as namaRuangan) "+
				"from RencanaOperasi rencanaOperasi  "+
				"left join rencanaOperasi.strukOrder strukOrder  "+
				"left join strukOrder.noRegistrasi pasienDaftar  "+
				"left join pasienDaftar.pasien pasien "+
				"left join rencanaOperasi.detailPelaksana detailPelaksana "+
				"left join detailPelaksana.jenisPelaksana jenisPelaksana "+
				"left join detailPelaksana.pegawai pegawai "+
				"left join strukOrder.ruangan ruangan "+
				"left join rencanaOperasi.jenisOperasi jenisOperasi "+
				"left join rencanaOperasi.kamar kamar "+
				"left join rencanaOperasi.noCm antrian "+
				"where rencanaOperasi.tglRencana is null and rencanaOperasi.tglPermintaanOperasi is not null")
		List<Map<String,Object>> findPermintaanOperasi();  
	   
	   //add asalRujukan
	   //add by iwankasan
	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi,  "+
				"rencanaOperasi.strukOrderId as strukOrderId, "+
				"rencanaOperasi.tglRencana as tglRencana, "+
				"rencanaOperasi.tglOperasi as tglOperasi, "+
				"kamar.namaKamar as namaKamar, "+
				"kamar.id as idKamar, "+
				"pasienDaftar.noRec as pasienDaftar, "+
				"jenisOperasi.name as namaJenisOperasi, "+
				"jenisOperasi.id as idJenisOperasi, "+
				"antrian.noRec as noRecAntrian, "+
				"antrian.asalRujukan.id as asalRujukan, " +
				"antrian.tglRegistrasi as tglRegistrasi, " +
				"jenisPelaksana.jenisPetugasPe as jenisPetugas, "+
				"pegawai.namaLengkap as namaPegawai, "+
				"pegawai.id as idPegawai, "+
				"ruangan.id as idRuangan, "+
				"pasien.namaPasien as namaPasien, "+
				"pasien.noCm as noCm, "+
				"ruangan.namaRuangan as namaRuangan) "+
				"from RencanaOperasi rencanaOperasi  "+
				"left join rencanaOperasi.strukOrder strukOrder  "+
				"left join strukOrder.noRegistrasi pasienDaftar  "+
				"left join pasienDaftar.pasien pasien "+
				"left join rencanaOperasi.detailPelaksana detailPelaksana "+
				"left join detailPelaksana.jenisPelaksana jenisPelaksana "+
				"left join detailPelaksana.pegawai pegawai "+
				"left join strukOrder.ruangan ruangan "+
				"left join rencanaOperasi.jenisOperasi jenisOperasi "+
				"left join rencanaOperasi.kamar kamar "+
				"left join rencanaOperasi.noCm antrian "+
				"where rencanaOperasi.tglOperasi is null and rencanaOperasi.tglRencana is not null and rencanaOperasi.tglPermintaanOperasi is not null")
		List<Map<String,Object>> findRencanaOperasi();  
	   
	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi,  "+
				"rencanaOperasi.strukOrderId as strukOrderId, "+
				"rencanaOperasi.tglRencana as tglRencana, "+
				"rencanaOperasi.tglOperasi as tglOperasi, "+
				"kamar.namaKamar as namaKamar, "+
				"kamar.id as idKamar, "+
				"pasienDaftar.noRec as pasienDaftar, "+
				"jenisOperasi.name as namaJenisOperasi, "+
				"jenisOperasi.id as idJenisOperasi, "+
				"antrian.noRec as noRecAntrian, "+
				"jenisPelaksana.jenisPetugasPe as jenisPetugas, "+
				"pegawai.namaLengkap as namaPegawai, "+
				"pegawai.id as idPegawai, "+
				"ruangan.id as idRuangan, "+
				"pasien.namaPasien as namaPasien, "+
				"pasien.noCm as noCm, "+
				"ruangan.namaRuangan as namaRuangan) "+
				"from RencanaOperasi rencanaOperasi  "+
				"left join rencanaOperasi.strukOrder strukOrder  "+
				"left join strukOrder.noRegistrasi pasienDaftar  "+
				"left join pasienDaftar.pasien pasien "+
				"left join rencanaOperasi.detailPelaksana detailPelaksana "+
				"left join detailPelaksana.jenisPelaksana jenisPelaksana "+
				"left join detailPelaksana.pegawai pegawai "+
				"left join strukOrder.ruangan ruangan "+
				"left join rencanaOperasi.jenisOperasi jenisOperasi "+
				"left join rencanaOperasi.kamar kamar "+
				"left join rencanaOperasi.noCm antrian "+
				"where rencanaOperasi.tglRencana is null and rencanaOperasi.tglPermintaanOperasi is not null "+
				"and to_char(rencanaOperasi.tglPermintaanOperasi,'yyyy-MM-dd') between :startDate and :endDate and pasien.noCm = :nocm")
		List<Map<String,Object>> findPermintaanOperasiByNocmAndDate(@Param("startDate") String startDate,@Param("endDate") String endDate,@Param("nocm") String nocm);  

	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi,  "+
				"rencanaOperasi.strukOrderId as strukOrderId, "+
				"rencanaOperasi.tglRencana as tglRencana, "+
				"rencanaOperasi.tglOperasi as tglOperasi, "+
				"kamar.namaKamar as namaKamar, "+
				"kamar.id as idKamar, "+
				"pasienDaftar.noRec as pasienDaftar, "+
				"jenisOperasi.name as namaJenisOperasi, "+
				"jenisOperasi.id as idJenisOperasi, "+
				"antrian.noRec as noRecAntrian, "+
				"jenisPelaksana.jenisPetugasPe as jenisPetugas, "+
				"pegawai.namaLengkap as namaPegawai, "+
				"pegawai.id as idPegawai, "+
				"ruangan.id as idRuangan, "+
				"pasien.namaPasien as namaPasien, "+
				"pasien.noCm as noCm, "+
				"ruangan.namaRuangan as namaRuangan) "+
				"from RencanaOperasi rencanaOperasi  "+
				"left join rencanaOperasi.strukOrder strukOrder  "+
				"left join strukOrder.noRegistrasi pasienDaftar  "+
				"left join pasienDaftar.pasien pasien "+
				"left join rencanaOperasi.detailPelaksana detailPelaksana "+
				"left join detailPelaksana.jenisPelaksana jenisPelaksana "+
				"left join detailPelaksana.pegawai pegawai "+
				"left join strukOrder.ruangan ruangan "+
				"left join rencanaOperasi.jenisOperasi jenisOperasi "+
				"left join rencanaOperasi.kamar kamar "+
				"left join rencanaOperasi.noCm antrian "+
				"where rencanaOperasi.tglOperasi is null and rencanaOperasi.tglRencana is not null "+
				"and to_char(rencanaOperasi.tglRencana,'yyyy-MM-dd') between :startDate and :endDate and pasien.noCm = :nocm")
		List<Map<String,Object>> findRencanaOperasiByNocmAndDate(@Param("startDate") String startDate,@Param("endDate") String endDate,@Param("nocm") String nocm);  
	   
	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi,  "+
				"rencanaOperasi.strukOrderId as strukOrderId, "+
				"rencanaOperasi.tglRencana as tglRencana, "+
				"rencanaOperasi.tglOperasi as tglOperasi, "+
				"kamar.namaKamar as namaKamar, "+
				"kamar.id as idKamar, "+
				"pasienDaftar.noRec as pasienDaftar, "+
				"jenisOperasi.name as namaJenisOperasi, "+
				"jenisOperasi.id as idJenisOperasi, "+
				"antrian.noRec as noRecAntrian, "+
				"jenisPelaksana.jenisPetugasPe as jenisPetugas, "+
				"pegawai.namaLengkap as namaPegawai, "+
				"pegawai.id as idPegawai, "+
				"ruangan.id as idRuangan, "+
				"pasien.namaPasien as namaPasien, "+
				"pasien.noCm as noCm, "+
				"ruangan.namaRuangan as namaRuangan) "+
				"from RencanaOperasi rencanaOperasi  "+
				"left join rencanaOperasi.strukOrder strukOrder  "+
				"left join strukOrder.noRegistrasi pasienDaftar  "+
				"left join pasienDaftar.pasien pasien "+
				"left join rencanaOperasi.detailPelaksana detailPelaksana "+
				"left join detailPelaksana.jenisPelaksana jenisPelaksana "+
				"left join detailPelaksana.pegawai pegawai "+
				"left join strukOrder.ruangan ruangan "+
				"left join rencanaOperasi.jenisOperasi jenisOperasi "+
				"left join rencanaOperasi.kamar kamar "+
				"left join rencanaOperasi.noCm antrian "+
				"where rencanaOperasi.tglOperasi is null and rencanaOperasi.tglRencana is not null "+
				"and rencanaOperasi.noRec = :noRec")
		Map<String,Object> findRencanaOperasiByNoRec(@Param("noRec") String noRec);  
	   
	   @Query("select new map (rencanaOperasi.noRec as noRec, "+
				"rencanaOperasi.tglPermintaanOperasi as tglPermintaanOperasi,  "+
				"rencanaOperasi.strukOrderId as strukOrderId, "+
				"rencanaOperasi.tglRencana as tglRencana, "+
				"rencanaOperasi.tglOperasi as tglOperasi, "+
				"kamar.namaKamar as namaKamar, "+
				"kamar.id as idKamar, "+
				"pasienDaftar.noRec as pasienDaftar, "+
				"jenisOperasi.name as namaJenisOperasi, "+
				"jenisOperasi.id as idJenisOperasi, "+
				"antrian.noRec as noRecAntrian, "+
				"jenisPelaksana.jenisPetugasPe as jenisPetugas, "+
				"pegawai.namaLengkap as namaPegawai, "+
				"pegawai.id as idPegawai, "+
				"ruangan.id as idRuangan, "+
				"pasien.namaPasien as namaPasien, "+
				"pasien.noCm as noCm, "+
				"ruangan.namaRuangan as namaRuangan) "+
				"from RencanaOperasi rencanaOperasi  "+
				"left join rencanaOperasi.strukOrder strukOrder  "+
				"left join strukOrder.noRegistrasi pasienDaftar  "+
				"left join pasienDaftar.pasien pasien "+
				"left join rencanaOperasi.detailPelaksana detailPelaksana "+
				"left join detailPelaksana.jenisPelaksana jenisPelaksana "+
				"left join detailPelaksana.pegawai pegawai "+
				"left join strukOrder.ruangan ruangan "+
				"left join rencanaOperasi.jenisOperasi jenisOperasi "+
				"left join rencanaOperasi.kamar kamar "+
				"left join rencanaOperasi.noCm antrian "+
				"where rencanaOperasi.tglRencana is null and rencanaOperasi.tglPermintaanOperasi is not null "+
				"and rencanaOperasi.noRec = :noRec")
		Map<String,Object> findPermintaanOperasiByNoRec(@Param("noRec") String noRec);  
	  
	   /*
		 * 
		 * strukorder dasar untuk update antrian pasien
		 * bedah
		 * by iwan kasan
		 */
		/*@Query("select new map (antrian.noRec as noRec, " +
				"pasienDaftar.noRec as noRecPasienDaftar, " +
				"ruangan.id as idRuangan, " +
				"antrian.statusAntrian as statusAntrian, " +
				"strukOrder.noRec as noRecStrukOrder, " +
				"antrian.statusPasien as statusPasien, " +
				"asalRujukan.id as asalRujukan, " +
				"antrian.tglRegistrasi as tglRegistrasi) " +
				"from AntrianPasienDiPeriksa antrian  " +
				" left join antrian.asalRujukan asalRujukan " +
				" left join antrian.pasienDaftar pasienDaftar " +
				" left join antrian.ruangan ruangan " +
				" left join antrian.strukOrder strukOrder " +
				" left join RencanaOperasi.noCm as rencanaOperasi" +
				" where antrian.strukOrder=:strukOrder  ")*/
	   @Query("select new map(antrian.noRec as noRec, strukOrder.noRec as noRecStrukOrder, " +
				" rencanaOperasi.noRec as rencanaOperasinoRec) " +
				" from AntrianPasienDiPeriksa antrian, RencanaOperasi rencanaOperasi, " +
			    " StrukOrder strukOrder where antrian.strukOrder = strukOrder.noRec " +
				" and rencanaOperasi.strukOrder = strukOrder.noRec " +
			    " and rencanaOperasi.noCm = antrian.noRec and antrian.strukOrder=:strukOrder")
		Map<String,Object> findAntrianByNoStrukOrder(@Param("strukOrder") String strukOrder);
	   
	   
	   @Query("select a from AntrianPasienDiPeriksa a where a.pasienDaftarId=:noRec and a.kelasId is not null and a.strukOrderId is null")
	   AntrianPasienDiPeriksa getAntrianSebelumnyaByNoRec(@Param("noRec") String noRec);
	   
	   
}
