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

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

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.RegistrasiPelayananPasienDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.RegistrasiPelayananPasien;
import com.jasamedika.medifirst2000.util.CommonUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class RegistrasiPelayananPasienDaoCustomImpl extends CoreDaoImpl<RegistrasiPelayananPasien>
		implements RegistrasiPelayananPasienDaoCustom {

	public RegistrasiPelayananPasienDaoCustomImpl() {
		super(RegistrasiPelayananPasien.class);
	}

	@Override
	public List<RegistrasiPelayananPasien> findAllRegistrasiPelayananPasien() {

		StringBuffer buffer = new StringBuffer();

		buffer.append("from RegistrasiPelayananPasien a ");
		buffer.append("INNER JOIN a.pasienDaftar b INNER JOIN a.noCm c");

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

		List<RegistrasiPelayananPasien> list = query.getResultList();
		List<RegistrasiPelayananPasien> listResult = new ArrayList<RegistrasiPelayananPasien>();
		return listResult;
	}

	@Override
	public Integer findAllAntrianPagingCount(Date startDate, Date endDate, Integer ruanganId) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(x.id) from RegistrasiPelayananPasien x where x.id IS NOT NULL  ");
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			buffer.append("and x.ruanganId =:ruanganId ");
		}
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			buffer.append("and x.tglRegistrasi 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<RegistrasiPelayananPasien> findAllAntrianPagingList(int rowStart, int rowEnd, Date startDate, Date endDate,Integer ruanganId) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select x from RegistrasiPelayananPasien x where x.id IS NOT NULL  ");
		if (CommonUtil.isNotNullOrEmpty(ruanganId)) {
			buffer.append("and x.ruanganId =:ruanganId ");
		}
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			buffer.append("and x.tglRegistrasi 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);
		}
		
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);

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

}
