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

@Repository
public class SuratMasukDaoCustomImpl extends CoreDaoImpl<Dokumen> implements SuratMasukDaoCustom {

	public SuratMasukDaoCustomImpl() {
		super(Dokumen.class);
	}

	@Override
	public int findCount(String dateStart, String dateEnd, Integer suratId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select count(dm.noRec) from DokumenMasuk dm left join dm.kddokumen dok left join dok.surat surat where dok.surat is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(dok.tglDokumen) between date(:dateStart) and date(:dateEnd) ");
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			buffer.append("and surat.id=:suratId ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			query.setParameter("suratId", suratId);
		}

		return ((Long) query.getSingleResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDaftarDraft(int rowStart, int rowEnd, String sort, String dir, String dateStart,
			String dateEnd, Integer suratId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(dok.id as dokumenId,dm.noRec as noRecDokMasuk,dok.noDokumen as noDokumen,dok.namaJudulDokumen as namaFile,"
						+ "surat.id as suratId,surat.nama as namaSurat,dok.pathFile as pathFile,dm.status as status) "
						+ "from DokumenMasuk dm left join dm.kddokumen dok left join dok.surat surat where dok.surat is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(dok.tglDokumen) between date(:dateStart) and date(:dateEnd) ");
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			buffer.append("and surat.id=:suratId ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			query.setParameter("suratId", suratId);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findCountDraftSuratMasuk(String dateStart, String dateEnd) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(dm.noRec) from DokumenMasuk dm left join dm.nokirimketujuanlast strukKirim "
				+ "left join strukKirim.kdruanganasal ruanganPengirim "
				+ "left join strukKirim.kdruangantujuan ruanganPenerima left join dm.kddokumen dok "
				+ "left join dok.surat surat where dm.nokirimketujuanlast is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(strukKirim.tglkirim) between date(:dateStart) and date(:dateEnd) ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		return ((Long) query.getSingleResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDaftarDraftSuratMasuk(int rowStart, int rowEnd, String sort, String dir,
			String dateStart, String dateEnd) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select new map(dok.id as dokumenId,dm.noRec as noRecDokMasuk,dok.noDokumen as noDokumen,surat.id as suratId,surat.nama as namaSurat,"
						+ "ruanganPengirim.id as ruanganPengirimId,ruanganPengirim.namaRuangan as ruanganPengirim,"
						+ "ruanganPenerima.id as ruanganPenerimaId,ruanganPenerima.namaRuangan as ruanganPenerima,dok.pathFile as pathFile,"
						+ "dm.isVerifikasi as statusVerifikasi) from DokumenMasuk dm "
						+ "left join dm.nokirimketujuanlast strukKirim "
						+ "left join strukKirim.kdruanganasal ruanganPengirim "
						+ "left join strukKirim.kdruangantujuan ruanganPenerima left join dm.kddokumen dok "
						+ "left join dok.surat surat where dm.nokirimketujuanlast is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(strukKirim.tglkirim) between date(:dateStart) and date(:dateEnd) ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findCountDokumenVerbalKonsep(String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select count(dm.noRec) from DokumenMasuk dm left join dm.nokirimketujuanlast strukKirim left join dm.kddokumen dok "
						+ "left join dok.surat surat left join strukKirim.kdruanganasal ruanganAsal "
						+ "left join strukKirim.kdruangantujuan ruanganTujuan where dm.nokirimketujuanlast is not null ");

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			buffer.append("where dm.noRec=:noRec ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}

		return ((Long) query.getSingleResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDokumenVerbalKonsep(int rowStart, int rowEnd, String sort, String dir,
			String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map(dok.id as dokumenId,dm.noRec as noRecDokMasuk,"
				+ "dok.noDokumen as noDokumen,surat.id as suratId,surat.nama as namaSurat,dm.keteranganlainnya as lembarCatatan,"
				+ "ruanganAsal.id as ruanganAsalId,ruanganAsal.namaRuangan as namaRuanganAsal,"
				+ "ruanganTujuan.id as ruanganTujuanId,ruanganTujuan.namaRuangan as namaRuanganTujuan,dok.pathFile as pathFile) "
				+ "from DokumenMasuk dm left join dm.nokirimketujuanlast strukKirim left join dm.kddokumen dok "
				+ "left join dok.surat surat left join strukKirim.kdruanganasal ruanganAsal "
				+ "left join strukKirim.kdruangantujuan ruanganTujuan where dm.nokirimketujuanlast is not null ");

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			buffer.append("and dm.noRec=:noRec ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findCountSebelumSuratMasuk(String dateStart, String dateEnd) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(dm.noRec) from from MappingKirimDokumenPasien map "
				+ "left join map.kdruangantujuanlast ruanganTujuan left join map.dokumenMasuk dm "
				+ "left join map.strukKirim strukKirim left join strukKirim.kdruanganasal ruanganAsal "
				+ "left join dm.kddokumen dok left join dok.surat surat left join dm.nostruk strukPelayanan "
				+ "left join strukPelayanan.kdpegawaipenerima penerima left join strukPelayanan.kdruanganasal ruanganPenerima "
				+ "left join strukKirim.kdpegawaipengirim pengirim " + "where dok.surat is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(strukPelayanan.tglterimakiriman) between date(:dateStart) and date(:dateEnd) ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		return ((Long) query.getSingleResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDaftarSebelumSuratMasuk(int rowStart, int rowEnd, String sort, String dir,
			String dateStart, String dateEnd) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map(dm.noRec as noRec,strukPelayanan.noRec as noRecStrukPelayanan,"
				+ "dok.id as id,dok.noDokumen as noDokumen,surat.id as suratId,surat.nama as namaSurat,dok.deskripsiDokumen as lembarCatatan,"
				+ "ruanganPengirim.id as ruanganAsalId,ruanganPengirim.namaRuangan as namaRuanganAsal,"
				+ "ruanganTujuan.id as ruanganTujuanId,ruanganTujuan.namaRuangan as namaRuanganTujuan,namaPengirim.namaLengkap as pengirim,"
				+ "penerima.id as penerimaId,penerima.namaLengkap as penerima) " + "from DokumenMasuk dm "
				+ "left join dm.ruanganPengirim ruanganPengirim "
				+ "left join dm.nokirimketujuanlast strukKirim left join strukKirim.kdruangantujuan ruanganTujuan "
				+ "left join strukKirim.kdpegawaipengirim namaPengirim left join dm.pegawaiPenerimaSurat penerima"
				+ "left join dm.kddokumen dok left join dok.surat surat where dok.surat is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(strukKirim.tglkirim) between date(:dateStart) and date(:dateEnd) ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findCountDaftarDraftVerifikasi(String dateStart, String dateEnd, Integer suratId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(dm.noRec) from DokumenMasuk dm "
				+ "left join dm.nokirimketujuanlast strukKirim left join strukKirim.kdruangantujuan ruanganTujuan left join dm.kddokumen dok "
				+ "left join dok.surat surat where dm.nokirimketujuanlast is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(dok.tglDokumen) between date(:dateStart) and date(:dateEnd) ");
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			buffer.append("and surat.id=:suratId ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			query.setParameter("suratId", suratId);
		}

		return ((Long) query.getSingleResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDaftarDraftVerifikasi(int rowStart, int rowEnd, String sort, String dir,
			String dateStart, String dateEnd, Integer suratId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(dok.id as dokumenId,dm.noRec as noRecDokMasuk,dok.noDokumen as noDokumen,dok.namaJudulDokumen as namaFile,"
						+ "surat.id as suratId,surat.nama as namaSurat,dok.pathFile as pathFile,dok.status as status,"
						+ "ruanganTujuan.id as ruanganTujuanId,ruanganTujuan.namaRuangan as namaRuanganTujuan,dm.isVerifikasi as statusVerifikasi) "
						+ "from DokumenMasuk dm "
						+ "left join dm.nokirimketujuanlast strukKirim left join strukKirim.kdruangantujuan ruanganTujuan left join dm.kddokumen dok "
						+ "left join dok.surat surat where dm.nokirimketujuanlast is not null ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(dok.tglDokumen) between date(:dateStart) and date(:dateEnd) ");
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			buffer.append("and surat.id=:suratId ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}

		if (CommonUtil.isNotNullOrEmpty(suratId)) {
			query.setParameter("suratId", suratId);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDaftarDraftSuratMasukByRuangan(
			String dateStart, String dateEnd,Integer idRuangan) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select new Map(dokumenMasuk.status as status,dokumen.namaJudulDokumen as judulDokumen, dokumen.id as id, dokumen.deskripsiDokumen as deskripsiDokumen,dokumen.kdDokumen as kdDokumen,dokumen.noDokumen as noDokumen,dokumen.pathFile as pathFile,"
				+ " dokumen.pegawaiPembuat.id as pembuatId, "
				+ " ruangan.id as idRuangan, ruangan.namaRuangan as namaRuangan, "
				+ " ruanganAsal.id as ruanganAsalId, ruanganAsal.namaRuangan as namaRuanganAsal ) "
				+ " from DokumenMasuk dokumenMasuk "
			+ " left join dokumenMasuk.kddokumen dokumen "
			+ " left join dokumenMasuk.ruanganPengirim ruanganAsal "
			+ " left join dokumenMasuk.kdruangantujuanlast ruangan "
			+ " where dokumen.tglDokumen IS NOT NULL    ");

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and dokumen.tglDokumen  between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')   ");
		}
		if (CommonUtil.isNotNullOrEmpty(idRuangan) ) {
			buffer.append("and ruangan.id = :idRuangan ");
		}
		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("tglAwal", dateStart);
			query.setParameter("tglAkhir", dateEnd);
		}
		if (CommonUtil.isNotNullOrEmpty(idRuangan) ) {
			query.setParameter("idRuangan",idRuangan);
		}
		 

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}
}
