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

/**
 * Repository Custom class
 * 
 * @author Roberto
 */
@Repository
public class LimbahB3KeluarDaoCustomImpl extends CoreDaoImpl<LimbahB3Keluar> implements LimbahB3KeluarDaoCustom {

	public LimbahB3KeluarDaoCustomImpl() {
		super(LimbahB3Keluar.class);
	}

	@Override
	public List<Map<String,Object>> findList(int rowStart, int rowEnd, String sort, String dir, String dateStart,String dateEnd, Integer jenisLimbah) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map(coalesce(keluar.periodeAwal,null) as periodeAwal,");
		buffer.append("coalesce(keluar.periodeAhir,null) as periodeAhir,");
		buffer.append("coalesce(keluar.total,0.00) as total,");
		buffer.append("coalesce(keluar.tanggal,null) as tanggal,");
		buffer.append("coalesce(keluar.jumlahLimbahB3Keluar,0.00) as jumlahLimbahB3Keluar,");
		buffer.append("coalesce(rekanan.namaRekanan,'-') as namaRekanan,");
		buffer.append("coalesce(keluar.tujuanPenyerahan,'-') as tujuanPenyerahan,");
		buffer.append("coalesce(masuk.jenisLimbahB3masuk,'-') as jenisLimbahB3masuk,");
		buffer.append("coalesce(masuk.id ,0) as jenisLimbahB3masukId, ");
		buffer.append("coalesce(keluar.maksimalPenyimpanan,0.00) as maksimalPenyimpnan,");
		buffer.append("keluar.noRec as noRec, ");
		buffer.append("keluar.perlakuanId as perlakuanId, ");
		buffer.append("rekanan.id as rekananId, ");		
		buffer.append("coalesce(keluar.tujuanPenyerahan,' ') as tujuanPenyerahan, ");
		buffer.append("coalesce(keluar.buktiNomerDokumen,' ') as buktiNomerDokumen, ");		
		buffer.append("coalesce(keluar.sisaLimbahB3,0.0) as sisaLimbah )");
		buffer.append(" from LimbahB3Keluar keluar left join keluar.rekanan rekanan left join keluar.jenisLimbahB3Masuk masuk where keluar.noRec is not null ");
		buffer.append("and (keluar.statusEnabled is null or keluar.statusEnabled is true) ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(keluar.tanggal) between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisLimbah)){
			buffer.append("and keluar.jenisLimbahB3Masuk.id =:jenisLimbah ");
		}	
		
		if (sort != "") {
			buffer.append("order by keluar." + sort + " " + dir);
		} else {
			buffer.append("order by keluar.noRec desc ");
		}
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisLimbah)){
			query.setParameter("jenisLimbah", jenisLimbah);
		}	
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}

	@Override
	public Integer findCount(String sort, String dateStart, String dateEnd, Integer jenisLimbah) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(keluar.noRec)");
		buffer.append(" from LimbahB3Keluar keluar left join keluar.rekanan rekanan where keluar.noRec is not null ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(keluar.tanggal) between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisLimbah)){
			buffer.append("and keluar.jenisLimbahB3Masuk.id =:jenisLimbah ");
		}	
		buffer.append("and (keluar.statusEnabled is null or keluar.statusEnabled is true) ");
		
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisLimbah)){
			query.setParameter("jenisLimbah", jenisLimbah);
		}	
		return ((Long) query.getSingleResult()).intValue();
	}

	@Override
	public Map<String,Object> limbahB3KeluarByNoRec(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map(coalesce(keluar.periodeAwal,null) as periodeAwal,");
		buffer.append("coalesce(keluar.periodeAhir,null) as periodeAhir,");
		buffer.append("coalesce(keluar.total,0.00) as total,");
		buffer.append("coalesce(keluar.tanggal,null) as tanggal,");
		buffer.append("coalesce(keluar.jumlahLimbahB3Keluar,0.00) as jumlahLimbahB3Keluar,");
		buffer.append("coalesce(rekanan.namaRekanan,'-') as namaRekanan,");
		buffer.append("coalesce(keluar.tujuanPenyerahan,'-') as tujuanPenyerahan,");
		buffer.append("coalesce(masuk.jenisLimbahB3masuk,'-') as jenisLimbahB3masuk,");
		buffer.append("coalesce(masuk.id ,0) as jenisLimbahB3masukId, ");
		buffer.append("coalesce(keluar.maksimalPenyimpanan,0.00) as maksimalPenyimpnan,");
		buffer.append("keluar.noRec as noRec, ");
		buffer.append("keluar.perlakuanId as perlakuanId, ");
		buffer.append("rekanan.id as rekananId, ");
		buffer.append("coalesce(keluar.tujuanPenyerahan,' ') as tujuanPenyerahan, ");
		buffer.append("coalesce(keluar.buktiNomerDokumen,' ') as buktiNomerDokumen, ");		
		buffer.append("coalesce(keluar.sisaLimbahB3,0.0) as sisaLimbah )");
		buffer.append(" from LimbahB3Keluar keluar left join keluar.rekanan rekanan left join keluar.jenisLimbahB3Masuk masuk where keluar.noRec is not null ");
		buffer.append("and (keluar.statusEnabled is null or keluar.statusEnabled is true) ");
		
		if(CommonUtil.isNotNullOrEmpty(noRec)){
			buffer.append("and keluar.noRec =:noRec ");
		}	
		
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(noRec)){
			query.setParameter("noRec",noRec);
		}
		
		Map<String,Object> map = (Map<String,Object>) query.getSingleResult();

		return map;
	}

}
