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

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.RegistrasiAsetDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.RegistrasiAset;
import com.jasamedika.medifirst2000.util.CommonUtil;

/**
 * Repository Custom class
 * 
 * @author Roberto
 */
@Repository
public class RegistrasiAsetDaoCustomImpl extends CoreDaoImpl<RegistrasiAset> implements
		RegistrasiAsetDaoCustom {

	public RegistrasiAsetDaoCustomImpl() {
		super(RegistrasiAset.class);
	}

	@Override
	public int barangAsetCount(Integer ruanganId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(registrasiAset.noRegisterAset) from RegistrasiAset registrasiAset "
				+ "left join registrasiAset.produk produk left join registrasiAset.ruangan ruangan where registrasiAset.noRec is not null ");

		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			buffer.append(" and ruangan.id=:ruanganId ");
		}

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

		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			query.setParameter("ruanganId", ruanganId);
		}

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

	}


	@Override
	public List<Map<String, Object>> baragAsetList(int rowStart, int rowEnd, String sort, String dir,Integer ruanganId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map ( produk.namaProduk as namaProduk,registrasiAset.noRegisterAset as noRegistrasiAset,ruangan.namaRuangan as namaRuangan) from RegistrasiAset registrasiAset "
				+ "left join registrasiAset.produk produk "
				+ "left join registrasiAset.ruangan ruangan "
				+ "where registrasiAset.noRec is not null  ");
		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			buffer.append(" and ruangan.id=:ruanganId ");
		}
		buffer.append(" order by registrasiAset.noRegisterAset asc ");
		
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(ruanganId)){
			query.setParameter("ruanganId", ruanganId);
		}
		
		List<Map<String, Object>> list = query.getResultList();
		
		return list;
	}

}
