package com.jasamedika.medifirst2000.dao.custom.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.CssdPengeringanDaoCustom;
import com.jasamedika.medifirst2000.entities.Ruangan;

/**
 * Repository Custom class
 * 
 * @author Roberto
 */

@Repository("CssdPengeringanDaoCustom")
public class CssdPengeringanDaoCustomImpl  implements CssdPengeringanDaoCustom {

	@PersistenceContext
	protected EntityManager em;

	@Override
	public Ruangan findRuanganAsalByStrukPelayanan(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select r from StrukPelayanan p left join p.kdruanganasal r where p.noRec=:noRec ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		Ruangan r= (Ruangan) query.getSingleResult();
		
		return r;
	}

	@Override
	public Ruangan findRuanganTujuanByStrukPelayanan(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select r from StrukPelayanan p left join p.kdruangan r where p.noRec=:noRec ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		Ruangan r= (Ruangan) query.getSingleResult();
		
		return r;
	}

	@Override
	public Map<String,Object> findStrukPelayananByNoRec(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map( strukPelayanan.nostruk as noOrder,p.tglPlanning as tanggalPlanning,p.tglPlanningAkhirAn as jamMulai,p.tglPlanningAwalAn as jamAwal , "
				+ "ruanganAsal.namaRuangan as ruanganAsal,ruanganTujuan.namaRuangan as ruanganTujuan , "
				+ "pegawaiPenerima.namaLengkap as petugas)"
				+ "from PlanningPelayanan p "
				+ "left join p.strukPelayanan strukPelayanan left join strukPelayanan.kdruangan ruanganTujuan "
				+ "left join strukPelayanan.kdruanganasal ruanganAsal "
				+ "left join strukPelayanan.menerima as pegawaiPenerima where  strukPelayanan.noRec=:noRec ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		Map<String,Object> r= (Map<String,Object>) query.getSingleResult();
		
		return r;
	}

	@Override
	public List<Map<String, Object>> findStrukPelayananDetailByNoRec(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map(produk.namaProduk as namaProduk,p.keterangan as hasil,p.qtyProdukExec as qtyProduk,coalesce(p.status,false) as status) "
				+ "from PlanningPelayanan p "
				+ "left join p.strukPelayanan strukPelayanan left join strukPelayanan.kdruangan ruanganTujuan "
				+ "left join strukPelayanan.kdruanganasal ruanganAsal "
				+ "left join strukPelayanan.menerima as pegawaiPenerima "
				+ "left join p.produk produk where  strukPelayanan.noRec=:noRec ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		List<Map<String,Object>> r= (List<Map<String,Object>>) query.getResultList();
		
		return r;
	}

	
}
