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

@Repository("HistoriPelayananCsDao")
public interface HistoriPelayananCsDao extends PagingAndSortingRepository<HistoriPelayananCs, String> {
	
	@Query("select count(h.noHistori) "
			+ "from HistoriPelayananCs h "
			+ "where h.noHistori LIKE %:kode% ")
	Long countNoHistori(@Param("kode") String kode);
	
	@Query("select a.noHistori "
			+ "from HistoriPelayananCs a "
			+ "where a.noHistori LIKE %:kode% "
			+ "order by a.noHistori asc")
	List<String> noHistori(@Param("kode") String kode);

	@Query("select new map (a.noRec as noRec, a.noHistori as noHistori, "
			+ " a.tglPelayananAwal as tglPelayananAwal, a.tglPelayananAkhir as tglPelayananAkhir, a.isCito as isCito, "
			+ " a.keterangan as keterangan, a.namaCustomer as namaCustomer, "
			+ " a.isPelayanan as isPelayanan, a.detailRespon as detailRespon, "
			+ " a.deskripsi as deskripsi, b.id as pegawaiId, b.namaLengkap as namaPegawai, "
			+ " c.id as ruanganId, d.id as jenisResponId, d.jeniRespon as jenisRespon, "
			+ " e.id as komunikasiId, e.komunikasi as komunikasi, c.namaRuangan as namaRuangan, "
			+ " f.alamatLengkap as alamatCustomer, g.noCm as noCm, g.namaPasien as namaPasien, "
			+ " h.id as idkelamain, h.jenisKelamin as jenisKelamin, "
			+ " i.kelompokTransaksi as kelompokTransaksi, j.kelompokProduk as kelompokProduk, "
			+ " (select p.namaLengkap from Pegawai p where p.id = x.pegawaiTerimaId) as pegawaiTerima) "
			+ " from HistoriPelayananCs a left join a.pegawaiTujuan b "
			+ " left join a.ruanganTujuan c left join a.jenisRespon d "
			+ " left join a.komunikasi e left join a.alamatCustomer f  "
			+ " left join a.noCM g left join a.kelamin h "
			+ " left join a.kelompokTransaksi i left join a.kelompokProduk j, StrukHistori x "
			+ " where a.noHistori=x.nonHistori and c.id=:id")
	List<Map<String, Object>> getListHistoriPelayananCs(
			@Param("id") Integer id);

	@Query("select NEW Map ("
			+ "a.id as idpegawai, "
			+ "a.namaLengkap as namaLengkap)"
			+ "from Pegawai a left join a.ruangan b where b.id=:id")
	List<Map<String, Object>> getListPegawai(
			@Param("id") Integer id);
	
	@Query("select model from Pasien model where model.noCm = :noCm")
	List<Map<String, Object>> getListPasienByNoCm(@Param("noCm") String noCm);
	
	@Query("select new map( p.id as idProduk, p.kelompokProduk as kelompokProduk ) from KelompokProduk p")
	List<Map<String, Object>> getListProdukInformasi();
	
	@Query("select new map (a.noRec as noRec, a.noHistori as noHistori, "
			+ " a.tglPelayananAwal as tglPelayananAwal, a.tglPelayananAkhir as tglPelayananAkhir, a.isCito as isCito, "
			+ " a.keterangan as keterangan, a.namaCustomer as namaCustomer, "
			+ " a.isPelayanan as isPelayanan, a.detailRespon as detailRespon, "
			+ " a.deskripsi as deskripsi, b.id as pegawaiId, b.namaLengkap as namaPegawai, "
			+ " c.id as ruanganId, d.id as jenisResponId, d.jeniRespon as jenisRespon, "
			+ " e.id as komunikasiId, e.komunikasi as komunikasi, c.namaRuangan as namaRuangan, "
			+ " f.alamatLengkap as alamatCustomer, g.noCm as noCm, g.namaPasien as namaPasien, "
			+ " h.id as idkelamain, h.jenisKelamin as jenisKelamin, "
			+ " i.kelompokTransaksi as kelompokTransaksi, j.kelompokProduk as kelompokProduk) "
			+ " from HistoriPelayananCs a left join a.pegawaiTujuan b "
			+ " left join a.ruanganTujuan c left join a.jenisRespon d "
			+ " left join a.komunikasi e left join a.alamatCustomer f  "
			+ " left join a.noCM g left join a.kelamin h "
			+ " left join a.kelompokTransaksi i left join a.kelompokProduk j "
			+ " where a.noRec=:norec")
	List<Map<String, Object>> getListDataHistoriCsByNoRec(@Param("norec") String norec);
 	
}
