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.PapTandaVitalDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.PapTandaVital;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Repository
public class PapTandaVitalDaoCustomImpl extends CoreDaoImpl<PapTandaVital> implements PapTandaVitalDaoCustom{

	public PapTandaVitalDaoCustomImpl() {
		super(PapTandaVital.class);
	}

	@Override
	public List<Map<String, Object>> getAllByPasienDaftar(String noCm, String startDate, String endDate, Integer status){
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		
		if(CommonUtil.isNotNullOrEmpty(status)){
			if(status==0){
				buffer.append("select NEW Map ("
					+ "a.noRec as noRec,"
					+ "a.tglInput as tglInput,"
					+ "a.suhu as suhu,"
					+ "a.nadi as nadi,"
					+ "a.pernapasan as pernapasan,"
					+ "a.tekananDarah as tekananDarah) ");
			} 
			else if(status==1){
				buffer.append("select NEW Map ("
					+ "a.noRec as noRec,"
					+ "a.tglInput as tglInput,"
					+ "a.alergi as alergi,"
					+ "a.keterangan as keterangan) ");
			} 
			else if(status==2){
				buffer.append("select NEW Map ("
					+ "a.noRec as noRec,"
					+ "a.tglInput as tglInput,"
					+ "a.beratBadan as beratBadan,"
					+ "a.tinggiBadan as tinggiBadan,"
					+ "a.lingkarKepala as lingkarKepala,"
					+ "a.imt as imt,"
					+ "a.statusGizi as statusGizi) ");
			}
			buffer.append("from PapTandaVital a "
					+ "left join a.pasienDaftar b "
					+ "left join b.pasienDaftar c "
					+ "left join c.pasien d "
					+ "where d.noCm=:noCm ");
			
		} else{
			buffer.append("select NEW Map ( "
					+ "a.noRec as noRec,"
					+ "a.tglInput as tglInput,"
					+ "a.suhu as suhu,"
					+ "a.nadi as nadi,"
					+ "a.pernapasan as pernapasan,"
					+ "a.tekananDarah as tekananDarah,"
					+ "a.alergi as alergi,"
					+ "a.beratBadan as beratBadan,"
					+ "a.tinggiBadan as tinggiBadan,"
					+ "a.lingkarKepala as lingkarKepala,"
					+ "a.imt as imt,"
					+ "a.statusGizi as statusGizi,"
					+ "a.keterangan as keterangan) "
					+ "from PapTandaVital a "
					+ "left join a.pasienDaftar b "
					+ "left join b.pasienDaftar c "
					+ "left join c.pasien d "
					+ "where d.noCm=:noCm ");
		}
		//---------------------------------------------
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			buffer.append("and a.tglInput between date(:startDate) and date(:endDate)");
		}
		//---------------------------------------------
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(noCm)){
			query.setParameter("noCm", noCm);
		}
		//---------------------------------------------
		if(CommonUtil.isNotNullOrEmpty(startDate) && CommonUtil.isNotNullOrEmpty(endDate)){
			query.setParameter("startDate", startDate);
			query.setParameter("endDate", endDate);
		}
		//---------------------------------------------
		if(CommonUtil.isNotNullOrEmpty(buffer)){
			result = query.getResultList();
		}
		//---------------------------------------------
		return result;
	}
}
