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

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

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.LaundryDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.KirimProduk;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Repository
public class LaundryDaoCustomImpl extends CoreDaoImpl<KirimProduk> implements LaundryDaoCustom{

	public LaundryDaoCustomImpl() {
		super(KirimProduk.class);
	}

	@Override
	public List<Map<String, Object>> getAllPenerimaanLinen(Date startPeriode,Date endPeriode){
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select distinct new Map(nostruk.noRec as noRecStrukPelayanan," 
				+"nostruk.nostruk as noStruk, "
				+"noplanning.noplanning as noPlanning,"
				+"nostruk.qtyproduk as berat, "
				+"produk.id as produkId,  "
				+"produk.namaProduk as namaProduk, " 
				+"ruanganasal.id as ruanganAsalId,   "
				+"ruanganasal.namaRuangan as namaRuanganAsal, "
				+ "noclosing.noClosing as noClosing, "  
				+"ruangan.id as ruanganId,   "
				+"ruangan.namaRuangan as namaRuangan, "  
				+"pegawai.id as petugasId,"
				+"pegawai.namaLengkap as namaPetugas,"
				+"satuanstandar.id as satuanId,   "
				+"satuanstandar.satuanStandar as namaSatuan, "  
				+"nostruk.tglstruk as tglTerima)  "
				+"from StrukPelayananDetail model   "
				+"left join model.nostruk nostruk   "
				+"left join nostruk.kdkelompoktransaksi kelompoktransaksi " 
				+"left join model.kdproduk4used produk   "
				+"left join nostruk.kdruangan ruangan    "
				+"left join nostruk.kdruanganasal ruanganasal  "
				+ "left join nostruk.noplanning noplanning " 
				+ "left join nostruk.noclosing noclosing "
				+"left join nostruk.kdpegawaipenerima pegawai "
				+"left join model.kdsatuanstandar satuanstandar ");
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append("where kelompoktransaksi.id = 6 and nostruk.tglstruk between :startPeriode and :endPeriode");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);			
			result = query.getResultList();
		} else {
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getAllPencucianLinen(Date startPeriode, Date endPeriode) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new Map("
					+ "strukPelayanan.noRec as noRecStrukPelayanan,"
					+ "strukPelayanan.nostruk as noStrukPelayanan,"
					+"strukPlanning.noRec as noRecStrukPlanning,"
					+"strukPlanning.noplanning as noPlanning,"
					+"to_char(planningPelayanan.tglPelayanan,'yyyy-mm-dd hh:mm:ss') as tglPelayanan,"
					+"alat.id as alatId,"
					+"alat.namaAlat as namaAlat,"
					+"coalesce(alat.kapasitasAlat,0) as kapasitasAlat,"
					+"planningPelayanan.qtyProdukExec as beratLinen,"
					+"ruanganAsal.id as ruanganAsalId, "
					+"ruanganAsal.namaRuangan as namaRuanganAsal, "
					+"produk.id as produkId,"
					+"produk.namaProduk as namaProduk) "
					+"from PlanningPelayananDetail model "
					+"left join model.planningPelayanan planningPelayanan "
					+"left join model.strukPelayanan strukPelayanan "
					+"left join planningPelayanan.strukPlaning strukPlanning "
					+"left join strukPlanning.kdruanganasal ruanganAsal "
					+"left join strukPlanning.kdkelompoktransaksi kelompoktransaksi "					
					+"left join model.produk produk "
					+"left join model.alat alat ");
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append("where kelompoktransaksi.id=72 and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);			
			result = query.getResultList();
		} else {
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> findBilasFromCuciWithTheSamaName(String bilas) {
		System.out.println("FIND BILAS : "+bilas);
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new Map (model.id as id,model.namaProduk as namaProduk) from Produk model ");
		if(CommonUtil.isNotNullOrEmpty(bilas)){
			buffer.append("where lower(namaProduk) like lower('"+bilas+"') "
					+ "order by model.id");
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		} else {
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getAllPembilasanLinen(Date startPeriode, Date endPeriode) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new Map("
					+"strukPelayanan.noRec as noRecStrukPelayanan,"
					+"strukPelayanan.nostruk as noStrukPelayanan,"
					+"strukPlanning.noRec as noRecStrukPlanning,"
					+"strukPlanning.noplanning as noPlanning,"
					+"to_char(planningPelayanan.tglPelayanan,'yyyy-mm-dd hh:mm:ss') as tglPelayanan,"
					+"alat.id as alatId,"
					+"alat.namaAlat as namaAlat,"
					+"coalesce(alat.kapasitasAlat,0) as kapasitasAlat,"
					+"planningPelayanan.qtyProdukExec as beratLinen,"
					+"ruanganAsal.id as ruanganAsalId, "
					+"ruanganAsal.namaRuangan as namaRuanganAsal, "
					+"produk.id as produkId,"
					+"produk.namaProduk as namaProduk) "
					+"from PlanningPelayananDetail model "
					+"left join model.strukPelayanan strukPelayanan "
					+"left join model.planningPelayanan planningPelayanan "
					+"left join planningPelayanan.strukPlaning strukPlanning "
					+"left join strukPlanning.kdruanganasal ruanganAsal "
					+"left join strukPlanning.kdkelompoktransaksi kelompoktransaksi "					
					+"left join model.produk produk "
					+"left join model.alat alat ");
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append("where kelompoktransaksi.id=73 and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode");
			buffer.append(" order by strukPelayanan.nostruk ");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);		
			result = query.getResultList();
		} else {
			buffer.append(" order by strukPelayanan.nostruk ");
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getAllPembilasanLinen2(Date startPeriode, Date endPeriode) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select distinct new Map("
					+"strukPelayanan.noRec as noRecStrukPelayanan) "
					+"from PlanningPelayananDetail model "
					+"left join model.strukPelayanan strukPelayanan "
					+"left join model.planningPelayanan planningPelayanan "
					+"left join planningPelayanan.strukPlaning strukPlanning "
					+"left join strukPlanning.kdruanganasal ruanganAsal "
					+"left join strukPlanning.kdkelompoktransaksi kelompoktransaksi "					
					+"left join model.produk produk "
					+"left join model.alat alat ");
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append("where kelompoktransaksi.id=73 and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);		
			result = query.getResultList();
		} else {
			buffer.append(" order by strukPelayanan.nostruk ");
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getAllPembilasanLinenByNoRecStrukPelayanan(Date startPeriode, 
			Date endPeriode,String noRecStrukPelayanan) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new Map("
					+"strukPelayanan.noRec as noRecStrukPelayanan,"
					+"strukPelayanan.nostruk as noStrukPelayanan,"
					+"strukPlanning.noRec as noRecStrukPlanning,"
					+"strukPlanning.noplanning as noPlanning,"
					+"to_char(planningPelayanan.tglPelayanan,'yyyy-mm-dd hh:mm:ss') as tglPelayanan,"
					+"alat.id as alatId,"
					+"alat.namaAlat as namaAlat,"
					+"coalesce(alat.kapasitasAlat,0) as kapasitasAlat,"
					+"planningPelayanan.qtyProdukExec as beratLinen,"
					+"ruanganAsal.id as ruanganAsalId, "
					+"ruanganAsal.namaRuangan as namaRuanganAsal, "
					+"produk.id as produkId,"
					+"produk.namaProduk as namaProduk) "
					+"from PlanningPelayananDetail model "
					+"left join model.strukPelayanan strukPelayanan "
					+"left join model.planningPelayanan planningPelayanan "
					+"left join planningPelayanan.strukPlaning strukPlanning "
					+"left join strukPlanning.kdruanganasal ruanganAsal "
					+"left join strukPlanning.kdkelompoktransaksi kelompoktransaksi "					
					+"left join model.produk produk "
					+"left join model.alat alat "
					+ "where kelompoktransaksi.id=73 and strukPelayanan.noRec=:noRecStrukPelayanan ");
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append(" and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode");
			buffer.append(" order by strukPelayanan.nostruk ");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);		
			query.setParameter("noRecStrukPelayanan", noRecStrukPelayanan);		
			result = query.getResultList();
		} else {
			buffer.append(" order by strukPelayanan.nostruk ");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("noRecStrukPelayanan", noRecStrukPelayanan);		
			result = query.getResultList();
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getAllPengeringan(Date startPeriode, Date endPeriode) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new Map("
					+"strukPelayanan.noRec as noRecStrukPelayanan,"
					+"strukPelayanan.nostruk as noStrukPelayanan,"
					+"strukPlanning.noRec as noRecStrukPlanning,"
					+"strukPlanning.noplanning as noPlanning,"
					+"to_char(planningPelayanan.tglPelayanan,'yyyy-mm-dd hh:mm:ss') as tglPelayanan,"
					+"planningPelayanan.qtyProdukExec as beratLinen,"
					+"ruanganAsal.id as ruanganAsalId, "
					+"ruanganAsal.namaRuangan as namaRuanganAsal, "
					+"produk.id as produkId,"
					+"produk.namaProduk as namaProduk,"
					+ "model.jumlahCycle as jumlahCycle) "
					+"from PlanningPelayananDetail model "
					+ "left join model.strukPelayanan strukPelayanan "
					+"left join model.planningPelayanan planningPelayanan "
					+"left join planningPelayanan.strukPlaning strukPlanning "
					+"left join strukPlanning.kdruanganasal ruanganAsal "
					+"left join strukPlanning.kdkelompoktransaksi kelompoktransaksi "					
					+"left join model.produk produk ");
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append("where kelompoktransaksi.id=74 and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);			
			result = query.getResultList();
		} else {
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getAllPelipatan(Date startPeriode, Date endPeriode) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select distinct new Map("
					+"strukPelayanan.noRec as noRecStrukPelayanan,"
					+"strukPelayanan.nostruk as noStrukPelayanan,"
					+"strukPlanning.noRec as noRecStrukPlanning,"
					+"strukPlanning.noplanning as noPlanning,"
					+"to_char(planningPelayanan.tglPelayanan,'yyyy-mm-dd hh:mm:ss') as tglPelayanan,"
					+"planningPelayanan.qtyProdukExec as beratLinen,"
					+"ruanganAsal.id as ruanganAsalId, "
					+"ruanganAsal.namaRuangan as namaRuanganAsal, "
					+"produk.id as produkId,"
					+"produk.namaProduk as namaProduk,"
					+ "model.jumlahCycle as jumlahCycle) "
					+"from PlanningPelayananDetail model "
					+ "left join model.strukPelayanan strukPelayanan "
					+"left join model.planningPelayanan planningPelayanan "
					+"left join planningPelayanan.strukPlaning strukPlanning "
					+"left join strukPlanning.kdruanganasal ruanganAsal "
					+"left join strukPlanning.kdkelompoktransaksi kelompoktransaksi "					
					+"left join model.produk produk ");
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append("where kelompoktransaksi.id=75 and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);			
			result = query.getResultList();
		} else {
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getAllPengambilanLinenEksternal(String namaPelanggan,Date startPeriode, Date endPeriode) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select distinct new Map("
					+"strukPelayanan.noRec as noRecStrukPelayanan,"
					+ "strukPelayanan.nostruk as noStrukPelayanan,"
					 +"to_char(strukPelayanan.tglAmbil,'yyyy-mm-dd hh:mm:ss') as tglAmbil,"
					 +"strukPelayanan.namaLengkapAmbil as namaLengkapAmbil,"
					 +"strukPelayanan.keteranganAmbil as keteranganAmbil,"
					+ "pelanggan.id as pelangganId,"
					+ "pelanggan.namaPasien as namaPelanggan,"
					+ "pelanggan.noHp as noTelepon,"
					+"strukPlanning.noRec as noRecStrukPlanning,"
					+"strukPlanning.noplanning as noPlanning,"
					+"to_char(planningPelayanan.tglPelayanan,'yyyy-mm-dd hh:mm:ss') as tglPelayanan,"
					+"planningPelayanan.qtyProdukExec as beratLinen,"
					+"ruanganAsal.id as ruanganAsalId, "
					+"ruanganAsal.namaRuangan as namaRuanganAsal,  "
					+"produk.id as produkId,"
					+"produk.namaProduk as namaProduk,"
					+ "noClosing.noRec as noRecClosing, "
					+ "model.jumlahCycle as jumlahCycle) "
					+"from PlanningPelayananDetail model "
					+ "left join model.strukPelayanan strukPelayanan "
					+"left join model.planningPelayanan planningPelayanan "
					+"left join planningPelayanan.strukPlaning strukPlanning "
					+"left join strukPlanning.kdruanganasal ruanganAsal "
					+"left join strukPlanning.kdkelompoktransaksi kelompoktransaksi "					
					+"left join model.produk produk "
					+ "left join strukPelayanan.nocm pelanggan "
					+ "left join strukPelayanan.noclosing noClosing "
					+ "where kelompoktransaksi.id=75 and ruanganAsal.id=93 ");
		
		if (CommonUtil.isNotNullOrEmpty(startPeriode) && CommonUtil.isNotNullOrEmpty(endPeriode)) {
			buffer.append("and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode ");
		}

		if (CommonUtil.isNotNullOrEmpty(namaPelanggan)) {
			buffer.append("and lower(pelanggan.namaPasien) like lower(:namaPelanggan) ");
		}

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

		if (CommonUtil.isNotNullOrEmpty(startPeriode) && CommonUtil.isNotNullOrEmpty(endPeriode)) {
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);
		}

		if (CommonUtil.isNotNullOrEmpty(namaPelanggan) && !namaPelanggan.equals("")) {
			query.setParameter("namaPelanggan", "%" + namaPelanggan + "%");
		}

		result = query.getResultList();
		/*
		if(CommonUtil.isNotNullOrEmpty(startPeriode) && 
				CommonUtil.isNotNullOrEmpty(endPeriode)){
			buffer.append("where kelompoktransaksi.id=75 and ruanganAsal.id=93 and planningPelayanan.tglPelayanan between :startPeriode and :endPeriode");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);			
			result = query.getResultList();
		} else {
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		*/
		return result;
	}

	@Override
	public List<Map<String, Object>> getAllRetur(Date startPeriode, Date endPeriode) {
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new Map (pelanggan.noCm as noPelanggan," 
						+"pelanggan.id as pelangganId,"
						+"pelanggan.namaPasien as namaPelanggan,"
						+"pelanggan.noHp as noTelepon,"
						+"noretur.noRetur as noRetur,"
						+"noretur.tglRetur as tglRetur) "
						+"from StrukPelayananDetail model "
						+"left join model.nostruk nostruk "
						+"left join nostruk.nocm pelanggan "
						+"left join model.noretur noretur "
						+ "left join noretur.kelompokTransaksi kelompokTransaksi "
						+ "left join noretur.ruangan ruangan "
					+ "where kelompokTransaksi.id=6 and ruangan.id=93 and model.noretur is not null ");
		
		if (CommonUtil.isNotNullOrEmpty(startPeriode) && CommonUtil.isNotNullOrEmpty(endPeriode)) {
			buffer.append("and noretur.tglRetur between :startPeriode and :endPeriode ");
		}

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

		if (CommonUtil.isNotNullOrEmpty(startPeriode) && CommonUtil.isNotNullOrEmpty(endPeriode)) {
			query.setParameter("startPeriode", startPeriode);
			query.setParameter("endPeriode", endPeriode);
		}

		result = query.getResultList();
		return result;
	}

}
