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

@Repository("PlanningPegawaiStatusDao")
public interface PlanningPegawaiStatusDao extends PagingAndSortingRepository<PlanningPegawaiStatus, String>{
	
	@Query("select a.noPlanning "
			+ "from PlanningPegawaiStatus a "
			+ "where a.noPlanning LIKE %:kode% "
			+ "order by a.noPlanning asc")
	List<String> noPlanning(@Param("kode") String kode);

	@Query("select count(a.noPlanning) "
			+ "from PlanningPegawaiStatus a "
			+ "where a.noPlanning LIKE %:kode% ")
	Long countNoPlanning(@Param("kode") String kode) ;
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.namaLengkap as namaLengkap,"
			+ "c.id as kategoriPegawaiId) "
			+ "from MapPegawaiJabatanToUnitKerja m "
			+ "left join m.pegawai a "
			+ "left join m.subUnitKerjaPegawai b "
			+ "left join m.unitKerjaPegawai e "
			+ "left join a.kategoryPegawai c "
			+ "left join a.statusPegawai d "
			+ "where m.isPrimary is true "
			+ "and m.statusEnabled is true "
			+ "and e.id=:id "
			+ "and c.id!=11 "
			+ "and d.id=2")
	List<Map<String, Object>> getListPegawai(
			@Param("id") Integer id);
	
	@Query("select NEW Map ("
			+ "a.id as id,"
			+ "a.statusPegawai as statusPegawai) "
			+ "from StatusPegawai a "
			+ "where a.id=1 or "
			+ "a.id=24 or "
			+ "a.id=25 or "
			+ "a.id=26 or "
			+ "a.id=27 or "
			+ "a.id=28 or "
			+ "a.id=29")
	List<Map<String, Object>> getListStatusPegawai();
	
	@Query("select NEW Map ("
			+ "a.nipPns as nip,"
			+ "a.id as idPegawai,"
			+ "b.id as jabatanId,"
			+ "b.namaJabatan as jabatan,"
			+ "d.id as unitKerjaId,"
			+ "d.name as unitKerja,"	
			+ "c.id as subUnitKerjaId,"
			+ "c.name as subUnitKerja,"	
			+ "a.tglMasuk as tglMasuk,"	
			+ "a.alamat as alamat,"
			+ "a.noHandphone as noHandphone,"
			+ "kategori.id as kategoriPegawaiId ) "
			+ "from MapPegawaiJabatanToUnitKerja m "
			+ "left join m.pegawai a "
			+ "left join m.jabatan b "
			+ "left join a.kategoryPegawai kategori "
			+ "left join m.subUnitKerjaPegawai c "
			+ "left join m.unitKerjaPegawai d "
			+ "where m.isPrimary is true "
			+ "and m.statusEnabled is true "
			+ "and a.id=:id")
	Map<String, Object> getDataPermohonanStatus(
			@Param("id") Integer id);
	
	@Query("select NEW Map(a.nipPns as nip,a.id as idPegawai,a.tglMasuk as tglMasuk,a.alamat as alamat,a.noHandphone as noHandphone,b.id as kategoriPegawaiId) "
			+ "from Pegawai a left join a.kategoryPegawai b where a.id=:id")
	Map<String, Object> getDataMapPermohonanStatus(@Param("id") Integer id);

	//--------------------------CUTI TAHUNAN----------------------
	@Query("select Count(a.id) "
			+ "from MapKalenderToHariLibur a "
			+ "left join a.hariLibur b "
			+ "left join a.tanggal c "
			+ "where b.id=:id and year(c.tanggal)=:thisYear")
	Long getCutiBersama(
			@Param("id") Integer id,
			@Param("thisYear") Integer thisYear);
	
	//--------------------------CUTI MELAHIRKAN----------------------//
	
	@Query("select NEW Map ("
			+ "a.id as id,"
			+ "a.kuotaTotalHari as kuotaTotalHari,"
			+ "a.kuotaMinHariTake as kuotaMinHariTake,"
			+ "a.qtyMaxTake as qtyMaxTake,"
			+ "a.isAkumulasi as isAkumulasi) "
			+ "from PegawaiSKCuti a "
			+ "left join a.statusPegawai b "
			+ "where b.id=:statusPegawaiId")
	Map<String, Object> getJatahCutiMelahirkan(
			@Param("statusPegawaiId") Integer statusPegawaiId);
	
	//---------------------APPROVAL---------------------------------
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec,"
			+ "a.noPlanning as noPlanning,"	
			+ "a.approvalStatus as approvalStatus,"
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan,"
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan,"
			+ "a.tglPengajuan as tglPengajuan,"
			+ "b.id as pegwaiId,"
			+ "b.namaLengkap as namaPegawai,"
			+ "c.id as statusPegawaiId,"
			+ "c.statusPegawai as statusPegawai,"
			+ "d.id as unitKerjaId,"
			+ "d.name as unitKerja,"
			+ "d.id as unitKerjaId,"
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, MapPegawaiJabatanToUnitKerja m "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join m.subUnitKerjaPegawai g "
			+ "left join m.unitKerjaPegawai d "
			+ "left join a.noSk e "
			+ "left join m.jabatan f "
			+ "where m.pegawaiId = b.id "
			+ "and m.isPrimary is true "
			+ "and m.statusEnabled is true "
			+ "and e.noRec is null "
			+ "and a.statusEnabled is true "
			+ "and d.id=:id "
			+ "and (c.id=1 or c.id=24 or c.id=25 or c.id=26 or c.id=27 or c.id=28 or c.id=29)")
	List<Map<String, Object>> getListPermohonanStatus(
			@Param("id") Integer id);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec,"
			+ "a.noPlanning as noPlanning,"	
			+ "a.approvalStatus as approvalStatus,"
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan,"
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan,"
			+ "a.tglPengajuan as tglPengajuan,"
			+ "b.id as pegwaiId,"
			+ "b.namaLengkap as namaPegawai,"
			+ "c.id as statusPegawaiId,"
			+ "c.statusPegawai as statusPegawai,"
			+ "d.id as unitKerjaId,"
			+ "d.name as unitKerja,"
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, MapPegawaiJabatanToUnitKerja m "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join m.subUnitKerjaPegawai g "
			+ "left join m.unitKerjaPegawai d "
			+ "left join a.noSk e "
			+ "left join m.jabatan f "
			+ "where m.pegawaiId = b.id "
			+ "and m.isPrimary is true "
			+ "and m.statusEnabled is true "
			+ "and e.noRec is null "
			+ "and a.statusEnabled is true "
			+ "and b.id=:id ")
	List<Map<String, Object>> getListPermohonanStatusByPegawai2(
			@Param("id") Integer id);
	
	String planningStatusPegawai = "select NEW Map ("
			+ "a.noRec as noRec,"
			+ "a.noPlanning as noPlanning,"			
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan,"
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan,"
			+ "a.tglPengajuan as tglPengajuan,"
			+ "b.id as pegwaiId,"
			+ "a.approvalStatus as approvalStatus, "
			+ "b.namaLengkap as namaPegawai,"
			+ "c.id as statusPegawaiId,"
			+ "c.statusPegawai as statusPegawai,"
			+ "d.id as unitKerjaId,"
			+ "d.name as unitKerja,"
			+ "g.id as subUnitKerjaId,"
			+ "g.name as subUnitKerja,"
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, MapPegawaiJabatanToUnitKerja m "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join m.subUnitKerjaPegawai g "
			+ "left join m.unitKerjaPegawai d "
			+ "left join a.noSk e "
			+ "left join m.jabatan f "
			+ "where m.pegawaiId = b.id "
			+ "and m.isPrimary is true "
			+ "and m.statusEnabled is true "
			+ "and e.noRec is null "
			+ "and a.statusEnabled is true "
			+ "and a.approvalStatus =1 "
			+ "and b.id=:id ";
	
	@Query( planningStatusPegawai
			+ "and "
			+ "(c.id=1 or "
			+ "c.id=24 or "
			+ "c.id=25 or "
			+ "c.id=26 or "
			+ "c.id=27 or "
			+ "c.id=28 or "
			+ "c.id=29)")
	List<Map<String, Object>> getListPermohonanStatusByPegawai(
			@Param("id") Integer id);
	
	@Query(planningStatusPegawai
			+ "and "
			+ "c.statusPegawai=:statusPegawai and "
			+ "to_char(a.tglKeputusan,'yyyy')=to_char(current_timestamp,'yyyy') ")
	List<Map<String, Object>> getListPermohonanStatusByPegawai(@Param("id") Integer id, @Param("statusPegawai") String statusPegawai);
	
	@Query(planningStatusPegawai
			+ "and "
			+ "c.statusPegawai=:statusPegawai")
	List<Map<String, Object>> getListPermohonanStatusByPegawaiAll(@Param("id") Integer id, @Param("statusPegawai") String statusPegawai);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec,"
			+ "a.noPlanning as noPlanning,"
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan,"
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan,"
			+ "a.tglPengajuan as tglPengajuan,"
			+ "b.id as pegwaiId,"
			+ "a.approvalStatus as approvalStatus, "
			+ "b.namaLengkap as namaPegawai,"
			+ "c.id as statusPegawaiId,"
			+ "c.statusPegawai as statusPegawai,"			
			+ "d.id as unitKerjaId,"
			+ "d.name as unitKerja,"
			+ "g.id as subUnitKerjaId,"
			+ "g.name as subUnitKerja,"
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, MapPegawaiJabatanToUnitKerja m "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join a.noSk e "
			+ "left join m.jabatan f "
			+ "left join m.subUnitKerjaPegawai g "
			+ "left join m.unitKerjaPegawai d "
			+ "where m.pegawaiId = b.id "
			+ "and m.isPrimary is true "
			+ "and m.statusEnabled is true "
			+ "and e.noRec is null "
			+ "and a.statusEnabled is true "
			+ "and a.approvalStatus =1 "
			+ "and (c.id=1 or c.id=24 or c.id=25 or c.id=26 or c.id=27 or c.id=28 or c.id=29)")
	List<Map<String, Object>> getListPermohonanStatusAll();
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec,"
			+ "a.noPlanning as noPlanning,"
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan,"
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan,"
			+ "a.deskripsiStatusPegawaiExec as deskripsiStatusPegawaiExec,"
			+ "a.keteranganLainyaExec as keteranganLainyaExec,"
			+ "a.tglPengajuan as tglPengajuan,"
			+ "a.tglKeputusan as tglKeputusan,"
			+ "a.approvalStatus as approvalStatus,"
			+ "b.id as pegwaiId,"
			+ "b.namaLengkap as namaPegawai,"
			+ "b.nipPns as nip,"
			+ "c.id as statusPegawaiId,"
			+ "c.statusPegawai as statusPegawai,"
			+ "d.id as unitKerjaId,"
			+ "d.name as unitKerja,"
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, MapPegawaiJabatanToUnitKerja m "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join m.subUnitKerjaPegawai g "
			+ "left join m.unitKerjaPegawai d "
			+ "left join a.noSk e "
			+ "left join m.jabatan f "
			+ "where m.pegawaiId = b.id "
			+ "and m.isPrimary is true "
			+ "and m.statusEnabled is true "
			+ "and e.noRec is null "
			+ "and (c.id=1 or c.id=24 or c.id=25 or c.id=26 or c.id=27 or c.id=28 or c.id=29)")
	List<Map<String, Object>> getApprovalStatus();
	
	@Query("select NEW Map ("
			+ "b.id as id) "
			+ "from Pegawai a "
			+ "left join a.shiftKerja b "
			+ "where a.id=:id")
	Map<String, Object> getKelompokShift(
			@Param("id") Integer id);
	
	@Query("select NEW Map ("
			+ "a.id as id,"
			+ "a.factorRate as factorRate,"
			+ "a.operatorFactorRate as operatorFactorRate,"
			+ "a.komponenIndex as komponenIndex) "
			+ "from KomponenIndex a "
			+ "where a.id=:id")
	Map<String, Object> getKomponenIndex(
			@Param("id") Integer id);
	
	
	@Query("select pegawai.id "+
			"from PegawaiSisaCuti psc "+
			"left join psc.pegawai pegawai "+
			"where psc.sisaCuti=:sisaCuti")
	List<Integer> getIdPegawai(
			@Param("sisaCuti") Integer sisaCuti);
	
	@Query("select count(jadwalKerja.id) "+
			"from PegawaiJadwalKerja jadwalKerja "+
			"left join jadwalKerja.pegawai pegawai "+
			"left join jadwalKerja.tanggal kalender "+
			"left join jadwalKerja.shift shift "+
			"where to_char(kalender.tanggal,'yyyy-MM-dd') between :startDate and :endDate "+
			"and shift.id  not in (:listKomponenIndexId) "+
			"and pegawai.id = :pegawaiId")
	Integer getDayHadir(@Param("pegawaiId") Integer pegawaiId,@Param("listKomponenIndexId") List<Integer> listKomponenIndexId,@Param("startDate") String startDate,@Param("endDate") String endDate);
	
	@Query("select  new map (list.noRec as noRec, list.tgl as tgl)  from ListTanggalCuti list where list.planningPegawaiStatusId = :noRec and list.statusEnabled is true  order by list.tgl asc")
	List<Map<String,Object>> getListTanggalCuti(@Param("noRec") String  noRec);
	
	@Query("select  new map (list.noRec as noRec, list.tgl as tgl)  from ListTanggalCuti list where list.approvalStatus is true and list.planningPegawaiStatusId = :noRec and list.statusEnabled is true order by list.tgl")
	List<Map<String,Object>> getListTanggalCutiApproved(@Param("noRec") String  noRec);
	
	@Query("select list.tgl as tgl "
			+ "from ListTanggalCuti list "
			+ "where list.planningPegawaiStatusId in (:listNorec) "
			+ "and list.statusEnabled is true "
			+ "and list.tgl in (:listTgl) "
			+ "and list.approvalStatus is true "
			+ "and list.planningPegawaiStatus.approvalStatus = 1")
	List<Date> getListAllTanggalCutiApproved(@Param("listNorec") List<String>  listNorec, @Param("listTgl") List<Date>  listTgl);
	
	@Query("select list.tgl as tgl "
			+ "from ListTanggalCuti list "
			+ "where list.planningPegawaiStatusId in (:listNorec) "
			+ "and list.statusEnabled is true "
			+ "and list.tgl in (:listTgl) "
			+ "and list.planningPegawaiStatus.approvalStatus = 0")
	List<Date> getListAllTanggalCutiNotYetApprove(@Param("listNorec") List<String>  listNorec, @Param("listTgl") List<Date>  listTgl);
	
	@Query("select distinct new Map (tglCuti.tgl as tglCuti) "+
			"from MapPegawaiJabatanToUnitKerja mapPegawaiPelimpah, "+
			"PlanningPegawaiStatus planningCuti "+
			"left join mapPegawaiPelimpah.pegawai pegawaiPelimpah "+
			"left join planningCuti.listTanggal tglCuti "+
			"where mapPegawaiPelimpah.pegawaiId = planningCuti.pegawaiId "+
			"and planningCuti.pegawaiId = pegawaiPelimpah.id "+
			"and planningCuti.noRec = :noRecPlanning "+
			"and pegawaiPelimpah.id = :idPegawaiPelimpah "+
			"and mapPegawaiPelimpah.statusEnabled is true "+
			"and planningCuti.statusEnabled is true "+
			"and pegawaiPelimpah.statusEnabled is true "+
			"and tglCuti.statusEnabled is true "+
			"order by tglCuti.tgl asc")
	public List<Map<String, Object>> getListTglCutiPelimpah(@Param("noRecPlanning") String noRecPlanning, @Param("idPegawaiPelimpah") Integer idPegawaiPelimpah);
	
//	@Query("select new Map (pegawaiPelimpah.namaLengkap as namaPegawai, "+
//			"pegawaiPelimpah.nipPns as nipPegawai, "+
//			"jabatanPelimpah.namaJabatan as jabatanPegawai, "+
//			"atasanLangsung.namaLengkap as namaAtasanLangsung, "+
//			"atasanLangsung.nipPns as nipAtasanLangsung) "+
//			"from MapPegawaiJabatanToUnitKerja mapPegawaiPelimpah, "+
//			"MappingPegawaiToAtasan mapAtasan "+
//			"left join mapPegawaiPelimpah.pegawai pegawaiPelimpah "+
//			"left join mapPegawaiPelimpah.jabatan jabatanPelimpah "+
//			"left join mapAtasan.atasanLangsung atasanLangsung "+
//			"where mapAtasan.pegawai = pegawaiPelimpah "+
//			"and mapPegawaiPelimpah.pegawai = mapAtasan.pegawai "+
//			"and pegawaiPelimpah.id = :idPegawaiPelimpah "+
//			"and mapPegawaiPelimpah.jabatanId = :idJabatanPelimpah "+
//			"and mapPegawaiPelimpah.statusEnabled is true "+
//			"and mapAtasan.statusEnabled is true "+
//			"and pegawaiPelimpah.statusEnabled is true "+
//			"and jabatanPelimpah.statusEnabled is true "+
//			"and atasanLangsung.statusEnabled is true")
	@Query("select new Map (pegawaiPelimpah.namaLengkap as namaPegawai, "+
			"pegawaiPelimpah.nipPns as nipPegawai, "+
			"jabatanPelimpah.namaJabatan as jabatanPegawai, "+
			"atasanLangsung.namaLengkap as namaAtasanLangsung, "+
			"atasanLangsung.nipPns as nipAtasanLangsung) "+
			"from MapPegawaiJabatanToUnitKerja mapPegawaiPelimpah "+
			"left join mapPegawaiPelimpah.pegawai pegawaiPelimpah "+
			"left join mapPegawaiPelimpah.jabatan jabatanPelimpah "+
			"left join mapPegawaiPelimpah.atasanLangsung atasanLangsung "+
			"where pegawaiPelimpah.id = :idPegawaiPelimpah "+
			"and mapPegawaiPelimpah.jabatanId = :idJabatanPelimpah "+
			"and mapPegawaiPelimpah.statusEnabled is true "+
			"and pegawaiPelimpah.statusEnabled is true "+
			"and jabatanPelimpah.statusEnabled is true "+
			"and atasanLangsung.statusEnabled is true")
	public Map<String, Object> getDataPelimpah(@Param("idPegawaiPelimpah") Integer idPegawaiPelimpah, @Param("idJabatanPelimpah") Integer idJabatanPelimpah);
	
	@Query("select new Map(pegawaiDilimpah.namaLengkap as namaPegawai, "+
			"pegawaiDilimpah.nipPns as nipPegawai, "+
			"coalesce(pangkatPegawaiDilimpah.namaPangkat, '-') as pangkatPegawai, "+
			"coalesce(golonganPegawaiDilimpah.name, '-') as golonganPegawai, "+
			"jabatanPegawaiDilimpah.namaJabatan as jabatanPegawai) "+
			"from MapPegawaiJabatanToUnitKerja mapPegawaiDilimpah "+
			"left join mapPegawaiDilimpah.pegawai pegawaiDilimpah "+
			"left join mapPegawaiDilimpah.jabatan jabatanPegawaiDilimpah "+
			"left join pegawaiDilimpah.pangkat pangkatPegawaiDilimpah "+
			"left join pegawaiDilimpah.Golongan golonganPegawaiDilimpah "+
			"where pegawaiDilimpah.id = :idPegawaiDilimpah "+
			"and mapPegawaiDilimpah.statusEnabled is true "+
			"and pegawaiDilimpah.statusEnabled is true "+
			"and jabatanPegawaiDilimpah.statusEnabled is true "+
//			"and pangkatPegawaiDilimpah.statusEnabled is true "+
//			"and golonganPegawaiDilimpah.statusEnabled is true "+
			"and mapPegawaiDilimpah.isPrimary is true")
	public Map<String, Object> getDataDilimpah(@Param("idPegawaiDilimpah") Integer idPegawaiDilimpah);
	
	@Query("select count(l.tgl) from PlanningPegawaiStatus p "
			+ "left join p.listTanggal l "
			+ "where p.statusEnabled is true "
			+ "and l.statusEnabled is true "
			+ "and p.approvalStatus = 0 and l.approvalStatus is null "
			+ "and p.pegawaiId = :idPegawai and p.statusPegawaiPlanId = :idPlan")
//			+ "and to_char(l.tgl,'yyyy') = to_char(current_date,'yyyy')")
	public Integer getJumlahPengajuanDiprosesByPegawai(@Param("idPegawai") Integer idPegawai, @Param("idPlan") Integer idPlan);
	
	@Query("select count(l.tgl) from PlanningPegawaiStatus p "
			+ "left join p.listTanggal l "
			+ "where p.statusEnabled is true "
			+ "and l.statusEnabled is true "
			+ "and p.approvalStatus = 0 and l.approvalStatus is null "
			+ "and p.pegawaiId = :idPegawai and p.statusPegawaiPlanId = :idPlan "
			+ "and to_char(l.tgl,'yyyy') = to_char(current_date,'yyyy')")
	public Integer getJumlahPengajuanDiprosesTahunIniByPegawai(@Param("idPegawai") Integer idPegawai, @Param("idPlan") Integer idPlan);
	
	@Query("select count(l.tgl) from PlanningPegawaiStatus p "
			+ "left join p.listTanggal l "
			+ "where p.statusEnabled is true "
			+ "and l.statusEnabled is true "
			+ "and p.approvalStatus = 0 and l.approvalStatus is null "
			+ "and p.pegawaiId = :idPegawai and p.statusPegawaiPlanId = :idPlan "
			+ "and to_char(l.tgl,'yyyy') > to_char(current_date,'yyyy')")
	public Integer getJumlahPengajuanDiprosesTahunDepanByPegawai(@Param("idPegawai") Integer idPegawai, @Param("idPlan") Integer idPlan);
	
	@Query("select count(l.tgl) from PlanningPegawaiStatus p "
			+ "left join p.listTanggal l "
			+ "where p.statusEnabled is true "
			+ "and l.statusEnabled is true "
			+ "and p.approvalStatus = 0 and l.approvalStatus is null "
			+ "and p.pegawaiId = :idPegawai "
			+ "and p.noPlanning <> :noPlanning "
			+ "and p.statusPegawaiPlanId = :idPlan")
//			+ "and to_char(l.tgl,'yyyy') = to_char(current_date,'yyyy')")
	public Integer getJumlahPengajuanDiprosesByPegawai(@Param("idPegawai") Integer idPegawai, @Param("idPlan") Integer idPlan, @Param("noPlanning") String noPlanning);
	
	@Query("select count(l.tgl) from PlanningPegawaiStatus p "
			+ "left join p.listTanggal l "
			+ "where p.statusEnabled is true "
			+ "and l.statusEnabled is true "
			+ "and p.approvalStatus = 0 and l.approvalStatus is null "
			+ "and p.pegawaiId = :idPegawai "
			+ "and p.noPlanning <> :noPlanning "
			+ "and p.statusPegawaiPlanId = :idPlan "
			+ "and to_char(l.tgl,'yyyy') = to_char(current_date,'yyyy')")
	public Integer getJumlahPengajuanDiprosesTahunIniByPegawai(@Param("idPegawai") Integer idPegawai, @Param("idPlan") Integer idPlan, @Param("noPlanning") String noPlanning);
	
	@Query("select count(l.tgl) from PlanningPegawaiStatus p "
			+ "left join p.listTanggal l "
			+ "where p.statusEnabled is true "
			+ "and l.statusEnabled is true "
			+ "and p.approvalStatus = 0 and l.approvalStatus is null "
			+ "and p.pegawaiId = :idPegawai "
			+ "and p.noPlanning <> :noPlanning "
			+ "and p.statusPegawaiPlanId = :idPlan "
			+ "and to_char(l.tgl,'yyyy') > to_char(current_date,'yyyy')")
	public Integer getJumlahPengajuanDiprosesTahunDepanByPegawai(@Param("idPegawai") Integer idPegawai, @Param("idPlan") Integer idPlan, @Param("noPlanning") String noPlanning);
	
	@Query("select NEW Map (a.noRec as noRec, "
			+ "a.statusEnabled as statusEnabled, "
			+ "a.noPlanning as noPlanning, "
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan, "
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan, "
			+ "a.deskripsiStatusPegawaiExec as deskripsiStatusPegawaiExec, "
			+ "a.keteranganLainyaExec as keteranganLainyaExec, "
			+ "a.tglPengajuan as tglPengajuan, "
			+ "a.tglKeputusan as tglKeputusan, "
			+ "a.approvalStatus as approvalStatus, "
			+ "a.alamatCuti as alamatCuti, "
			+ "a.nomorTelepon as nomorTelepon, "
			+ "a.isCutiLuarNegeri as isCutiLuarNegeri, "
			+ "a.isCutiLuarKota as isCutiLuarKota, "
			+ "a.noSuratTugas as noSuratTugas, "
			+ "a.noNotaDinas as noNotaDinas, "
			+ "a.tglNotaDinas as tglNotaDinas, "
			+ "a.alamatTugas as alamatTugas, "
			+ "a.jenisPerawatan as sakit, "
			+ "a.jabatanIdPemberiNotaDinas as jabatanIdPemberiNotaDinas, "
			+ "listTgl.noRec as norecTgl, "
			+ "listTgl.tgl as tgl, "
			+ "listTgl.approvalStatus as approvalStatusTgl, "
			+ "fp.namaJabatan as jabatanPemberiNotaDinas, "
			+ "subUnitKerja.id as idUnitKerja, "
			+ "subUnitKerja.name as unitKerja, "
			+ "b.id as pegwaiId, "
			+ "b.namaLengkap as namaPegawai, "
			+ "b.nipPns as nip, "
			+ "b.eselonId as eselonId, "
			+ "c.id as statusPegawaiId, "
			+ "c.statusPegawai as statusPegawai, "
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, "
			+ "MapPegawaiJabatanToUnitKerja mp "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join a.noSk e "
			+ "left join a.listTanggal listTgl "
			+ "left join a.jabatanPemberiNotaDinas fp "
			+ "left join mp.jabatan f "
			+ "left join mp.subUnitKerjaPegawai subUnitKerja "
			+ "where mp.pegawaiId = b.id "
			+ "and a.pegawaiId = mp.pegawaiId "
			+ "and a.statusEnabled is true "
			+ "and listTgl.statusEnabled is true "
			+ "and mp.statusEnabled is true "
			+ "and mp.isPrimary is true "
			+ "and e.noRec is null "
			+ "and ((to_char(listTgl.tgl, 'yyyy') = :currentYear and a.approvalStatus <> :approvalStatusId1) "
			+ "or (to_char(listTgl.tgl, 'yyyy') <> :currentYear and a.approvalStatus = :approvalStatusId2)) "
			+ "and c.id in (:ketidakhadiranId) "
			+ "and b.id = :idPegawai "
			+ "order by a.tglPengajuan desc, listTgl.tgl asc")
	public List<Map<String, Object>> getApprovalStatus(@Param("idPegawai") Integer idPegawai, @Param("currentYear") String currentYear,
			@Param("approvalStatusId1") Integer approvalStatusId1, @Param("approvalStatusId2") Integer approvalStatusId2, 
			@Param("ketidakhadiranId") List<Integer> ketidakhadiranId);
	
	@Query("select NEW Map (a.noRec as noRec, "
			+ "a.statusEnabled as statusEnabled, "
			+ "a.noPlanning as noPlanning, "
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan, "
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan, "
			+ "a.deskripsiStatusPegawaiExec as deskripsiStatusPegawaiExec, "
			+ "a.keteranganLainyaExec as keteranganLainyaExec, "
			+ "a.tglPengajuan as tglPengajuan, "
			+ "a.tglKeputusan as tglKeputusan, "
			+ "a.approvalStatus as approvalStatus, "
			+ "a.alamatCuti as alamatCuti, "
			+ "a.nomorTelepon as nomorTelepon, "
			+ "a.isCutiLuarNegeri as isCutiLuarNegeri, "
			+ "a.isCutiLuarKota as isCutiLuarKota, "
			+ "a.noSuratTugas as noSuratTugas, "
			+ "a.noNotaDinas as noNotaDinas, "
			+ "a.tglNotaDinas as tglNotaDinas, "
			+ "a.alamatTugas as alamatTugas, "
			+ "a.jabatanIdPemberiNotaDinas as jabatanIdPemberiNotaDinas, "
			+ "listTgl.noRec as norecTgl, "
			+ "listTgl.tgl as tgl, "
			+ "listTgl.approvalStatus as approvalStatusTgl, "
			+ "fp.namaJabatan as jabatanPemberiNotaDinas, "
			+ "subUnitKerja.id as idUnitKerja, "
			+ "subUnitKerja.name as unitKerja, "
			+ "b.id as pegwaiId, "
			+ "b.namaLengkap as namaPegawai, "
			+ "b.nipPns as nip, "
			+ "b.eselonId as eselonId, "
			+ "c.id as statusPegawaiId, "
			+ "c.statusPegawai as statusPegawai, "
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, "
			+ "MapPegawaiJabatanToUnitKerja mp "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join a.noSk e "
			+ "left join a.listTanggal listTgl "
			+ "left join a.jabatanPemberiNotaDinas fp "
			+ "left join mp.jabatan f "
			+ "left join mp.subUnitKerjaPegawai subUnitKerja "
			+ "where mp.pegawaiId = b.id "
			+ "and a.pegawaiId = mp.pegawaiId "
			+ "and a.statusEnabled is true "
			+ "and listTgl.statusEnabled is true "
			+ "and mp.statusEnabled is true "
			+ "and mp.isPrimary is true "
			+ "and ((to_char(listTgl.tgl, 'yyyy') = :currentYear and a.approvalStatus <> :approvalStatusId1) "
			+ "or (to_char(listTgl.tgl, 'yyyy') <> :currentYear and a.approvalStatus = :approvalStatusId2)) "
			+ "and e.noRec is null "
			+ "and c.id in (:ketidakhadiranId) "
			+ "order by a.tglPengajuan desc, listTgl.tgl asc")
	public List<Map<String, Object>> getApprovalStatus(@Param("currentYear") String currentYear, @Param("approvalStatusId1") Integer approvalStatusId1, 
			@Param("approvalStatusId2") Integer approvalStatusId2, @Param("ketidakhadiranId") List<Integer> ketidakhadiranId);
	
	@Query("select NEW Map (a.noRec as noRec, "
			+ "a.noPlanning as noPlanning, "
			+ "a.deskripsiStatusPegawaiPlan as deskripsiStatusPegawaiPlan, "
			+ "a.keteranganLainyaPlan as keteranganLainyaPlan, "
			+ "a.deskripsiStatusPegawaiExec as deskripsiStatusPegawaiExec, "
			+ "a.keteranganLainyaExec as keteranganLainyaExec, "
			+ "a.tglPengajuan as tglPengajuan, "
			+ "a.tglKeputusan as tglKeputusan, "
			+ "a.approvalStatus as approvalStatus, "
			+ "a.alamatCuti as alamatCuti, "
			+ "a.nomorTelepon as nomorTelepon, "
			+ "a.isCutiLuarNegeri as isCutiLuarNegeri, "
			+ "a.isCutiLuarKota as isCutiLuarKota, "
			+ "a.noSuratTugas as noSuratTugas, "
			+ "a.noNotaDinas as noNotaDinas, "
			+ "a.tglNotaDinas as tglNotaDinas, "
			+ "a.alamatTugas as alamatTugas, "
			+ "a.jenisPerawatan as sakit, "
			+ "a.jabatanIdPemberiNotaDinas as jabatanIdPemberiNotaDinas, "
			+ "listTgl.noRec as norecTgl, "
			+ "listTgl.tgl as tgl, "
			+ "listTgl.approvalStatus as approvalStatusTgl, "
			+ "fp.namaJabatan as jabatanPemberiNotaDinas, "
			+ "subUnitKerja.id as idUnitKerja, "
			+ "subUnitKerja.name as unitKerja, "
			+ "b.id as pegwaiId, "
			+ "b.namaLengkap as namaPegawai, "
			+ "b.nipPns as nip, "
			+ "c.id as statusPegawaiId, "
			+ "c.statusPegawai as statusPegawai, "
			+ "f.namaJabatan as namaJabatan) "
			+ "from PlanningPegawaiStatus a, "
			+ "MapPegawaiJabatanToUnitKerja mp "
			+ "left join a.pegawai b "
			+ "left join a.statusPegawaiPlan c "
			+ "left join a.noSk e "
			+ "left join a.jabatanPemberiNotaDinas fp "
			+ "left join a.listTanggal listTgl "
			+ "left join mp.jabatan f "
			+ "left join mp.subUnitKerjaPegawai subUnitKerja "
			+ "where mp.pegawaiId = b.id "
			+ "and a.pegawaiId = mp.pegawaiId "
			+ "and a.statusEnabled = true "
			+ "and listTgl.statusEnabled = true "
			+ "and mp.statusEnabled is true "
			+ "and mp.isPrimary is true "
			+ "and a.approvalStatus = :approvalStatusId "
			+ "and e.noRec is null "
			+ "and c.id in (:ketidakhadiranId) "
			+ "order by a.tglPengajuan desc, listTgl.tgl asc")
	public List<Map<String, Object>> getApprovalStatusSdm(@Param("approvalStatusId") Integer approvalStatusId, 
			@Param("ketidakhadiranId") List<Integer> ketidakhadiranId);
	
	@Query("select new Map(pegawai.namaLengkap as namaLengkap, "
			+ "coalesce(pegawai.nipPns, pegawai.nip) as nip, "
			+ "pangkat.namaPangkat || ' / ' || golongan.golonganPegawai as pangkatGol, "
			+ "jabatan.namaJabatan as namaJabatan, "
			+ "transaksiCuti.statusPegawaiPlanId as idPlanning, "
			+ "statusPegawai.statusPegawai as statusPegawai, "
			+ "tglCuti.tgl as tglCuti) "
			+ "from MapPegawaiJabatanToUnitKerja mappingJabatan, PlanningPegawaiStatus transaksiCuti "
			+ "left join mappingJabatan.pegawai pegawai "
			+ "left join mappingJabatan.jabatan jabatan "
			+ "left join transaksiCuti.listTanggal tglCuti "
			+ "left join transaksiCuti.statusPegawaiPlan statusPegawai "
			+ "left join pegawai.pangkat pangkat "
			+ "left join pangkat.kdGolonganPegawai golongan "
			+ "where transaksiCuti.pegawaiId = pegawai.id "
			+ "and mappingJabatan.pegawaiId = transaksiCuti.pegawaiId "
			+ "and mappingJabatan.statusEnabled is true "
			+ "and pegawai.statusEnabled is true "
			+ "and mappingJabatan.isPrimary is true "
			+ "and tglCuti.statusEnabled is true "
			+ "and transaksiCuti.noRec = :norec")
	public List<Map<String, Object>> getCetakCutiLuarNegeri(@Param("norec") String norec);
	
}
