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

@Repository
public class DaftarPengajuanAnggaranDaoCustomImpl extends CoreDaoImpl<DetailSpekAnggaran>
		implements DaftarPengajuanAnggaranDaoCustom {

	public DaftarPengajuanAnggaranDaoCustomImpl() {
		super(DetailSpekAnggaran.class);
	}

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

		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(da.noRec) ");
		buffer.append(
				"from DetailSpekAnggaran dsa join dsa.detailAnggaran da join da.kegiatanAnggaran ka join ka.headAnggaran ha ");
		buffer.append("where dsa.detailAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

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

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

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

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

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

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

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select new map(dsa.noRec as noRec,kl.kodeKemenLemb as kodeKemenLemb,kl.namaKemenLemb as namaKemenLemb,uk.kdUnitKerja as kdUnitKerja,uk.namaExternal as namaUnitKerja,"
						+ "uo.kodeUnitOrg as kodeUnitOrg,uo.namaUnitOrg as namaUnitOrg,ha.tahun as tahun,pgm.kodeProgram as kodeProgram,pgm.namaProgram as namaProgram,"
						+ "kd.kodeKegiatanDetail as kodeKegiatanDetail,kd.namaKegiatanDetail as namaKegiatanDetail,out.id as outputId,out.namaOutput as namaOutput,"
						+ "kgt.kodeKegiatan as kodeKegiatan,kgt.namaKegiatan as namaKegiatan,"
						+ "jp.kodeJenisPengadaan as kodeJenisPengadaan,jp.namaJenisPengadaan as namaJenisPengadaan,jb.kodeJenisBelanja as kodeJenisBelanja,"
						+ "jb.namaJenisBelanja as namaJenisBelanja,komp.kodeKomponen as kodeKomponen,komp.namaKomponen as namaKomponen,ap.id as asalProdukId,"
						+ "ap.asalProduk as asalProduk,coalesce(paket.spesifikasi,'-') as namaPaketPengadaan,SUM(dsa.volumeBarang) as volume,"
						+ "coalesce(dsa.hargaSatuanBarang,0) as hargaSatuan,SUM(dsa.totalHargaBarang) as totalHarga) "
						+ "from DetailSpekAnggaran dsa " + "join dsa.detailAnggaran da "
						+ "join da.kegiatanAnggaran ka " + "join ka.headAnggaran ha " + "join dsa.jenisPengadaan jp "
						+ "join dsa.jenisBelanja jb " + "left join da.asalProduk ap " + "left join da.komponen komp "
						+ "left join ka.kegiatanDetail kd left join ka.output out "
						+ "left join ka.kegiatan kgt left join ha.kemenLemb kl "
						+ "left join ha.unitKerja uk left join ha.unitOrganisasi uo "
						+ "left join ha.ruangan rgn left join ha.program pgm "
						+ "left join dsa.detailSpekAnggaran paket "
						+ "where dsa.detailAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

		if (sort != "") {
			buffer.append(
					"group by dsa.noRec,kl.kodeKemenLemb,kl.namaKemenLemb,uk.kdUnitKerja,uk.namaExternal,uo.kodeUnitOrg,uo.namaUnitOrg,ha.tahun,pgm.kodeProgram,"
							+ "pgm.namaProgram,kd.kodeKegiatanDetail,kd.namaKegiatanDetail,out.id,out.namaOutput,kgt.kodeKegiatan,kgt.namaKegiatan,"
							+ "jp.kodeJenisPengadaan,jp.namaJenisPengadaan,jb.kodeJenisBelanja,jb.namaJenisBelanja,"
							+ "komp.kodeKomponen,komp.namaKomponen,ap.id,ap.asalProduk,paket.spesifikasi,dsa.hargaSatuanBarang ");
			buffer.append("order by paket." + sort + " " + dir);
		} else {
			buffer.append(
					"group by dsa.noRec,kl.kodeKemenLemb,kl.namaKemenLemb,uk.kdUnitKerja,uk.namaExternal,uo.kodeUnitOrg,uo.namaUnitOrg,ha.tahun,pgm.kodeProgram,"
							+ "pgm.namaProgram,kd.kodeKegiatanDetail,kd.namaKegiatanDetail,out.id,out.namaOutput,kgt.kodeKegiatan,kgt.namaKegiatan,"
							+ "jp.kodeJenisPengadaan,jp.namaJenisPengadaan,jb.kodeJenisBelanja,jb.namaJenisBelanja,"
							+ "komp.kodeKomponen,komp.namaKomponen,ap.id,ap.asalProduk,paket.spesifikasi,dsa.hargaSatuanBarang ");
			buffer.append("order by ha.tahun,paket.spesifikasi asc");
		}
		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	// @SuppressWarnings("unchecked")
	// @Override
	// public List<Map<String, Object>> getDetailKegiatan(Integer tahun, String
	// noRec) {
	//
	// StringBuffer buffer = new StringBuffer();
	// buffer.append(
	// "select distinct new map(da.noRec as noRec,kd.id as id,ha.tahun as
	// tahun,kd.kodeKegiatanDetail as kodeKegiatanDetail,"
	// + "kd.namaKegiatanDetail as namaKegiatanDetail) from DetailSpekAnggaran
	// dsa join dsa.detailAnggaran da join da.kegiatanAnggaran ka "
	// + "join ka.headAnggaran ha left join ka.kegiatanDetail kd "
	// + "where dsa.detailAnggaran is not null and dsa.isActive=true ");
	//
	// if (CommonUtil.isNotNullOrEmpty(tahun)) {
	// buffer.append("and ha.tahun =:tahun ");
	// }
	//
	// if (CommonUtil.isNotNullOrEmpty(noRec)) {
	// buffer.append("and da.noRec=:noRec ");
	// }
	//
	// Query query = em.createQuery(buffer.toString());
	//
	// if (CommonUtil.isNotNullOrEmpty(tahun)) {
	// query.setParameter("tahun", tahun);
	// }
	//
	// if (CommonUtil.isNotNullOrEmpty(noRec)) {
	// query.setParameter("noRec", noRec);
	// }
	//
	// List<Map<String, Object>> list = query.getResultList();
	//
	// return list;
	// }

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getOutput(Integer tahun, String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(dsa.noRec as noRec,ha.tahun as tahun,out.id as outputId,out.namaOutput as namaOutput) "
						+ "from DetailSpekAnggaran dsa " + "join dsa.detailAnggaran da "
						+ "join da.kegiatanAnggaran ka " + "join ka.headAnggaran ha " + "join dsa.jenisPengadaan jp "
						+ "join dsa.jenisBelanja jb " + "left join da.asalProduk ap " + "left join da.komponen komp "
						+ "left join ka.kegiatanDetail kd left join ka.output out "
						+ "left join ka.kegiatan kgt left join ha.kemenLemb kl "
						+ "left join ha.unitKerja uk left join ha.unitOrganisasi uo "
						+ "left join ha.ruangan rgn left join ha.program pgm "
						+ "left join dsa.detailSpekAnggaran paket "
						+ "where dsa.detailAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getJenisPengadaan(Integer tahun, String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(dsa.noRec as noRec,ha.tahun as tahun,jp.kodeJenisPengadaan as kodeJenisPengadaan,jp.namaJenisPengadaan as namaJenisPengadaan) "
						+ "from DetailSpekAnggaran dsa " + "join dsa.detailAnggaran da "
						+ "join da.kegiatanAnggaran ka " + "join ka.headAnggaran ha " + "join dsa.jenisPengadaan jp "
						+ "join dsa.jenisBelanja jb " + "left join da.asalProduk ap " + "left join da.komponen komp "
						+ "left join ka.kegiatanDetail kd left join ka.output out "
						+ "left join ka.kegiatan kgt left join ha.kemenLemb kl "
						+ "left join ha.unitKerja uk left join ha.unitOrganisasi uo "
						+ "left join ha.ruangan rgn left join ha.program pgm "
						+ "left join dsa.detailSpekAnggaran paket "
						+ "where dsa.detailAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getJenisBelanja(Integer tahun, String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(dsa.noRec as noRec,ha.tahun as tahun,jb.kodeJenisBelanja as kodeJenisBelanja,jb.namaJenisBelanja as namaJenisBelanja) "
						+ "from DetailSpekAnggaran dsa " + "join dsa.detailAnggaran da "
						+ "join da.kegiatanAnggaran ka " + "join ka.headAnggaran ha " + "join dsa.jenisPengadaan jp "
						+ "join dsa.jenisBelanja jb " + "left join da.asalProduk ap " + "left join da.komponen komp "
						+ "left join ka.kegiatanDetail kd left join ka.output out "
						+ "left join ka.kegiatan kgt left join ha.kemenLemb kl "
						+ "left join ha.unitKerja uk left join ha.unitOrganisasi uo "
						+ "left join ha.ruangan rgn left join ha.program pgm "
						+ "left join dsa.detailSpekAnggaran paket "
						+ "where dsa.detailAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getKomponen(Integer tahun, String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(dsa.noRec as noRec,ha.tahun as tahun,komp.kodeKomponen as kodeKomponen,komp.namaKomponen as namaKomponen) "
						+ "from DetailSpekAnggaran dsa " + "join dsa.detailAnggaran da "
						+ "join da.kegiatanAnggaran ka " + "join ka.headAnggaran ha " + "join dsa.jenisPengadaan jp "
						+ "join dsa.jenisBelanja jb " + "left join da.asalProduk ap " + "left join da.komponen komp "
						+ "left join ka.kegiatanDetail kd left join ka.output out "
						+ "left join ka.kegiatan kgt left join ha.kemenLemb kl "
						+ "left join ha.unitKerja uk left join ha.unitOrganisasi uo "
						+ "left join ha.ruangan rgn left join ha.program pgm "
						+ "left join dsa.detailSpekAnggaran paket "
						+ "where dsa.detailAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getSpesifikasi(Integer tahun, String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(da.noRec as noRec,ha.tahun as tahun,coalesce(dsa.spesifikasi,'-') as spesifikasi,dsa.volumeBarang as volume,"
						+ "coalesce(dsa.hargaSatuanBarang,0) as hargaSatuan,coalesce(dsa.totalHargaBarang,0) as totalHarga) "
						+ "from DetailSpekAnggaran dsa " + "join dsa.detailAnggaran da "
						+ "join da.kegiatanAnggaran ka " + "join ka.headAnggaran ha "
						+ "where dsa.detailAnggaran is not null and dsa.detailSpekAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public double getGrandTotal(Integer tahun) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select SUM(dsa.totalHargaBarang) as grandTotal from DetailSpekAnggaran dsa "
				+ "join dsa.detailAnggaran da " + "join da.kegiatanAnggaran ka " + "join ka.headAnggaran ha "
				+ "where dsa.detailAnggaran is not null and dsa.isActive=true and dsa.totalHargaBarang is not null ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun =:tahun ");
		}

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

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

		return ((Double) query.getSingleResult()).doubleValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getPaketPengadaan(Integer tahun, String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(da.noRec as noRec,ha.tahun as tahun,coalesce(paket.spesifikasi,'-') as namaPaketPengadaan) "
						+ "from DetailSpekAnggaran dsa join dsa.detailAnggaran da join da.kegiatanAnggaran ka "
						+ "join ka.headAnggaran ha join dsa.detailSpekAnggaran paket "
						+ "where dsa.detailAnggaran is not null and dsa.detailSpekAnggaran is not null and dsa.isActive=true ");

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and ha.tahun=:tahun ");
		}

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

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

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

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

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

		return list;
	}

}
