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.MappingPegawaiToAtasan;
import com.jasamedika.medifirst2000.entities.Pegawai;

@Repository("MappingPegawaiToAtasanDao")
public interface MappingPegawaiToAtasanDao extends PagingAndSortingRepository<MappingPegawaiToAtasan, String>{
	
	@Query(" select mappingPegawaiToAtasan"+  
			" from MappingPegawaiToAtasan mappingPegawaiToAtasan"+
			" left join mappingPegawaiToAtasan.pegawai pegawai"+
			" where pegawai.id=:id ")
	public MappingPegawaiToAtasan findMappingPegawaiToAtasan(@Param("id") Integer id);
	
//	@Query(" select pegawai "+
//			"from MappingPegawaiToAtasan mappingAtasanToJabatan "+
//			"left join mappingAtasanToJabatan.pegawai pegawai "+
//			"left join mappingAtasanToJabatan.atasanLangsung atasanLangsung "+
//			"left join mappingAtasanToJabatan.atasanPejabatPenilai atasanPejabatPenilai "+
//			"where (atasanLangsung.id = :id or atasanPejabatPenilai.id=:id) and mappingAtasanToJabatan.statusEnabled is true")
	@Query("select pegawai "
			+ "from MapPegawaiJabatanToUnitKerja mapp "
			+ "left join mapp.pegawai pegawai "
			+ "left join mapp.atasanLangsung atasanLangsung "
			+ "left join mapp.pejabatPenilai pejabatPenilai "
			+ "where mapp.statusEnabled is true "
			+ "and mapp.isPrimary is true "
			+ "and (atasanLangsung.id = :id or pejabatPenilai.id = :id)")
	public List<Pegawai> findPegawaiYangDinilai(@Param("id") Integer id);
	
//	@Query(" select pegawai.id "+
//			"from MappingPegawaiToAtasan mappingAtasanToJabatan "+
//			"left join mappingAtasanToJabatan.pegawai pegawai "+
//			"left join mappingAtasanToJabatan.atasanLangsung atasanLangsung "+
//			"left join mappingAtasanToJabatan.atasanPejabatPenilai atasanPejabatPenilai "+
//			"where (atasanLangsung.id = :id or atasanPejabatPenilai.id=:id) and mappingAtasanToJabatan.statusEnabled is true")
	@Query("select pegawai.id "
			+ "from MapPegawaiJabatanToUnitKerja mapp "
			+ "left join mapp.pegawai pegawai "
			+ "left join mapp.atasanLangsung atasanLangsung "
			+ "left join mapp.pejabatPenilai pejabatPenilai "
			+ "where mapp.statusEnabled is true "
			+ "and mapp.isPrimary is true "
			+ "and (atasanLangsung.id = :id or pejabatPenilai.id = :id)")
	public List<Integer> findIdPegawaiYangDinilai(@Param("id") Integer id);
	
//	@Query(" select pegawai.id "+
//			"from MappingPegawaiToAtasan mappingAtasanToJabatan "+
//			"left join mappingAtasanToJabatan.pegawai pegawai "+
//			"left join mappingAtasanToJabatan.atasanLangsung atasanLangsung "+
//			"where atasanLangsung.id = :id and mappingAtasanToJabatan.statusEnabled is true")
	@Query("select pegawai.id "
			+ "from MapPegawaiJabatanToUnitKerja mapp "
			+ "left join mapp.pegawai pegawai "
			+ "left join mapp.atasanLangsung atasanLangsung "
			+ "where mapp.statusEnabled is true "
			+ "and mapp.isPrimary is true "
			+ "and atasanLangsung.id = :id")
	public List<Integer> findIdPegawaiYangDibawahiSpec(@Param("id") Integer id);
	
//	@Query(" select pegawai.id "+
//			"from MappingPegawaiToAtasan mappingAtasanToJabatan "+
//			"left join mappingAtasanToJabatan.pegawai pegawai "+
//			"left join mappingAtasanToJabatan.atasanPejabatPenilai atasanPejabatPenilai "+
//			"where atasanPejabatPenilai.id=:id and mappingAtasanToJabatan.statusEnabled is true")
	@Query("select pegawai.id "
			+ "from MapPegawaiJabatanToUnitKerja mapp "
			+ "left join mapp.pegawai pegawai "
			+ "left join mapp.pejabatPenilai pejabatPenilai "
			+ "where mapp.statusEnabled is true "
			+ "and mapp.isPrimary is true "
			+ "and pejabatPenilai.id = :id")
	public List<Integer> findIdPegawaiYangDinilaiSpec(@Param("id") Integer id);
	
//	@Query(" select new map(atasanLangsung.id as idAtasanLangsung, atasanLangsung.namaLengkap as namaAtasanLangsung, "+
//			"atasanPejabatPenilai.id as idAtasanPejabatPenilai, atasanPejabatPenilai.namaLengkap as namaAtasanPejabatPenilai ) "+
//			"from MappingPegawaiToAtasan mappingAtasanToJabatan "+
//			"left join mappingAtasanToJabatan.pegawai pegawai "+
//			"left join mappingAtasanToJabatan.atasanLangsung atasanLangsung "+
//			"left join mappingAtasanToJabatan.atasanPejabatPenilai atasanPejabatPenilai "+
//			"where pegawai.id = :id and mappingAtasanToJabatan.statusEnabled is true")
	@Query("select new Map(atasanLangsung.id as idAtasanLangsung, atasanLangsung.namaLengkap as namaAtasanLangsung, "
			+ "pejabatPenilai.id as idAtasanPejabatPenilai, pejabatPenilai.namaLengkap as namaAtasanPejabatPenilai) "
			+ "from MapPegawaiJabatanToUnitKerja mapp "
			+ "left join mapp.pegawai pegawai "
			+ "left join mapp.atasanLangsung atasanLangsung "
			+ "left join mapp.pejabatPenilai pejabatPenilai "
			+ "where mapp.statusEnabled is true "
			+ "and mapp.isPrimary is true "
			+ "and pegawai.id = :id")
	public List<Map<String,Object>> findAtasanPegawai(@Param("id") Integer id);
	
	@Query("select new Map(atasanLangsung.id as idAtasanLangsung, atasanLangsung.namaLengkap as namaAtasanLangsung, "
			+ "pejabatPenilai.id as idAtasanPejabatPenilai, pejabatPenilai.namaLengkap as namaAtasanPejabatPenilai) "
			+ "from MapPegawaiJabatanToUnitKerja mapp "
			+ "left join mapp.pegawai pegawai "
			+ "left join mapp.atasanLangsung atasanLangsung "
			+ "left join mapp.pejabatPenilai pejabatPenilai "
			+ "where mapp.statusEnabled is true "
			+ "and mapp.jabatanId = :jabatanId "
			+ "and pegawai.id = :id")
	public List<Map<String,Object>> findAtasanPegawaiByJabatan(@Param("id") Integer id, @Param("jabatanId") Integer idJabatan);
	
	@Query("select new Map(atasanLangsung.id as idAtasanLangsung, atasanLangsung.namaLengkap as namaAtasanLangsung) "
			+ "from MapPegawaiJabatanToUnitKerja mapp "
			+ "left join mapp.pegawai pegawai "
			+ "left join mapp.atasanLangsung atasanLangsung "
			+ "where mapp.statusEnabled is true "
			+ "and mapp.atasanLangsungId = :idAtasan "
			+ "and mapp.pegawaiId = :id")
	public List<Map<String,Object>> findAtasanPegawai(@Param("id") Integer id, @Param("idAtasan") Integer idAtasan);
	
//	@Query(" select new map(atasanLangsung.nipPns as nipPnsAtasanLangsung, atasanLangsung.namaLengkap as namaAtasanLangsung, "+
//			"atasanPejabatPenilai.nipPns as nipPnsAtasanPejabatPenilai, atasanPejabatPenilai.namaLengkap as namaAtasanPejabatPenilai, "+
//			"jabatanAtasanLangsung.namaJabatan as jabatanAtasanLangsung, jabatanAtasanPejabatPenilai.namaJabatan as jabatanAtasanPejabatPenilai ) "+
//			"from MappingPegawaiToAtasan mappingAtasanToJabatan, "
//			+ "MapPegawaiJabatanToUnitKerja mapPegawai, "
//			+ "MapPegawaiJabatanToUnitKerja mapAtasan, "
//			+ "MapPegawaiJabatanToUnitKerja mapPejabat "
//			+ "left join mappingAtasanToJabatan.pegawai pegawai "
//			+ "left join mappingAtasanToJabatan.atasanLangsung atasanLangsung "
//			+ "left join mappingAtasanToJabatan.atasanPejabatPenilai atasanPejabatPenilai "
//			+ "left join mapAtasan.jabatan jabatanAtasanLangsung "
//			+ "left join mapPejabat.jabatan jabatanAtasanPejabatPenilai "
//			+ "where mapPegawai.pegawaiId = pegawai.id "
//			+ "and mapAtasan.pegawaiId = atasanLangsung.id "
//			+ "and mapPejabat.pegawaiId = atasanPejabatPenilai.id "
//			+ "and mapPegawai.isPrimary is true "
//			+ "and mapAtasan.isPrimary is true "
//			+ "and mapPejabat.isPrimary is true "
//			+ "and pegawai.id = :id "
//			+ "and mappingAtasanToJabatan.statusEnabled is true")
	@Query("select new Map(atasanLangsung.nipPns as nipPnsAtasanLangsung,atasanLangsung.namaLengkap as namaAtasanLangsung,"
			+ "atasanPejabatPenilai.nipPns as nipPnsAtasanPejabatPenilai,atasanPejabatPenilai.namaLengkap as namaAtasanPejabatPenilai,"
			+ "jabatanAtasanLangsung.namaJabatan as jabatanAtasanLangsung,"
			+ "jabatanAtasanPejabatPenilai.namaJabatan as jabatanAtasanPejabatPenilai) "
			+ "from MapPegawaiJabatanToUnitKerja mapPegawai, MapPegawaiJabatanToUnitKerja mapAtasan, MapPegawaiJabatanToUnitKerja mapPejabat "
			+ "left join mapPegawai.pegawai pegawai "
			+ "left join mapPegawai.atasanLangsung atasanLangsung "
			+ "left join mapPegawai.pejabatPenilai atasanPejabatPenilai "
			+ "left join mapAtasan.jabatan jabatanAtasanLangsung "
			+ "left join mapPejabat.jabatan jabatanAtasanPejabatPenilai "
			+ "where mapPegawai.pegawaiId = pegawai.id "
			+ "and mapAtasan.pegawaiId = atasanLangsung.id "
			+ "and mapPejabat.pegawaiId = atasanPejabatPenilai.id "
			+ "and mapPegawai.statusEnabled is true "
			+ "and mapAtasan.statusEnabled is true "
			+ "and mapPejabat.statusEnabled is true "
			+ "and mapPegawai.isPrimary is true "
			+ "and mapAtasan.isPrimary is true "
			+ "and mapPejabat.isPrimary is true "
			+ "and pegawai.id = :id")
	public Map<String,Object> getAtasanPegawai(@Param("id") Integer id);
	
//	@Query(" select new map(atasanLangsung.id as idAtasanLangsung, atasanLangsung.namaLengkap as namaAtasanLangsung, "+
//			"atasanPejabatPenilai.id as idAtasanPejabatPenilai, atasanPejabatPenilai.namaLengkap as namaAtasanPejabatPenilai, "+
//			"pegawai.id as idPegawai, pegawai.namaLengkap as namaPegawai) "+
//			"from MappingPegawaiToAtasan mappingAtasanToJabatan "+
//			"left join mappingAtasanToJabatan.pegawai pegawai "+
//			"left join mappingAtasanToJabatan.atasanLangsung atasanLangsung "+
//			"left join mappingAtasanToJabatan.atasanPejabatPenilai atasanPejabatPenilai "+
//			"where mappingAtasanToJabatan.statusEnabled is true ")
	@Query("select new Map(atasanLangsung.id as idAtasanLangsung, atasanLangsung.namaLengkap as namaAtasanLangsung, "
			+ "atasanPejabatPenilai.id as idAtasanPejabatPenilai, atasanPejabatPenilai.namaLengkap as namaAtasanPejabatPenilai, "
			+ "pegawai.id as idPegawai, pegawai.namaLengkap as namaPegawai) "
			+ "from MapPegawaiJabatanToUnitKerja mappingAtasanToJabatan "
			+ "left join mappingAtasanToJabatan.pegawai pegawai "
			+ "left join mappingAtasanToJabatan.atasanLangsung atasanLangsung "
			+ "left join mappingAtasanToJabatan.pejabatPenilai atasanPejabatPenilai "
			+ "where mappingAtasanToJabatan.statusEnabled is true "
			+ "and mappingAtasanToJabatan.isPrimary is true")
	public List<Map<String,Object>> findMappingPegawai();
	
//	@Query("select new map(ma.atasanLangsung.id as atasanLangsung, "+
//			"ma.atasanPejabatPenilai.id as atasanPenilai) "+
//			"from MappingPegawaiToAtasan ma "+
//			"where ma.statusEnabled is true "+
//			"and ma.pegawai.id = :idPegawai")
	@Query("select new map(ma.atasanLangsung.id as atasanLangsung, "+
			"ma.pejabatPenilai.id as atasanPenilai) "+
			"from MapPegawaiJabatanToUnitKerja ma "+
			"where ma.statusEnabled is true "+
			"and ma.isPrimary is true "+
			"and ma.pegawai.id = :idPegawai")
	public List<Map<String, Object>> getAtasan(@Param("idPegawai") Integer idPegawai);
	
//	@Query("select new map (ma.pegawai.id as bawahan, "+
//			"ma.atasanPejabatPenilai.id as atasanPenilai) "+
//			"from MappingPegawaiToAtasan ma "+
//			"where ma.statusEnabled is true "+
//			"and ma.atasanLangsung.id = :idAtasanLgsg")
	@Query("select new map (ma.pegawai.id as bawahan, "+
			"ma.pejabatPenilai.id as atasanPenilai) "+
			"from MapPegawaiJabatanToUnitKerja ma "+
			"where ma.statusEnabled is true "+
			"and ma.isPrimary is true "+
			"and ma.atasanLangsung.id = :idAtasanLgsg")
	public List<Map<String, Object>> getPegawaiBawahanLgsg(@Param("idAtasanLgsg") Integer idAtasanLgsg);
	
//	@Query("select new map(ma.pegawai.id as bawahan, "+
//			"ma.atasanLangsung.id as atasanLangsung) "+
//			"from MappingPegawaiToAtasan ma "+
//			"where ma.statusEnabled is true "+
//			"and ma.atasanPejabatPenilai.id = :idAtasanPenilai")
	@Query("select new map(ma.pegawai.id as bawahan, "+
			"ma.atasanLangsung.id as atasanLangsung) "+
			"from MapPegawaiJabatanToUnitKerja ma "+
			"where ma.statusEnabled is true "+
			"and ma.isPrimary is true "+
			"and ma.pejabatPenilai.id = :idAtasanPenilai")
	public List<Map<String, Object>> getPegawaiDinilai(@Param("idAtasanPenilai") Integer idAtasanPenilai);
	
	@Query("select ma.id "+
			"from MappingPegawaiToAtasan ma "+
			"where ma.statusEnabled is true "+
			"and ma.pegawai.id = :idPegawai")
	public List<Integer> getIdMappAtasan(@Param("idPegawai") Integer idPegawai);
	
	@Query("select ma.id "+
			"from MappingPegawaiToAtasan ma "+
			"where ma.statusEnabled is true "+
			"and ma.atasanLangsung.id = :idAtasanLgsg")
	public List<Integer> getIdMappPegawaiBawahanLgsg(@Param("idAtasanLgsg") Integer idAtasanLgsg);
	
	@Query("select ma.id "+
			"from MappingPegawaiToAtasan ma "+
			"where ma.statusEnabled is true "+
			"and ma.atasanPejabatPenilai.id = :idAtasanPenilai")
	public List<Integer> getIdMappPegawaiDinilai(@Param("idAtasanPenilai") Integer idAtasanPenilai);
	
//	@Query("select new Map(al.id as id,al.namaLengkap as namaLengkap) "+
//		   "from MappingPegawaiToAtasan mpa "+
//		   "left join mpa.pegawai pgw "+
//		   "left join mpa.atasanLangsung al "+
//		   "where mpa.statusEnabled is true "+
//		   "and pgw.statusEnabled is true "+
//		   "and al.statusEnabled is true "+
//		   "and pgw.id = :idAtasanLangsung")
	@Query("select new Map(al.id as id,al.namaLengkap as namaLengkap) "+
			   "from MapPegawaiJabatanToUnitKerja mpa "+
			   "left join mpa.pegawai pgw "+
			   "left join mpa.atasanLangsung al "+
			   "where mpa.statusEnabled is true "+
			   "and pgw.statusEnabled is true "+
			   "and al.statusEnabled is true "+
			   "and mpa.isPrimary is true "+
			   "and pgw.id = :idAtasanLangsung")
	public Map<String, Object> getAtasanLgsgAsPejabatPenilai(@Param("idAtasanLangsung") Integer idAtasanLangsung);

}
