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

/**
 * Repository Custom class
 * 
 * @author Roberto
 */
@Repository
public class LimbahB3MasukDaoCustomImpl extends CoreDaoImpl<LimbahB3Masuk> implements
		LimbahB3MasukDaoCustom {

	public LimbahB3MasukDaoCustomImpl() {
		super(LimbahB3Masuk.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 (");
		buffer.append("pelayanan.noRec as noRec, ");
		buffer.append("pelayanan.tglstruk as tglstruk, ");
		buffer.append("coalesce(jenisLimbah.jenisLimbahB3masuk,'-') as jenisLimbahB3masuk, ");
		buffer.append("jenisLimbah.id as limbahB3masukId, ");
		buffer.append("coalesce(pelayanan.qtyproduk,0) as qtyproduk, ");
		buffer.append("coalesce(satuanWaktuKesling.namaSatuanWaktuKesling,'-') as satuanWaktuKesling, ");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan, ");
		buffer.append("coalesce(ruanganAsal.namaRuangan,'-') as ruanganAsal, ");
		buffer.append("coalesce(petugas.petugasPengirim,'-') as petugasPengirim, ");
		buffer.append("coalesce(satuanWaktuKesling.id,0) as satuanWaktuKeslingId, ");
		buffer.append("coalesce(ruanganTujuan.id,0) as ruanganTujuanId, ");
		buffer.append("coalesce(ruanganAsal.id,0) as ruanganAsalId, ");
		buffer.append("detail.tglpelayanan as tglPelayan, ");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as namaLengkap ) ");
		buffer.append("from StrukPelayananDPetugas petugas ");
		buffer.append("right join petugas.nostruk detail ");
		buffer.append("left join detail.nostruk pelayanan  ");
		buffer.append("left join pelayanan.satuanWaktuKesling satuanWaktuKesling ");
		buffer.append("left join pelayanan.jenisLimbahB3Masuk jenisLimbah ");
		buffer.append("left join pelayanan.kdruangan ruanganTujuan ");
		buffer.append("left join pelayanan.kdruanganasal ruanganAsal ");
		buffer.append("left join petugas.kdpegawai pegawai ");
		buffer.append("left join pelayanan.kdkelompoktransaksi kelompokTransaksi ");
		buffer.append("where kelompokTransaksi.id = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdLimbahInStruk') ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(pelayanan.tglstruk) between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisLimbah)){
			buffer.append("and jenisLimbah.id =:jenisLimbah ");
		}else{
			buffer.append("and petugas.noRec is not null ");
		}	
		
		buffer.append("and (pelayanan.statusEnabled = true or pelayanan.statusEnabled is null) ");
		if (sort != "") {
			buffer.append("order by pelayanan." + sort + " " + dir);
		} else {
			buffer.append("order by pelayanan.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(pelayanan.tglstruk) ");
		buffer.append("from StrukPelayananDPetugas petugas ");
		buffer.append("right join petugas.nostruk detail ");
		buffer.append("left join detail.nostruk pelayanan  ");
		buffer.append("left join pelayanan.satuanWaktuKesling satuanWaktuKesling ");
		buffer.append("left join pelayanan.jenisLimbahB3Masuk jenisLimbah ");
		buffer.append("left join pelayanan.kdruangan ruanganTujuan ");
		buffer.append("left join pelayanan.kdruanganasal ruanganAsal ");
		buffer.append("left join petugas.kdpegawai pegawai ");
		buffer.append("left join pelayanan.kdkelompoktransaksi kelompokTransaksi ");
		buffer.append("where kelompokTransaksi.id = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdLimbahInStruk') ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(pelayanan.tglstruk) between :dateStart and :dateEnd ");
		}
		buffer.append("and (pelayanan.statusEnabled = true or pelayanan.statusEnabled is null) ");
		
		if(CommonUtil.isNotNullOrEmpty(jenisLimbah)){
			buffer.append("and jenisLimbah.id =:jenisLimbah ");
		}	
		
		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 List<Map<String,Object>> findListByPeriodeV2(String dateStart, String dateEnd) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map ( to_char(pelayanan.tglstruk, 'yyyy/MM/dd') as tglstruk,");
		buffer.append("jenisLimbah.id as idJenisLimbah, ");
		buffer.append("coalesce(jenisLimbah.jenisLimbahB3masuk,'-') as jenisLimbahB3masuk,"); 
		buffer.append("sum(coalesce(pelayanan.qtyproduk,0)) as qtyproduk, ");
//		buffer.append("coalesce(ruanganAsal.namaRuangan,'-') as ruanganAsal ) ");
		buffer.append("'R. Operasional' as ruanganAsal ) ");
		buffer.append("from StrukPelayananDPetugas petugas ");
		buffer.append("right join petugas.nostruk detail ");
		buffer.append("left join detail.nostruk pelayanan  ");
		buffer.append("left join pelayanan.satuanWaktuKesling satuanWaktuKesling "); 
		buffer.append("left join pelayanan.jenisLimbahB3Masuk jenisLimbah ");
		buffer.append("left join pelayanan.kdruangan ruanganTujuan ");
		buffer.append("left join pelayanan.kdruanganasal ruanganAsal ");
		buffer.append("left join petugas.kdpegawai pegawai ");
		buffer.append("left join pelayanan.kdkelompoktransaksi kelompokTransaksi "); 
		buffer.append("where kelompokTransaksi.id = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdLimbahInStruk') ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){ 
//		buffer.append("and date(pelayanan.tglstruk) between date( :dateStart) and date(:dateEnd) ");
			buffer.append(" and date(pelayanan.tglstruk) between date(:dateStart) and date(:dateEnd) ");	
		}
		buffer.append("and (pelayanan.statusEnabled = true or pelayanan.statusEnabled is null) ");
		buffer.append("and jenisLimbah.jenisLimbahB3masuk is not null ");
		buffer.append("group by to_char(pelayanan.tglstruk, 'yyyy/MM/dd'),jenisLimbah.id,jenisLimbah.jenisLimbahB3masuk ");
//		buffer.append("ruanganAsal.namaRuangan ");
		
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",dateStart);
			query.setParameter("dateEnd", dateEnd);
		}
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}
	
	@Override
	public Map<String,Object> findByNoRec(String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map (");
		buffer.append("pelayanan.noRec as noRec, ");
		buffer.append("pelayanan.tglstruk as tglStrukPelayanan, ");
		buffer.append("coalesce(jenisLimbah.jenisLimbahB3masuk,'-') as jenisLimbahB3masuk, ");
		buffer.append("jenisLimbah.id as limbahB3masukId, ");
		buffer.append("coalesce(pelayanan.qtyproduk,0) as qtyproduk, ");
		buffer.append("coalesce(satuanWaktuKesling.namaSatuanWaktuKesling,'-') as satuanWaktuKesling, ");
		buffer.append("coalesce(satuanWaktuKesling.id,0) as satuanWaktuKeslingId, ");		
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan, ");
		buffer.append("coalesce(ruanganAsal.namaRuangan,'-') as ruanganAsal, ");
		buffer.append("coalesce(ruanganTujuan.id,0) as ruanganTujuanId, ");
		buffer.append("coalesce(ruanganAsal.id,0) as ruanganAsalId, ");
		buffer.append("coalesce(petugas.petugasPengirim,'-') as petugasPengirim, ");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as namaLengkap ) ");
		buffer.append("from StrukPelayananDPetugas petugas ");
		buffer.append("right join petugas.nostruk detail ");
		buffer.append("left join detail.nostruk pelayanan  ");
		buffer.append("left join pelayanan.satuanWaktuKesling satuanWaktuKesling ");
		buffer.append("left join pelayanan.jenisLimbahB3Masuk jenisLimbah ");
		buffer.append("left join pelayanan.kdruangan ruanganTujuan ");
		buffer.append("left join pelayanan.kdruanganasal ruanganAsal ");
		buffer.append("left join petugas.kdpegawai pegawai ");
		buffer.append("left join pelayanan.kdkelompoktransaksi kelompokTransaksi ");
		buffer.append("where kelompokTransaksi.id = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdLimbahInStruk') ");
		buffer.append("and (pelayanan.statusEnabled = true or pelayanan.statusEnabled is null) ");
		buffer.append("and pelayanan.noRec =:noRec ");

		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(noRec)){
			query.setMaxResults(1);
			query.setParameter("noRec",noRec);
		}

		Map<String,Object> r= (Map<String,Object>) query.getSingleResult();
		
		return r;
	}



}
