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

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

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.ItPerbaikanDaoCustom;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

@Repository("ItPerbaikanDaoCustom")
public class ItPerbaikanDaoCustomImpl   implements ItPerbaikanDaoCustom {	
	
	@PersistenceContext
	protected EntityManager em;
	
	@Override
	public List<Map<String, Object>> getLaporanPerbaikanIT(String startDate,String endDate,Integer ruanganId,Integer statusPekerjaan){
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map (a.noRec as noRec, a.noOrder as noOrder,h.namaProduk as namaProduk,b.namaLengkap as pelapor,c.statusPengerjaan as statusPengerjaan, ");
		buffer.append("c.statusRespon as statusRespon, c.waktuRespon as waktuRespon, c.ketStatusRespon as keteranganRespon, e.namaRuangan as namaRuangan, ");
		buffer.append("e.id as ruanganId, a.keluhan as keluhan, a.tglPesan as tglPesan, g.tglMulaiPeriksa as tglMulaiPeriksa, b.id as pelaporId, ");
		buffer.append("d.noRec as noRecAsset, g.analisaTeknisi as analisaTeknisi, g.analisaKerusakan as analisaKerusakan, g.jenisPekerjaan as jenisPekerjaan, j.jenisProduk as kategoriKerusakan) ");
		buffer.append("from ItPerbaikan a ");
		buffer.append("left join a.pelapor b ");
		buffer.append("left join a.itStatusPerbaikan c ");
		buffer.append("left join a.registrasiAset d ");
		buffer.append("left join b.ruangan e ");
		buffer.append("left join a.itPelaksanaanPerbaikan g ");
		buffer.append("left join d.produk h ");
		buffer.append("left join h.detailJenisProduk i ");
		buffer.append("left join i.jenisProduk j ");
		buffer.append("where (a.statusEnabled is null or a.statusEnabled is true) ");
		
		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			buffer.append(" and a.tglPesan between :startDate and :endDate ");
		}
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			buffer.append(" and e.id = :ruanganId ");
		}
		if (CommonUtil.isNotNullOrEmpty(statusPekerjaan)) {
			buffer.append(" and c.statusPengerjaan = :statusPekerjaan ");
		}
		
		buffer.append(" order by a.noOrder,a.tglPesan,g.tglMulaiPeriksa ");
		
		Query query = em.createQuery(buffer.toString());
		
		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			query.setParameter("startDate", DateUtil.toDate(startDate));
			query.setParameter("endDate", DateUtil.toDate(endDate));
		}
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			query.setParameter("ruanganId", ruanganId);
		}
		if (CommonUtil.isNotNullOrEmpty(statusPekerjaan)) {
			query.setParameter("statusPekerjaan", statusPekerjaan);
		}

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

	@Override
	public List<Map<String, Object>> getAllPermintaanPerbaikanRev1(int rowStart, int rowEnd, String sort, String dir,String tanggalAwal, String tanggalAkhir,
			Integer ruanganId,Integer statusPekerjaan) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map (");
		buffer.append("a.id as id, a.noRec as noRec, a.noOrder as noOrder, d.kdProduk as kdProduk, ");
		buffer.append("d.namaProduk as namaProduk, a.tglPesan as tglPesan, e.merkProduk as merkProduk, ");
		buffer.append("f.typeProduk as typeProduk, c.noSeri as noSeri, a.keluhan as keluhan, b.namaLengkap as pelapor, ");
		buffer.append("h.namaRuangan as namaRuangan, g.statusPengerjaan as statusPengerjaan, ");
		buffer.append("g.statusRespon as statusRespon, g.ketStatusRespon as ketStatusRespon, ");
		buffer.append("h.id as ruanganId) ");
		buffer.append("from ItPerbaikan a ");
		buffer.append("left join a.pelapor b ");
		buffer.append("left join a.registrasiAset c ");
		buffer.append("left join c.produk d ");
		buffer.append("left join c.merkProduk e ");
		buffer.append("left join c.typeProduk f ");
		buffer.append("left join a.itStatusPerbaikan g ");
		buffer.append("left join b.ruangan h ");
		buffer.append("where (a.statusEnabled is null or a.statusEnabled is true) ");
		
		if(CommonUtil.isNotNullOrEmpty(tanggalAwal) && CommonUtil.isNotNullOrEmpty(tanggalAkhir)){
			buffer.append("and date(a.tglPesan) between  :tanggalAwal and :tanggalAkhir ");
		}

		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			buffer.append("and h.id =:ruanganId ");
		}
		if(CommonUtil.isNotNullOrEmpty(statusPekerjaan)){
			buffer.append("and g.statusPengerjaan =:statusPekerjaan ");
		}
		
		if (sort != "") {
			buffer.append("order by a." + sort + " " + dir);
		} else {
			buffer.append("order by a.noOrder desc ");
		}
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(tanggalAwal) && CommonUtil.isNotNullOrEmpty(tanggalAkhir)){
			query.setParameter("tanggalAwal",DateUtil.toDate(tanggalAwal));
			query.setParameter("tanggalAkhir", DateUtil.toDate(tanggalAkhir));
		}
		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			query.setParameter("ruanganId",ruanganId);
		}
		if(CommonUtil.isNotNullOrEmpty(statusPekerjaan)){
			query.setParameter("statusPekerjaan",statusPekerjaan);
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}

	@Override
	public Integer getCountPermintaanPerbaikanRev1(String tanggalAwal, String tanggalAkhir,
			Integer ruanganId,Integer statusPekerjaan) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(a) ");
		buffer.append("from ItPerbaikan a ");
		buffer.append("left join a.pelapor b ");
		buffer.append("left join a.registrasiAset c ");
		buffer.append("left join c.produk d ");
		buffer.append("left join c.merkProduk e ");
		buffer.append("left join c.typeProduk f ");
		buffer.append("left join a.itStatusPerbaikan g ");
		buffer.append("left join b.ruangan h ");
		buffer.append("where (a.statusEnabled is null or a.statusEnabled is true) ");
		
		if(CommonUtil.isNotNullOrEmpty(tanggalAwal) && CommonUtil.isNotNullOrEmpty(tanggalAkhir)){
			buffer.append("and date(a.tglPesan) between  :tanggalAwal and :tanggalAkhir ");
		}
		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			buffer.append("and h.id =:ruanganId ");
		}
		if(CommonUtil.isNotNullOrEmpty(statusPekerjaan)){
			buffer.append("and g.statusPengerjaan =:statusPekerjaan ");
		}

		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(tanggalAwal) && CommonUtil.isNotNullOrEmpty(tanggalAkhir)){
			query.setParameter("tanggalAwal",DateUtil.toDate(tanggalAwal));
			query.setParameter("tanggalAkhir", DateUtil.toDate(tanggalAkhir));
		}
		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			query.setParameter("ruanganId",ruanganId);
		}
		if(CommonUtil.isNotNullOrEmpty(statusPekerjaan)){
			query.setParameter("statusPekerjaan",statusPekerjaan);
		}

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


}
