package com.jasamedika.medifirst2000.dao;

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.EvaluasiJabatan;
import com.jasamedika.medifirst2000.entities.NilaiKelompokJabatan;

/**
 * Repository class for EvaluasiJabatan
 * 
 * @author Generator
 */
@Repository("EvaluasiJabatanDao")
public interface EvaluasiJabatanDao extends PagingAndSortingRepository<EvaluasiJabatan, String> {

	@Query("select model from EvaluasiJabatan model where model.tahun=:tahun  and model.ruangan.id=:ruangan  and model.bulan=:bulan ")
	List<EvaluasiJabatan> findEvaluasi(@Param("bulan") String bulan, @Param("tahun") Integer tahun,
			@Param("ruangan") Integer ruangan);

	@Query("select nilaiKelompokJabatan " + "from NilaiKelompokJabatan nilaiKelompokJabatan "
			+ "left join nilaiKelompokJabatan.kelompokJabatan kelompokJabatan where kelompokJabatan.id "
			+ "in (Select kelompokJabatan.id " + "from MappingKelompokToJabatan mapping left "
			+ "join mapping.jabatan jabatan "
			+ "left join mapping.kelompokJabatan kelompokJabatan where jabatan.id=:idJabatan) ")
	List<NilaiKelompokJabatan> findGrade(@Param("idJabatan") Integer idJabatan);

	List<EvaluasiJabatan> findByTahun(Integer tahun);

	List<EvaluasiJabatan> findByTahunAndPegawaiId(Integer tahun, Integer pegawaiId);

	@Query("select nj from Jabatan j, NilaiKelompokJabatan nj " + "left join nj.kelompokJabatan kj "
			+ "where j.kelompokJabatanId = kj.id " + "and j.statusEnabled is true " + "and kj.statusEnabled is true "
			+ "and nj.statusEnabled is true " + "and :totalNilai between nj.nilaiTerendah and nj.nilaiTertinggi "
			+ "and j.id = :idJabatan")
	NilaiKelompokJabatan findDetailKelompokJabatan(@Param("idJabatan") Integer idJabatan,
			@Param("totalNilai") Integer totalNilai);

	@Query("select distinct new Map(j.id as idJabatan," + "uk.name as unitKerja," + "j.namaJabatan as namaJabatan) "
			+ "from EvaluasiJabatan ej " + "inner join ej.jabatan j " + "inner join j.unitKerja uk "
			+ "where ej.statusEnabled is true and j.statusEnabled is true and uk.statusEnabled is true "
			+ "order by uk.name, j.namaJabatan asc")
	List<Map<String, Object>> findDistinctEvaluasiJabatan();

	@Query("select new Map(ej.tahun as tahun,ej.bulan as bulan,ej.tahun || '-' || ej.bulan as periodePerhitungan,"
			+ "ej.tglHitung as tglHitung,to_char(ej.tglHitung, 'dd-MM-yyyy HH:mm:ss') as tglHitungFormatted,"
			+ "uk.name as unitKerja,j.id as idJabatan,j.namaJabatan as namaJabatan,"
			+ "nj.gradeDesc as grade,nj.detailKelompokJabatan as kelompokJabatan,"
			+ "f1.profile as f1,f2.profile as f2,f3.profile as f3,"
			+ "f4.profile as f4,f5.profile as f5,f7.profile as f7,"
			+ "f8.profile as f8,f9.profile as f9,f10.profile as f10,"
			+ "fa.profile as fa,fb.profile as fb,fc.profile as fc,fd.profile as fd,fe.profile as fe,ff.profile as ff,"
			+ "fg.profile as fg,fh.profile as fh,fi.profile as fi,fj.profile as fj,fk.profile as fk,fl.profile as fl,"
			+ "cast(ej.totalNilai as long) as nilaiJabatan) " + "from EvaluasiJabatan ej " + "inner join ej.jabatan j "
			+ "inner join j.unitKerja uk " + "left join ej.grade nj " + "left join ej.faktor1 f1 "
			+ "left join ej.faktor2 f2 " + "left join ej.faktor3 f3 " + "left join ej.faktor4 f4 "
			+ "left join ej.faktor5 f5 " + "left join ej.faktor7 f7 " + "left join ej.faktor8 f8 "
			+ "left join ej.faktor9 f9 " + "left join ej.faktor10 f10 " + "left join ej.faktorA fa "
			+ "left join ej.faktorB fb " + "left join ej.faktorC fc " + "left join ej.faktorD fd "
			+ "left join ej.faktorE fe " + "left join ej.faktorF ff " + "left join ej.faktorG fg "
			+ "left join ej.faktorH fh " + "left join ej.faktorI fi " + "left join ej.faktorJ fj "
			+ "left join ej.faktorK fk " + "left join ej.faktorL fl "
			+ "where ej.statusEnabled is true and j.statusEnabled is true and uk.statusEnabled is true "
			+ "order by uk.name asc, j.namaJabatan asc, ej.tglHitung desc")
	List<Map<String, Object>> findAllEvaluasiJabatan();

	@Query("select distinct pg.id " + "from MapPegawaiJabatanToUnitKerja mj " + "inner join mj.pegawai pg "
			+ "where mj.statusEnabled is true " + "and pg.statusEnabled is true " + "and mj.jabatanId = :jabatanId")
	List<Integer> findAllPegawaiRemunByJabatan(@Param("jabatanId") Integer idJabatan);

	@Query("select distinct mj.id " + "from MapPegawaiJabatanToUnitKerja mj " + "inner join mj.pegawai pg "
			+ "where mj.statusEnabled is true " + "and pg.statusEnabled is true " + "and mj.jabatanId = :jabatanId")
	List<Integer> findAllMappingJabatanRemun(@Param("jabatanId") Integer idJabatan);

	@Query("select new Map(ej.gradeId as gradeId,ej.totalNilai as totalNilai) " + "from EvaluasiJabatan ej "
			+ "where ej.statusEnabled is true " + "and ej.jabatanId = :jabatanId " + "order by ej.tglHitung desc")
	List<Map<String, Object>> findEvaluasiByJabatan(@Param("jabatanId") Integer idJabatan);

}
