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.dto.SewaAsramaDto;
import com.jasamedika.medifirst2000.entities.Kamar;
import com.jasamedika.medifirst2000.entities.Kelas;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.dto.RumahDukaDto;

@Repository("KamarDao")
public interface KamarDao extends PagingAndSortingRepository<Kamar, Integer> {

	@Query("select distinct p.ruangan from Kamar p ")
	public List<Ruangan> getRuanganRawatInapFromKamar();

	@Query("select p from Kamar p where p.ruanganId=:idRuangan and p.kelasId=:idKelas")
	public List<Kamar> getKamarByRuanganAndKelas(@Param("idRuangan") Integer idRuangan,
			@Param("idKelas") Integer idKelas);

	@Query("select distinct p.kelas from Kamar p where p.ruanganId=:idRuangan ")
	public List<Kelas> getKelasByRuangan(@Param("idRuangan") Integer idRuangan);

	@Query("select new Map(kamar.id as id, kamar.namaKamar as namaKamar) "
			+ "from Kamar kamar where kamar.statusEnabled is true "
			+ "and kamar.ruanganId = :ruanganId order by kamar.namaKamar")
	public List<Map<String, Object>> getKamarByRuangan(@Param("ruanganId") Integer ruanganId);

	@Query("select distinct NEW com.jasamedika.medifirst2000.dto.SewaAsramaDto(k.id,k.namaKamar,k.kdKamar,so.tglPelayananAwal,"
			+ "so.tglPelayananAkhir,so.status) from Kamar k, StrukOrder so, MappingKamarAsrama mka "
			+ "where date(so.tglPelayananAwal) between date(:periodeAwal) and date(:periodeAkhir) and so.status='DIPAKAI' "
			+ "and k.id=mka.kamarId and k.id=:idKamar and so.noOrderIntern like '%SA%'")
	public List<SewaAsramaDto> findStatusKamar(@Param("periodeAwal") String periodeAwal,
			@Param("periodeAkhir") String periodeAkhir, @Param("idKamar") Integer idKamar);

	@Query("select k from Kamar k where k.keterangan='Tersedia' and k.id=:id")
	public Kamar findStatusTersedia(@Param("id") Integer id);

	@Query("select k from Kamar k where k.keterangan='DIPAKAI' and k.id=:id")
	public Kamar findStatusDipakai(@Param("id") Integer id);

	@Query("select NEW Map(k.id as idKamar,k.namaKamar as namaKamar) "
			+ "from Kamar k where lower(k.namaKamar) like '%duka%' order by k.id")
	public List<RumahDukaDto> findKamarRumahDuka();

	@Query("select NEW com.jasamedika.medifirst2000.dto.SewaAsramaDto(k.id,k.namaKamar,k.kdKamar,h.hargaSatuan,r.id,r.namaRuangan,p.id,p.namaProduk) "
			+ "from Kamar k, HargaNettoProdukByKelas h, Ruangan r, Kelas kls, Produk p where r.id=k.ruanganId "
			+ "and p.id=h.produkId and kls.id=h.kelasId and kls.id=k.kelasId and r.namaRuangan like %:namaRuangan% "
			+ "and p.id=(Select cast(s.nilaiField as int) from SettingDataFixed s where s.namaField='KdProdukSewaAsrama') order by k.id")
	public List<Kamar> findKamar(@Param("namaRuangan") String namaRuangan);

}
