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

@Repository("logbookKinerjaDao")
public interface LogbookKinerjaDao extends PagingAndSortingRepository<LogbookKinerja, String> {

	@Query("select new Map(lk.noRec as noRec," + "pg.id as pegawaiId,pg.namaLengkap as namaPegawai,"
			+ "jb.id as jabatanId,jb.namaJabatan as namaJabatan,"
			+ "ik.id as indikatorId,ik.namaIndikator as namaIndikator,"
			+ "si.id as satuanIndikatorId,si.satuanIndikator as satuanIndikator,"
			+ "ik.jenisIndikator as jenisIndikatorId," + "(case when ik.jenisIndikator = 1 then 'Kuantitas' "
			+ "when ik.jenisIndikator = 2 then 'Kualitas' "
			+ "when ik.jenisIndikator = 3 then 'Perilaku' end) as jenisIndikator," + "lk.target as target,"
			+ "lk.bobot as bobot," + "lk.bulan as bulan) " + "from LogbookKinerja lk "
			+ "inner join lk.indikatorKinerja ik " + "inner join lk.pegawai pg " + "inner join lk.jabatan jb "
			+ "inner join ik.satuanIndikator si " + "where lk.statusEnabled is true " + "and ik.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and lk.statusVerifikasi is false "
			+ "and ik.statusVerifikasi is false " + "and pg.id = :pegawaiId " + "and jb.id = :jabatanId")
	List<Map<String, Object>> findPengajuanIndikatorByPegawaiJabatan(@Param("pegawaiId") Integer pegawaiId,
			@Param("jabatanId") Integer jabatanId);

	@Query("select new Map(lk.noRec as noRec,ik.namaIndikator as namaIndikator) " + "from LogbookKinerja lk "
			+ "inner join lk.indikatorKinerja ik " + "where lk.statusEnabled is true and ik.statusEnabled is true "
			+ "and lk.statusVerifikasi is true and ik.statusVerifikasi is true " + "and lk.pegawaiId = :pegawaiId "
			+ "and lk.jabatanId = :jabatanId " + "and to_char(lk.bulan,'yyyy-MM') = :bulan "
			+ "and ik.id = :indikatorId")
	List<Map<String, Object>> findDupKontrakKinerja(@Param("pegawaiId") Integer idPegawai,
			@Param("jabatanId") Integer idJabatan, @Param("bulan") String bulan,
			@Param("indikatorId") Integer idIndikator);

	@Query("select new Map(ikm.jenisIndikator as jenisIndikatorId,ikm.id as indikatorId,ikm.namaIndikator as namaIndikator,"
			+ "si.id as satuanId,si.satuanIndikator as satuanIndikator,"
			+ "ikt.noRec as noRecMap,ikt.tanggalMulaiBerlaku as tglBerlaku,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 si.statusEnabled is true "
			+ "and ikt.jabatanId = :jabatanId " + "and to_char(ikt.tanggalMulaiBerlaku,'yyyy-MM') <= :bulan "
			+ "order by ikm.jenisIndikator, ikm.namaIndikator, "
			+ "ikt.tanggalMulaiBerlaku desc, ikt.tanggalPembaharuanData desc")
	List<Map<String, Object>> findIndikatorByJabatan(@Param("jabatanId") Integer idJabatan,
			@Param("bulan") String bulan);

	@Query("select new Map(ikt.jabatanId as jabatanId,"
			+ "ikm.jenisIndikator as jenisIndikatorId,ikm.id as indikatorId,ikm.namaIndikator as namaIndikator,"
			+ "si.id as satuanId,si.satuanIndikator as satuanIndikator,"
			+ "ikt.noRec as noRecMap,ikt.tanggalMulaiBerlaku as tglBerlaku,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 si.statusEnabled is true "
			+ "and ikt.jabatanId in (:listIdJabatan) " + "and to_char(ikt.tanggalMulaiBerlaku,'yyyy-MM') <= :bulan "
			+ "order by ikm.jenisIndikator, ikm.namaIndikator, "
			+ "ikt.tanggalMulaiBerlaku desc, ikt.tanggalPembaharuanData desc")
	List<Map<String, Object>> findIndikatorByJabatan(@Param("listIdJabatan") List<Integer> listIdJabatan,
			@Param("bulan") String bulan);

	@Query("select new Map(lkh.noRec as noRec,ikm.jenisIndikator as jenisIndikatorId,"
			+ "ikm.id as indikatorId,ikm.namaIndikator as namaIndikator,"
			+ "si.id as satuanId,si.satuanIndikator as satuanIndikator," + "lkh.target as target,lkh.bobot as bobot,"
			+ "lkh.statusVerifikasi as isStatusVerifikasi,"
			+ "(case when lkh.statusVerifikasi is true then 'Terverifikasi' "
			+ "else 'Belum Verifikasi' end) as statusVerifikasi) " + "from LogbookKinerja lkh "
			+ "inner join lkh.indikatorKinerja ikm " + "inner join ikm.satuanIndikator si "
			+ "where lkh.statusEnabled is true " + "and ikm.statusEnabled is true "
			+ "and ikm.statusVerifikasi is true " + "and si.statusEnabled is true " + "and lkh.pegawaiId = :pegawaiId "
			+ "and lkh.jabatanId = :jabatanId " + "and to_char(lkh.bulan,'yyyy-MM') = :bulan "
			+ "order by ikm.jenisIndikator, ikm.namaIndikator")
	List<Map<String, Object>> findKontrakKinerja(@Param("pegawaiId") Integer idPegawai,
			@Param("jabatanId") Integer idJabatan, @Param("bulan") String bulan);

	@Query("select new Map(lk.noRec as noRec,ik.namaIndikator as namaIndikator) " + "from LogbookKinerja lk "
			+ "inner join lk.indikatorKinerja ik " + "where ik.id = :indikatorId")
	List<Map<String, Object>> findKontrakKinerja(@Param("indikatorId") Integer idIndikator);

	@Query("select distinct new Map(lk.noRec as noRec," + "pg.id as idPegawai,pg.namaLengkap as namaPegawai,"
			+ "coalesce(mj.nilaiJabatan,0) as nilaiJabatan," + "jb.id as idJabatan,jb.namaJabatan as namaJabatan,"
			+ "jb.kelompokJabatanId as kelompokJabatanId," + "ik.jenisIndikator as idJenisIndikator,"
			+ "(case when ik.jenisIndikator = 1 then 'Kuantitas' " + "when ik.jenisIndikator = 2 then 'Kualitas' "
			+ "when ik.jenisIndikator = 3 then 'Perilaku' end) as jenisIndikator,"
			+ "ik.id as idIndikator,ik.namaIndikator as namaIndikator,"
			+ "si.id as satuanId,si.satuanIndikator as satuanIndikator,"
			+ "lk.target as target,lk.capaian/lk.target as fragCapaian,lk.bobot as bobot) "
			+ "from LogbookKinerja lk, MapPegawaiJabatanToUnitKerja mj " + "inner join lk.indikatorKinerja ik "
			+ "inner join ik.satuanIndikator si " + "inner join lk.pegawai pg " + "inner join lk.jabatan jb "
			+ "where lk.pegawaiId = mj.pegawaiId " + "and lk.jabatanId = mj.jabatanId " + "and mj.pegawaiId = pg.id "
			+ "and mj.jabatanId = jb.id " + "and lk.statusEnabled is true " + "and ik.statusEnabled is true "
			+ "and lk.statusVerifikasi is true " + "and ik.statusVerifikasi is true " + "and si.statusEnabled is true "
			+ "and mj.statusEnabled is true " + "and lk.pegawaiId = :pegawaiId " + "and lk.jabatanId = :jabatanId "
			+ "and to_char(lk.bulan,'yyyy-MM') = :bulan " + "order by ik.jenisIndikator, ik.namaIndikator")
	List<Map<String, Object>> findLogbookKinerja(@Param("pegawaiId") Integer idPegawai,
			@Param("jabatanId") Integer idJabatan, @Param("bulan") String bulan);

	@Query("select lkt from LogbookKinerja lkt " + "where lkt.statusEnabled is true "
			+ "and lkt.pegawaiId = :pegawaiId " + "and to_char(lkt.bulan,'yyyy-MM') = :bulan "
			+ "and lkt.indikatorKinerjaId = :indikatorId")
	List<LogbookKinerja> findLogbookKinerja(@Param("pegawaiId") Integer idPegawai, @Param("bulan") String bulan,
			@Param("indikatorId") Integer idIndikator);

	@Query("select distinct new Map(pg.id as id,pg.namaLengkap as namaLengkap,pg.tglMasuk as tglMasuk) "
			+ "from MapPegawaiJabatanToUnitKerja mj " + "inner join mj.pegawai pg " + "inner join mj.jabatan jb "
			+ "where mj.statusEnabled is true " + "and pg.statusEnabled is true " + "and jb.statusEnabled is true "
			+ "and pg.id = :pegawaiId " + "and pg.kategoryPegawaiId in (:listKategoryPegawaiId)")
	List<Map<String, Object>> findAksesPegawaiByStaf(@Param("pegawaiId") Integer idPegawai,
			@Param("listKategoryPegawaiId") List<Integer> listIdKategoriPegawai);

	@Query("select distinct new Map(pg.id as id,pg.namaLengkap as namaLengkap," + "pg.tglMasuk as tglMasuk,"
			+ "pg.jenisPegawaiId as jenisPegawaiId) " + "from MapPegawaiJabatanToUnitKerja mj "
			+ "inner join mj.pegawai pg " + "inner join mj.jabatan jb " + "where mj.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and jb.statusEnabled is true " + "and pg.id = :pegawaiId "
			+ "and pg.kategoryPegawaiId in (:listKategoryPegawaiId) "
			+ "and jb.kelompokJabatanId in (:listKelompokJabatabId)")
	List<Map<String, Object>> findAksesPegawaiByStaf(@Param("pegawaiId") Integer idPegawai,
			@Param("listKategoryPegawaiId") List<Integer> listIdKategoriPegawai,
			@Param("listKelompokJabatabId") List<Integer> listIdKelompokJabatan);

	@Query("select distinct new Map(pgb.id as id,pgb.namaLengkap as namaLengkap,pgb.tglMasuk as tglMasuk) "
			+ "from MapPegawaiJabatanToUnitKerja mjp, MapPegawaiJabatanToUnitKerja mja " + "inner join mjp.pegawai pgb "
			+ "inner join mjp.atasanLangsung pg " + "inner join mja.jabatan jb "
			+ "where mjp.atasanLangsungId = mja.pegawaiId " + "and mja.pegawaiId = pg.id "
			+ "and mjp.statusEnabled is true " + "and mja.statusEnabled is true " + "and pgb.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and jb.statusEnabled is true " + "and pg.id = :pegawaiId "
			+ "and pgb.kategoryPegawaiId in (:listKategoryPegawaiId)")
	List<Map<String, Object>> findAksesPegawaiByAtasan(@Param("pegawaiId") Integer idPegawai,
			@Param("listKategoryPegawaiId") List<Integer> listIdKategoriPegawai);

	@Query("select distinct new Map(pgb.id as id,pgb.namaLengkap as namaLengkap," + "pgb.tglMasuk as tglMasuk,"
			+ "pgb.jenisPegawaiId as jenisPegawaiId) "
			+ "from MapPegawaiJabatanToUnitKerja mjp, MapPegawaiJabatanToUnitKerja mja " + "inner join mjp.pegawai pgb "
			+ "inner join mjp.atasanLangsung pg " + "inner join mjp.jabatan jb "
			+ "where mjp.atasanLangsungId = mja.pegawaiId " + "and mja.pegawaiId = pg.id "
			+ "and mjp.statusEnabled is true " + "and mja.statusEnabled is true " + "and pgb.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and jb.statusEnabled is true " + "and pg.id = :pegawaiId "
			+ "and pgb.kategoryPegawaiId in (:listKategoryPegawaiId) "
			+ "and jb.kelompokJabatanId in (:listKelompokJabatabId)")
	List<Map<String, Object>> findAksesPegawaiByAtasan(@Param("pegawaiId") Integer idPegawai,
			@Param("listKategoryPegawaiId") List<Integer> listIdKategoriPegawai,
			@Param("listKelompokJabatabId") List<Integer> listIdKelompokJabatan);

	@Query("select distinct new Map(pgb.id as id,pgb.namaLengkap as namaLengkap,pgb.tglMasuk as tglMasuk) "
			+ "from MapPegawaiJabatanToUnitKerja mjp, MapPegawaiJabatanToUnitKerja mjn " + "inner join mjp.pegawai pgb "
			+ "inner join mjp.pejabatPenilai pg " + "inner join mjn.jabatan jb "
			+ "where mjp.pejabatPenilaiId = mjn.pegawaiId " + "and mjn.pegawaiId = pg.id "
			+ "and mjp.statusEnabled is true " + "and mjn.statusEnabled is true " + "and pgb.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and jb.statusEnabled is true " + "and pg.id = :pegawaiId "
			+ "and pgb.kategoryPegawaiId in (:listKategoryPegawaiId)")
	List<Map<String, Object>> findAksesPegawaiByPenilai(@Param("pegawaiId") Integer idPegawai,
			@Param("listKategoryPegawaiId") List<Integer> listIdKategoriPegawai);

	@Query("select distinct new Map(pgb.id as id,pgb.namaLengkap as namaLengkap," + "pgb.tglMasuk as tglMasuk,"
			+ "pgb.jenisPegawaiId as jenisPegawaiId) "
			+ "from MapPegawaiJabatanToUnitKerja mjp, MapPegawaiJabatanToUnitKerja mjn " + "inner join mjp.pegawai pgb "
			+ "inner join mjp.pejabatPenilai pg " + "inner join mjp.jabatan jb "
			+ "where mjp.pejabatPenilaiId = mjn.pegawaiId " + "and mjn.pegawaiId = pg.id "
			+ "and mjp.statusEnabled is true " + "and mjn.statusEnabled is true " + "and pgb.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and jb.statusEnabled is true " + "and pg.id = :pegawaiId "
			+ "and pgb.kategoryPegawaiId in (:listKategoryPegawaiId) "
			+ "and jb.kelompokJabatanId in (:listKelompokJabatabId)")
	List<Map<String, Object>> findAksesPegawaiByPenilai(@Param("pegawaiId") Integer idPegawai,
			@Param("listKategoryPegawaiId") List<Integer> listIdKategoriPegawai,
			@Param("listKelompokJabatabId") List<Integer> listIdKelompokJabatan);

	@Query("select new Map(lk.noRec as noRec,lk.pegawaiId as pegawaiId) "
			+ "from LogbookKinerja lk where lk.indikatorKinerjaId = :indikatorId " + "and lk.jabatanId = :jabatanId "
			+ "and lk.statusEnabled is true " + "and to_char(lk.bulan,'yyyy-MM') >= to_char(now(),'yyyy-MM') "
			+ "and lk.statusVerifikasi is false")
	List<Map<String, Object>> findUsedKontrakByMapping(@Param("indikatorId") Integer idIndikator,
			@Param("jabatanId") Integer idJabatan);

	@Query("select new Map(jb.kelompokJabatanId as kelompokJabatanId," + "ikm.jenisIndikator as jenisIndikatorId,"
			+ "(case when ikm.jenisIndikator = 1 then 'Kuantitas' " + "when ikm.jenisIndikator = 2 then 'Kualitas' "
			+ "when ikm.jenisIndikator = 3 then 'Perilaku' end) as jenisIndikator,"
			+ "ikm.id as indikatorId,ikm.namaIndikator as namaIndikator,"
			+ "si.id as satuanId,si.satuanIndikator as satuan," + "lkt.bobot as bobot," + "lkt.target as target,"
			+ "lkt.capaian as realisasi) " + "from LogbookKinerja lkt " + "inner join lkt.jabatan jb "
			+ "inner join lkt.indikatorKinerja ikm " + "inner join ikm.satuanIndikator si "
			+ "where lkt.statusEnabled is true and lkt.statusVerifikasi is true "
			+ "and ikm.statusEnabled is true and ikm.statusVerifikasi is true " + "and si.statusEnabled is true "
			+ "and lkt.pegawaiId = :pegawaiId " + "and lkt.jabatanId = :jabatanId "
			+ "and to_char(lkt.bulan,'yyyy-MM') = :bulan")
	List<Map<String, Object>> findPenilaianKinerja(@Param("pegawaiId") Integer idPegawai,
			@Param("jabatanId") Integer idJabatan, @Param("bulan") String bulan);

	String strQryIKI = "select new Map(uk.id as unitKerjaId,uk.name as unitKerja,"
			+ "sk.id as subunitKerjaId,sk.name as subunitKerja," + "pg.id as pegawaiId,pg.namaLengkap as namaLengkap,"
			+ "jb.id as jabatanId,jb.namaJabatan as namaJabatan,jb.kelompokJabatanId as kelompokJabatanId,"
			+ "mj.nilaiJabatan as nilaiJabatan," + "ikm.jenisIndikator as jenisIndikatorId,"
			+ "(case when ikm.jenisIndikator = 1 then 'Kuantitas' " + "when ikm.jenisIndikator = 2 then 'Kualitas' "
			+ "when ikm.jenisIndikator = 3 then 'Perilaku' end) as jenisIndikator,"
			+ "ikm.id as indikatorId,ikm.namaIndikator as namaIndikator,"
			+ "lkt.bobot as bobot,lkt.target as target,lkt.capaian as realisasi) "
			+ "from LogbookKinerja lkt, MapPegawaiJabatanToUnitKerja mj " + "inner join lkt.indikatorKinerja ikm "
			+ "inner join mj.pegawai pg " + "inner join mj.jabatan jb " + "inner join mj.unitKerjaPegawai uk "
			+ "inner join mj.subUnitKerjaPegawai sk " + "where lkt.pegawaiId = mj.pegawaiId "
			+ "and lkt.pegawaiId = pg.id " + "and lkt.jabatanId = mj.jabatanId " + "and lkt.jabatanId = jb.id "
			+ "and uk.id = sk.unitKerjaId " + "and lkt.statusEnabled is true and lkt.statusVerifikasi is true "
			+ "and mj.statusEnabled is true " + "and ikm.statusEnabled is true and ikm.statusVerifikasi is true "
			+ "and pg.statusEnabled is true " + "and jb.statusEnabled is true " + "and uk.statusEnabled is true "
			+ "and sk.statusEnabled is true " + "and to_char(lkt.bulan,'yyyy-MM') = :bulan ";

	String cdtUnitKerja = "and uk.id = :unitKerjaId ";

	String cdtSubunitKerja = "and sk.id = :subunitKerjaId ";

	String cdtPegawai = "and pg.id = :pegawaiId ";

	String sortIKI = "order by uk.name, sk.name, pg.namaLengkap";

	@Query(strQryIKI + sortIKI)
	List<Map<String, Object>> findPenilaianKinerja(@Param("bulan") String bulan);

	@Query(strQryIKI + cdtUnitKerja + sortIKI)
	List<Map<String, Object>> findPenilaianKinerjaUnitKerja(@Param("bulan") String bulan,
			@Param("unitKerjaId") Integer idUnitKerja);

	@Query(strQryIKI + cdtUnitKerja + cdtSubunitKerja + sortIKI)
	List<Map<String, Object>> findPenilaianKinerjaSubunitKerja(@Param("bulan") String bulan,
			@Param("unitKerjaId") Integer idUnitKerja, @Param("subunitKerjaId") Integer idSubunitKerja);

	@Query(strQryIKI + cdtPegawai + sortIKI)
	List<Map<String, Object>> findPenilaianKinerjaPegawai(@Param("bulan") String bulan,
			@Param("pegawaiId") Integer idPegawai);

	@Query(strQryIKI + cdtUnitKerja + cdtSubunitKerja + cdtPegawai + sortIKI)
	List<Map<String, Object>> findPenilaianKinerjaPegawai(@Param("bulan") String bulan,
			@Param("unitKerjaId") Integer idUnitKerja, @Param("subunitKerjaId") Integer idSubunitKerja,
			@Param("pegawaiId") Integer idPegawai);

	@Query("select new Map(lkt.noRec as noRec,lkt.jabatanId as jabatanId) "
			+ "from LogbookKinerja lkt, MapPegawaiJabatanToUnitKerja mj " + "where lkt.pegawaiId = mj.pegawaiId "
			+ "and lkt.jabatanId = mj.jabatanId " + "and to_char(lkt.bulan,'yyyy-MM') = :bulan "
			+ "and lkt.pegawaiId = :pegawaiId " + "and lkt.indikatorKinerjaId = :indikatorId "
			+ "and lkt.statusEnabled is true and lkt.statusVerifikasi is true " + "and mj.statusEnabled is true")
	Map<String, Object> findKontrakForVerify(@Param("bulan") String bulan, @Param("pegawaiId") Integer idPegawai,
			@Param("indikatorId") Integer idIndikator);

	@Query("select distinct lkt.pegawaiId " + "from LogbookKinerja lkt "
			+ "where lkt.indikatorKinerjaId = :indikatorId " + "and lkt.bulan between :start and :end "
			+ "and lkt.capaian = 0.0 " + "and lkt.statusEnabled is true " + "and lkt.statusVerifikasi is true")
	List<Integer> findPegawaiKontrakByIndikator(@Param("start") Date start, @Param("end") Date end,
			@Param("indikatorId") Integer idIndikator);

	@Query("select distinct new Map(jb.id as id,jb.namaJabatan as namaJabatan,"
			+ "jb.levelJabatan as levelJabatan,jb.unitKerjaId as idUnitKerja) " + "from LogbookKinerja lkt "
			+ "inner join lkt.jabatan jb " + "where lkt.statusEnabled is true " + "and lkt.pegawaiId = :pegawaiId "
			+ "and to_char(lkt.bulan,'yyyy-MM') = :bulan")
	List<Map<String, Object>> findJabatanByPegawai(@Param("pegawaiId") Integer idPegawai, @Param("bulan") String bulan);
}
