package com.jasamedika.medifirst2000.dao.custom;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.dto.DokumenInternalDto;
import com.jasamedika.medifirst2000.entities.DokumenInternal;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class DokumenInternalDaoCustomImpl extends CoreDaoImpl<DokumenInternal> implements DokumenInternalDaoCustom {

	public DokumenInternalDaoCustomImpl() {
		super(DokumenInternal.class);
	}

	@Override
	public List<DokumenInternalDto> findDokumenInternal(int rowStart, int rowEnd, String sort,String dir, String dateStart, String dateEnd, String nokirim) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("Select NEW com.jasamedika.medifirst2000.dto.DokumenInternalDto("
				+ "kirim.nokirim,"
				+ "internal )"
				+ "from DokumenInternal internal left join internal.nokirim kirim where internal.noRec is not null ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and internal.tglTerima between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(nokirim)){
			buffer.append("and nokirim.nokirim =:nokirim ");
		}	
		
		if (sort != "") {
			buffer.append("order by internal." + sort + " " + dir);
		} else {
			buffer.append("order by internal.noRec desc ");
		}
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		if(CommonUtil.isNotNullOrEmpty(nokirim)){
			query.setParameter("nokirim", nokirim);
		}	
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<DokumenInternalDto> list = query.getResultList();

		return list;
	}

	@Override
	public Integer findDokumenInternalCount(String sort, String dateStart, String dateEnd, String nokirim) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("Select count(internal.noRec) from DokumenInternal internal join internal.nokirim kirim where internal.noRec is not null ");
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			buffer.append("and internal.tglTerima between :dateStart and :dateEnd ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(nokirim)){
			buffer.append("and nokirim.nokirim =:nokirim ");
		}	
		
		Query query = em.createQuery(buffer.toString());
		if(CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)){
			query.setParameter("dateStart",DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}
		
		if(CommonUtil.isNotNullOrEmpty(nokirim)){
			query.setParameter("nokirim", nokirim);
		}	
		return ((Long) query.getSingleResult()).intValue();

	}
	
}
