package com.jasamedika.medifirst2000.dao;

import java.util.Date;
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.DiagnosaPasien;

/**
 * 
 * @author Adik
 */
@Repository("DiagnosaPasienDao")

public interface DiagnosaPasienDao extends PagingAndSortingRepository<DiagnosaPasien, String> {
	// custom query
	@Query("select model from DiagnosaPasien model where model.pasienDaftar.pasienDaftar.pasien.noCm=:noCm and model.tanggalPendaftaran=:tanggalPendaftaran")
	public List<DiagnosaPasien> findDiagnosaPasienByNoCmAndTglInput(@Param("noCm") String noCm,@Param("tanggalPendaftaran") Date tanggalPendaftaran);

	@Query("select model from DiagnosaPasien model where model.pasienDaftar.pasienDaftar.pasien.id=:pasienId")
	public List<DiagnosaPasien> findByPasien(@Param("pasienId") Integer pasienId);

	@Query("select model from DiagnosaPasien model where model.pasienDaftar.pasienDaftar.pasien.noCm=:noCm")
	public List<DiagnosaPasien> findDiagnosaPasienByNoCm(@Param("noCm") String noCm);

	@Query("select a from DiagnosaPasien a where a.pasienDaftar.noRec=:noRec")
	public List<DiagnosaPasien> findDiagnosaPasienByAntrian(@Param("noRec") String noRec);
	
	@Query("select pasienDaftar.noRegistrasi,pasien.noCm, ruangan.namaRuangan,pasien.namaPasien, diagnosa.namaDiagnosa,to_char(pasienDaftar.tglRegistrasi,'yyyy-MM-dd'),to_char(pasienDaftar.tglPulang,'yyyy-MM-dd'),dokterPenanggungJawab.namaLengkap from DiagnosaPasien diagnosaPasien left join diagnosaPasien.diagnosis diagnosisDetail left join diagnosisDetail.diagnosa diagnosa left join diagnosisDetail.jenisDiagnosa jenisDiagnosa left join diagnosisDetail.pasienDaftar antrian left join antrian.pasienDaftar pasienDaftar left join pasienDaftar.pasien pasien left join pasienDaftar.dokterPenanggungJawab dokterPenanggungJawab left join  pasienDaftar.ruangan ruangan where jenisDiagnosa.id=4 and pasienDaftar.noRegistrasi=:noRegistrasi ")
	public List<Object[]> findDiagnosaPasien(@Param("noRegistrasi") String noRegistrasi);

	@Query("select new Map(diagnosa.namaDiagnosa as namaDiagnosa,diagnosa.kdDiagnosa as kdDiagnosa) "
		+"from DetailDiagnosaPasien model "
		+"left join model.diagnosa diagnosa " 
		+"left join model.pasienDaftar pasiendaftar "
		+"where pasiendaftar.noRec=:noRecAntrian")
	public List<Map<String,Object>> getDiagnosaPasienByNoRec(@Param("noRecAntrian")String noRec);
	
	
	@Query("select new Map(diagnosa.kdDiagnosa as kdDiagnosa, "+
			"diagnosa.namaDiagnosa as namaDiagnosa, "+
			"jenisDiagnosa.jenisDiagnosa as jenisDiagnosa, "+
			"jenisDiagnosa.id as idDiagnosa, "+
			"ruangan.namaRuangan as namaRuangan, "+
			"ruangan.id as idRuangan) "+
			"from DiagnosaPasien diagnosaPasien "+
			"left join diagnosaPasien.diagnosis diagnosis "+
			"left join diagnosis.diagnosa diagnosa "+
			"left join diagnosis.jenisDiagnosa jenisDiagnosa "+
			"left join diagnosaPasien.pasienDaftar antrian  "+
			"left join antrian.pasienDaftar pasienDaftar "+
			"left join antrian.ruangan ruangan "+
			"where diagnosis.noRec is not null and  pasienDaftar.noRec = :noRecAntrian")
		public List<Map<String,Object>> getDiagnosaPasienByNoRecPasienDaftar(@Param("noRecAntrian")String noRec);
	
	
}
