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.MappingJenisLinenToProdukDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.MappingJenisLinenToProduk;

@Repository
public class MappingJenisLinenToProdukDaoCustomImpl extends CoreDaoImpl<MappingJenisLinenToProduk> implements
MappingJenisLinenToProdukDaoCustom {
	
	public MappingJenisLinenToProdukDaoCustomImpl() {
		super(MappingJenisLinenToProduk.class);
	}
	
	
	@Override
	public List<Map<String, Object>> findDaftarMasterBahanBakuByLinen(int rowStart, int rowEnd, String sort, String dir) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map ( ");
		buffer.append("b.jenisLinen as jenislinen, ");
		buffer.append("c.namaAlat as mesin, ");
		buffer.append("d.namaProduk as produk, ");
		buffer.append("e.satuanStandar as satuanstandar, ");
		buffer.append("b.id as jenislinenid, ");
		buffer.append("c.id as mesinid, ");
		buffer.append("d.id as produkid, ");
		buffer.append("e.id as satuanstandarid, ");
		buffer.append("a.qty as qty ) ");
		buffer.append("from MappingJenisLinenToProduk a ");
		buffer.append("left join a.jenisLinen b ");
		buffer.append("left join a.alat c ");
		buffer.append("left join a.produk d ");
		buffer.append("left join a.satuanStandar e ");
		if (sort != "") {
			buffer.append("order by " + sort + " " + dir);
		} else {
			buffer.append("order by a.id desc ");
		}
		Query query = em.createQuery(buffer.toString());
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}
	
	@Override
	public Integer findCountDaftarMasterBahanBakuByLinen() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(a) ");
		buffer.append("from MappingJenisLinenToProduk a ");
		buffer.append("left join a.jenisLinen b ");
		buffer.append("left join a.alat c ");
		buffer.append("left join a.produk d ");
		buffer.append("left join a.satuanStandar e ");
		Query query = em.createQuery(buffer.toString());

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

}
