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

@Repository
public class OrderProdukDaoCustomImpl extends CoreDaoImpl<Produk> implements OrderProdukDaoCustom{

	public OrderProdukDaoCustomImpl() {
		super(Produk.class);
	}

	@Override
	public List<Map<String, Object>> getAllProdukProduksiGizi(String menu){
		List<Map<String, Object>> result = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append("select new Map (produkasal.namaProduk as menu, "
				+"model.qtyProdukAsal as jumlah) "
				+"from ProdukProduksi model "
				+"left join model.produkAsal produkasal ");
		
		if(CommonUtil.isNotNullOrEmpty(menu)){
			buffer.append("where lower(produkasal.namaProduk) like lower(:menu)");
			Query query = em.createQuery(buffer.toString());
			query.setParameter("menu", "%"+menu+"%");
			result = query.getResultList();
		} else {
			Query query = em.createQuery(buffer.toString());
			result = query.getResultList();
		}
		return result;
	}

	@Override
	public List<OrderPelayanan> getOrderPelayanan(String noRec) {
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select orderPelayanan "+
					" from OrderPelayanan orderPelayanan "+
					" where orderPelayanan.noRec = :noRec");
	 
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		List<OrderPelayanan> result = query.getResultList();

		return result;
	}
	
}
