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

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

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.RekananDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.Rekanan;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Repository
public class RekananDaoCustomImpl extends CoreDaoImpl<Rekanan> implements RekananDaoCustom {

	public RekananDaoCustomImpl() {
		super(Rekanan.class);
	}

	@Override
	public List<Map<String, Object>> findAllRekananByInstitusiForPesertaDidik() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map ");
		buffer.append(" ( ");
		buffer.append(" r.namaRekanan as namaRekanan , r.id as id ");
		buffer.append(" ) ");
		buffer.append("from Rekanan r "
			+ " order by r.namaRekanan asc ");
		 
		Query query = em.createQuery(buffer.toString());
	
	 
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}
	@Override
	public List<Map<String, Object>> findAllRekananDetailMouPKS() {
		Date now = new Date();
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map ");
		buffer.append(" ( ");
		buffer.append(" rmpks.id as id, rmpks.statusinstitusi as statusinstitusi, rmpks.akreditasi as akreditasi, ");
		buffer.append(" rmpks.tkt as tkt, rmpks.startAkreditasi as startAkreditasi,rmpks.endPKL as endPKL, ");
		buffer.append("rmpks.bangunan as bangunan,rmpks.hambatan as hambatan, rmpks.saran as saran, rmpks.keterangan as keterangan, ");
		buffer.append(" kj.kualifikasiJurusan as kualifikasijurusan, kj.id as kjid, ");
		buffer.append(" ps.nama as namaps, ps.id as psid, ");
		buffer.append(" sk.id as skid ,sk.noSK as noSK,");
		buffer.append(" r.id as idRekanan ,r.namaRekanan as namaRekanan");
		buffer.append(" ) from RekananMouPKS rmpks "
			+ " left join rmpks.jurusan kj "
			+ " left join rmpks.programStudi ps "
			+ " left join rmpks.suratKeputusan sk "
			+ " left join rmpks.rekanan r"
			+ " where sk.tglBerlakuAkhir >=  '"+now +"' ");
		 
		Query query = em.createQuery(buffer.toString());
	
	 
		List<Map<String,Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findAllRekananMouPKS() {
//		Date now = new Date();
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map ");
		buffer.append(" ( ");
		buffer.append(" rmpks.id as id, rmpks.statusinstitusi as statusinstitusi, rmpks.akreditasi as akreditasi, ");
		buffer.append(" rmpks.tkt as tkt, rmpks.startAkreditasi as startAkreditasi,rmpks.endPKL as endPKL, ");
		buffer.append("rmpks.bangunan as bangunan,rmpks.hambatan as hambatan, rmpks.saran as saran, rmpks.keterangan as keterangan");
		//buffer.append(" kj.kualifikasiJurusan as kualifikasijurusan, kj.id as kjid, ");
		//buffer.append(" ps.nama as namaps, ps.id as psid, ");
		//buffer.append(" sk.id as skid ");
		buffer.append(" ) from RekananMouPKS rmpks ");
			//+ " left join KualifikasiJurusan kj "
			//+ " left join ProgramStudi ps "
			//+ " left join SuratKeputusan sk "
			//+ " where sk.tglBerlakuAkhir <=  '"+now +"' ");
		 
		Query query = em.createQuery(buffer.toString());
	
	 
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int listRuanganCount(Integer rekananId, Integer jenisRekananId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(rekanan.id) ");
		buffer.append(" from Rekanan rekanan where rekanan.id is not null ");
		
		if(CommonUtil.isNotNullOrEmpty(rekananId)){
			buffer.append(" and rekanan.id=:rekananId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisRekananId)){
			buffer.append(" and jenisRekanan.id=:jenisRekananId ");
		}
	
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(rekananId)){
			query.setParameter("rekananId",rekananId );
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisRekananId)){
			query.setParameter("jenisRekananId",jenisRekananId );
		}
	
		
		return ((Long) query.getSingleResult()).intValue();
	}

	@Override
	public List<Map<String, Object>> listRuangan(Integer page, Integer limit, String sort, String dir,Integer rekananId, Integer jenisRekananId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map(rekanan.namaRekanan as namaRekanan,jenisRekanan.namaJenisRekanan as namaJenisRekanan,rekanan.alamatLengkap as alamatLengkap,rekanan.telepon as telepon) ");
		buffer.append(" from Rekanan rekanan left join rekanan.jenisRekanan jenisRekanan where rekanan.id is not null ");
		
		if(CommonUtil.isNotNullOrEmpty(rekananId)){
			buffer.append(" and rekanan.id=:rekananId ");
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisRekananId)){
			buffer.append(" and jenisRekanan.id=:jenisRekananId ");
		}
	
		Query query = em.createQuery(buffer.toString());
		
		if(CommonUtil.isNotNullOrEmpty(rekananId)){
			query.setParameter("rekananId",rekananId );
		}
		
		if(CommonUtil.isNotNullOrEmpty(jenisRekananId)){
			query.setParameter("jenisRekananId",jenisRekananId );
		}
	
		List<Map<String,Object>>data=query.getResultList();
		return data;
	}

}
