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

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.StokProdukDetailDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StokProdukDetail;
import com.jasamedika.medifirst2000.util.CommonUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class StokProdukDetailDaoCustomImpl extends CoreDaoImpl<StokProdukDetail> implements StokProdukDetailDaoCustom {

	public StokProdukDetailDaoCustomImpl() {
		super(StokProdukDetail.class);
	}
	
	@Override
	public StokProdukDetail findLastStok(Integer produkId,Ruangan ruanganAsal,Ruangan ruanganTujuan,List<String> noRec,String metodePengambilanBarang) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select stokProdukDetail from StokProdukDetail stokProdukDetail left join stokProdukDetail.ruangan ruangan ");
		buffer.append(" left join stokProdukDetail.strukPelayanan strukPelayanan ");
		buffer.append(" left join stokProdukDetail.produk produk ");
		buffer.append(" where produk.id=:produkId ");
		buffer.append(" and ruangan.id=:ruanganAsalId and stokProdukDetail.qtyProduk > 0 ");
		if(CommonUtil.isNotNullOrEmpty(noRec)){
			buffer.append(" and stokProdukDetail.noRec not in (:noRec) ");
		}
		if(metodePengambilanBarang.equalsIgnoreCase("fifo")){
			buffer.append(" order by strukPelayanan.tglstruk desc");
		}else{
			buffer.append(" order by stokProdukDetail.tglkadaluarsa desc");
		}
		

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

		StokProdukDetail result = new StokProdukDetail();
		
		List<StokProdukDetail> data=query.getResultList();
		if(CommonUtil.isNotNullOrEmpty(data)){
			result=data.get(0);
		}
		

		return result;
	}
	
	@Override
	public StokProdukDetail findLastStokIfNull(Integer produkId,Ruangan ruanganAsal,Ruangan ruanganTujuan,List<String> noRec,String metodePengambilanBarang) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select stokProdukDetail from StokProdukDetail stokProdukDetail left join stokProdukDetail.ruangan ruangan ");
		buffer.append(" left join stokProdukDetail.strukPelayanan strukPelayanan ");
		buffer.append(" left join stokProdukDetail.produk produk ");
		buffer.append(" where produk.id=:produkId ");
		buffer.append(" and ruangan.id=:ruanganAsalId  ");
		if(CommonUtil.isNotNullOrEmpty(noRec)){
			buffer.append(" and stokProdukDetail.noRec not in (:noRec) ");
		}
		if(metodePengambilanBarang.equalsIgnoreCase("fifo")){
			buffer.append(" order by strukPelayanan.tglstruk desc");
		}else{
			buffer.append(" order by stokProdukDetail.tglkadaluarsa desc");
		}
		

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

		StokProdukDetail result = new StokProdukDetail();
		
		List<StokProdukDetail> data=query.getResultList();
		if(CommonUtil.isNotNullOrEmpty(data)){
			result=data.get(0);
		}
		

		return result;
	}


	
}
