package com.jasamedika.medifirst2000.dao.custom.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.StrukOrderDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.KelompokUser;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 */
@Repository
public class StrukOrderDaoCustomImpl extends CoreDaoImpl<StrukOrder> implements StrukOrderDaoCustom {

	public StrukOrderDaoCustomImpl() {
		super(StrukOrder.class);
	}
	public String GetSettingDataFixed(String prefix) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select model.nilaiField from SettingDataFixed ")
				.append(" model  where model.namaField ='"+prefix+"' ");
		Query query = em.createQuery(buffer.toString());

		return (String)query.getSingleResult();
	}

	@Override
	public int findStrukOrderCount(String sort, String dateStart, String dateEnd, String noOrder, KelompokUser kelompokUser, Ruangan ruangan) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(strukOrder.noRec) ");
		buffer.append("from StrukKirim kirim ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		/*if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			buffer.append("and ruangan.ruanganId =:ruanganId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(ruanganTujuanId)){
			buffer.append("and ruanganTujuan.ruanganTujuanId =:ruanganTujuanId ");
		}*/
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
			
		if(kelompokUser.getKelompokUser().equalsIgnoreCase("logistik")){
			buffer.append("and strukOrder.ruanganTujuanId ="+GetSettingDataFixed("kdRuanganLogistik")+" ");
		}else if(kelompokUser.getKelompokUser().equalsIgnoreCase("bagianUmum")){
			buffer.append("and strukOrder.ruanganTujuanId ="+GetSettingDataFixed("kdRuanganLogistikUmum")+" ");
		}else{
			if(CommonUtil.isNotNullOrEmpty(ruangan)){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			}
		}
			
		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(ruanganId)){
			query.setParameter("ruanganId", ruanganId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(ruanganTujuanId)){
			query.setParameter("ruanganTujuanId", ruanganTujuanId);
		}
*/		
		if (CommonUtil.isNotNullOrEmpty(noOrder)) {
			query.setParameter("noOrder", noOrder);
		}
		if ((kelompokUser.getKelompokUser().equalsIgnoreCase("logistik"))) {
			
		}else if ((kelompokUser.getKelompokUser().equalsIgnoreCase("bagianUmum"))) {
			
		}else{
			if (CommonUtil.isNotNullOrEmpty(ruangan)) {
				query.setParameter("ruanganId", ruangan.getId());
			}
		}
		
		return ((Long) query.getSingleResult()).intValue();

	}


	@Override
	public List<Map<String,Object>> findStrukOrder(int rowStart, int rowEnd, String sort, String dir, String dateStart, String dateEnd, String noOrder, KelompokUser kelompokUser, Ruangan ruangan) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("kirim.nokirim as noKirim,kirim.noRec as noRecKirim,");
		buffer.append("jenisPermintaan.name as jenisPermintaan )");
		buffer.append("from StrukKirim kirim ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		/*if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			buffer.append("and ruangan.ruanganId =:ruanganId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(ruanganTujuanId)){
			buffer.append("and ruanganTujuan.ruanganTujuanId =:ruanganTujuanId ");
		}*/
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		if(kelompokUser.getKelompokUser().equalsIgnoreCase("logistik")){
			buffer.append("and strukOrder.ruanganTujuanId ="+GetSettingDataFixed("kdRuanganLogistik")+" ");
		}else if(kelompokUser.getKelompokUser().equalsIgnoreCase("bagianUmum")){
			buffer.append("and strukOrder.ruanganTujuanId ="+GetSettingDataFixed("kdRuanganLogistikUmum")+" ");
		}else{
			if(CommonUtil.isNotNullOrEmpty(ruangan)){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			}
		}
		
		if (CommonUtil.isNotNullOrEmpty(sort)) {
			buffer.append("order by strukOrder." + sort + " " + dir);
		} else {
			buffer.append("order by strukOrder.tglOrder asc ");
		}
		
			
		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(ruanganId)){
			query.setParameter("ruanganId", ruanganId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(ruanganTujuanId)){
			query.setParameter("ruanganTujuanId", ruanganTujuanId);
		}*/
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if ((kelompokUser.getKelompokUser().equalsIgnoreCase("logistik"))) {

		} else if ((kelompokUser.getKelompokUser().equalsIgnoreCase("bagianUmum"))) {

		} else {
			if (CommonUtil.isNotNullOrEmpty(ruangan)) {
				query.setParameter("ruanganId", ruangan.getId());
			}
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}
	@Override
	public List<String> findNoVerifikasi(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select k.noVerifikasi.noverifikasi from KirimProduk k left join k.noKirim strukKirim where strukKirim.noRec=:noRec" );
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		
		List<String> list = query.getResultList();

		return list;
	}
	
	
	@Override
	public int findStrukOrderUnitKerjaCount(String sort, String dateStart, String dateEnd, Integer ruanganTujuanId,Integer ruanganId, String noOrder, KelompokUser kelompokUser, Ruangan ruangan) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(strukOrder.noRec) ");
		buffer.append("from StrukKirim kirim ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and kirim.noRec is null ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
			
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			buffer.append("and strukOrder.ruanganTujuanId =:ruanganId ");
		}
			
		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(noOrder)) {
			query.setParameter("noOrder", noOrder);
		}
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		return ((Long) query.getSingleResult()).intValue();

	}


	@Override
	public List<Map<String,Object>> findStrukOrderUnitKerja(int rowStart, int rowEnd, String sort, String dir, String dateStart, String dateEnd, Integer ruanganTujuanId, Integer ruanganId, String noOrder, KelompokUser kelompokUser, Ruangan ruangan) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("kirim.nokirim as noKirim,kirim.noRec as noRecKirim,");
		buffer.append("jenisPermintaan.name as jenisPermintaan )");
		buffer.append("from StrukKirim kirim ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and kirim.noRec is null  ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			buffer.append("and strukOrder.ruanganTujuanId =:ruanganId ");
		}
		
		if (CommonUtil.isNotNullOrEmpty(sort)) {
			buffer.append("order by strukOrder." + sort + " " + dir);
		} else {
			buffer.append("order by strukOrder.tglOrder asc ");
		}
		
			
		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(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}
	@Override
	public int informasiPengirimanBarangCount(String sort, String dateStart, String dateEnd, String noOrder, KelompokUser kelompokUser, Ruangan ruangan,String parameter) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(strukOrder.noRec) ");
		buffer.append("from StrukKirim kirim ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and strukOrder.keteranganOrder!='Permintaan Barang Dari Ruangan (Aset)'  ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			if(parameter.equalsIgnoreCase("ruanganTujuan")){
				buffer.append("and strukOrder.ruanganTujuanId =:ruanganId ");
			}else if(parameter.equalsIgnoreCase("ruanganAsal")){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			}
			
		}
	
		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(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		return ((Long) query.getSingleResult()).intValue();

	}
	@Override
	public List<Map<String, Object>> informasiPengirimanBarangList(int rowStart, int rowEnd, String sort, String dir,
			String dateStart, String dateEnd,  String noOrder,
			KelompokUser kelompokUser, Ruangan ruangan,String parameter) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukKonfirmasi.noKonfirmasi as noKonfirmasi, ");
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("kirim.nokirim as noKirim,kirim.noRec as noRecKirim,");
		buffer.append("jenisPermintaan.name as jenisPermintaan,strukOrder.keteranganOrder as keteranganOrder )");
		buffer.append("from KirimProduk kirimProduk ");
		buffer.append("right join kirimProduk.noKirim kirim ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and strukOrder.keteranganOrder!='Permintaan Barang Dari Ruangan (Aset)' and kirimProduk.kirimProduk is null ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			if(parameter.equalsIgnoreCase("ruanganTujuan")){
				buffer.append("and strukOrder.ruanganTujuanId =:ruanganId ");
			}else if(parameter.equalsIgnoreCase("ruanganAsal")){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			}
			
		}
		
		
		buffer.append("group by ");
		buffer.append("strukKonfirmasi.noKonfirmasi,  ");
		buffer.append("strukOrder.noRec,  ");
		buffer.append("strukOrder.noOrderIntern, ");
		buffer.append("str(date(strukOrder.tglOrder)), ");
		buffer.append("ruangan.namaRuangan, ");
		buffer.append("pegawai.namaLengkap, ");
		buffer.append("ruanganTujuan.namaRuangan, ");
		buffer.append("kirim.nokirim,kirim.noRec, ");
		buffer.append("jenisPermintaan.name, ");
		buffer.append("strukOrder.keteranganOrder  ");
		if (CommonUtil.isNotNullOrEmpty(sort)) {
			buffer.append(" order by strukOrder." + sort + " " + dir);
		} else {
			buffer.append(" order by strukOrder.tglOrder 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(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();
		
		return list;
		
	}
	@Override
	public int informasiPenerimaanBarangCount(String sort, String dateStart, String dateEnd, String noOrder,KelompokUser kelompokUser, Ruangan ruangan,Integer kelompokTransaksiBarangId) {

		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(strukOrder.noRec) ");
		buffer.append("from KirimProduk kirimProduk left join kirimProduk.noKirim kirim ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and kirim is not null and strukKonfirmasi.kelompokTransaksi.id=:kelompokTransaksiBarangId ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		//if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			//if(parameter.equalsIgnoreCase("ruanganTujuan")){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			//}else if(parameter.equalsIgnoreCase("ruanganAsal")){
				//buffer.append("and strukOrder.ruanganId =:ruanganId ");
			//}
			
		//}
		
			
		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(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		query.setParameter("kelompokTransaksiBarangId", kelompokTransaksiBarangId);
			
		return ((Long) query.getSingleResult()).intValue();
		
	
	}
	@Override
	public List<Map<String, Object>> informasiPenerimaanBarangList(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, String noOrder, KelompokUser kelompokUser, Ruangan ruangan,Integer kelompokTransaksiBarangId) {

		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,strukKonfirmasi.noKonfirmasi as noKonfirmasi, ");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("kirim.nokirim as noKirim,kirim.noRec as noRecKirim,");
		buffer.append("jenisPermintaan.name as jenisPermintaan )");
		buffer.append("from KirimProduk kirimProduk left join kirimProduk.noKirim kirim ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and kirim is not null and strukKonfirmasi.kelompokTransaksi.id=:kelompokTransaksiBarangId ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		//if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			//if(parameter.equalsIgnoreCase("ruanganTujuan")){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			//}else if(parameter.equalsIgnoreCase("ruanganAsal")){
				//buffer.append("and strukOrder.ruanganId =:ruanganId ");
			//}
			
		//}
		
		if (CommonUtil.isNotNullOrEmpty(sort)) {
			buffer.append("order by strukOrder." + sort + " " + dir);
		} else {
			buffer.append("order by strukOrder.tglOrder asc ");
		}
		
			
		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(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		query.setParameter("kelompokTransaksiBarangId", kelompokTransaksiBarangId);
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();

		return list;
		
	
	}
	@Override
	public int informasiPengirimanBarangAsetCount(String sort, String dateStart, String dateEnd, String noOrder,KelompokUser kelompokUser, Ruangan ruangan, String parameter) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(strukOrder.noRec)");
		buffer.append("from KirimProdukAset kirimProduk ");
		buffer.append("right join kirimProduk.noKirim kirim ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and strukOrder.keteranganOrder='Permintaan Barang Dari Ruangan (Aset)'");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			if(parameter.equalsIgnoreCase("ruanganTujuan")){
				buffer.append("and strukOrder.ruanganTujuanId =:ruanganId ");
			}else if(parameter.equalsIgnoreCase("ruanganAsal")){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			}
			
		}
		
		
		/*buffer.append("group by ");
		buffer.append("strukKonfirmasi.noKonfirmasi,  ");
		buffer.append("strukOrder.noRec,  ");
		buffer.append("strukOrder.noOrderIntern, ");
		buffer.append("str(date(strukOrder.tglOrder)), ");
		buffer.append("ruangan.namaRuangan, ");
		buffer.append("pegawai.namaLengkap, ");
		buffer.append("ruanganTujuan.namaRuangan, ");
		buffer.append("kirim.nokirim,kirim.noRec, ");
		buffer.append("jenisPermintaan.name, ");
		buffer.append("strukOrder.keteranganOrder  ");*/
		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(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		return ((Long) query.getSingleResult()).intValue();

	}
	@Override
	public List<Map<String, Object>> informasiPengirimanBarangAsetList(int rowStart, int rowEnd, String sort,
			String dir, String dateStart, String dateEnd, String noOrder, KelompokUser kelompokUser, Ruangan ruangan,
			String parameter) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukKonfirmasi.noKonfirmasi as noKonfirmasi, ");
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("kirim.nokirim as noKirim,kirim.noRec as noRecKirim,");
		buffer.append("jenisPermintaan.name as jenisPermintaan,strukOrder.keteranganOrder as keteranganOrder )");
		buffer.append("from KirimProdukAset kirimProduk ");
		buffer.append("right join kirimProduk.noKirim kirim ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and strukOrder.keteranganOrder='Permintaan Barang Dari Ruangan (Aset)'");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("KdKelompokTransaksiOrderBarang")+" ");
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			if(parameter.equalsIgnoreCase("ruanganTujuan")){
				buffer.append("and strukOrder.ruanganTujuanId =:ruanganId ");
			}else if(parameter.equalsIgnoreCase("ruanganAsal")){
				buffer.append("and strukOrder.ruanganId =:ruanganId ");
			}
			
		}
		
		
		buffer.append("group by ");
		buffer.append("strukKonfirmasi.noKonfirmasi,  ");
		buffer.append("strukOrder.noRec,  ");
		buffer.append("strukOrder.noOrderIntern, ");
		buffer.append("str(date(strukOrder.tglOrder)), ");
		buffer.append("ruangan.namaRuangan, ");
		buffer.append("pegawai.namaLengkap, ");
		buffer.append("ruanganTujuan.namaRuangan, ");
		buffer.append("kirim.nokirim,kirim.noRec, ");
		buffer.append("jenisPermintaan.name, ");
		buffer.append("strukOrder.keteranganOrder  ");
		if (CommonUtil.isNotNullOrEmpty(sort)) {
			buffer.append(" order by strukOrder." + sort + " " + dir);
		} else {
			buffer.append(" order by strukOrder.tglOrder 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(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();
		
		return list;
	}
	@Override
	public int getInformasiPerbaikanMasukCount(String sort, String dateStart, String dateEnd, String noOrder,KelompokUser kelompokUser, Ruangan ruangan,String jenisOrder) {
		return 0;
	}
	@Override
	public List<Map<String, Object>> getInformasiPerbaikanMasukList(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, String noOrder, KelompokUser kelompokUser, Ruangan ruangan,String jenisOrder) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukKonfirmasi.noKonfirmasi as noVerifikasi,");
		buffer.append("op.noRec as noRecOrderPelayanan, ");
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,");
		buffer.append("coalesce(pegawaiPengirim.namaLengkap,'-') as namaTeknisi, ");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("strukOrder.keteranganOrder as keteranganOrder,");
		buffer.append("strukOrder.keteranganLainnya as keteranganLainnya )"
		+ "");
		buffer.append("from KirimProduk kirimProduk ");
		buffer.append("right join kirimProduk.noKirim kirim , OrderPelayanan op  ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi  left join op.registrasiAset registerAset left join registerAset.produk produk ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join kirim.pegawaiPengirim pegawaiPengirim  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai  ");
		buffer.append("where  strukOrder.noRec is not null and op.strukOrderId=strukOrder.noRec and strukOrder.keteranganOrder!='Permintaan Barang Dari Ruangan (Aset)' and kirimProduk.kirimProduk is null ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		if(CommonUtil.isNotNullOrEmpty(jenisOrder)){
			buffer.append("and strukOrder.keteranganOrder=:jenisOrder ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("kelompokTransaksiPermintaanPerbaikanIpsrs")+" ");
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			
			buffer.append("and strukOrder.ruanganTujuanId =:ruanganId ");
			
			
		}
		
		
		buffer.append("group by strukOrder.noRec, ");
		buffer.append("strukOrder.noOrderIntern,");
		buffer.append("str(date(strukOrder.tglOrder)),");
		buffer.append("pegawaiPengirim.namaLengkap,");
		buffer.append("ruangan.namaRuangan,");
		buffer.append("pegawai.namaLengkap,");
		buffer.append("ruanganTujuan.namaRuangan,");
		buffer.append("strukKonfirmasi.noKonfirmasi,");
		buffer.append("strukOrder.keteranganOrder,");
		buffer.append("op.noRec,");
		buffer.append("strukOrder.keteranganLainnya");
		if (CommonUtil.isNotNullOrEmpty(sort)) {
			buffer.append(" order by strukOrder." + sort + " " + dir);
		} else {
			buffer.append(" order by strukOrder.noOrderIntern 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(jenisOrder)){
			query.setParameter("jenisOrder", jenisOrder);
		}
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();
		
		return list;
		
	}
	
	
	@Override
	public int getInformasiPerbaikanKeluarCount(String sort, String dateStart, String dateEnd, String noOrder,KelompokUser kelompokUser, Ruangan ruangan,String jenisOrder) {
		return 0;
	}
	@Override
	public List<Map<String, Object>> getInformasiPerbaikanKeluarList(int rowStart, int rowEnd, String sort, String dir,String dateStart, String dateEnd, String noOrder, KelompokUser kelompokUser, Ruangan ruangan,String jenisOrder) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukKonfirmasi.noKonfirmasi as noVerifikasi,");
		buffer.append("op.noRec as noRecOrderPelayanan, ");
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,");
		buffer.append("coalesce(pegawaiPengirim.namaLengkap,'-') as namaTeknisi, ");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("strukOrder.keteranganOrder as keteranganOrder,");
		buffer.append("jenisProduk.id as idJenis, ");
		buffer.append("strukOrder.keteranganLainnya as keteranganLainnya )"
		+ "");
		buffer.append("from KirimProduk kirimProduk ");
		buffer.append("right join kirimProduk.noKirim kirim , OrderPelayanan op  ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi  ");
		buffer.append("left join op.registrasiAset registerAset ");
		buffer.append("left join registerAset.produk produk ");
		buffer.append("left join produk.detailJenisProduk detailJenisProduk ");
		buffer.append("left join  detailJenisProduk.jenisProduk jenisProduk ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join kirim.pegawaiPengirim pegawaiPengirim  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai  ");
		buffer.append("where  strukOrder.noRec is not null ");
		buffer.append("and op.strukOrderId=strukOrder.noRec ");
		buffer.append("and strukOrder.keteranganOrder!='Permintaan Barang Dari Ruangan (Aset)' ");
		buffer.append("and kirimProduk.kirimProduk is null ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisOrder)){
			buffer.append("and strukOrder.keteranganOrder=:jenisOrder ");
		}
		
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			buffer.append("and strukOrder.noOrder =:noOrder ");
		}
		buffer.append("and strukOrder.kelompokTransaksi.id ="+GetSettingDataFixed("kelompokTransaksiPermintaanPerbaikanIpsrs")+" ");
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			
			buffer.append("and strukOrder.ruanganId =:ruanganId ");
			
			
		}
		
		
		buffer.append("group by strukOrder.noRec, ");
		buffer.append("strukOrder.noOrderIntern,");
		buffer.append("str(date(strukOrder.tglOrder)),");
		buffer.append("pegawaiPengirim.namaLengkap,");
		buffer.append("ruangan.namaRuangan,");
		buffer.append("pegawai.namaLengkap,");
		buffer.append("ruanganTujuan.namaRuangan,");
		buffer.append("strukKonfirmasi.noKonfirmasi,");
		buffer.append("strukOrder.keteranganOrder,");
		buffer.append("op.noRec,");
		buffer.append("strukOrder.keteranganLainnya, " );
		buffer.append("jenisProduk.id ");
		if (CommonUtil.isNotNullOrEmpty(sort)) {
			buffer.append(" order by strukOrder." + sort + " " + dir);
		} else {
			buffer.append(" order by strukOrder.noOrderIntern 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(jenisOrder)){
			query.setParameter("jenisOrder", jenisOrder);
		}
		
		if(CommonUtil.isNotNullOrEmpty(noOrder)){
			query.setParameter("noOrder", noOrder);
		}
		
		if (CommonUtil.isNotNullOrEmpty(ruangan)) {
			query.setParameter("ruanganId", ruangan.getId());
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();
		
		return list;
		
	}
	@Override
	public Map<String, Object> getInformasiPerbaikanHeader(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("strukKonfirmasi.noKonfirmasi as noVerifikasi, ");
		buffer.append("strukOrder.noRec as noRec, ");
		buffer.append("strukOrder.noOrderIntern as noOrder,");
		buffer.append("strukOrder.tglOrder as tanggalOrder,");
		buffer.append("coalesce(pegawaiPengirim.namaLengkap,'-') as namaTeknisi, ");
		buffer.append("coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append("coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append("coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append("kirim.nokirim as noRespon,");
		buffer.append("strukOrder.keteranganOrder as keteranganOrder,");
		buffer.append("strukOrder.keteranganLainnya as keteranganLainnya )"
		+ "");
		buffer.append("from KirimProduk kirimProduk ");
		buffer.append("right join kirimProduk.noKirim kirim , OrderPelayanan op ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi  left join op.registrasiAset registerAset left join registerAset.produk produk ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join kirim.pegawaiPengirim pegawaiPengirim  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("where strukOrder.noRec is not null and op.strukOrderId=strukOrder.noRec and strukOrder.noRec=:noRec ");
		
		
		buffer.append("group by strukOrder.noRec, ");
		buffer.append("strukOrder.noOrderIntern,");
		buffer.append("str(date(strukOrder.tglOrder)),");
		buffer.append("pegawaiPengirim.namaLengkap,");
		buffer.append("ruangan.namaRuangan,");
		buffer.append("pegawai.namaLengkap,");
		buffer.append("ruanganTujuan.namaRuangan,");
		buffer.append("kirim.nokirim,");
		buffer.append("strukKonfirmasi.noKonfirmasi,");
		buffer.append("strukOrder.keteranganOrder,");
		buffer.append("strukOrder.keteranganLainnya");
		Query query = em.createQuery(buffer.toString());
		
		query.setMaxResults(1);
		query.setParameter("noRec", noRec);	
		Map<String,Object> list = (Map<String, Object>) query.getSingleResult();
		
		return list;
	}
	
	@Override
	public Map<String, Object> getInformasiPerbaikanDetail(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map( " );
		buffer.append("registerAset.noRegisterAset as noRegisterAset, ");
		buffer.append("strukOrder.keteranganLainnya as keteranganLainnya,");
		buffer.append("produk.namaProduk as namaProduk, ");
		buffer.append("coalesce(registerAset.noSeri,'-') as noSeri,");
		buffer.append("coalesce(typeProduk.typeProduk,'-') as typeProduk,");
		buffer.append("coalesce(merkProduk.merkProduk,'-') as merkProduk)");
		buffer.append("from KirimProduk kirimProduk ");
		buffer.append("right join kirimProduk.noKirim kirim , OrderPelayanan op ");
		buffer.append("left join kirimProduk.noKonfirmasi strukKonfirmasi  left join op.registrasiAset registerAset left join registerAset.produk produk ");
		buffer.append("right join kirim.noorder strukOrder  ");
		buffer.append("left join strukOrder.ruangan ruangan ");
		buffer.append("left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append("left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append("left join strukOrder.pegawaiOrder pegawai ");
		buffer.append("left join registerAset.typeProduk typeProduk ");
		buffer.append("left join registerAset.merkProduk merkProduk ");
		
		buffer.append("where strukOrder.noRec is not null and op.strukOrderId=strukOrder.noRec and strukOrder.noRec=:noRec ");
		
		
		buffer.append("group by registerAset.noRegisterAset, ");
		buffer.append("strukOrder.keteranganLainnya,");
		buffer.append("produk.namaProduk, ");
		buffer.append("registerAset.noSeri,");
		buffer.append("typeProduk.typeProduk,");
		buffer.append("merkProduk.merkProduk)");
		Query query = em.createQuery(buffer.toString());
		
		query.setParameter("noRec", noRec);	
		Map<String,Object> list = (Map<String, Object>) query.getSingleResult();
		
		return list;
	}
	@Override
	public Map<String,Object> findNoKirim(String noRec) {
		System.out.println("noRec " +noRec);
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map(coalesce(sk.nokirim,'-') as noRespon,kp.noRec as noRecKirimProduk) from KirimProduk kp left join kp.noKirim sk "
				+ "left join sk.noorder so "
				+ "where so.noRec=:noRec ");
		Query query = em.createQuery(buffer.toString());
		query.setMaxResults(1);
		query.setParameter("noRec", noRec);	

		return (Map<String, Object>) query.getSingleResult();
		
	}
	@Override
	public String findAsalSukuCadang(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select coalesce(asalSukuCadang.asalSukuCadang,'-') as asalSukuCadang  "
				+ "from PlanningPelayanan pp "
				+ "left join pp.strukPlaning sp "
				+ "left join sp.asalSukuCadang asalSukuCadang "
				
				+ "left join pp.strukOrder so "
				+ "where so.noRec=:noRec");
		Query query = em.createQuery(buffer.toString());
		query.setMaxResults(1);
		query.setParameter("noRec", noRec);	
		
		return (String) query.getSingleResult();
	}
	@Override
	public int monitoringPerbaikan(String sort, String dateStart, String dateEnd, Integer ruanganId, String noAset,
			Integer produkId) {
		return 0;
	}
	@Override
	public List<Map<String, Object>> monitoringPerbaikan(int rowStart, int rowEnd, String sort, String dir,
			String dateStart, String dateEnd, Integer ruanganId, String noAset, Integer produkId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map ( registerAset.noRec as noRecRegistrasiAset,");
		buffer.append(" strukKonfirmasi.noKonfirmasi as noKonfirmasi,");
		buffer.append(" strukKonfirmasi.tglKonfirmasi as tanggalMulai,");
		buffer.append(" strukVerifikasi.tglverifikasi as tanggalSelesai,");
		buffer.append(" strukOrder.tglOrder as tanggalLapor,");
		buffer.append(" kirim.tglkirim as tanggalRespon,");
		buffer.append(" strukKonfirmasi.tglKonfirmasi as tanggalKonfirmasi,");
		buffer.append(" registerAset.noRegisterAset as noAset,");
		buffer.append(" strukVerifikasi.noverifikasi as noVerifikasi,");
		buffer.append(" produk.namaProduk as namaProduk,");
		buffer.append(" op.noRec as noRecOrderPelayanan, ");
		buffer.append(" strukOrder.noRec as noRec, ");
		buffer.append(" strukOrder.noOrderIntern as noOrder,");
		buffer.append(" strukOrder.tglOrder as tanggalOrder,");
		buffer.append(" coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append(" coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append(" coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append(" strukOrder.keteranganOrder as keteranganOrder,");
		buffer.append(" strukOrder.keteranganLainnya as keteranganLainnya ) ");
		
		buffer.append(" from KirimProduk kirimProduk ");
		buffer.append(" left join kirimProduk.noVerifikasi strukVerifikasi");
		buffer.append(" right join kirimProduk.noKirim kirim , OrderPelayanan op  ");
		buffer.append(" left join kirimProduk.noKonfirmasi strukKonfirmasi  left join op.registrasiAset registerAset left join registerAset.produk produk ");
		buffer.append(" right join kirim.noorder strukOrder  ");
		buffer.append(" left join kirim.pegawaiPengirim pegawaiPengirim  ");
		buffer.append(" left join strukOrder.ruangan ruangan ");
		buffer.append(" left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append(" left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append(" left join strukOrder.pegawaiOrder pegawai  ");
		buffer.append(" where  strukOrder.noRec is not null and op.strukOrderId=strukOrder.noRec and strukOrder.keteranganOrder!='Permintaan Barang Dari Ruangan (Aset)' and kirimProduk.kirimProduk is null ");

		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(strukOrder.tglOrder) between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			buffer.append("and ruangan.id =:ruanganId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(noAset)){
			buffer.append("and registerAset.noRegisterAset like  :noAset ");
		}
		
		if (CommonUtil.isNotNullOrEmpty(produkId)) {
			buffer.append("and produk.id =:produkId ");
		}
		
		buffer.append(" and strukOrder.ruanganTujuanId =229 ");
		buffer.append(" group by strukOrder.noRec, ");
		buffer.append(" strukVerifikasi.tglverifikasi, ");
		buffer.append(" strukKonfirmasi.tglKonfirmasi, ");
		buffer.append(" strukVerifikasi.noverifikasi, ");
		buffer.append(" registerAset.noRegisterAset,");
		buffer.append(" produk.namaProduk,");
		buffer.append(" strukOrder.noOrderIntern,");
		buffer.append(" str(date(strukOrder.tglOrder)),");
		buffer.append(" ruangan.namaRuangan,");
		buffer.append(" pegawai.namaLengkap,");
		buffer.append(" ruanganTujuan.namaRuangan,");
		buffer.append(" strukKonfirmasi.noKonfirmasi,");
		buffer.append(" strukOrder.keteranganOrder,");
		buffer.append(" op.noRec,");
		buffer.append(" strukOrder.keteranganLainnya,");
		buffer.append(" kirim.tglkirim,");
		buffer.append(" strukKonfirmasi.tglKonfirmasi,registerAset.noRec");
		buffer.append(" order by strukOrder.noOrderIntern 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(ruanganId)){
			query.setParameter("ruanganId", ruanganId);
		}
		
		if(CommonUtil.isNotNullOrEmpty(noAset)){
			query.setParameter("noAset", "%"+noAset+"%");
		}
		
		if (CommonUtil.isNotNullOrEmpty(produkId)) {
			query.setParameter("produkId", produkId);
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
			
		List<Map<String,Object>> list = query.getResultList();
		
		return list;
	}
	@Override
	public String findStatusPekerjaan(String noRec) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select coalesce(statusPekerjaan.statusPekerjaan,'-') as statusPekerjaan   "
				+ "from PlanningPelayanan pp "
				+ "left join pp.strukPlaning sp "
				+ "left join sp.asalSukuCadang asalSukuCadang "
				+ "left join sp.statusPekerjaan statusPekerjaan "
				+ "left join pp.strukOrder so "
				+ "where so.noRec=:noRec");
		Query query = em.createQuery(buffer.toString());
		query.setMaxResults(1);
		query.setParameter("noRec", noRec);	
		
		return (String) query.getSingleResult();
	}
	
	@Override
	public Map<String, Object> detailMonitoringPerbaikan(String strukOrderId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new Map ( ");
		buffer.append(" strukKonfirmasi.noKonfirmasi as noKonfirmasi,");
		buffer.append(" strukKonfirmasi.tglKonfirmasi as tanggalMulai,");
		buffer.append(" strukVerifikasi.tglverifikasi as tanggalSelesai,");
		buffer.append(" strukOrder.tglOrder as tanggalLapor,");
		buffer.append(" kirim.tglkirim as tanggalRespon,");
		buffer.append(" strukKonfirmasi.tglKonfirmasi as tanggalKonfirmasi,");
		buffer.append(" registerAset.noRegisterAset as noAset,");
		buffer.append(" strukVerifikasi.noverifikasi as noVerifikasi,");
		buffer.append(" produk.namaProduk as namaProduk,");
		buffer.append(" op.noRec as noRecOrderPelayanan, ");
		buffer.append(" strukOrder.noRec as noRec, ");
		buffer.append(" strukOrder.noOrderIntern as noOrder,");
		buffer.append(" strukOrder.tglOrder as tanggalOrder,");
		buffer.append(" coalesce(ruangan.namaRuangan,'-') as ruanganPemesan,");
		buffer.append(" coalesce(pegawai.namaLengkap,'-') as userPemesan,");
		buffer.append(" coalesce(ruanganTujuan.namaRuangan,'-') as ruanganTujuan,");
		buffer.append(" strukOrder.keteranganOrder as keteranganOrder,");
		buffer.append(" strukOrder.keteranganLainnya as keteranganLainnya ) ");
		
		buffer.append(" from KirimProduk kirimProduk ");
		buffer.append(" left join kirimProduk.noVerifikasi strukVerifikasi");
		buffer.append(" right join kirimProduk.noKirim kirim , OrderPelayanan op  ");
		buffer.append(" left join kirimProduk.noKonfirmasi strukKonfirmasi  left join op.registrasiAset registerAset left join registerAset.produk produk ");
		buffer.append(" right join kirim.noorder strukOrder  ");
		buffer.append(" left join kirim.pegawaiPengirim pegawaiPengirim  ");
		buffer.append(" left join strukOrder.ruangan ruangan ");
		buffer.append(" left join strukOrder.ruanganTujuan ruanganTujuan ");
		buffer.append(" left join strukOrder.jenisPermintaan jenisPermintaan ");
		buffer.append(" left join strukOrder.pegawaiOrder pegawai  ");
		buffer.append(" where  strukOrder.noRec=:strukOrderId and op.strukOrderId=strukOrder.noRec and strukOrder.keteranganOrder!='Permintaan Barang Dari Ruangan (Aset)' and kirimProduk.kirimProduk is null ");

		
		buffer.append(" group by strukOrder.noRec, ");
		buffer.append(" strukVerifikasi.tglverifikasi, ");
		buffer.append(" strukKonfirmasi.tglKonfirmasi, ");
		buffer.append(" strukVerifikasi.noverifikasi, ");
		buffer.append(" registerAset.noRegisterAset,");
		buffer.append(" produk.namaProduk,");
		buffer.append(" strukOrder.noOrderIntern,");
		buffer.append(" str(date(strukOrder.tglOrder)),");
		buffer.append(" ruangan.namaRuangan,");
		buffer.append(" pegawai.namaLengkap,");
		buffer.append(" ruanganTujuan.namaRuangan,");
		buffer.append(" strukKonfirmasi.noKonfirmasi,");
		buffer.append(" strukOrder.keteranganOrder,");
		buffer.append(" op.noRec,");
		buffer.append(" strukOrder.keteranganLainnya,");
		buffer.append(" kirim.tglkirim,");
		buffer.append(" strukKonfirmasi.tglKonfirmasi");
		buffer.append(" order by strukOrder.noOrderIntern desc");
		Query query = em.createQuery(buffer.toString());
		query.setParameter("strukOrderId", strukOrderId);
		
		query.setMaxResults(1);
			
		Map<String,Object> list = (Map<String, Object>) query.getSingleResult();
		
		return list;
	}
	
	
	@Override
	public List<Map<String, Object>> sparePartByNoRec(String strukOrderId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map(produk.namaProduk as namaProduk,p.qtyProduk as qtyProduk,satuanStandar.satuanStandar as satuanStandar) from PlanningPelayananDbhp p left join p.produk produk "
				+ "left join produk.satuanStandar satuanStandar "
				+ "where p.strukOrderId=:strukOrderId ");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("strukOrderId", strukOrderId);
			
		List<Map<String,Object>> list =  query.getResultList();
		
		return list;
	}
	
	@Override
	public List<Map<String, Object>> HistoryPenunjang(String dateStart, String dateEnd, Integer id, String noRegistrasi){
		List<Map<String,Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map ("
				+ "a.cito as cito,"
				+ "a.statusOrder as statusOrder,"
				+ "a.alamatTempatTujuan as alamatTempatTujuan,"
				+ "a.isDelivered as isDelivered,"
				+ "a.isPickup as isPickup,"
				+ "a.dokter as dokter,"
				+ "a.keteranganKeperluan as keteranganKeperluan,"
				+ "a.keteranganLainnya as keteranganLainnya,"
				+ "a.keteranganLainnyaQuo as keteranganLainnyaQuo,"
				+ "a.keteranganOrder as keteranganOrder,"
				+ "a.namaPasienPemesan as namaPasienPemesan,"
				+ "a.namaPJawabKeluarga as namaPJawabKeluarga,"
				+ "a.namaRekananSales as namaRekananSales,"
				+ "a.namaRekananSalesQuo as namaRekananSalesQuo,"
				+ "a.namaTempatTujuan as namaTempatTujuan,"
				+ "a.noKontrakSpk as noKontrakSpk,"
				+ "a.noOrder as noOrder,"
				+ "a.noOrderIntern as noOrderIntern,"
				+ "a.noOrderRfq as noOrderRfq,"
				+ "a.noQuotation as noQuotation,"
				+ "a.noTelpMobile as noTelpMobile,"
				+ "a.noTelpMobileRekananSales as noTelpMobileRekananSales,"
				+ "a.noTelpMobileRekananSalesQuo as noTelpMobileRekananSalesQuo,"
				+ "a.noTelpMobileTempatTujuan as noTelpMobileTempatTujuan,"
				+ "a.noUrutLogin as noUrutLogin,"
				+ "a.noUrutRuangan as noUrutRuangan,"
				+ "a.qtyJenisProduk as qtyJenisProduk,"
				+ "a.qtyProduk as qtyProduk,"
				+ "a.tataCaraPembayaranQuo as tataCaraPembayaranQuo,"
				+ "a.tglOrder as tglOrder,"
				+ "a.tglOrderExpired as tglOrderExpired,"
				+ "a.tglPelayananAkhir as tglPelayananAkhir,"
				+ "a.tglPelayananAwal as tglPelayananAwal,"
				+ "a.tglQuotation as tglQuotation,"
				+ "a.tglQuotationExpired as tglQuotationExpired,"
				+ "a.totalBeaMaterai as totalBeaMaterai,"
				+ "a.totalBeratKg as totalBeratKg,"
				+ "a.totalBiayaKirim as totalBiayaKirim,"
				+ "a.totalBiayaTambahan as totalBiayaTambahan,"
				+ "a.totalDiscount as totalDiscount,"
				+ "a.totalHargaSatuan as totalHargaSatuan,"
				+ "a.totalHarusDibayar as totalHarusDibayar,"
				+ "a.totalPph as totalPph,"
				+ "a.totalPpn as totalPpn,"
				+ "a.tglValidasi as tglValidasi,"
				+ "a.noCetak as noCetak,"
				+ "b.id as accountId,"
				+ "b.namaAccount as namaAccount,"
				+ "c.id as caraBayarQuoId,"
				+ "c.caraBayar as caraBayar,"
				+ "d.id as diagnosaId,"
				+ "d.namaDiagnosa as namaDiagnosa,"
				+ "e.id as jenisDokumenId,"
				+ "e.jenisDokumen as jenisDokumen,"
				+ "f.id as jenisOrderId,"
				+ "f.jenisOrder as jenisOrder,"
				+ "g.id as kelompokTransaksiId,"
				+ "g.kelompokTransaksi as kelompokTransaksi,"
				+ "h.id as metodeDelivery,"
				+ "h.metodeDelivery as metodeDelivery,"
				+ "i.id as pegawaiOrder,"
				+ "i.namaLengkap as namaLengkap,"
				+ "j.id as rekananId,"
				+ "j.namaRekanan as namaRekanan,"
				+ "k.id as rekananSalesId,"
				+ "k.namaRekanan as namaRekananSales,"
				+ "l.id as rekananSalesQuoId,"
				+ "l.namaRekanan as namaRekananSalesQuo,"
				+ "m.id as ruanganId,"
				+ "m.namaRuangan as namaRuangan,"
				+ "n.id as ruanganTujuanId,"
				+ "n.namaRuangan as namaRuanganTujuan,"
				+ "o.id as noCmId,"
				+ "o.namaPasien as namaPasien,"
				+ "p.noRec as noRegistrasiNoRec,"
				+ "p.noRegistrasi as noRegistrasi,"
				+ "q.id as statusBarangId,"
				+ "q.name as statusBarangName,"
				+ "r.id as jenisPermintaanId,"
				+ "r.name as jenisPermintaanName) "
				+ "from StrukOrder a "
				+ "left join a.account b "
				+ "left join a.caraBayarQuo c "
				+ "left join a.diagnosa d "
				+ "left join a.jenisDokumen e "
				+ "left join a.jenisOrder f "
				+ "left join a.kelompokTransaksi g "
				+ "left join a.metodeDelivery h  "
				+ "left join a.pegawaiOrder i  "
				+ "left join a.rekanan j  "
				+ "left join a.rekananSales k  "
				+ "left join a.rekananSalesQuo l "
				+ "left join a.ruangan m "
				+ "left join a.ruanganTujuan n  "
				+ "left join a.noCm o "
				+ "left join a.noRegistrasi p "
				+ "left join a.statusBarang q "
				+ "left join a.jenisPermintaan r "
				+ "left join a.noRegistrasi s ");
		
		if(CommonUtil.isNotNullOrEmpty(id)){
			buffer.append("where m.id=:id ");
		}
		if(CommonUtil.isNotNullOrEmpty(noRegistrasi)){
			buffer.append("and s.noRec=:noRegistrasi ");
		}
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(a.tglOrder) between date(:dateStart) and date(:dateEnd)");
		}
		
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(id)){
			query.setParameter("id", id);
		}
		if(CommonUtil.isNotNullOrEmpty(noRegistrasi)){
			query.setParameter("noRegistrasi", noRegistrasi);
		}
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}
		
		if(CommonUtil.isNotNullOrEmpty(buffer)){
			result = query.getResultList();
		}
		return result;
	}
	
	@Override
	public StrukOrder getOneStrukOrder(String noRec) {
		StrukOrder strukOrder = new StrukOrder();
		try {
			StringBuffer buffer = new StringBuffer();
			buffer.append("select model from StrukOrder model where model.noRec="+noRec);
			Query query = em.createQuery(buffer.toString());
			
			strukOrder = (StrukOrder) query.getSingleResult();
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return strukOrder;
	}

}
