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

@Repository("TempatTidurDao")
public interface TempatTidurDao extends PagingAndSortingRepository<TempatTidur, Integer> {

	@Query("select count(p.id) from TempatTidur p where p.statusBedId=1 ")
	public Integer countTempatTidurTerpakai();

	@Query("select count(p.id) from TempatTidur p where p.statusBedId=2 ")
	public Integer countTempatTidurKosong();

	@Query("select count(p.id) from TempatTidur p where p.statusBedId=3 ")
	public Integer countTempatTidurProsesAdministrasi();

	@Query("select count(p.id) from TempatTidur p where p.statusBedId=1 and p.kamarId=:idKamar and p.kamar.kelasId=:idKelas")
	public Integer countTempatTidurTerpakaiByKamarAndKelas(@Param("idKamar") Integer idKamar,
			@Param("idKelas") Integer idKelas);

	@Query("select count(p.id) from TempatTidur p where p.statusBedId=2 and p.kamarId=:idKamar and p.kamar.kelasId=:idKelas")
	public Integer countTempatTidurKosongByKamarAndKelas(@Param("idKamar") Integer idKamar,
			@Param("idKelas") Integer idKelas);

	@Query("select count(p.id) from TempatTidur p where p.statusBedId=3 and p.kamarId=:idKamar and p.kamar.kelasId=:idKelas")
	public Integer countTempatTidurProsesAdministrasiByKamarAndKelas(@Param("idKamar") Integer idKamar,
			@Param("idKelas") Integer idKelas);

	@Query("select count(p.id) from TempatTidur p")
	public Integer countAllTempatTidur();

	@Query("SELECT p FROM TempatTidur p WHERE p.kamarId=:idKamar and p.kamar.kelasId=:idKelas ")
	public List<TempatTidur> findByKamarAndKelas(@Param("idKamar") Integer idKamar, @Param("idKelas") Integer idKelas);

	@Query("select new map(k.qtyBed as jmlbed, k.jumlaKamarIsi as bedIsi, k.jumlaKamarKosong as bedKosong, "
			+ " k.id as idKamar, k.namaKamar as namaKamar, r.id as idRuangan, r.namaRuangan as namaRuangan) "
			+ " from Kamar k, Ruangan r where k.ruanganId = r.id")
	public List<TempatTidur> getJmlTempatTidur();

	@Query("select new map(k.qtyBed as jmlbed, k.jumlaKamarIsi as bedIsi, k.jumlaKamarKosong as bedKosong, "
			+ " k.id as idKamar, k.namaKamar as namaKamar, r.id as idRuangan, r.namaRuangan as namaRuangan) "
			+ " from Kamar k, Ruangan r where k.ruanganId = r.id and k.ruanganId=:idRuangan")
	public List<TempatTidur> getJmlTempatTidurByIdRuangan(@Param("idRuangan") Integer idRuangan);

	@Query("select new map(k.qtyBed as jmlbed, k.jumlaKamarIsi as bedIsi, k.jumlaKamarKosong as bedKosong, "
			+ " k.id as idKamar, k.namaKamar as namaKamar, r.id as idRuangan, r.namaRuangan as namaRuangan) "
			+ " from Kamar k, Ruangan r where k.ruanganId = r.id and k.ruanganId=:idRuangan")
	public TempatTidur getTempatTidur(@Param("idRuangan") Integer idRuangan);

	@Query("select count(tt.id) from TempatTidur tt " + "inner join tt.kamar km where tt.statusEnabled is true "
			+ "and tt.kodeExternal = 'NEW' and km.ruanganId not in (:ruanganTanjung)")
	public Integer countTempatTidurAktif(@Param("ruanganTanjung") List<Integer> ruanganTanjung);

	@Query("select count(tt.id) from TempatTidur tt " + "inner join tt.kamar km where tt.statusEnabled is true "
			+ "and tt.kodeExternal = 'NEW' and km.ruanganId not in (:ruanganTanjung) and km.ruanganId = :idRuangan")
	public Integer countTempatTidurAktif(@Param("ruanganTanjung") List<Integer> ruanganTanjung,
			@Param("idRuangan") Integer idRuangan);

	@Query("select new Map(tt.id as id," + "tt.namaExternal as namaExternal,"
			+ "km.id as idKamar,km.namaKamar as namaKamar," + "ru.id as idRuangan,ru.namaRuangan as namaRuangan,"
			+ "rk.id as idRuangPerawatanKemenkes,rk.name as namaRuangPerawatanKemenkes,"
			+ "tt.statusEnabled as statusEnabled,tt.createdDate as createdDate) " + "from TempatTidur tt "
			+ "inner join tt.kamar km " + "inner join km.ruangan ru " + "left join tt.ruangPerawatanKemenkes rk "
			+ "where ru.departemenId = 16 " + "and tt.statusEnabled is true "
			+ "order by ru.namaRuangan,km.namaKamar,tt.namaExternal")
	public List<Map<String, Object>> findAllActiveBed();

}
