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.CrudRepository;
import org.springframework.data.repository.query.Param;

import com.jasamedika.medifirst2000.entities.CssdCaraSterilisasi;

public interface CssdSterilisasiDao extends CrudRepository<CssdCaraSterilisasi, Integer>{
	@Query("select new map(p.id as id, p.namaProduk as namaProduk, " +
			" s.id as satuanId, s.satuanStandar as satuanStandar) " +
			" from Produk p left join " +
			" p.detailJenisProduk d left join " +
			" p.satuanStandar s where d.id in(450)")
	List<Map<String, Object>> getProdukCssd();
	
	@Query("select new map(j.id as id, j.jenisPaket as jenisPaket) from JenisPaket j")
	List<Map<String, Object>> getJenisPaket();
	
	@Query("select new map(p.id as id, p.namaPaket as namaPaket) " +
			" from Paket p left join p.jenisPaket j where j.id=:jenisPaket")
	List<Map<String, Object>> getPaketCssdByJenisPaket(@Param("jenisPaket")Integer jenisPaket);
	
	@Query("select new map(pk.id as idPaket, " +
			" pk.namaPaket as namaPaket, " +
			" p.id as id, p.namaProduk as namaProduk, " +
			" m.qtyProduk as qtyProduk, s.id as satuanId, " +
			" s.satuanStandar as satuanStandar, " +
			" pk.harga as harga) " +
			" from MapPaketToProduk m  " +
			" left join m.paket pk " +
			" left join m.produk p  " +
			" left join p.satuanStandar s " +
			" where pk.id=:paketId")
	List<Map<String, Object>> getProdukbyPaket(@Param("paketId")Integer paketId);
	
	@Query("select new Map (ruanganAsal.namaRuangan as namaRuangan, "
				+ " strukPelayanan.noRec as strukPelayananId, "
				+ " strukPelayanan.noUrut as noUrut,strukPelayanan.nostruk as noOrder, "
				+ " strukPelayanan.tglstruk as tanggalOrder,prosesSterilisasi.name as prosesSterilisasi, "
				+ " strukPelayanan.tglAmbil as tglAmbil, "
				+ " menyerahkan.namaLengkap as menyerahkan, "
				+ " pegawaiPenerima.namaLengkap as petugasPenerima) "
				+ " from StrukPelayanan strukPelayanan "
				+ " left join strukPelayanan.cssdProsesSterilisasi prosesSterilisasi "
				+ " left join strukPelayanan.menerima pegawaiPenerima  "
				+ " left join strukPelayanan.menyerahkan menyerahkan "
				+ " left join strukPelayanan.kdruanganasal ruanganAsal "
				+ " where strukPelayanan.keteranganlainnya in('Sterilisasi Cssd') " 
				+ " and strukPelayanan.tglstruk between date(:startDate) and date(:endDate)")
	List<Map<String, Object>> getPermintaanSterilisasiDariRuangan(@Param("startDate")String startDate, @Param("endDate")String endDate);
	
	@Query("select new Map (ruanganAsal.namaRuangan as namaRuangan, "
			+ " strukPelayanan.noRec as strukPelayananId, "
			+ " strukPelayanan.noUrut as noUrut,strukPelayanan.nostruk as noOrder, "
			+ " strukPelayanan.tglstruk as tanggalOrder,prosesSterilisasi.name as prosesSterilisasi, "
			+ " strukPelayanan.tglAmbil as tglAmbil, "
			+ " menyerahkan.namaLengkap as menyerahkan, "
			+ " pegawaiPenerima.namaLengkap as petugasPenerima) "
			+ " from StrukPelayananDetail strukPelayananDetail "
			+ " left join strukPelayananDetail.nostruk strukPelayanan "
			+ " left join strukPelayanan.cssdProsesSterilisasi prosesSterilisasi "
			+ " left join strukPelayanan.menerima pegawaiPenerima  "
			+ " left join strukPelayanan.menyerahkan menyerahkan "
			+ " left join strukPelayanan.kdruanganasal ruanganAsal "
			+ " where strukPelayanan.keteranganlainnya in('Sterilisasi Cssd') " 
			+ " and strukPelayanan.tglstruk between date(:startDate) and date(:endDate)")
	List<Map<String, Object>> getPermintaanSterilisasiDetailDariRuangan(@Param("startDate")String startDate, @Param("endDate")String endDate);
	
	@Query("select new Map (departemen.id as idDepartemen, "
			+ "departemen.namaDepartemen as namaDepartemen, "
			+ "ruangan.id as idRuangan, "
			+ "ruangan.namaRuangan as namaRuangan, "
			+ "produk.id as idProduk, "
			+ "produk.namaProduk as namaProduk, "
			+ "levelProduk.levelProduk as levelProduk, "
			+ "to_char(strukPelayanan.tglstruk, 'dd/MM/yyyy') as tglStruk, "
			+ "coalesce(strukPelayananDetail.qtyproduk, 0) as qty) "
			+ "from StrukPelayanan strukPelayanan "
			+ "inner join strukPelayanan.strukPelayananDetail strukPelayananDetail "
			+ "left join strukPelayanan.kdruangan ruangan "
			+ "inner join strukPelayananDetail.kdproduk produk "
			+ "inner join ruangan.departemen departemen "
			+ "left join produk.levelProduk levelProduk "
			+ "where strukPelayanan.keteranganlainnya = 'Sterilisasi Cssd' "
			+ "and strukPelayanan.tglterimakiriman is not null "
			+ "and to_char(strukPelayanan.tglstruk, 'yyyy-MM-dd') between :startDate and :endDate "
			+ "order by departemen.id, ruangan.id, strukPelayanan.tglstruk")
	List<Map<String, Object>> getRawDetailRekapSterilisasi(@Param("startDate")String startDate, @Param("endDate")String endDate);
	
	@Query("select new Map (departemen.id as idDepartemen, "
			+ "departemen.namaDepartemen as namaDepartemen, "
			+ "ruangan.id as idRuangan, "
			+ "ruangan.namaRuangan as namaRuangan, "
			+ "produk.id as idProduk, "
			+ "produk.namaProduk as namaProduk, "
			+ "levelProduk.levelProduk as levelProduk, "
			+ "to_char(strukPelayanan.tglstruk, 'dd/MM/yyyy') as tglStruk, "
			+ "coalesce(strukPelayananDetail.qtyproduk, 0) as qty) "
			+ "from StrukPelayanan strukPelayanan "
			+ "inner join strukPelayanan.strukPelayananDetail strukPelayananDetail "
			+ "left join strukPelayanan.kdruangan ruangan "
			+ "inner join strukPelayananDetail.kdproduk produk "
			+ "inner join ruangan.departemen departemen "
			+ "left join produk.levelProduk levelProduk "
			+ "where strukPelayanan.keteranganlainnya = 'Sterilisasi Cssd' "
			+ "and departemen.id = :idDepartemen " 
			+ "and strukPelayanan.tglterimakiriman is not null "
			+ "and to_char(strukPelayanan.tglstruk, 'yyyy-MM-dd') between :startDate and :endDate "
			+ "order by departemen.id, ruangan.id, strukPelayanan.tglstruk")
	List<Map<String, Object>> getRawDetailRekapSterilisasiByDepartemen(@Param("startDate")String startDate, @Param("endDate")String endDate, @Param("idDepartemen")Integer idDepartemen);
	
	@Query("select new Map (departemen.id as idDepartemen, "
			+ "departemen.namaDepartemen as namaDepartemen, "
			+ "ruangan.id as idRuangan, "
			+ "ruangan.namaRuangan as namaRuangan, "
			+ "produk.id as idProduk, "
			+ "produk.namaProduk as namaProduk, "
			+ "levelProduk.levelProduk as levelProduk, "
			+ "to_char(strukPelayanan.tglstruk, 'dd/MM/yyyy') as tglStruk, "
			+ "sum(coalesce(strukPelayananDetail.qtyproduk, 0)) as qty) "
			+ "from StrukPelayanan strukPelayanan "
			+ "inner join strukPelayanan.strukPelayananDetail strukPelayananDetail "
			+ "left join strukPelayanan.kdruangan ruangan "
			+ "inner join strukPelayananDetail.kdproduk produk "
			+ "inner join ruangan.departemen departemen "
			+ "left join produk.levelProduk levelProduk "
			+ "where strukPelayanan.keteranganlainnya = 'Sterilisasi Cssd' "
			+ "and produk.id = :idProduk "
			+ "and strukPelayanan.tglterimakiriman is not null "
			+ "and to_char(strukPelayanan.tglstruk, 'yyyy-MM-dd') between :startDate and :endDate "
			+ "group by departemen.id, ruangan.id, produk.id, levelProduk.levelProduk, strukPelayanan.tglstruk "
			+ "order by departemen.id, ruangan.id, strukPelayanan.tglstruk")
	List<Map<String, Object>> getRawDetailRekapSterilisasiByProduk(@Param("startDate")String startDate, @Param("endDate")String endDate, @Param("idProduk")Integer idProduk);	
	
	@Query("select new Map (departemen.id as idDepartemen, "
			+ "departemen.namaDepartemen as namaDepartemen, "
			+ "ruangan.id as idRuangan, "
			+ "ruangan.namaRuangan as namaRuangan, "
			+ "produk.id as idProduk, "
			+ "produk.namaProduk as namaProduk, "
			+ "levelProduk.levelProduk as levelProduk, "
			+ "to_char(strukPelayanan.tglstruk, 'dd/MM/yyyy') as tglStruk, "
			+ "sum(coalesce(strukPelayananDetail.qtyproduk, 0)) as qty) "
			+ "from StrukPelayanan strukPelayanan "
			+ "inner join strukPelayanan.strukPelayananDetail strukPelayananDetail "
			+ "left join strukPelayanan.kdruangan ruangan "
			+ "inner join strukPelayananDetail.kdproduk produk "
			+ "inner join ruangan.departemen departemen "
			+ "left join produk.levelProduk levelProduk "
			+ "where strukPelayanan.keteranganlainnya = 'Sterilisasi Cssd' "
			+ "and ruangan.id = :idRuangan "
			+ "and strukPelayanan.tglterimakiriman is not null "
			+ "and to_char(strukPelayanan.tglstruk, 'yyyy-MM-dd') between :startDate and :endDate "
			+ "group by departemen.id, ruangan.id, produk.id, levelProduk.levelProduk, strukPelayanan.tglstruk "
			+ "order by departemen.id, ruangan.id, strukPelayanan.tglstruk")
	List<Map<String, Object>> getRawDetailRekapSterilisasiByRuangan(@Param("startDate")String startDate, @Param("endDate")String endDate, @Param("idRuangan")Integer idRuangan);
	
	@Query("select distinct new Map (departemen.id as idDepartemen, "
			+ "departemen.namaDepartemen as namaDepartemen) "
			+ "from StrukPelayanan strukPelayanan "
			+ "inner join strukPelayanan.strukPelayananDetail strukPelayananDetail "
			+ "left join strukPelayanan.kdruangan ruangan "
			+ "inner join strukPelayananDetail.kdproduk produk "
			+ "inner join ruangan.departemen departemen "
			+ "left join produk.levelProduk levelProduk "
			+ "where strukPelayanan.keteranganlainnya = 'Sterilisasi Cssd' "
			+ "and strukPelayanan.tglterimakiriman is not null "
			+ "and to_char(strukPelayanan.tglstruk, 'yyyy-MM-dd') between :startDate and :endDate "
			+ "order by departemen.id")
	List<Map<String, Object>> getListDepartemenRekapSterilisasi(@Param("startDate")String startDate, @Param("endDate")String endDate);
	
	@Query("select distinct new Map (produk.id as idProduk, "
			+ "produk.namaProduk as namaProduk) "
			+ "from StrukPelayanan strukPelayanan "
			+ "inner join strukPelayanan.strukPelayananDetail strukPelayananDetail "
			+ "left join strukPelayanan.kdruangan ruangan "
			+ "inner join strukPelayananDetail.kdproduk produk "
			+ "inner join ruangan.departemen departemen "
			+ "left join produk.levelProduk levelProduk "
			+ "where strukPelayanan.keteranganlainnya = 'Sterilisasi Cssd' "
			+ "and strukPelayanan.tglterimakiriman is not null "
			+ "and to_char(strukPelayanan.tglstruk, 'yyyy-MM-dd') between :startDate and :endDate "
			+ "order by produk.id")
	List<Map<String, Object>> getListProdukRekapSterilisasi(@Param("startDate")String startDate, @Param("endDate")String endDate);

	@Query("select distinct new Map (ruangan.id as idRuangan, "
			+ "ruangan.namaRuangan as namaRuangan) "
			+ "from StrukPelayanan strukPelayanan "
			+ "inner join strukPelayanan.strukPelayananDetail strukPelayananDetail "
			+ "left join strukPelayanan.kdruangan ruangan "
			+ "inner join strukPelayananDetail.kdproduk produk "
			+ "inner join ruangan.departemen departemen "
			+ "left join produk.levelProduk levelProduk "
			+ "where strukPelayanan.keteranganlainnya = 'Sterilisasi Cssd' "
			+ "and strukPelayanan.tglterimakiriman is not null "
			+ "and to_char(strukPelayanan.tglstruk, 'yyyy-MM-dd') between :startDate and :endDate "
			+ "order by ruangan.id")
	List<Map<String, Object>> getListRuanganRekapSterilisasi(@Param("startDate")String startDate, @Param("endDate")String endDate);
}
