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

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

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

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.GenericDaoCustom;
import com.jasamedika.medifirst2000.util.CommonUtil;

/**
 * Repository Custom class
 * 
 * @author None
 * @param <T>
 */
@Repository
public class GenericDaoCustomImpl implements GenericDaoCustom {
	@PersistenceContext
	protected EntityManager em;

	@Override
	public List findPapNoCmAndTglInput(String table, String noCm, Date tglInput) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + table
				+ " model where model.pasien.noCm=:noCm and model.tglInput=:tglMeraskanKeluhan");

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

		return query.getResultList();
	}

	@Override
	public List<Object> findPapNoCm(String previx, String noCm) {
		StringBuffer buffer = new StringBuffer();
		if (previx.equals("PapDataTandaVital"))
			buffer.append("select model from " + previx + " model where model.pasien.pasienDaftar.pasien.noCm=:noCm");
		else
			buffer.append(
					"select model from " + previx + " model where model.pasienDaftar.pasienDaftar.pasien.noCm=:noCm");

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

		return query.getResultList();
	}

	@Override
	public List<Object> findMedicalRecordNoCmAndTglInput(String table, String noCm, Date tglInput) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + table
				+ " model where model.pasienDaftar.pasienDaftar.pasien.noCm=:noCm and (model.pasienDaftar.pasienDaftar.tglRegistrasi)=(:tglRegistrasi)");

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

		return query.getResultList();
	}

	@Override
	public List<Object> findMedicalRecordTglInput(String previx, Date from, Date until) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + previx
				+ " model where  model.pasienDaftar.pasienDaftar.tglRegistrasi between :from and :until");

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

		if (CommonUtil.isNotNullOrEmpty(from)) {
			from.setHours(0);
			from.setMinutes(0);
			from.setSeconds(0);
			query.setParameter("from", from);
		}
		if (CommonUtil.isNotNullOrEmpty(until)) {
			until.setHours(23);
			until.setMinutes(59);
			until.setSeconds(59);
			query.setParameter("until", until);
		}

		return query.getResultList();
	}

	@Override
	public List<Object> findMedicalRecordNoRec(String i, String noRec) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + i + " model left join "
				+ "model.pasienDaftar a where a.noRec =:noRec");

		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

		return query.getResultList();
	}

	@Override
	public List<Object> findMedicalRecordNoRecAndTglInput(String i, String noRec, Date tglInput) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + i
				+ " model where  model.pasienDaftar.noRec =:noRec and model.tglInput=:tglInput");

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

		return query.getResultList();
	}

	@Override
	public List<Object> findMedicalRecordNoRecAndTglInputPasien(String i, String noRec, Date tglInput) {
		StringBuffer buffer = new StringBuffer();

		buffer.append(
				"select model from " + i + " model where  model.pasien.noRec =:noRec and model.tglInput=:tglInput");

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

		return query.getResultList();
	}

	@Override
	public Integer checkInputan(String i, String noRec) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select count(model.noRec) from " + i + " model where  model.pasienDaftar.noRec =:noRec");

		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

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

	@Override
	public List<Object> findMedicalRecordCustomTgl(String previx, Date from, Date until, String tgl) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + previx + " model where  model." + tgl + " between :from and :until");

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

		if (CommonUtil.isNotNullOrEmpty(from)) {
			from.setHours(0);
			from.setMinutes(0);
			from.setSeconds(0);
			query.setParameter("from", from);
		}
		if (CommonUtil.isNotNullOrEmpty(until)) {
			until.setHours(23);
			until.setMinutes(59);
			until.setSeconds(59);
			query.setParameter("until", until);
		}

		return query.getResultList();
	}

	@Override
	public List<Object> findMedicalRecordNoRecAntrianPasienDiPeriksa(String i, String noRec) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + i + " model where  model.antrianPasienDiPeriksa.noRec =:noRec");

		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

		return query.getResultList();
	}

	@Override
	public List<Object> findMedicalRecordPasienNoRecAntrianPasienDiPeriksa(String i, String noRec) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select model from " + i + " model where  model.pasien.noRec =:noRec");

		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);

		return query.getResultList();
	}
}
