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.ItJadwalPerawatanDaoCustom;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

@Repository("ItJadwalPerawatanDaoCustom")
public class ItJadwalPerawatanDaoCustomImpl   implements ItJadwalPerawatanDaoCustom {
	
	@PersistenceContext
	protected EntityManager em;

	@Override
	public Integer getCountJadwalPerawatan(String startDate, String endDate,String statusPemeliharaan) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(itJadwalPerawatan) ");
		buffer.append(" from ItJadwalPerawatan itJadwalPerawatan " );
		buffer.append(" left join itJadwalPerawatan.strukOrder strukOrder " );
		buffer.append(" left join itJadwalPerawatan.ruangan ruangan " );
		buffer.append(" where (itJadwalPerawatan.statusEnabled is null or itJadwalPerawatan.statusEnabled is true) " );
		
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			buffer.append(" and date(itJadwalPerawatan.tanggal) between date(:startDate) and date(:endDate) ");
		}
		if(CommonUtil.isNotNullOrEmpty(statusPemeliharaan)){
			buffer.append("and itJadwalPerawatan.statusPengerjaan =:statusPekerjaan ");
		}

		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(statusPemeliharaan)){
			query.setParameter("statusPekerjaan",statusPemeliharaan);
		}

		return ((Long) query.getSingleResult()).intValue();
	}
	
	@Override
	public List<Map<String, Object>> getJadwalPerawatanAll(int rowStart, int rowEnd, String sort, String dir,String startDate,String endDate,String statusPemeliharaan) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map(itJadwalPerawatan.noRec as noRec, " );
		buffer.append(" to_char(itJadwalPerawatan.tanggal,'yyyy-mm-dd') as tanggalInput, " );
		buffer.append(" itJadwalPerawatan.tglJadwal as tglJadwal, " );
		buffer.append(" itJadwalPerawatan.keterangan as keterangan, " );
		buffer.append(" itJadwalPerawatan.statusPengerjaan as statusPengerjaan, " );
		buffer.append(" ruangan.id as idRuangan, " );
		buffer.append(" ruangan.namaRuangan as namaRuangan, " );
		buffer.append(" strukOrder.noOrder as noOrder) " );
		buffer.append(" from ItJadwalPerawatan itJadwalPerawatan " );
		buffer.append(" left join itJadwalPerawatan.strukOrder strukOrder " );
		buffer.append(" left join itJadwalPerawatan.ruangan ruangan " );
		buffer.append(" where (itJadwalPerawatan.statusEnabled is null or itJadwalPerawatan.statusEnabled is true) " );
		
		
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			buffer.append(" and date(itJadwalPerawatan.tanggal) between date(:startDate) and date(:endDate)");
		}

		if(CommonUtil.isNotNullOrEmpty(statusPemeliharaan)){
			buffer.append("and itJadwalPerawatan.statusPengerjaan =:statusPemeliharaan ");
		}
		
		if (sort != "") {
			buffer.append("order by itJadwalPerawatan." + sort + " " + dir);
		} else {
			buffer.append("order by itJadwalPerawatan.tglJadwal desc ");
		}
		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(statusPemeliharaan)){
			query.setParameter("statusPemeliharaan",statusPemeliharaan);
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}
	
}
