package com.jasamedika.medifirst2000.dao;

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

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.entities.SettingPirSdm;

@Repository("SettingPirSdmDao")
public interface SettingPirSdmDao extends PagingAndSortingRepository<SettingPirSdm, Integer> {

	@Query("select new map (unitKerja.id as idUnitKerja, " + "unitKerja.name as unitKerja, " + "setting.id as id, "
			+ "coalesce(setting.iku,0) as iku, " + "to_char(setting.tglAwal, 'yyyy-MM') as periode) "
			+ "from SettingPirSdm setting " + "left join setting.unitKerja unitKerja "
			+ "where setting.tglAwal between :start and :end " + "and unitKerja.id =:idUnitKerja ")
	Map<String, Object> getIKUUnitKerja(@Param("idUnitKerja") Integer idUnitKerja, @Param("start") Date start,
			@Param("end") Date end);

	@Query("select new map ( coalesce(setting.pir,0) as pir, " + "setting.id as id, "
			+ "to_char(setting.tglAwal, 'yyyy') as tahun) " + "from SettingPirSdm setting "
			+ "where setting.pir is not null and to_char(setting.tglAwal, 'yyyy') =:thn  ")
	List<Map<String, Object>> getPIR(@Param("thn") String thn);

	@Query("select new map (unitKerja.id as idUnitKerja, " + "unitKerja.name as unitKerja, " + "setting.id as id, "
			+ "coalesce(setting.iku,0) as iku, " + "to_char(setting.tglAwal, 'yyyy-MM') as periode) "
			+ "from SettingPirSdm setting " + "left join setting.unitKerja unitKerja "
			+ "where setting.iku is not null " + " order by to_char(setting.tglAwal, 'yyyy-MM')")
	List<Map<String, Object>> getAllIKU();

}
