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.CssdMonitoringSteamDaoCustom;
import com.jasamedika.medifirst2000.entities.Ruangan;

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

@Repository("CssdMonitoringSteamDaoCustom")
public class CssdMonitoringSteamDaoCustomImpl  implements CssdMonitoringSteamDaoCustom {

	@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;
	}
	
	public int checkPenyimpanan(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(sp.noRec) from PlanningPelayanan p left join p.strukPlaning planning left join p.strukPelayanan sp where sp.noRec=:noRec and planning.deskripsiplanning='MonitoringSteam' ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		return ((Long) query.getSingleResult()).intValue();
	}

	@Override
	public Map<String,Object> findStrukPelayananByNoRec(String noRec) {
		

		StringBuffer buffer = new StringBuffer();
		if(checkPenyimpanan(noRec)>0){
			buffer.append(" select new Map(strukPelayanan.noUrut as noBundel,planing.noRec as noRec,strukPelayanan.nostruk as noOrder,p.tglPlanning as tanggalPlanning, "
					+ "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 p.strukPlaning planing "
					+ "left join strukPelayanan.kdruanganasal ruanganAsal "
					+ "left join strukPelayanan.menerima as pegawaiPenerima where  strukPelayanan.noRec=:noRec and planing.deskripsiplanning='MonitoringSteam' ");
		
		}else{
			buffer.append(" select new Map(strukPelayanan.noUrut as noBundel,'' as noRec, strukPelayanan.nostruk as noOrder,'-' as tanggalPlanning, "
					+ "ruanganAsal.namaRuangan as ruanganAsal,ruanganTujuan.namaRuangan as ruanganTujuan , "
					+ "pegawaiPenerima.namaLengkap as petugas)"
					+ "from 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.setMaxResults(1);
		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();
		if(checkPenyimpanan(noRec)>0){
			buffer.append(" select new map(p.noRec as noRec,hasil.alatBerkarat as alatBerkarat,");
			buffer.append(" hasil.jamMulaiSterilisasi as jamMulaiSterilisasi,");
			buffer.append(" hasil.jamSelesaiSterilisasi as jamSelesaiSterilisasi,");
			buffer.append(" hasil.kebersihanAlat as kebersihanAlat,");
			buffer.append(" hasil.kelayakanAlat as kelayakanAlat,");
			buffer.append(" hasil.kelenturanAlat as kelenturanAlat,");
			buffer.append(" hasil.kemampuanMenjepit as kemampuanMenjepit,");
			buffer.append(" hasil.ketajamanAlat as ketajamanAlat,");
			buffer.append(" hasil.keutuhanAlat as keutuhanAlat,");
			buffer.append(" hasil.kondisiAlat as kondisiAlat,");
			buffer.append(" hasil.linenYangdikemas as linenYangDikemas,");
			buffer.append(" hasil.tindakan as tindakan, ");
			buffer.append(" hasil.warnaAlat as warnaAlat, ");
			buffer.append(" produk.id as produkId,produk.namaProduk as namaProduk,p.qtyProdukExec as qtyProduk)");
			buffer.append(" from CssdUjiVisual hasil left join hasil.planningPelayanan p left join p.strukPlaning planing ");
			buffer.append(" left join p.strukPelayanan strukPelayanan left join strukPelayanan.kdruangan ruanganTujuan ");
			buffer.append(" left join strukPelayanan.kdruanganasal ruanganAsal ");
			buffer.append(" left join strukPelayanan.menerima as pegawaiPenerima ");
			buffer.append(" left join p.produk produk left join produk.satuanStandar satuan where  strukPelayanan.noRec=:noRec and planing.namaplanning='MonitoringSteam' ");
		}else{
			buffer.append(" select new map('' as noRec,'' as alatBerkarat,");
			buffer.append(" '' as jamMulaiSterilisasi,");
			buffer.append(" '' as jamSelesaiSterilisasi,");
			buffer.append(" '' as kebersihanAlat,");
			buffer.append(" '' as kelayakanAlat,");
			buffer.append(" '' as kelenturanAlat,");
			buffer.append(" '' as kemampuanMenjepit,");
			buffer.append(" '' as ketajamanAlat,");
			buffer.append(" '' as keutuhanAlat,");
			buffer.append(" '' as kondisiAlat,");
			buffer.append(" '' as linenYangDikemas,");
			buffer.append(" '' as tindakan, ");
			buffer.append(" '' as warnaAlat,produk.id as produkId,produk.namaProduk as namaProduk,detail.qtyproduk as qtyProduk) "
					+ "from StrukPelayananDetail detail left join detail.nostruk strukPelayanan left join strukPelayanan.kdruangan ruanganTujuan "
					+ "left join strukPelayanan.kdruanganasal ruanganAsal "
					+ "left join strukPelayanan.menerima as pegawaiPenerima "
					+ "left join detail.kdproduk produk  left join produk.satuanStandar satuan 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;
	
	}

	
}
