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

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.AntrianPasienRegistrasiDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.AntrianPasienRegistrasi;
import com.jasamedika.medifirst2000.util.CommonUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class AntrianPasienRegistrasiDaoCustomImpl extends CoreDaoImpl<AntrianPasienRegistrasi>
		implements AntrianPasienRegistrasiDaoCustom {

	public AntrianPasienRegistrasiDaoCustomImpl() {
		super(AntrianPasienRegistrasi.class);
	}

	@Override
	public Integer findAllAntrianPagingCount(Date startDate, Date endDate, Integer ruanganId, String namaPasien) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select count(x.id) from AntrianPasienRegistrasi x where  x.noReservasi <>'-' and x.noRec IS NOT NULL ");
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			buffer.append("and x.ruanganId =:ruanganId ");
		}
		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			buffer.append("and x.tglReservasi between :startDate and :endDate  ");
		}

		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			query.setParameter("ruanganId", ruanganId);
		}

		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			query.setParameter("startDate", startDate);
			query.setParameter("endDate", endDate);
		}
		return ((Long) query.getSingleResult()).intValue();
	}

	@Override
	public List<AntrianPasienRegistrasi> findAllAntrianPagingList(int rowStart, int rowEnd, Date startDate,
			Date endDate, Integer ruanganId, String namaPasien,String noReservasi) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select x from AntrianPasienRegistrasi x where x.noReservasi <>'-' and x.noRec IS NOT NULL  ");
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			buffer.append("and x.ruanganId =:ruanganId ");
		}
		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			buffer.append("and x.tglReservasi between :startDate and :endDate  ");
		}
		if (CommonUtil.isNotNullOrEmpty(namaPasien)) {
			buffer.append("and x.pasien.namaPasien like :namaPasien ");
		}
		
		if (CommonUtil.isNotNullOrEmpty(noReservasi)) {
			buffer.append("and x.noReservasi =:noReservasi");
		}
		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			query.setParameter("ruanganId", ruanganId);
		}

		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			query.setParameter("startDate", startDate);
			endDate.setHours(23);
			endDate.setMinutes(59);
			endDate.setSeconds(59);
			query.setParameter("endDate", endDate);
		}
		if (CommonUtil.isNotNullOrEmpty(noReservasi)) {
			query.setParameter("noReservasi", noReservasi);
		}
		if (CommonUtil.isNotNullOrEmpty(namaPasien)) {
			query.setParameter("namaPasien", "%"+namaPasien+"%");
		}
		query.setFirstResult(rowStart);
		//query.setMaxResults(rowEnd);

		return (List<AntrianPasienRegistrasi>) query.getResultList();
	}

	@Override
	public List<AntrianPasienRegistrasi> findAllAntrianBaruPagingList(int rowStart, int rowEnd, Date startDate,
			Date endDate, Integer ruanganId, String namaPasien) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select x from AntrianPasienRegistrasi x where x.noReservasi ='-' and x.noRec IS NOT NULL  ");
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			buffer.append("and x.ruanganId =:ruanganId ");
		}
		if (CommonUtil.isNotNullOrEmpty(namaPasien)) {
			buffer.append("and x.pasien.namaPasien like :namaPasien ");
		}
		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			buffer.append("and x.tglReservasi between :startDate and :endDate  ");
		}

		buffer.append("order by x.noAntrian ");

		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			query.setParameter("ruanganId", ruanganId);
		}
		if (CommonUtil.isNotNullOrEmpty(namaPasien)) {
			query.setParameter("namaPasien", "%"+namaPasien+"%");
		}
		if (CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)) {
			query.setParameter("startDate", startDate);
			endDate.setHours(23);
			endDate.setMinutes(59);
			endDate.setSeconds(59);
			query.setParameter("endDate", endDate);
		}

		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

		return (List<AntrianPasienRegistrasi>) query.getResultList();
	}

	@Override
	public AntrianPasienRegistrasi findByNoRec(String id) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select x from AntrianPasienRegistrasi x where  x.noRec =:id  ");
		Query query = em.createQuery(buffer.toString());
		query.setParameter("id", id);
		
		return ((List<AntrianPasienRegistrasi>)query.getResultList()).get(0) ;
	}

}
