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

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

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.dao.custom.B3RuanganInputDataDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.B3DaftarBahanBerbahaya;
import com.jasamedika.medifirst2000.entities.B3RuanganInputData;

@Repository
public class B3RuanganInputDataDaoCustomImpl extends 
			CoreDaoImpl<B3RuanganInputData> implements B3RuanganInputDataDaoCustom{

	public B3RuanganInputDataDaoCustomImpl() {
		super(B3RuanganInputData.class);
	}
	
	@Override
	public List<Map<String, Object>> getRuangan(){
		Query query = em.createQuery("select NEW Map (model.id as ruanganId, model.namaRuangan as namaRuangan) from Ruangan model");
		return query.getResultList();
	}
	
	@Override
	public List<Map<String, Object>> getB3StokGlobal(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map (");
		buffer.append("a.qtyProduk as jumlahStok, ");
		buffer.append("a.id as produkId, ");
		buffer.append("b.namaProduk as namaProduk, ");
		buffer.append("c.satuanStandar as wadahKemasan) ");
		buffer.append("from StokProdukGlobal a ");
		buffer.append("left join a.produk b ");
		buffer.append("left join b.satuanStandar c ");
		buffer.append("left join b.detailJenisProduk d ");
		buffer.append("left join d.jenisProduk e ");
		buffer.append("left join e.kelompokProduk f ");
		buffer.append("where f.kelompokProduk='Barang Persediaan'");
		
		Query query = em.createQuery(buffer.toString());
		return query.getResultList();
	}
	
	@Override
	public List<Map<String, Object>> getB3BahanRuangan(Integer ruanganId){
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map (");
		buffer.append("a.noRec as noRec, ");
		buffer.append("b.id as ruanganId, ");
		buffer.append("b.namaRuangan as namaRuangan, ");
		buffer.append("c.namaLengkap as namaPetugas, ");
		buffer.append("d.banyakPemakaian as banyakPemakaian, ");
		buffer.append("d.lamaPemakaian as lamaPemakaian, ");
		buffer.append("d.satuanPemakaian as satuanPemakaian, ");
		buffer.append("d.jumlah as jumlah, ");
		buffer.append("d.karakteristik as karakteristik, ");
		buffer.append("d.tempatPenyimpanan as tempatPenyimpanan, ");
		buffer.append("e.qtyProduk as jumlahStok, ");
		buffer.append("f.id as produkId, ");
		buffer.append("f.namaProduk as namaProduk, ");
		buffer.append("g.satuanStandar as wadahKemasan) ");
		buffer.append("from B3RuanganInputData a ");
		buffer.append("left join a.ruangan b ");
		buffer.append("left join a.petugas c ");
		buffer.append("left join a.b3DaftarBahanBerbahaya d ");
		buffer.append("left join d.bahanB3 e ");
		buffer.append("left join e.produk f ");
		buffer.append("left join f.satuanStandar g ");
		buffer.append("where b.id=:ruanganId");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("ruanganId", ruanganId);
		return query.getResultList();
	}
	
	@Override
	@Transactional
	public Integer updateB3DaftarBahanBerbahaya(B3DaftarBahanBerbahaya model){
		StringBuffer buffer = new StringBuffer();
		buffer.append("update B3DaftarBahanBerbahaya model set ");
		buffer.append("model.bahanB3Id=:bahanB3Id, ");
		buffer.append("model.banyakPemakaian=:banyakPemakaian, ");
		buffer.append("model.lamaPemakaian=:lamaPemakaian, ");
		buffer.append("model.satuanPemakaian=:satuanPemakaian, ");
		buffer.append("model.jumlah=:jumlah, ");
		buffer.append("model.karakteristik=:karakteristik, ");
		buffer.append("model.tempatPenyimpanan=:tempatPenyimpanan, ");		
		buffer.append("where model.noRec=:noRec");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", model.getNoRec());
		query.setParameter("bahanB3Id", model.getBahanB3Id());
		query.setParameter("banyakPemakaian", model.getBanyakPemakaian());
		query.setParameter("lamaPemakaian", model.getLamaPemakaian());
		query.setParameter("satuanPemakaian", model.getSatuanPemakaian());
		query.setParameter("jumlah", model.getJumlah());
		query.setParameter("karakteristik", model.getKarakteristik());
		query.setParameter("tempatPenyimpanan", model.getTempatPenyimpanan());
		query.setParameter("noRec", model.getNoRec());
		return query.executeUpdate();
	}
	
	@Override
	@Transactional
	public void deleteB3DaftarBahanBerbahaya(String noRec){
		B3DaftarBahanBerbahaya b3DaftarBahanBerbahaya = em.find(B3DaftarBahanBerbahaya.class, noRec);
		em.remove(em.contains(b3DaftarBahanBerbahaya) ? b3DaftarBahanBerbahaya : em.merge(b3DaftarBahanBerbahaya));
	}

}
