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 com.jasamedika.medifirst2000.dao.custom.UnitCostDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.UnitCost;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Repository
public class UnitCostDaoCustomImpl extends CoreDaoImpl<UnitCost> implements UnitCostDaoCustom{

	public UnitCostDaoCustomImpl() {
		super(UnitCost.class);
	}

	@Override
	public List<Map<String,Object>> getPelayananpasienDetails(Integer idRuangan, Integer idInstalasiDepartemen, String periodeAwal,
			String periodeAkhir, Integer idLayanan) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map (count(sa.ruanganId) as qty, ");
		buffer.append("sa.ruanganId as idRuangan, ");
		buffer.append("pt.produkId as idProduk, ");
		buffer.append("pt.hargaSatuan as hargaSatuan, ");
		buffer.append("(count( sa.ruanganId) * coalesce(pt.hargaSatuan,0)) as totalHarga) ");
		buffer.append("from PelayananPasienDetail pt ");
		buffer.append("left join pt.pasienDaftar sa ");
		buffer.append(" where  1=1 ");
	 
		if (CommonUtil.isNotNullOrEmpty(idRuangan)) {
			buffer.append(" and  sa.ruanganId =:idRuangan ");
		}
		 
		if (CommonUtil.isNotNullOrEmpty(idLayanan)) {
			buffer.append(" and  pt.produkId =:idLayanan ");
		}
		if (CommonUtil.isNotNullOrEmpty(periodeAwal) && CommonUtil.isNotNullOrEmpty(periodeAkhir)) {
			buffer.append(" and date(pt.tglPelayanan) between date(:periodeAwal) and date(:periodeAkhir) ");
		}
		buffer.append(" group by sa.ruanganId,pt.produkId, pt.hargaSatuan ");
		Query query = em.createQuery(buffer.toString());
		 if (CommonUtil.isNotNullOrEmpty(periodeAwal) && CommonUtil.isNotNullOrEmpty(periodeAkhir)) {
			 query.setParameter("periodeAwal", periodeAwal);
			 query.setParameter("periodeAkhir", periodeAkhir);
		}
		 if (CommonUtil.isNotNullOrEmpty(idRuangan) ) {
			 query.setParameter("idRuangan", idRuangan);
			 
		}
		List<Map<String,Object>> list = query.getResultList();
		 
		 
		return list;
	}

	@Override
	public List<Map<String,Object>> getUnitCostDetails(Integer idRuangan, Integer idInstalasiDepartemen,
			String periodeAwal, String periodeAkhir, Integer idLayanan) {
		 
		//Get data pelayanan
		StringBuffer bufferPelayanan = new StringBuffer();
		bufferPelayanan.append("  select new map( sum(coalesce(ucd.qty,0)) as qty, ucd.unitCostId as idUnitCost, uc.namaUnitCost as namaUnitCost, ");
		bufferPelayanan.append(" sum(coalesce(ucd.qty,0) * coalesce(ucd.harga,0)) as total,uc.ruanganId as idRuangan,uc.produkId as idProduk )");
		bufferPelayanan.append(" from UnitCostDetail ucd left join ucd.unitCost uc");
		
		bufferPelayanan.append(" where 1=1 ");
		  
		if (CommonUtil.isNotNullOrEmpty(idLayanan) ) {
			bufferPelayanan.append(" and uc.produkId  = :idLayanan");
		}
		
		 if (CommonUtil.isNotNullOrEmpty(idRuangan)) {
			 bufferPelayanan.append(" and uc.ruanganId = :idRuangan ");
		}
 
		 
		 bufferPelayanan.append(" group by ucd.unitCostId,uc.ruanganId,uc.produkId,uc.namaUnitCost ");
		 Query queryPelayanan = em.createQuery(bufferPelayanan.toString());
		 if (CommonUtil.isNotNullOrEmpty(idRuangan)) {
			 queryPelayanan.setParameter("idRuangan", idRuangan);
		}
		 if (CommonUtil.isNotNullOrEmpty(idLayanan)) {
			 queryPelayanan.setParameter("idLayanan", idLayanan);
		}
		List<Map<String,Object>> list = queryPelayanan.getResultList();
		 
		return list;
	}

	@Override
	public List<Map<String, Object>> getListPelayanan(Integer idRuangan) {
		
		//Get data pelayanan
		StringBuffer bufferPelayanan = new StringBuffer();
		bufferPelayanan.append("  select new map(  p.id as idProduk, p.namaProduk as namaProduk ) ");
		bufferPelayanan.append(" from UnitCost uc ");
		bufferPelayanan.append(" left join uc.produk p ");
		bufferPelayanan.append(" left join uc.ruangan r ");
	 	  
		if (CommonUtil.isNotNullOrEmpty(idRuangan)) {
			 bufferPelayanan.append(" where  r.id = :idRuangan ");
		}

		Query queryPelayanan = em.createQuery(bufferPelayanan.toString());
		if (CommonUtil.isNotNullOrEmpty(idRuangan)) {
			 queryPelayanan.setParameter("idRuangan", idRuangan);
		}

		List<Map<String,Object>> list = queryPelayanan.getResultList();
		 
		return list;
	}

	@Override
	public List<Map<String, Object>> getListUnitCost(Integer idRuangan, Integer idInstalasi, String namaKegiatan) {
		 StringBuffer sqlBuffer= new StringBuffer();
		 sqlBuffer.append("SELECT new map( uc.id as id, uc.namaUnitCost as namaKegiatan, r.namaRuangan as ruangan ) ");
		 sqlBuffer.append("FROM UnitCost uc ");
		 sqlBuffer.append("LEFT JOIN uc.ruangan r ");
		 sqlBuffer.append("WHERE 1=1 ");
		 
		 if (CommonUtil.isNotNullOrEmpty(idRuangan)){
			 sqlBuffer.append(" AND r.id = :idRuangan ");
		 }
		 if (CommonUtil.isNotNullOrEmpty(namaKegiatan)){
			 sqlBuffer.append(" AND uc.namaUnitCost = :namaKegiatan ");
		 }
		 
		Query query = em.createQuery(sqlBuffer.toString());
		if (CommonUtil.isNotNullOrEmpty(idRuangan)) {
			query.setParameter("idRuangan", idRuangan);
		}
		if (CommonUtil.isNotNullOrEmpty(namaKegiatan)){
			query.setParameter("namaKegiatan",namaKegiatan);
		 }
		List<Map<String,Object>> list = query.getResultList();
		 
		return list;
	} 

}
