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

@Repository("pelayananPasienPerawatDao")
public interface PelayananPasienPerawatDao extends PagingAndSortingRepository<PelayananPasienPerawat, String> {

	@Query("select new Map(pp.noRec as noRec,pp.antrianPasienDiperiksaId as noRecAPD,"
			+ "pr.id as produkId,pr.namaProduk as namaProduk,"
			+ "pp.tglPelayanan as tglPelayanan,to_char(pp.tglPelayanan,'dd-MM-yyyy HH24:MI:ss') as tglPelayananFormatted,"
			+ "pp.jumlah as jumlah,pp.skor as skor,(pp.jumlah*pp.skor) as tSkor,"
			+ "pg.id as pegawaiId,pg.namaLengkap as namaLengkap) " + "from PelayananPasienPerawat pp "
			+ "inner join pp.produk pr " + "left join pp.pegawai pg " + "where pp.statusEnabled is true "
			+ "and pp.antrianPasienDiperiksaId = :norecAPD " + "and pg.id = :pegawaiId " + "order by pp.tglPelayanan")
	List<Map<String, Object>> findByAntrianPasienDiperiksa(@Param("norecAPD") String noRec,
			@Param("pegawaiId") Integer idPegawai);

	@Query("select new Map(ppp.noRec as noRec," + "ppp.tglPelayanan as tglPelayanan," + "ppp.skor as skor,"
			+ "ppp.jumlah as jumlah," + "ppp.skor*ppp.jumlah as tSkor," + "ppp.antrianPasienDiperiksaId as detail,"
			+ "pn.id as produkId," + "pn.namaProduk as namaProduk," + "pn.klasifikasi as indikatorId,"
			+ "(case when pn.klasifikasi = 1 then 'Asuhan' " + "when pn.klasifikasi = 2 then 'Tindakan' "
			+ "when pn.klasifikasi = 3 then 'Laporan' " + "else '-' end) as namaIndikator," + "pg.id as pegawaiId,"
			+ "pg.namaLengkap as namaLengkap) " + "from PelayananPasienPerawat ppp " + "inner join ppp.produk pn "
			+ "inner join ppp.pegawai pg " + "where ppp.statusEnabled is true " + "and pn.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and to_char(ppp.tglPelayanan,'yyyy-MM') = :bulan "
			+ "and ppp.pegawaiId = :pegawaiId " + "order by pn.klasifikasi, pn.namaProduk, ppp.tglPelayanan")
	List<Map<String, Object>> findPelayananPerawat(@Param("bulan") String bulan, @Param("pegawaiId") Integer idPegawai);

	@Query("select new Map(ppp.tglPelayanan as tglPelayanan,"
			+ "to_char(ppp.tglPelayanan,'yyyy-MM-dd HH24:MI:ss') as tglPelayananFormatted," + "ppp.jumlah as jumlah,"
			+ "pd.noRegistrasi as noRegistrasi," + "ps.noCm as noCm,ps.namaPasien as namaPasien,"
			+ "pn.id as produkId,pn.namaProduk as namaProduk," + "ru.namaRuangan as namaRuangan) "
			+ "from PelayananPasienPerawat ppp " + "inner join ppp.antrianPasienDiperiksa apd "
			+ "inner join ppp.pegawai pg " + "inner join ppp.produk pn " + "inner join apd.pasienDaftar pd "
			+ "inner join apd.ruangan ru " + "inner join pd.pasien ps " + "where ppp.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and pd.statusEnabled is true " + "and pg.id = :pegawaiId "
			+ "and pn.id = :produkId " + "and to_char(ppp.tglPelayanan,'yyyy-MM-dd') = :tglPelayanan")
	List<Map<String, Object>> findPelayananPerawatDetail(@Param("pegawaiId") Integer idPegawai,
			@Param("produkId") Integer idProduk, @Param("tglPelayanan") String tglPelayanan);

	@Query("select ppp from PelayananPasienPerawat ppp " + "where ppp.statusEnabled is true "
			+ "and to_char(ppp.tglPelayanan,'yyyy-MM') = :bulan " + "and ppp.pegawaiId = :pegawaiId")
	List<PelayananPasienPerawat> findAllPelayananByKontrak(@Param("bulan") String bulan,
			@Param("pegawaiId") Integer idPegawai);

}
