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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.NeracaLimbahDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.NeracaLimbah;

/**
 * Repository Custom class
 * 
 * @author Roberto
 */
@Repository
public class NeracaLimbahDaoCustomImpl extends CoreDaoImpl<NeracaLimbah> implements
		NeracaLimbahDaoCustom {

	public NeracaLimbahDaoCustomImpl() {
		super(NeracaLimbah.class);
	}

	@Override
	public List<Map<String,Object>> findList(int rowStart, int rowEnd, String sort, String dir) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map ");
		buffer.append(" ( ");
		buffer.append(" coalesce(p.Catatan,'-') as catatan, ");
		buffer.append(" coalesce(p.jumlahYangBelumTerkelola,0.00) as jumlahYangBelumTerkelola, ");
		buffer.append(" coalesce(p.kinerjaPengelolaan,0.00) as kinerjaPengelolaan, ");
		buffer.append(" p.periodeAhir as periodeAhir, ");
		buffer.append(" coalesce(p.noRec,null) as noRec, ");
		buffer.append(" p.periodeAwal as periodeAwal, ");
		buffer.append(" coalesce(p.residu,0.00) as residu ");
		buffer.append(" ) ");
		buffer.append("from NeracaLimbah p ");
		buffer.append("where (p.statusEnabled = true or p.statusEnabled is null) ");
		if (sort != "") {
			buffer.append("order by p." + sort + " " + dir);
		} else {
			buffer.append("order by p.noRec 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 findCount() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select count(p.noRec) ");
		
		buffer.append("from NeracaLimbah p ");
		buffer.append("where (p.statusEnabled = true or p.statusEnabled is null) ");
		
		Query query = em.createQuery(buffer.toString());
		
		return ((Long) query.getSingleResult()).intValue();
	}
	
	@Override
	public List<Map<String,Object>> getB3KeluarByPerlakuan(String startDate, String endDate) {
		System.out.println("startDate,endDate = "+startDate+" - "+endDate);
		List<Map<String, Object>> listData = new ArrayList<>();
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map(p.id as id, p.name as Perlakuan, ");
		buffer.append(" sum(l.jumlahLimbahB3Keluar) as ttlqtyKeluar) ");
		buffer.append(" from LimbahB3Keluar l ");
		buffer.append(" inner join l.perlakuan p ");
		buffer.append(" inner join l.jenisLimbahB3Masuk j where ");		
		buffer.append(" (date(l.tanggal) between date('"+startDate+"') and date('"+endDate+"')) ");
		buffer.append(" and (l.statusEnabled = true or l.statusEnabled is null) ");		
		buffer.append(" group by p.id, p.name ");
		
		Query query = em.createQuery(buffer.toString());
		List<Map<String,Object>> listLimbahB3Keluar = query.getResultList();
		
		
		StringBuffer buffer2 = new StringBuffer();
		buffer2.append("select new map(p.id as id, p.name as name) from Perlakuan p ");
		buffer.append(" where (p.statusEnabled = true or p.statusEnabled is null) ");
		
		Query query2 = em.createQuery(buffer2.toString());
		List<Map<String,Object>> listPerlakuan = query2.getResultList();
		
		for(Map<String, Object> value1 : listPerlakuan){
			Map<String, Object> perlakuanMap = new HashMap<>();
			Integer idPerlakuan1 = (Integer) value1.get("id");
			boolean isTrue=true;
			for(Map<String, Object> value2 : listLimbahB3Keluar){
				Integer idPerlakuan2 = (Integer) value2.get("id");
				if(idPerlakuan2==idPerlakuan1){
					listData.add(value2);
					isTrue = false;
				}
			}
			
			if(isTrue){
				perlakuanMap.put("id", idPerlakuan1);
				perlakuanMap.put("Perlakuan", (String) value1.get("name"));
				perlakuanMap.put("ttlqtyKeluar", new Double(0));
				listData.add(perlakuanMap);
			}
			
		}

		return listData;
	}

}
