package com.jasamedika.medifirst2000.dao;

import java.util.Date;
import java.util.List;

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

/**
 * Repository class for HasilPemeriksaan
 * 
 * @author Generator
 */
@Repository("HasilPemeriksaanDao")
public interface HasilPemeriksaanDao extends PagingAndSortingRepository<HasilPemeriksaan, String> {
	
	@Query("select p from HasilPemeriksaan p where p.strukHasilPemeriksaan.strukOrder.noOrder=:noOrder order by p.detail.qCharDetailPemeriksaan,p.detail.detailPemeriksaan")
	List<HasilPemeriksaan> findByNoOrder(@Param("noOrder") String noOrder);
	
	@Query("select max(cast(p.noHasilPeriksa as int)) from StrukHasilPemeriksaan p  ")
	Integer getCountByMonthAndYearRuangan();

	@Query("select p from HasilPemeriksaan p where p.strukHasilPemeriksaan.strukOrder.tglOrder <:date and p.detail.id =:id  and p.strukHasilPemeriksaan.strukOrder.noCm.noCm=:noCm")
	List<HasilPemeriksaan> findHistoryDetail(@Param("date")Date date,@Param("id") Integer id,@Param("noCm") String noCm);
	
	@Query("select p from HasilPemeriksaan p where p.strukHasilPemeriksaan.tglHasil between :startDate and :endDate  and p.produk.detailJenisProduk.jenisProduk.id =:id")
	List<HasilPemeriksaan> getReportHasilPemeriksaanOld(@Param("startDate")Date startDate, @Param("endDate")Date endDate, @Param("id")Integer id);
	
	@Query("select distinct p from HasilPemeriksaan p,OrderPelayanan q,StrukHasilPemeriksaan r "
			+ " where q.noHasilPeriksa.noRec=r.noRec "
			+ " and p.strukHasilPemeriksaan.noRec=r.noRec "
			+ " and p.strukHasilPemeriksaan.tglHasil between :startDate and :endDate  and p.produk.detailJenisProduk.jenisProduk.id =:id")
	List<HasilPemeriksaan> getReportHasilPemeriksaan(@Param("startDate")Date startDate, @Param("endDate")Date endDate, @Param("id")Integer id);
	
	HasilPemeriksaan findByDetailIdAndStrukHasilPemeriksaanStrukOrderNoOrderIntern(Integer idPemeriksaan,String noOrder);

	@Query("select count(*),model.produk.id from HasilPemeriksaan model group by model.produk.id order by count(*) desc")
	List<HasilPemeriksaan> getPemeriksaanSeringLaboratorium(Integer id);
	
	@Query("select hp from "
			+ " HasilPemeriksaan hp "
			+ " left join hp.detail pdl "
			+ " left join hp.strukHasilPemeriksaan shp "
			+ " left join shp.strukOrder so "
			+ " WHERE "
			+ " so.noOrder = :noOrder  ")
	List<HasilPemeriksaan> getHasilByNoOrder(@Param("noOrder") String noOrder); 
}
