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

@Repository("ObservasiTransaksiDao")
public interface ObservasiTransaksiDao extends PagingAndSortingRepository<ObservasiTransaksi, String>{
	
	@Query("select new map (observasi.namaExternal as observasi,observasi.id as idObservasi, "+
			"jenisObservasi.name as jenisObservasi,jenisObservasi.id as idJenisObservasi) "+
			"from Observasi observasi  "+
			"left join observasi.jenisObservasi jenisObservasi  "+
			"where observasi.observasiId = :id  "+
			"order by jenisObservasi.id, observasi.id")  
	List<Map<String,Object>> getObservasi(@Param("id") Integer id);
	
	@Query("select jenisObservasi.name as jenisObservasi "+
			"from JenisObservasi jenisObservasi "+
			"order by jenisObservasi.id")  
	List<String> getJenisObservasi();
	
	@Query("select new map (observasiTransaksi.valueI as valueI, "+
			"observasiTransaksi.valueII as valueII,observasi.namaExternal as observasi, "+
			"jenisObservasi.name as jenisObservasi, "+
			"observasi.id as idObservasi,to_char(observasiTransaksi.tglObservasi, 'yyyy-MM-dd HH:mm') as tglObservasi) "+
			"from ObservasiTransaksi observasiTransaksi "+
			"left join observasiTransaksi.observasi observasi "+
			"left join observasi.jenisObservasi jenisObservasi "+ 
			"left join observasiTransaksi.noRegistrasi pasienDaftar "+
			"where to_char(observasiTransaksi.tglObservasi, 'yyyy-MM-dd HH:mm') "+
			"between :startDate and :endDate and pasienDaftar.noRegistrasi =:noRegistrasi "+
			"order by observasiTransaksi.tglObservasi")  
	List<Map<String,Object>> getValueObservasi(@Param("startDate") String startDate, @Param("endDate") String endDate, @Param("noRegistrasi") String noRegistrasi);
	
	@Query("select new map (variabel.variabel as variabel, detail.hasil as hasil, "+
			" detail.valueI as valueI, detail.valueII as valueII) "+
			"from Variabel variabel "+
			"left join variabel.jenisObservasi jenisObservasi "+
			"left join variabel.variabelDetail detail "+
			"where jenisObservasi.name  like :jenisObservasi")  
	List<Map<String,Object>> getValueVariabel(@Param("jenisObservasi") String jenisObservasi);
}
