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.IndikatorKinerjaJabatan;

@Repository("indikatorKinerjaJabatanDao")
public interface IndikatorKinerjaJabatanDao extends PagingAndSortingRepository<IndikatorKinerjaJabatan, String> {

	@Query("select new Map(ikt.noRec as noRec,"
			+ "ikt.tanggalMulaiBerlaku as tglBerlaku,to_char(ikt.tanggalMulaiBerlaku,'dd-MM-yyyy') as tglBerlakuFormatted,"
			+ "ikm.jenisIndikator as jenisIndikator,ikm.id as indikatorId,ikm.namaIndikator as namaIndikator,"
			+ "si.id as satuanId,si.satuanIndikator as satuanIndikator,"
			+ "ikt.tanggalPembaharuanData as tglPembaharuanData) " + "from IndikatorKinerjaJabatan ikt "
			+ "inner join ikt.indikatorKinerja ikm " + "inner join ikm.satuanIndikator si "
			+ "where ikt.statusEnabled is true " + "and ikm.statusEnabled is true and ikm.statusVerifikasi is true "
			+ "and ikt.jabatanId = :jabatanId "
			+ "and to_char(ikt.tanggalMulaiBerlaku,'yyyy-MM') <= to_char(now(),'yyyy-MM') "
			+ "order by ikm.jenisIndikator asc,ikm.namaIndikator asc,"
			+ "ikt.tanggalMulaiBerlaku desc,ikt.tanggalPembaharuanData desc")
	List<Map<String, Object>> findAllByJabatan(@Param("jabatanId") Integer idJabatan);

	@Query("select new Map(uk.id as unitKerjaId,uk.name as unitKerja,"
			+ "jj.id as jenisJabatanId,jj.jenisJabatan as jenisJabatan,"
			+ "jb.id as jabatanId,jb.namaJabatan as namaJabatan) " + "from Jabatan jb "
			+ "inner join jb.jenisJabatan jj " + "inner join jb.unitKerja uk " + "where jb.statusEnabled is true "
			+ "and jj.statusEnabled is true and jj.id in (7,8,9) " + "and uk.statusEnabled is true "
			+ "order by uk.name, jj.jenisJabatan, jb.namaJabatan")
	List<Map<String, Object>> findAllJabatan();

	@Query("select new Map(jb.id as jabatanId," + "ikt.noRec as noRec," + "ikt.tanggalMulaiBerlaku as tglBerlaku,"
			+ "ikt.tanggalPembaharuanData as tglPembaharuanData) " + "from IndikatorKinerjaJabatan ikt "
			+ "inner join ikt.jabatan jb " + "where ikt.statusEnabled is true "
			+ "and ikt.indikatorKinerjaId = :indikatorId "
			+ "and to_char(ikt.tanggalMulaiBerlaku,'yyyy-MM') <= to_char(now(),'yyyy-MM') "
			+ "and jb.statusEnabled is true " + "order by jb.namaJabatan, jb.id, "
			+ "ikt.tanggalMulaiBerlaku desc, ikt.tanggalPembaharuanData desc")
	List<Map<String, Object>> findAllByIndikator(@Param("indikatorId") Integer idIndikator);

	@Query("select new Map(ikt.noRec as noRec," + "ikt.statusEnabled as statusEnabled," + "ikt.kdProfile as kdProfile,"
			+ "ikt.tanggalMulaiBerlaku as tglBerlaku," + "ikt.indikatorKinerjaId as indikatorId,"
			+ "ikt.jabatanId as jabatanId) " + "from IndikatorKinerjaJabatan ikt " + "where ikt.statusEnabled is true "
			+ "and ikt.indikatorKinerjaId in (:listIndikatorId) " + "and ikt.jabatanId in (:listJabatanId) "
			+ "and to_char(ikt.tanggalMulaiBerlaku,'yyyy-MM') <= to_char(now(),'yyyy-MM')")
	List<Map<String, Object>> findAllByIndikator(@Param("listIndikatorId") List<Integer> listIdIndikator,
			@Param("listJabatanId") List<Integer> listIdJabatan);

	@Query("select new Map(ikt.indikatorKinerjaId as idIndikator," + "ikt.jabatanId as idJabatan,"
			+ "ikt.tanggalMulaiBerlaku as tglBerlaku) " + "from IndikatorKinerjaJabatan ikt "
			+ "where ikt.statusEnabled is true " + "and ikt.indikatorKinerjaId = :indikatorId "
			+ "and ikt.jabatanId = :jabatanId " + "and ikt.tanggalMulaiBerlaku < :tglBerlaku")
	List<Map<String, Object>> findDupMap(@Param("indikatorId") Integer idIndikator,
			@Param("jabatanId") Integer idJabatan, @Param("tglBerlaku") Date tglBerlaku);

	@Query("select distinct new Map(ikm.id as id,ikm.namaIndikator as namaIndikator,jb.levelJabatan as levelJabatan) "
			+ "from IndikatorKinerjaJabatan ikt " + "inner join ikt.indikatorKinerja ikm "
			+ "inner join ikt.jabatan jb " + "where ikt.statusEnabled is true " + "and ikm.statusEnabled is true "
			+ "and jb.statusEnabled is true "
			+ "and to_char(ikt.tanggalMulaiBerlaku,'yyyy-MM') <= to_char(now(),'yyyy-MM') "
			+ "and ikm.jenisIndikator = :jenisIndikator " + "and jb.unitKerjaId = :unitKerjaId")
	List<Map<String, Object>> findIndikatorByUnit(@Param("jenisIndikator") Integer jenisIndikator,
			@Param("unitKerjaId") Integer idUnitKerja);

	@Query("select new Map(ikt.noRec as noRec,ikt.jabatanId as jabatanId) " + "from IndikatorKinerjaJabatan ikt "
			+ "where ikt.indikatorKinerjaId = :indikatorId "
			+ "and to_char(ikt.tanggalMulaiBerlaku,'yyyy-MM') >= to_char(now(),'yyyy-MM') "
			+ "and ikt.statusEnabled is true")
	List<Map<String, Object>> findUsedMappingByIndikator(@Param("indikatorId") Integer idIndikator);

}
