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

@Repository("PelayananLaboratoriumTempDao")
public interface PelayananLaboratoriumTempDao extends PagingAndSortingRepository<LisOrderTmpSysmex, Integer> {
	// tabel header dan detail belum di relasikan
	@Query("SELECT so.noOrderIntern FROM StrukOrder so where so.noOrder = :noOrder")
	String getNolab(@Param("noOrder") String noOrder);

	@Query("select NEW MAP(lis.ono as noLab,  lis.orderTestID as ordertestid, lis.pid as rm , lis.requestDt as dt)  FROM "
			+ " LisOrderTmpSysmex lis  where  lis.ono  = :noOrder  ")
	Map<String, Object> getOrderFromLisOrderTmp(@Param("noOrder") String noOrder);
	
	@Query("select NEW MAP(lis.ono as noLab, lis.requestDt as tglRequest)  FROM "
			+ " ReshdSysmex lis  where  lis.pid  = :rm  ")
	List<Map<String, Object>> getNolabFromReshd(@Param("rm") String rm);
	
	@Query("select NEW MAP(dt.ono as idPasLab , dt.testCd as kdTesExt, dt.hisDetil as kdTes, dt.testNm as namaTes, dt.resultValue as hasil, dt.validateBy as validator,  "
			+ " dt.resultFt as hasilFt, dt.unit as satuan, dt.refRange as nilaiNormal, dt.testComment as komen,	dt.dispSeq as disseq , dt.testGroup as group , dt.flag as flag) FROM "
			+ "ResdtSysmex dt where  dt.ono  = :noLab order by dt.dispSeq asc")
	List<Map<String, Object>> getOrderFromLisDetail(@Param("noLab") String noLab);

	@Query("select NEW MAP( labdet.id as idDetail,	labdet.detailPemeriksaan as detailPeriksa, "
			+ "prod.namaProduk as namaProduk)  from ProdukDetailLaboratorium labdet left join labdet.produk prod "
			+ "where prod.id = :idProd ")
	List<Map<String, Object>> getProduk(@Param("idProd") Integer idProd);

	@Query("select NEW MAP(hp.noRec as noRec , pdl.id as idDetail) from "
			+ " HasilPemeriksaan hp "
			+ " left join hp.detail pdl "
			+ " left join hp.strukHasilPemeriksaan shp "
			+ " left join shp.strukOrder so "
			+ " WHERE "
			+ " so.noOrder = :noOrder  ")
	List<Map<String, Object>> getNoRecHasilPemeriksaanByNoOrder(@Param("noOrder") String noOrder); 
	
	@Query("select New MAP (so.noOrderIntern as idLab, p.detail.detailPemeriksaan as namaPemeriksaan, p.hasil as hasilPemeriksaan, "
			 + " p.satuan as satuan, p.comment as keterangan, p.flag as flag,shp.tglHasil as tglRequest,  p.nilaiNormal as nilaiNormal,"
			 + " p.produk.namaProduk as paket, p.urutan as urutan) "
			 + " from HasilPemeriksaan p "
			 + " left join p.strukHasilPemeriksaan shp  "
			 + " left join shp.strukOrder so "
			 + " left join so.noCm pasien "
			 + " where so.noOrder =:noOrder")
	List<Map<String, Object>> getHasilPemeriksaan(@Param("noOrder") String noOrder); 
	
}