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

@Repository("ipsrsPemakaianRuanganDao")
public interface IpsrsPemakaianRuanganDao extends PagingAndSortingRepository<IpsrsPemakaianRuangan, String>{

	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.namaRuangan as namaRuangan) "
			+ "from Ruangan a")
	List<Map<String, Object>> getRuanganPemakaianRuangan();
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.jenisPemakaian as jenisPemakaian) "
			+ "from IpsrsJenisPemakaian a")
	List<Map<String, Object>> getJenisPemakaianRuangan();
	
	@Query("select NEW Map ("
			+ "a.satuan as satuan, "
			+ "a.id as jenisPemakaian, "
			+ "b.namaProduk as namaProduk) "
			+ "from IpsrsKapasitasJenisPemakaian a "
			+ "left join a.produk b "
			+ "where a.ipsrsJenisPemakaianId=:id")
	List<Map<String, Object>> getKapasitasJenisPemakaianRuangan(@Param("id") Integer id);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "a.periode as periode, "
			+ "a.jumlahMeterAwal as jumlahMeterAwal, "
			+ "a.jumlahMeterAhir as jumlahMeterAhir, "
			+ "a.jumlahPemakaian as jumlahPemakaian, "
			+ "a.biayaPerBulan as biayaPerBulan, "
			+ "b.id as idRuangan, "
			+ "b.namaRuangan as namaRuangan, "
			+ "c.id as idSatuan, "
			+ "c.satuan as satuan, "
			+ "d.id as idJenisPemakaian, "
			+ "d.namaProduk as jenisPemakaian) "
			+ "from IpsrsPemakaianRuangan a "
			+ "left join a.ruangan b "
			+ "left join a.ipsrsKapasitasJenisPemakaian c "
			+ "left join c.produk d")
	List<Map<String, Object>> getAllPemakaianRuangan();
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "a.periode as periode, "
			+ "a.jumlahMeterAwal as jumlahMeterAwal, "
			+ "a.jumlahMeterAhir as jumlahMeterAhir, "
			+ "a.jumlahPemakaian as jumlahPemakaian, "
			+ "a.biayaPerBulan as biayaPerBulan, "
			+ "b.namaRuangan as namaRuangan, "
			+ "c.satuan as satuan, "
			+ "d.namaProduk as jenisPemakaian) "
			+ "from IpsrsPemakaianRuangan a "
			+ "left join a.ruangan b "
			+ "left join a.ipsrsKapasitasJenisPemakaian c "
			+ "left join c.produk d "
			+ "where c.ipsrsJenisPemakaianId=:id and "
			+ "date(a.periode) between "
			+ "date(:periodeAwal) and "
			+ "date(:periodeAkhir)")
	List<Map<String, Object>> findPemakaianRuanganByPeriode(
			@Param("periodeAwal") String periodeAwal,
			@Param("periodeAkhir") String periodeAkhir,
			@Param("id") Integer id);
	
	@Query("select NEW Map ("
			+ "a.jumlahPemakaian as jumlahPemakaian, "
			+ "c.hargaNetto1 as hargaSatuan) "
			+ "from IpsrsPemakaianRuangan a "
			+ "left join a.ipsrsKapasitasJenisPemakaian b, "
			+ "StokProdukGlobal c where "
			+ "month(a.periode)=:bulan and "
			+ "year(a.periode)=:tahun and "
			+ "a.ipsrsKapasitasJenisPemakaianId=:id and "
			+ "c.produkId=b.produkId")
	List<Map<String, Object>> findPemakaianGasMedis(
			@Param("bulan") Integer bulan, 
			@Param("tahun") Integer tahun,
			@Param("id") Integer id);

}
