package com.jasamedika.medifirst2000.dao;

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

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

@Repository("CheckInDao")
public interface CheckInDao extends PagingAndSortingRepository<CheckIn, String> {
	@Query("select new map(checkIn.noRec as noRec, " +
			" antrian.noRec as noRecAntrian, " +
			" checkIn.beratBadan as beratBadan, " +
			" checkIn.tinggiBadan as tinggiBadan, " +
			" checkIn.makanMinumTerakhir as makanMinumTerakhir, " +
			" checkIn.jumlah as jumlah, checkIn.kesadaran as kesadaran, " +
			" checkIn.td as td, checkIn.n as n, checkIn.rr as rr, " +
			" puasa.id as idPuasa, puasa.name as namePuasa, " +
			" jenisOperasi.id as idJenisOperasi, " +
			" jenisOperasi.name as namejenisOperasi, " +
			" antrian.tglRegistrasi as tglRegistrasi, " +
			" kelas.id as idKelas, kelas.namaKelas as namaKelas, " +
			" ruangan.id as idRuangan, ruangan.namaRuangan as namaRuangan, " +
			" pegawai.id as idPegawai, pegawai.namaLengkap as namaPegawai, " +
			" pasien.jenisKelamin.jenisKelamin as jenisKelamin, " +
			" pasien.namaPasien as namaPasien, checkIn.statusEnabled as statusEnabled, " +
			" pasien.noCm as noCm, alamats.alamatLengkap as alamatLengkap) " +
			" from CheckIn checkIn " +
			" left join checkIn.puasa puasa " +
			" left join checkIn.jenisOperasi jenisOperasi " +
			" left join checkIn.pasienDaftar antrian " +
			" left join antrian.ruangan ruangan " +
			" left join antrian.kelas as kelas " +
			" left join antrian.pegawai pegawai " +
			" left join antrian.pasienDaftar pasienDaftar " +
			" left join pasienDaftar.pasien pasien " +
			" left join pasien.alamats alamats " +
			" where checkIn.statusEnabled in('true') and checkIn.noRec=:noRec")
	List<Map<String, Object>> getCheckInByNoRec(@Param("noRec") String noRec);
	
	@Query("select new map(checkIn.noRec as noRec, " +
			" antrian.noRec as noRecAntrian, " +
			" checkIn.beratBadan as beratBadan, " +
			" checkIn.tinggiBadan as tinggiBadan, " +
			" checkIn.makanMinumTerakhir as makanMinumTerakhir, " +
			" checkIn.jumlah as jumlah, checkIn.kesadaran as kesadaran, " +
			" checkIn.td as td, checkIn.n as n, checkIn.rr as rr, " +
			" puasa.id as idPuasa, puasa.name as namePuasa, " +
			" jenisOperasi.id as idJenisOperasi, " +
			" jenisOperasi.name as namejenisOperasi, " +
			" antrian.tglRegistrasi as tglRegistrasi, " +
			" kelas.id as idKelas, kelas.namaKelas as namaKelas, " +
			" ruangan.id as idRuangan, ruangan.namaRuangan as namaRuangan, " +
			" pegawai.id as idPegawai, pegawai.namaLengkap as namaPegawai, " +
			" pasien.jenisKelamin.jenisKelamin as jenisKelamin, " +
			" pasien.namaPasien as namaPasien, " +
			" pasien.noCm as noCm, alamats.alamatLengkap as alamatLengkap) " +
			" from CheckIn checkIn " +
			" left join checkIn.puasa puasa " +
			" left join checkIn.jenisOperasi jenisOperasi " +
			" left join checkIn.pasienDaftar antrian " +
			" left join antrian.ruangan ruangan " +
			" left join antrian.kelas as kelas " +
			" left join antrian.pegawai pegawai " +
			" left join antrian.pasienDaftar pasienDaftar " +
			" left join pasienDaftar.pasien pasien " +
			" left join pasien.alamats alamats where antrian.noRec=:noRec")
	List<Map<String, Object>> getCheckInByNoRecAntrian(@Param("noRec") String noRec);

 	@Query("select new map(obatKhususDetail.noRec as noRec, " +
 			" obatKhususDetail.value as value, " +
 			" checkIn.noRec as noRecCheckIn, " +
 			" pengobatanKhusus.id as idPengobatanKhusus, " +
 			" pengobatanKhusus.name as namePengobatanKhusus, " +
 			" obatKhususDetail.statusEnabled as statusEnabled) " +
 			" from PengobatanKhususDetail obatKhususDetail " +
 			" left join obatKhususDetail.checkIn checkIn " +
 			" left join obatKhususDetail.pengobatanKhusus pengobatanKhusus " +
 			" where obatKhususDetail.statusEnabled in('true') and checkIn.noRec=:noRec")
 	List<Map<String, Object>> getPengobatanKhususDetail(@Param("noRec") String noRec);
 	
 	
 	@Query("select new map(alatTerpasangDetail.noRec as noRec, " +
 			" checkIn.noRec as noRecCheckIn, " +
 			" alatTerpasang.id as idAlatTerpasang, " +
 			" alatTerpasang.name as namaAlatTerpasang, " +
 			" alatTerpasangDetail.statusEnabled as statusEnabled) " +
 			" from AlatTerpasangDetail alatTerpasangDetail " +
 			" left join alatTerpasangDetail.checkIn checkIn " +
 			" left join alatTerpasangDetail.alatTerpasang alatTerpasang " +
 			" where alatTerpasangDetail.statusEnabled in('true') and checkIn.noRec=:noRec")
 	List<Map<String, Object>> getAlatTerpasangDetail(@Param("noRec") String noRec);
 	
 	//modifikasi by iwankasan
 	//update by query
 	//06.11.2017
 	@Modifying(clearAutomatically=true)
	@Query("update AntrianPasienDiPeriksa a set a.statusAntrian='1' where " +
			   " a.noRec=:noRec")
	public void updateNoAntrianByNoRec(@Param("noRec") String noRec);
 	
}
