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.DetailRupDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.dto.DetailRupDto;
import com.jasamedika.medifirst2000.entities.DetailRup;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class DetailRupDaoCustomImpl extends CoreDaoImpl<DetailRup> implements DetailRupDaoCustom {

	public DetailRupDaoCustomImpl() {
		super(DetailRup.class);
	}

	@Override
	public List<DetailRupDto> findDetailRup(int rowStart, int rowEnd, String sort, String dir, String dateStart,
			String dateEnd, List<Integer> pengendaliId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"Select NEW com.jasamedika.medifirst2000.dto.DetailRupDto(spek,rup) from DetailRup rup right join rup.detailSpekAnggaran spek where spek.isActive=true and spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.detailSpekAnggaran is not null ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append(
					"and date(spek.detailAnggaran.tanggalPengajuan) >= :dateStart and date(spek.detailAnggaran.tanggalPengajuan) <= :dateEnd ");
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		} else {
			buffer.append("and spek.pengendali.id is null ");
		}

		if (sort != "") {
			buffer.append("order by spek." + sort + " " + dir);
		} else {
			buffer.append("order by spek.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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<DetailRupDto> list = query.getResultList();

		return list;
	}

	@Override
	public Integer findDetailRupCount(String sort, String dateStart, String dateEnd, List<Integer> pengendaliId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"Select count(spek) from  DetailRup rup right join rup.detailSpekAnggaran spek where spek.isActive=true and spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.detailSpekAnggaran is not null ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append(
					"and date(spek.detailAnggaran.tanggalPengajuan) >= :dateStart and date(spek.detailAnggaran.tanggalPengajuan) <= :dateEnd ");
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		} else {
			buffer.append("and spek.pengendali.id is null ");
		}
		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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

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

	}

	@Override
	public List<DetailRupDto> findByNoRec(List<String> noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"Select NEW com.jasamedika.medifirst2000.dto.DetailRupDto(spek,rup) from DetailRup rup right join rup.detailSpekAnggaran spek where spek.noRec is not null");
		buffer.append(
				" and spek.noRec in (:noRec) and spek.isActive=true  and spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.detailSpekAnggaran is not null ");

		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		return query.getResultList();
	}

	@Override
	public int findCountPreAddKartuPengendali(String sort, String dateStart, String dateEnd,
			List<Integer> pengendaliId,Integer kegiatanId, Integer akunId, Integer asalProdukId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(detailRup) ");
		buffer.append(" from DetailRup detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.detailAnggaran detail ");
		buffer.append(" left join detail.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan");
		buffer.append(" left join kegiatanAnggaran.headAnggaran head ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join detail.asalProduk asalProduk ");
		buffer.append(" left join detail.komponen komponen ");
		buffer.append(" left join produk.satuanStandar satuanStandar");
		buffer.append(" left join kegiatanAnggaran.kegiatanDetail kegiatanDetail where detailRup.noRec is not null and spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.isActive=true ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(detail.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		}
		if(CommonUtil.isNotNullOrEmpty(kegiatanId)){
			buffer.append("and kegiatanAnggaran.kegiatanId=:kegiatanId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(akunId)){
			buffer.append("and detail.akunId=:akunId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(asalProdukId)){
			buffer.append("and detail.asalProdukId=:asalProdukId ");
		}
		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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}
		if(CommonUtil.isNotNullOrEmpty(kegiatanId)){
			query.setParameter("kegiatanId", kegiatanId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(akunId)){
			query.setParameter("akunId", akunId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(asalProdukId)){
			query.setParameter("asalProdukId", asalProdukId);
		}

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

	@Override
	public List<Map<String, Object>> findListPreAddKartuPengendali(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, List<Integer> pengendaliId,Integer kegiatanId, Integer akunId, Integer asalProdukId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map( ");
		buffer.append(" coalesce(head.tahun,0) as tahun,");
		buffer.append(" coalesce(detailRup.keterangan,'-') as keterangan,");
		buffer.append(" coalesce(kegiatan.namaKegiatan,'-') as namaKegiatan,");
		buffer.append(" coalesce(detailRup.noRec,'-') as noRec,");
		buffer.append(" coalesce(spek.noRec,'-') as noRecSpek,");
		buffer.append(" coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail ,");
		buffer.append(" coalesce(komponen.namaKomponen,'-') as namaKomponen,");
		buffer.append(" coalesce(asalProduk.asalProduk,'-') as sumberDana,");
		buffer.append(" coalesce(spek.detailSpekAnggaran.spesifikasi,'-') as spesifikasi,");
		buffer.append(" coalesce(produk.namaProduk,'-') as namaProduk,");
		buffer.append(" coalesce(satuanStandar.satuanStandar,'-') as namaSatuan,");
		buffer.append(" coalesce(spek.volumeBarang*spek.hargaSatuanBarang,0.0) as jumlahBiaya,");
		buffer.append(" coalesce(detail.tanggalPengajuan,null) as tanggalPengajuan,");
		buffer.append(" coalesce(detailRup.pelaksanaanPekerjaanAwal,null) as pelaksanaanPekerjaanAwal,");
		buffer.append(" coalesce(detailRup.pelaksanaanPekerjaanAhir,null) as pelaksanaanPekerjaanAhir,");
		buffer.append(" coalesce(detailRup.pelaksanaanPemilihanAwal,null) as pelaksanaanPemilihanAwal,");
		buffer.append(" coalesce(detailRup.pelaksanaanPemilihanAhir,null) as pelaksanaanPemilihanAhir )");
		buffer.append(" from DetailRup detailRup ");
		buffer.append(" left join detailRup.detailSpekAnggaran spek ");
		buffer.append(" left join spek.detailAnggaran detail ");
		buffer.append(" left join detail.komponen komponen ");
		buffer.append(" left join detail.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan");
		buffer.append(" left join kegiatanAnggaran.headAnggaran head ");
		buffer.append(" left join spek.produk produk ");
		buffer.append(" left join detail.asalProduk asalProduk ");
		buffer.append(" left join produk.satuanStandar satuanStandar");
		buffer.append(" left join kegiatanAnggaran.kegiatanDetail kegiatanDetail  where detailRup.noRec is not null  and spek.isActive=true ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(detail.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		} else {
			buffer.append("and spek.pengendali.id is null ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(kegiatanId)){
			buffer.append("and kegiatanAnggaran.kegiatanId=:kegiatanId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(akunId)){
			buffer.append("and detail.akunId=:akunId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(asalProdukId)){
			buffer.append("and detail.asalProdukId=:asalProdukId ");
		}
		
		
		if (sort != "") {
			buffer.append("order by detailRup." + sort + " " + dir);
		} else {
			buffer.append("order by detailRup.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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(kegiatanId)){
			query.setParameter("kegiatanId", kegiatanId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(akunId)){
			query.setParameter("akunId", akunId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(asalProdukId)){
			query.setParameter("asalProdukId", asalProdukId);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findPreAddRupCount(String sort, String dateStart, String dateEnd, List<Integer> pengendaliId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count( distinct detailSpekAnggaran.noRec)  ");
		buffer.append(" from DetailSpekAnggaran spek  ");
		buffer.append(" left join spek.detailAnggaran detail  ");
		buffer.append(" left join spek.detailSpekAnggaran detailSpekAnggaran  ");
		buffer.append(" left join detail.komponen komponen  left join detail.kegiatanAnggaran kegiatanAnggaran  ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan left join kegiatanAnggaran.headAnggaran head  ");
		buffer.append(" left join spek.produk produk  left join detail.asalProduk asalProduk  left join produk.satuanStandar satuanStandar ");
		buffer.append(" left join kegiatanAnggaran.kegiatanDetail kegiatanDetail  ");
		buffer.append(" where spek.isActive=true ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append(
					"and date(detailSpekAnggaran.detailAnggaran.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		}
		
		buffer.append("and spek.noRec not in ( select rup.detailSpekAnggaranId from DetailRup rup   ) ");
		
		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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

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

	@Override
	public List<Map<String, Object>> findPreAddRupList(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, List<Integer> pengendaliId) {
		StringBuffer buffer = new StringBuffer();

		buffer.append(" select new map (  ");
		buffer.append(" coalesce(detailSpekAnggaran.noRec,'-') as noRec, ");
		buffer.append(" coalesce(head.tahun,0) as tahun, ");
		buffer.append(" coalesce(kegiatan.namaKegiatan,'-') as namaKegiatan, ");
		buffer.append(" coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail , ");
		buffer.append(" coalesce(komponen.namaKomponen,'-') as namaKomponen, ");
		buffer.append(" coalesce(asalProduk.asalProduk,'-') as sumberDana, ");
		buffer.append(" coalesce(detailSpekAnggaran.spesifikasi,'-') as spesifikasi, ");
		buffer.append(" sum(spek.volumeBarang*spek.hargaSatuanBarang) as jumlahBiaya ) ");
		buffer.append(" from DetailSpekAnggaran spek  ");
		buffer.append(" left join spek.detailAnggaran detail  ");
		buffer.append(" left join spek.detailSpekAnggaran detailSpekAnggaran  ");
		buffer.append(" left join detail.komponen komponen  left join detail.kegiatanAnggaran kegiatanAnggaran  ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan left join kegiatanAnggaran.headAnggaran head  ");
		buffer.append(" left join spek.produk produk  left join detail.asalProduk asalProduk  left join produk.satuanStandar satuanStandar ");
		buffer.append(" left join kegiatanAnggaran.kegiatanDetail kegiatanDetail  ");
		buffer.append(" where spek.isVerifikasi='VERIFIKASI_PENGENDALI'  and spek.isActive=true ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append(" and date(detailSpekAnggaran.detailAnggaran.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		} else {
			buffer.append("and spek.pengendali.id is null ");
		}

		buffer.append("and spek.noRec not in ( select rup.detailSpekAnggaranId from DetailRup rup   ) ");
		
		buffer.append("group by detailSpekAnggaran.noRec,head.tahun,kegiatan.namaKegiatan,kegiatanDetail.namaKegiatanDetail,komponen.namaKomponen,asalProduk.asalProduk,detailSpekAnggaran.spesifikasi ");

		if (sort != "") {
			buffer.append("order by detailSpekAnggaran." + sort + " " + dir);
		} else {
			buffer.append("order by detailSpekAnggaran.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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}
	
	

	@Override
	public int findRupCount(String sort, String dateStart, String dateEnd, List<Integer> pengendaliId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("  select count(distinct header.noRec)  from DetailPengadaan dp join dp.detailRup rup left join rup.detailSpekAnggaran spek ");
		buffer.append("  left join rup.headRup header  "); 
		buffer.append("  left join spek.detailAnggaran detail ");  
		buffer.append("  left join spek.detailSpekAnggaran detailSpekAnggaran ");  
		buffer.append("  left join detail.komponen komponen ");
		buffer.append("  left join detail.kegiatanAnggaran kegiatanAnggaran ");  
		buffer.append("  left join kegiatanAnggaran.kegiatan kegiatan ");
		buffer.append("  left join kegiatanAnggaran.headAnggaran head   ");
		buffer.append("  left join spek.produk produk  ");
		buffer.append("  left join detail.asalProduk asalProduk "); 
		buffer.append("  left join produk.satuanStandar satuanStandar "); 
		buffer.append("  left join kegiatanAnggaran.kegiatanDetail kegiatanDetail "); 
		buffer.append("  left join dp.pengadaan pengadaan   ");
		buffer.append("  where spek.isVerifikasi !='PENGAJUAN' "); 
		buffer.append("  and spek.isActive=true  ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append(
					"and date(detailSpekAnggaran.detailAnggaran.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		} else {
			buffer.append("and spek.pengendali.id is null ");
		}
		
		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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

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

	@Override
	public List<Map<String, Object>> findRupList(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, List<Integer> pengendaliId) {
		StringBuffer buffer = new StringBuffer();

		buffer.append(" select new map (  ");
		buffer.append(" coalesce(header.noRec,'-') as noRec, ");
		buffer.append(" coalesce(head.tahun,0) as tahun, ");
		buffer.append(" coalesce(kegiatan.namaKegiatan,'-') as namaKegiatan, ");
		buffer.append(" coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail , ");
		buffer.append(" coalesce(komponen.namaKomponen,'-') as namaKomponen, ");
		buffer.append(" coalesce(asalProduk.asalProduk,'-') as sumberDana, ");
		buffer.append(" coalesce(detailSpekAnggaran.spesifikasi,'-') as spesifikasi, ");
		buffer.append(" sum(spek.volumeBarang*spek.hargaSatuanBarang) as jumlahBiaya,pengadaan.namaPengadaan as namPengadaan ) ");
		buffer.append(" from DetailPengadaan dp join dp.detailRup rup left join rup.detailSpekAnggaran spek left join rup.headRup header  ");
		buffer.append(" left join spek.detailAnggaran detail  ");
		buffer.append(" left join spek.detailSpekAnggaran detailSpekAnggaran  ");
		buffer.append(" left join detail.komponen komponen  left join detail.kegiatanAnggaran kegiatanAnggaran  ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan left join kegiatanAnggaran.headAnggaran head  ");
		buffer.append(" left join spek.produk produk  left join detail.asalProduk asalProduk  left join produk.satuanStandar satuanStandar ");
		buffer.append(" left join kegiatanAnggaran.kegiatanDetail kegiatanDetail  left join dp.pengadaan pengadaan  ");
		buffer.append(" where spek.isVerifikasi !='PENGAJUAN'  and spek.isActive=true ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append(" and date(detailSpekAnggaran.detailAnggaran.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spek.pengendali.id in (:pengendaliId) ");
		} else {
			buffer.append("and spek.pengendali.id is null ");
		}

		
		buffer.append("group by header.noRec,detailSpekAnggaran.noRec,pengadaan.namaPengadaan,head.tahun,kegiatan.namaKegiatan,kegiatanDetail.namaKegiatanDetail,komponen.namaKomponen,asalProduk.asalProduk,detailSpekAnggaran.spesifikasi ");

		if (sort != "") {
			buffer.append("order by detailSpekAnggaran." + sort + " " + dir);
		} else {
			buffer.append("order by detailSpekAnggaran.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(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findDetailSpekAnggaranByParentForRup(List<String> noRec) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map (  ");
		buffer.append(" coalesce(detail.tanggalPengajuan,null) as tanggalPengajuan, ");
		buffer.append(" coalesce(spek.noRec,'-') as noRec, ");
		buffer.append(" coalesce(output.namaOutput,'-') as output, ");
		buffer.append(" coalesce(spek.volumeBarang,0.0) as volumeBarang , ");
		buffer.append(" coalesce(spek.hargaSatuanBarang,0.0) as hasrgaSatuanBarang , ");
		buffer.append(" (spek.volumeBarang*spek.hargaSatuanBarang ) as jumlahBiaya , ");
		buffer.append(" coalesce(kegiatan.namaKegiatan,'-') as namaKegiatan, ");
		buffer.append(" coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail , ");
		buffer.append(" coalesce(komponen.namaKomponen,'-') as namaKomponen, ");
		buffer.append(" coalesce(akun.namaMataAnggaran,'-') as namaAkun, ");
		buffer.append(" coalesce(asalProduk.asalProduk,'-') as sumberDana, ");
		buffer.append(" coalesce(produk.namaProduk,'-') as namaProduk,  ");
		buffer.append(" coalesce(detailSpekAnggaran.spesifikasi,'-') as spesifikasi) ");
		buffer.append(" from DetailSpekAnggaran spek  left join spek.detailAnggaran detail  ");
		buffer.append(" left join spek.detailSpekAnggaran detailSpekAnggaran ");
		buffer.append(" left join detail.akun akun ");
		buffer.append(" left join detail.komponen komponen  left join detail.kegiatanAnggaran kegiatanAnggaran   ");
		buffer.append(" left join kegiatanAnggaran.output output ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan left join kegiatanAnggaran.headAnggaran head   ");
		buffer.append(" left join spek.produk produk  left join detail.asalProduk asalProduk  ");
		buffer.append(" left join produk.satuanStandar satuanStandar  left join kegiatanAnggaran.kegiatanDetail kegiatanDetail   ");
		buffer.append(" where spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.isActive=true ");
	
		buffer.append("  and detailSpekAnggaran.noRec in (:noRec) ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		List<Map<String, Object>> result = query.getResultList();
		
		return result;
		
	}
	
	@Override
	public List<Map<String, Object>> findDetailSpekAnggaranByParentForKartuPengendali(List<String> noRec) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map (  ");
		buffer.append(" coalesce(detail.tanggalPengajuan,null) as tanggalPengajuan, ");
		buffer.append(" coalesce(rup.noRec,'-') as noRec, ");
		buffer.append(" coalesce(output.namaOutput,'-') as output, ");
		buffer.append(" coalesce(spek.volumeBarang,0.0) as volumeBarang , ");
		buffer.append(" coalesce(spek.hargaSatuanBarang,0.0) as hasrgaSatuanBarang , ");
		buffer.append(" (spek.volumeBarang*spek.hargaSatuanBarang ) as jumlahBiaya , ");
		buffer.append(" coalesce(kegiatan.namaKegiatan,'-') as namaKegiatan, ");
		buffer.append(" coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail , ");
		buffer.append(" coalesce(komponen.namaKomponen,'-') as namaKomponen, ");
		buffer.append(" coalesce(akun.namaMataAnggaran,'-') as namaAkun, ");
		buffer.append(" coalesce(asalProduk.asalProduk,'-') as sumberDana, ");
		buffer.append(" coalesce(produk.namaProduk,'-') as namaProduk,  ");
		buffer.append(" coalesce(detailSpekAnggaran.spesifikasi,'-') as spesifikasi) ");
		buffer.append(" from DetailRup rup join  rup.detailSpekAnggaran spek  left join spek.detailAnggaran detail   ");
		buffer.append(" left join spek.detailSpekAnggaran detailSpekAnggaran ");
		buffer.append(" left join detail.akun akun ");
		buffer.append(" left join detail.komponen komponen  left join detail.kegiatanAnggaran kegiatanAnggaran   ");
		buffer.append(" left join kegiatanAnggaran.output output ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan left join kegiatanAnggaran.headAnggaran head   ");
		buffer.append(" left join spek.produk produk  left join detail.asalProduk asalProduk  ");
		buffer.append(" left join produk.satuanStandar satuanStandar  left join kegiatanAnggaran.kegiatanDetail kegiatanDetail   ");
		buffer.append(" where spek.isActive=true ");
		
		buffer.append("  and spek.noRec in (:noRec) ");
		
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		List<Map<String, Object>> result = query.getResultList();
		
		return result;
		
	}

	@Override
	public List<Map<String, Object>> findDetailRupByParent(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map (  ");
		
		buffer.append(" coalesce(rup.pelaksanaanPemilihanAwal,null) as pelaksanaanPemilihanAwal, ");
		buffer.append(" coalesce(rup.pelaksanaanPemilihanAhir,null) as pelaksanaanPemilihanAhir, ");
		buffer.append(" coalesce(rup.pelaksanaanPekerjaanAwal,null) as pelaksanaanPekerjaanAwal, ");
		buffer.append(" coalesce(rup.pelaksanaanPekerjaanAhir,null) as pelaksanaanPekerjaanAhir, ");
		buffer.append(" coalesce(rup.keterangan,'-') as keterangan, ");
		buffer.append(" coalesce(detail.tanggalPengajuan,null) as tanggalPengajuan, ");
		buffer.append(" coalesce(rup.noRec,'-') as noRec, ");
		buffer.append(" coalesce(output.namaOutput,'-') as output, ");
		buffer.append(" coalesce(spek.volumeBarang,0.0) as volumeBarang , ");
		buffer.append(" coalesce(spek.hargaSatuanBarang,0.0) as hargaSatuanBarang , ");
		buffer.append(" (spek.volumeBarang*spek.hargaSatuanBarang ) as jumlahBiaya , ");
		buffer.append(" coalesce(kegiatan.namaKegiatan,'-') as namaKegiatan, ");
		buffer.append(" coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail , ");
		buffer.append(" coalesce(komponen.namaKomponen,'-') as namaKomponen, ");
		buffer.append(" coalesce(akun.namaMataAnggaran,'-') as namaAkun, ");
		buffer.append(" coalesce(asalProduk.asalProduk,'-') as sumberDana, ");
		buffer.append(" coalesce(produk.namaProduk,'-') as namaProduk,  ");
		buffer.append(" coalesce(pengadaan.namaPengadaan,'-') as namaPengadaan,  ");
		buffer.append(" coalesce(detailSpekAnggaran.spesifikasi,'-') as spesifikasi) ");
		buffer.append(" from DetailPengadaan detailPengadaan left join detailPengadaan.detailRup rup left join  rup.detailSpekAnggaran spek  left join spek.detailAnggaran detail   ");
		buffer.append(" left join detailPengadaan.pengadaan pengadaan ");
		buffer.append(" left join spek.detailSpekAnggaran detailSpekAnggaran ");
		buffer.append(" left join detail.akun akun ");
		buffer.append(" left join detail.komponen komponen  left join detail.kegiatanAnggaran kegiatanAnggaran   ");
		buffer.append(" left join kegiatanAnggaran.output output ");
		buffer.append(" left join kegiatanAnggaran.kegiatan kegiatan left join kegiatanAnggaran.headAnggaran head   ");
		buffer.append(" left join spek.produk produk  left join detail.asalProduk asalProduk  ");
		buffer.append(" left join produk.satuanStandar satuanStandar  left join kegiatanAnggaran.kegiatanDetail kegiatanDetail   ");
		buffer.append(" where spek.isVerifikasi !='PENGAJUAN' and spek.isActive=true ");
		
		buffer.append("  and rup.headRupId = (:noRec) ");
		
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		List<Map<String, Object>> result = query.getResultList();
		
		return result;
		
	}


	/*
	 * @Override public List<DetailRupDto> findBySpek(String noRec) {
	 * StringBuffer buffer = new StringBuffer(); buffer.append(
	 * "Select NEW com.jasamedika.medifirst2000.dto.DetailRupDto(spek,rup) from DetailRup rup left join rup.detailSpekAnggaran spek where spek.noRec is not null"
	 * ); buffer.append(
	 * " and spek.detailSpekAnggaran.noRec=:noRec and spek.isActive=true  and spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.detailSpekAnggaran is not null "
	 * );
	 * 
	 * Query query = em.createQuery(buffer.toString());
	 * query.setParameter("noRec", noRec); return query.getResultList(); }
	 * 
	 * @Override public List<DetailRupDto> findByDetail(String noRec) {
	 * StringBuffer buffer = new StringBuffer(); buffer.append(
	 * "Select NEW com.jasamedika.medifirst2000.dto.DetailRupDto(spek,rup) from DetailRup rup left join rup.detailSpekAnggaran spek where spek.noRec is not null"
	 * ); buffer.append(
	 * " and spek.detailAnggaran.noRec=:noRec and spek.isActive=true and spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.detailSpekAnggaran is not null  "
	 * );
	 * 
	 * Query query = em.createQuery(buffer.toString());
	 * query.setParameter("noRec", noRec); return query.getResultList(); }
	 * 
	 * @Override public List<DetailRupDto> findByKegiatan(String noRec) {
	 * StringBuffer buffer = new StringBuffer(); buffer.append(
	 * "Select NEW com.jasamedika.medifirst2000.dto.DetailRupDto(spek,rup) from DetailRup rup left join rup.detailSpekAnggaran spek where spek.noRec is not null"
	 * ); buffer.append(
	 * " and spek.detailAnggaran.kegiatanAnggaran.noRec=:noRec and spek.isActive=true  and spek.isVerifikasi='VERIFIKASI_PENGENDALI' and spek.detailSpekAnggaran is not null "
	 * );
	 * 
	 * Query query = em.createQuery(buffer.toString());
	 * query.setParameter("noRec", noRec); return query.getResultList(); }
	 */
}
