package com.jasamedika.medifirst2000.dao;

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

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
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.Konsultasi;

@Repository("KonsultasiDao")
public interface KonsultasiDao
		extends PagingAndSortingRepository<Konsultasi, String>, JpaSpecificationExecutor<Konsultasi> {

	@Query("select model from Konsultasi model where model.antrianPasienDiPeriksa.noRec=:noRec")
	public List<Konsultasi> findAllByNoRecAntrian(@Param("noRec") String noRec);

	@Query("select new map (strukOrder.noRec as noRec, " + "ruanganAsal.id as idRuanganAsal,  "
			+ "ruanganAsal.namaRuangan as namaRuanganAsal,  " + "ruanganTujuan.id as idRuanganTujuan,  "
			+ "ruanganTujuan.namaRuangan as namaRuanganTujuan,  " + "pasienDaftar.noRegistrasi as noRegistrasi, "
			+ "to_char(strukOrder.tglOrder,'yyyy-MM-dd') as tglOrder, " + "antrian.statusAntrian as statusAntrian, "
			+ "pasien.noCm as noCm, antrian.noRec as noRecAntrian) " + "from StrukOrder strukOrder  "
			+ "left join strukOrder.ruangan ruanganAsal  " + "left join strukOrder.ruanganTujuan ruanganTujuan  "
			+ "left join strukOrder.noRegistrasi pasienDaftar " + "left join pasienDaftar.pasien pasien, "
			+ "AntrianPasienDiPeriksa antrian " + "where antrian.strukOrderId = strukOrder.noRec "
			+ "and pasien.noCm = :noCm and to_char(strukOrder.tglOrder,'yyyy-MM-dd')between :startDate and :endDate")
	public List<Map<String, Object>> findRiwayatKonsultasi(@Param("noCm") String noCm,
			@Param("startDate") String startDate, @Param("endDate") String endDate);

	@Query("select NEW Map(" + "a.noRec as noRecJawaban," + "a.keteranganLainnya as keteranganKonsultasi,"
			+ "a.jawabanKonsultasi as jawabanKonsultasi) " + "from PasienDiRujukKeIntExt a "
			+ "left join a.noRegistrasi b " + "where b.noRec=:noRec")
	public Map<String, Object> getIkhtiarKlinik(@Param("noRec") String noRec);

	@Query("select new Map(pd.noRegistrasi as noRegistrasi, " + "ps.namaPasien as namaPasien, "
			+ "rk.namaRekanan as namaRekanan, " + "ru.namaRuangan as namaRuangan, "
			+ "to_char(pp.tglPelayanan,'yyyy-MM-dd HH:mm') as tglPelayanan, " + "pr.namaProduk as namaProduk, " + "pp.hargaJual as hargaJual, "
			+ "pg.namaLengkap as pegawaiTindakan, " + "dpj.namaLengkap as dokterPenanggungJawab) "
			+ "from PelayananPasienPetugas ppp " + "inner join ppp.pelayananPasien pp " + "left join ppp.kdpegawai pg "
			+ "inner join pp.produk pr " + "inner join pp.pasienDaftar apd " + "inner join pr.detailJenisProduk djp "
			+ "inner join apd.pasienDaftar pd " + "inner join pd.pasien ps " + "left join pd.dokterPenanggungJawab dpj "
			+ "inner join apd.ruangan ru " + "left join pd.rekanan rk "
			+ "where pp.tglPelayanan between :tglAwal and :tglAkhir " + "and ru.departemenId not in (:listIdDepartemen) "
			+ "and (pg.id is null or pg.id in (:listIdPegawai)) " + "and pr.id <> :idProduk "
			+ "and djp.jenisProdukId not in (:listIdJenisProduk) " + "and djp.id not in (:listIdDetailJenisProduk) "
			+ "order by dpj.namaLengkap, ru.namaRuangan")
	public List<Map<String, Object>> findAllKonsultasiTindakanKosong(@Param("tglAwal") Date tglAwal,
			@Param("tglAkhir") Date tglAkhir, @Param("listIdDepartemen") List<Integer> listIdDepartemen,
			@Param("listIdPegawai") List<Integer> listIdPegawai, @Param("idProduk") Integer idProduk,
			@Param("listIdJenisProduk") List<Integer> listIdJenisProduk,
			@Param("listIdDetailJenisProduk") List<Integer> listIdDetailJenisProduk);

}
