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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.ObservasiTerintegrasiPasienDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.ObservasiTerintegrasiPasien;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Repository
public class ObservasiTerintegrasiPasienDaoCustomImpl extends CoreDaoImpl<ObservasiTerintegrasiPasien> implements ObservasiTerintegrasiPasienDaoCustom{

	public ObservasiTerintegrasiPasienDaoCustomImpl() {
		super(ObservasiTerintegrasiPasien.class);
	}

	@Override
	public List<Map<String, Object>> getAll(Integer jenisPegawai, Integer pasienId, String dateStart, String dateEnd){
		List<Map<String, Object>> result = new ArrayList<>();
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map("
				+ "g.namaPasien as namaPasien,"
				+ "e.namaLengkap as pegawaiInput,"
				+ "a.kdObservasiTerintegrasiPasein as kdObservasiTerintegrasiPasein,"
				+ "a.tekananDarahS as tekananDarahS,"
				+ "a.tekananDarahD as tekananDarahD,"
				+ "a.fn as fn,"
				+ "a.fp as fp,"
				+ "a.saO2 as saO2,"
				+ "a.skalaNyeri as skalaNyeri,"
				+ "a.skalaJatuh as skalaJatuh,"
				+ "a.gcs as gcs,"
				+ "a.pupilK1 as pupilK1,"
				+ "a.pupilK2 as pupilK2,"
				+ "a.ekg as ekg,"
				+ "a.masalah as masalah,"
				+ "a.hariJamInput as hariJamInput,");

		if(CommonUtil.isNotNullOrEmpty(jenisPegawai)){
			if(jenisPegawai==1){
				buffer.append("c.namaLengkap as konfirmasiDokter,");
			} else{
				buffer.append("b.namaLengkap as konfirmasiPerawat,");
			}
		}
		buffer.append("a.intervensi as intervensi) "
				+ "from ObservasiTerintegrasiPasien a "
				+ "left join a.konfirmasiPerawat b "
				+ "left join a.konfirmasiDokter c "
				+ "left join a.pasien d "
				+ "left join a.pegawaiInput e "
				+ "left join d.pasienDaftar f "
				+ "left join f.pasien g "
				+ "left join b.jenisPegawai h "
				+ "left join c.jenisPegawai i "
				+ "where g.id=:pasienId ");
		
		if(CommonUtil.isNotNullOrEmpty(dateStart)&&CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and date(a.hariJamInput) between date(:dateStart) and (:dateEnd) ");
		}
		
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(pasienId)){
			query.setParameter("pasienId", pasienId);
		}
		if(CommonUtil.isNotNullOrEmpty(dateStart)&&CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart", dateStart);
			query.setParameter("dateEnd", dateEnd);
		}
		if(CommonUtil.isNotNullOrEmpty(buffer)){
			result = query.getResultList();
		}
		return result;
	}
}
