package com.jasamedika.medifirst2000.dao;

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

import javax.persistence.LockModeType;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Lock;
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.Alamat;
import com.jasamedika.medifirst2000.entities.Dokumen;
import com.jasamedika.medifirst2000.entities.Pasien;

/**
 * Repository class for Pasien
 * 
 * @author Askur
 */
@Repository("PasienDao")
public interface PasienDao extends PagingAndSortingRepository<Pasien, Integer> {
	@Query("select model from Pasien model where model.namaPasien=:nama")
	public List<Pasien> findPasienByNama(@Param("nama") String nama);

	@Query("select model from Pasien model where model.noCm = :noCm")
	public List<Pasien> findPasienBynoCm(@Param("noCm") String noCm);

	@Query("select model from Pasien model where model.namaPasien=:nama")
	public Page<Pasien> findPagePasienByNama(@Param("nama") String nama, Pageable Page);

	@Query("select new map(model.id as id, model.namaPasien as nama) from Pasien model")
	public List<Map<String, String>> getMapPasien();

	@Query(value = "SELECT " + "namaPasien AS NAMA " + "FROM Pasien", nativeQuery = true)
	List<Object[]> getNativePasien();

	@Query("select distinct model.noCm from Pasien model where length(model.noCm)=8 ")
	public List<String> findAllNoCm();

	@Query("select model from Pasien model,DataAsuransi ap where ap.pasien.noCm = model.noCm and  ap.noKepesertaan = :noBpjs")
	public List<Pasien> findPasienBynoBpjs(@Param("noBpjs") String noBpjs);

	/**
	 * Data asuransi di laravel sepertinya sudah tidak dipakai
	 */
	@Query("select model from Pasien model where model.noBpjs = :noBpjs")
	public List<Pasien> findPasienByNoBpjs(@Param("noBpjs") String noBpjs);

	@Query("select model from Pasien model where model.id =:id")
	public Pasien findById(@Param("id") Integer id);

	@Query("select model.dokumenRekamMedis from Pasien model where model.id =:id")
	public Dokumen findDokumenById(@Param("id") Integer id);

	@Query("select (model.noCm) from Pasien model where length(model.noCm)=:panjangNoCm ")
	@Lock(LockModeType.PESSIMISTIC_WRITE)
	public List<String> MaxNoCm(@Param("panjangNoCm") int panjangNoCm);

	@Query("select model from Pasien model where model.noCm = :noCm")
	public List<Pasien> findByNoCm(@Param("noCm") String noCm);

	@Query("select a.noCm, a.namaPasien, to_char(a.tglLahir,'yyyy-MM-dd'), a.jenisKelamin.jenisKelamin from Pasien a where a.noCm= :noCm")
	public List<Object[]> findByNocm(@Param("noCm") String noCm);

	@Query("select model from Pasien model where model.noCm = :noCm")
	public Pasien findPasienByNoCm(@Param("noCm") String noCm);

	@Query("select NEW Map (" + "a.id as id, " + "a.namaPasien as namaPasien, " + "a.tempatLahir as tempatLahir, "
			+ "a.tglLahir as tglLahir, " + "a.noBpjs as noBpjs, " + "a.noAsuransiLain as noAsuransiLain, "
			+ "a.noTelepon as noTelepon, " + "a.noHp as noHp, " + "a.namaIbu as namaIbu, " + "a.namaAyah as namaAyah, "
			+ "a.namaSuamiIstri as namaSuamiIstri, " + "a.noIdentitas as noIdentitas, " + "a.tglDaftar as tglDaftar, "
			+ "aa as jenisKelamin, " + "ab as agama, " + "ac as statusPerkawinan, " + "ad as namaPendidikan, "
			+ "ae as pekerjaan, " + "af as kebangsaan, " + "ag as negara, " + "ah as alamatLengkap, "
			+ "ah as kodePos, " + "aha as namaDesaKelurahan, " + "ahb as namaKecamatan, " + "ahc as namaKotaKabupaten, "
			+ "ahd as namaPropinsi) " + "from Pasien a " + "left join a.jenisKelamin aa " + "left join a.agama ab "
			+ "left join a.statusPerkawinan ac " + "left join a.pendidikan ad " + "left join a.pekerjaan ae "
			+ "left join a.kebangsaan af " + "left join a.negara ag " + "left join a.alamats ah "
			+ "left join ah.desaKelurahan aha " + "left join ah.kecamatan ahb " + "left join ah.kotaKabupaten ahc "
			+ "left join ah.propinsi ahd " + "where a.noCm=:noCm")
	public Map<String, Object> findMapPasienByNoCm(@Param("noCm") String noCm);

	@Query("select new Map(ps.id as id," + "ps.noBpjs as nomorkartu," + "ps.noIdentitas as nik,"
			+ "ps.namaPasien as nama," + "ps.jenisKelaminId as jeniskelamin,"
			+ "to_char(ps.tglLahir,'yyyy-MM-dd') as tanggallahir," + "ps.noHp as nohp) " + "from Pasien ps "
			+ "where ps.statusEnabled is true " + "and ps.noCm = :noRm " + "order by ps.id desc")
	public List<Map<String, Object>> findByNoRM(@Param("noRm") String noRm);

	@Query("select NEW Map (" + "a.id as id, " + "a.namaKelas as namaKelas) " + "from Kelas a")
	public List<Map<String, Object>> getkelas();

	@Query("select NEW Map (" + "a.id as id, " + "a.namaRuangan as namaRuangan) " + "from Ruangan a "
			+ "left join a.kelasHead b " + "left join a.departemen c " + "where b.id=:id or c.id=16")
	public List<Map<String, Object>> getRuanganByKelas(@Param("id") Integer id);

	@Query("select NEW Map (" + "a.id as id, " + "a.namaKamar as namaKamar, " + "a.kdKamar as kdKamar) "
			+ "from Kamar a " + "left join a.ruangan b " + "where b.id=:id")
	public List<Map<String, Object>> getKamarByRuangan(@Param("id") Integer id);

	@Query("select NEW Map (" + "a.id as id, " + "a.nomorBed as nomorBed, " + "c.statusBed as statusBed) "
			+ "from TempatTidur a " + "left join a.kamar b " + "left join a.statusBed c " + "where b.id=:id")
	public List<Map<String, Object>> getKasurByKamar(@Param("id") Integer id);

	@Query("select NEW Map (" + "a.id as id, " + "a.namaPasien as nama, " + "a.tempatLahir as tempatLahir, "
			+ "a.tglLahir as tglLahir, " + "a.noBpjs as noBpjs, " + "a.noAsuransiLain as noAsuransiLain, "
			+ "a.noTelepon as noTelepon, " + "a.noHp as noHp, " + "a.namaIbu as namaIbu, " + "a.namaAyah as namaAyah, "
			+ "a.namaSuamiIstri as namaSuamiIstri, " + "a.noIdentitas as nik, " + "a.tglDaftar as tglDaftar,"
			+ "a.namaKeluarga as namaKeluarga, " + "b.id as jenisKelaminId," + "c.id as agamaId,"
			+ "d.id as statusKawinId," + "e.id as pendidikanId," + "f.id as pekerjaanId," + "g.id as kebangsaanId,"
			+ "h.id as negaraId) " + "from Pasien a " + "left join a.jenisKelamin b " + "left join a.agama c "
			+ "left join a.statusPerkawinan d " + "left join a.pendidikan e " + "left join a.pekerjaan f "
			+ "left join a.kebangsaan g " + "left join a.negara h " + "where a.noCm=:noCm order by tglDaftar desc")
	public List<Map<String, Object>> getPasien(@Param("noCm") String noCm);

	@Query("select a from Alamat a left join a.pasien b where b.noCm=:noCm")
	public Alamat getAlamatByPasien(@Param("noCm") String noCm);

	@Query("select pasien from AntrianPasienDiPeriksa antrian " + "left join antrian.pasienDaftar pasienDaftar "
			+ "left join pasienDaftar.pasien pasien " + "where antrian.noRec = :noRec ")
	public List<Pasien> findPasienByNoRecAntrian(@Param("noRec") String noRec);

	@Query("select new Map(ibu.namaPasien as namaIbu,ibu.noCm as noCmIbu,ibu.tglDaftar as tglDaftar,"
			+ "anak.namaPasien as namaAnak,anak.noCm as noCmAnak,anak.tglLahir as tglLahir) "
			+ "from Pasien ibu, Pasien anak " + "where ibu.noCm = anak.reportDisplay "
			+ "and ibu.statusEnabled is true and anak.statusEnabled is true "
			+ "and to_char(anak.tglLahir,'yyyy-MM-dd') between :tglAwal and :tglAkhir " + "order by ibu.namaPasien")
	public List<Map<String, Object>> findIbuAnak(@Param("tglAwal") String tglAwal, @Param("tglAkhir") String tglAkhir);
}
