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

@Repository("HasilPemeriksaanSwaPantauLimbahCairDao")
public interface HasilPemeriksaanSwaPantauLimbahCairDao extends PagingAndSortingRepository<HasilPemeriksaanSwaPantauLimbahCair, String> {

	@Query("select model.inletMeterAirSekarang from HasilPemeriksaanSwaPantauLimbahCair model where date(model.tanggal)=(select max(m.tanggal) from HasilPemeriksaanSwaPantauLimbahCair m)")
	public Double getInletMeterAirLalu();
	
	@Query("select model.outletMeterAirSekarang from HasilPemeriksaanSwaPantauLimbahCair model where date(model.tanggal)=(select max(m.tanggal) from HasilPemeriksaanSwaPantauLimbahCair m)")
	public Double getOutletMeterAirLalu();
	
	@Query("select model from HasilPemeriksaanSwaPantauLimbahCair model where date(model.tanggal) between date(:periodeAwal) and date(:periodeAkhir)")
	public List<HasilPemeriksaanSwaPantauLimbahCair> findByPeriode(@Param("periodeAwal") String periodeAwal,
			@Param("periodeAkhir") String periodeAkhir);

	@Query("select new map (coalesce(swa.inletDebit,0.0) as inletDebit,"
			+ "coalesce(swa.inletMeterAirLalu,0.0) as inletMeterAirLalu,"
			+ "coalesce(swa.inletMeterAirSekarang,0.0) as inletMeterAirSekarang,"
			+ "coalesce(swa.keterangan,'-') as keterangan,"
			+ "coalesce(swa.outletDebit,0.0) as outletDebit,"
			+ "coalesce(swa.outletMeterAirLalu,0.0) as outletMeterAirLalu,"
			+ "coalesce(swa.outletMeterAirSekarang,0.0) as outletMeterAirSekarang,"
			+ "coalesce(swa.tanggal,null) as tanggal) from HasilPemeriksaanSwaPantauLimbahCair swa where swa.noRec=:noRec ")
	public Map<String,Object> findById(@Param("noRec") String noRec);
	
	@Query("select new map(l.noRec as noRec, " +
			" to_char(l.tanggal, 'yyyy-MM-dd') as tanggal, " +
			" l.inletMeterAirLalu as inletMeterAirLalu, " +
			" l.inletMeterAirSekarang as inletMeterAirLalu, " +
			" l.inletDebit as inlet, " +
			" l.outletMeterAirSekarang as outletMeterAirSekarang, " +
			" l.outletMeterAirLalu as outletMeterAirLalu, " +
			" l.outletDebit as outlet, " + 
			" coalesce((select a.nilai from HasilPemeriksaanSwaDetail a where a.hasilPemeriksaanSwaPantauLimbahCairId=l.noRec and a.parameterId=1),0.0) as do, " +
			" coalesce((select b.nilai from HasilPemeriksaanSwaDetail b where b.hasilPemeriksaanSwaPantauLimbahCairId=l.noRec and b.parameterId=2),0.0) as suhu, " +
			" coalesce((select c.nilai from HasilPemeriksaanSwaDetail c where c.hasilPemeriksaanSwaPantauLimbahCairId=l.noRec and c.parameterId=3),0.0) as ph, " +
			" coalesce((select d.nilai from HasilPemeriksaanSwaDetail d where d.hasilPemeriksaanSwaPantauLimbahCairId=l.noRec and d.parameterId=5),0.0) as tds, " +
			" coalesce((select e.nilai from HasilPemeriksaanSwaDetail e where e.hasilPemeriksaanSwaPantauLimbahCairId=l.noRec and e.parameterId=6),0.0) as conductivity, " +
			" l.keterangan as ket) " +
			" from HasilPemeriksaanSwaPantauLimbahCair l " +
			" where to_char(l.tanggal, 'yyyy-MM-dd') " +
			" between :startDate and :endDate " +
			" order by l.tanggal asc")
	List<Map<String, Object>> getHasilPemeriksaanPantauLImbahCair(@Param("startDate")String startDate, @Param("endDate")String endDate);
	
	@Query("select new map(h.noRec as noRecDetail, " +
			" h.nilai as nilai, " +
			" p.id as idParameter, p.nama as nama, " +
			" s.id as idSatuan, s.satuanStandar as satuanStandar) " +
			" from HasilPemeriksaanSwaDetail h " +
			" left join h.hasilPemeriksaanSwaPantauLimbahCair l " +
			" left join h.parameter p left join " +
			" h.satuanStandar s " +
			" where l.noRec=:noRec")
	List<Map<String, Object>> getHasilPemeriksaanPantauLImbahCairDetail(@Param("noRec")String noRec);
}
