package com.jasamedika.medifirst2000.dao;

import java.util.Date;
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.StrukResep;

/**
 * Repository class for StrukResep
 * 
 * @author Generator
 */
@Repository("StrukResepDao")
public interface StrukResepDao extends PagingAndSortingRepository<StrukResep, String> {
	
	@Query("select p from StrukResep p where p.strukOrder.noOrder=:noOrder ")
	StrukResep FindByNoOrder(@Param("noOrder")String noOrder);
	
	@Query("select count(p) from StrukResep p where p.ruanganTujuan.id=:id and date(tglResep)=date(:tgl)")
	Integer findMaxRuangan(@Param("id") Integer id ,@Param("tgl") Date tgl);
	
	@Query("select NEW Map ("
			+ "a.noResep as noResep,"
			+ "a.tglResep as tglResep,"
			+ "a.namaPemberi as namaPemberi,"
			+ "a.namaLengkapAmbilResep as namaPengambil,"
			+ "b.noRec as noRecOrder,"
			+ "b.noOrder as noStruk,"
			+ "b.tglOrder as tglStruk,"
			+ "c.noCm as noCm,"
			+ "c.namaPasien as namaPasien,"
			+ "e.noRec as noRegistrasi) "
			+ "from StrukResep a "
			+ "left join a.strukOrder b "
			+ "left join b.noCm c "
			+ "left join a.ruanganTujuan d "
			+ "left join a.pasien e "
			+ "where d.id=:id")
	List<Map<String, Object>> getReturStrukResep(
			@Param("id") Integer id);
	
	@Query("select NEW Map ("
			+ "b.nostruk as noStruk,"
			+ "c.noRec as noSbmLast,"
			+ "e.id as produkId) "
			+ "from PelayananPasien a "
			+ "left join a.strukPelayanan b "
			+ "left join b.nosbmlast c "
			+ "left join a.strukOrder d "
			+ "left join a.produk e "
			+ "where d.noRec=:noRec")
	List<Map<String, Object>> getPelayananPasien(
			@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "a.id as id,"
			+ "a.namaRuangan as namaRuangan) "
			+ "from Ruangan a "
			+ "left join a.departemen b "
			+ "where b.id=:departemenId")
	List<Map<String, Object>> getRuanganFarmasi(
			@Param("departemenId") Integer departemenId);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRecPelayananPasien,"
			+ "a.jumlah as jumlah,"
			+ "b.id as produkId,"
			+ "b.namaProduk as namaProduk,"
			+ "a.hargaNetto as hargaBarang) "
			+ "from PelayananPasien a "
			+ "left join a.produk b "
			+ "left join a.strukOrder c "
			+ "where c.noRec=:noRec")
	List<Map<String, Object>> detailReturResep(
			@Param("noRec") String noRec);
	
	@Query("select setting.nilaiField from SettingDataFixed setting where setting.namaField=:kode")
	public String findIdRuanganFarmasi(@Param("kode") String kode);	
}
