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

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

@Repository("CssdUjiSwapDaoCustom")
public class CssdUjiSwapDaoCustomImpl  implements CssdUjiSwapDaoCustom {

	@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 checkUjiSwap(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='Uji Swap' ");
		
		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();
		System.out.println(checkUjiSwap(noRec));
		if(checkUjiSwap(noRec)>0){
			buffer.append(" select new Map( planing.noRec as noRec,strukPelayanan.nostruk as noOrder,p.tglPlanning as tanggalPlanning,p.tglPlanningAwalAn as jamMulai,p.tglPlanningAkhirAn as jamAkhir , "
					+ "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='Uji Swap' ");
		
		}else{
			buffer.append(" select new Map('' as noRec, strukPelayanan.nostruk as noOrder,'-' as tanggalPlanning,'-' as jamMulai,'-' as jamAkhir , "
					+ "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(checkUjiSwap(noRec)>0){
			buffer.append(" select new map(p.jumlahBakteri as jumlahBakteri,p.noRec as noRec,produk.id as produkId,produk.namaProduk as namaProduk,p.keterangan as hasil,p.qtyProdukExec as qtyProduk,coalesce(p.status,false) as status) "
					+ "from PlanningPelayanan p left join p.strukPlaning planing "
					+ "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 and planing.deskripsiplanning='Uji Swap' order by p.status desc ");
		}else{
			buffer.append(" select new map('' as jumlahBakteri,'' as noRec,produk.id as produkId,produk.namaProduk as namaProduk,'-' as hasil,detail.qtyproduk as qtyProduk,'-' as status) "
					+ "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  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;
	}

	
}
