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

@Repository("PegawaiSKGajiDao")
public interface PegawaiSKGajiDao extends PagingAndSortingRepository<PegawaiSKGaji, Integer> {

	// NIP PNS, Nama Lengkap, Golongan, Pendidikan, Masa Kerja, (Jabatan), Komponen Gaji, Harga
	@Query("select new Map(p.nipPns AS NIP, p.namaLengkap AS NamaLengkap, g.golonganPegawai AS Golongan, pdk.namaPendidikan AS Pendidikan, "
			+ "jf.namaJabatan AS Jabatan, kh.komponenHarga AS KomponenHarga, gj.hargaSatuan AS Harga) "
			+ "from PegawaiSKGaji gj "
			+ "left join gj.pegawai p "
			+ "left join p.detailKategoryPegawai dkp "
			+ "left join p.golonganPegawai g "
			+ "left join p.pendidikan pdk "
			+ "left join p.jabatanFungsional jf "
			+ "left join gj.komponenHarga kh "
			+ "where dkp.detailKategoryPegawai.id=:idDetailKategoriPegawai "
			+ "and kh.komponenHarga in(45, 52, 56, 57) ")
	List<Map<String, Object>> findPegawaiGajiSKByKategoriPegawai(@Param("idDetailKategoriPegawai") Integer idDetailKategoriPegawai);
	
	// Gaji Pokok (ID=45)
	@Query("select new Map(gj.id AS ID, p.nipPns AS NIP, p.namaLengkap AS NamaLengkap, gp.golonganPegawai AS Golongan, pdk.namaPendidikan AS Pendidikan, "
			+ "EXTRACT(year FROM (current_date)) - EXTRACT(year FROM (p.tglMasuk)) AS MasaKerja, jf.namaJabatan AS Jabatan, kh.komponenHarga AS KomponenHarga, gjmkp.hargaSatuan AS Harga) "
			+ "from PegawaiSKGaji gj, GajiPerMasaKerjaPendidikan gjmkp "
			+ "left join gj.pegawai p "
			+ "left join p.detailKategoryPegawai dkp "
			+ "left join p.golonganPegawai gp "
			+ "left join p.pendidikan pdk "
			+ "left join p.jabatanFungsional jf "
			+ "left join gj.komponenHarga kh "
			+ "where dkp.detailKategoryPegawai.id=:idDetailKategoriPegawai "
			+ "and gj.komponenHarga.komponenHarga='Gaji Pokok' "
			+ "and gjmkp.pendidikan=p.pendidikan "
			+ "and gjmkp.range = (select r.id from Range r where r.namaRange='Range Masa Kerja' and EXTRACT(year FROM (current_date)) - EXTRACT(year FROM (p.tglMasuk)) between r.rangeMin and r.rangeMax) "
			+ "order by NIP")
	List<Map<String, Object>> findGajiPokokPegawai(@Param("idDetailKategoriPegawai") Integer idDetailKategoriPegawai);
	
	// Tunjangan Fungsional (ID=52)
	@Query("select new Map(gj.id AS ID, p.nipPns AS NIP, p.namaLengkap AS NamaLengkap, gp.golonganPegawai AS Golongan, pdk.namaPendidikan AS Pendidikan, "
			+ "EXTRACT(year FROM (current_date)) - EXTRACT(year FROM (p.tglMasuk)) AS MasaKerja, jf.namaJabatan AS Jabatan, kh.komponenHarga AS KomponenHarga, gjjp.hargaSatuan AS Harga) "
			+ "from PegawaiSKGaji gj, GajiPerJabatanPendidikan gjjp "
			+ "left join gj.pegawai p "
			+ "left join p.detailKategoryPegawai dkp "
			+ "left join p.golonganPegawai gp "
			+ "left join p.pendidikan pdk "
			+ "left join p.jabatanFungsional jf "
			+ "left join gj.komponenHarga kh "
			+ "where dkp.detailKategoryPegawai.id=:idDetailKategoriPegawai "
			+ "and gj.komponenHarga.komponenHarga='Tunjangan Fungsional' "
			+ "and gjjp.jabatan=p.jabatanFungsional "
			+ "and gjjp.pendidikan=p.pendidikan "
			+ "order by NIP")
	List<Map<String, Object>> findTunjanganFungsional(@Param("idDetailKategoriPegawai") Integer idDetailKategoriPegawai);
	
	// Tunjangan Bahaya Radiasi (ID=56)
	@Query("select new Map(gj.id AS ID, p.nipPns AS NIP, p.namaLengkap AS NamaLengkap, gp.golonganPegawai AS Golongan, pdk.namaPendidikan AS Pendidikan, "
			+ "EXTRACT(year FROM (current_date)) - EXTRACT(year FROM (p.tglMasuk)) AS MasaKerja, jf.namaJabatan AS Jabatan, kh.komponenHarga AS KomponenHarga, gjlt.hargaSatuan AS Harga) "
			+ "from PegawaiSKGaji gj, GajiPerLevelTingkat gjlt, MapPegawaiJabatanToUnitKerja mapPegawai "
			+ "left join gj.pegawai p "
			+ "left join p.detailKategoryPegawai dkp "
			+ "left join p.golonganPegawai gp "
			+ "left join p.pendidikan pdk "
			+ "left join p.jabatanFungsional jf "
			+ "left join gj.komponenHarga kh "
			+ "where mapPegawai.pegawaiId = p.id "
			+ "and mapPegawai.isPrimary is true "
			+ "and mapPegawai.statusEnabled is true "
			+ "and dkp.detailKategoryPegawai.id = :idDetailKategoriPegawai "
			+ "and gj.komponenHarga.komponenHarga = 'Tunjangan Bahaya Radiasi' "
			+ "and gjlt.jabatan = mapPegawai.jabatan "
			+ "and gjlt.levelTingkat = p.levelTingkat "
			+ "order by NIP")
	List<Map<String, Object>> findTunjanganBahayaRadiasi(@Param("idDetailKategoriPegawai") Integer idDetailKategoriPegawai);
	
	// Tunjangan Layanan (ID=57)
	@Query("select new Map(gj.id AS ID, p.nipPns AS NIP, p.namaLengkap AS NamaLengkap, gp.golonganPegawai AS Golongan, pdk.namaPendidikan AS Pendidikan, "
			+ "EXTRACT(year FROM (current_date)) - EXTRACT(year FROM (p.tglMasuk)) AS MasaKerja, jf.namaJabatan AS Jabatan, kh.komponenHarga AS KomponenHarga, gjj.hargaSatuan AS Harga) "
			+ "from PegawaiSKGaji gj, GajiPerJabatan gjj, MapPegawaiJabatanToUnitKerja mapPegawai "
			+ "left join gj.pegawai p "
			+ "left join p.detailKategoryPegawai dkp "
			+ "left join p.golonganPegawai gp "
			+ "left join p.pendidikan pdk "
			+ "left join p.jabatanFungsional jf "
			+ "left join gj.komponenHarga kh "
			+ "where mapPegawai.pegawaiId = p.id "
			+ "and mapPegawai.isPrimary is true "
			+ "and mapPegawai.statusEnabled is true "
			+ "and dkp.detailKategoryPegawai.id = :idDetailKategoriPegawai "
			+ "and gj.komponenHarga.komponenHarga = 'Tunjangan Layanan' "
			+ "and gjj.jabatan = mapPegawai.jabatan ")
	List<Map<String, Object>> findTunjanganLayanan(@Param("idDetailKategoriPegawai") Integer idDetailKategoriPegawai);
	
	
	//@Query("select v from komponen_gaji_kotor_v v")
	//List<Map<String, Object>> findGajiKotor(@Param("idDetailKategoriPegawai") Integer idDetailKategoriPegawai);
	
	//@Query("select v from komponen_gaji_bersih_v v")
	//List<Map<String, Object>> findGajiBersih(@Param("idDetailKategoriPegawai") Integer idDetailKategoriPegawai);
	
	@Query("select new Map(model.id as pegawaiSkGajiId, "
			+"suratkeputusan.id as suratKeputusanId, "
			+"suratkeputusan.noSK as noSuratKeputusan, "
			+"suratkeputusan.namaSK as namaSuratKeputusan, "
			+"to_char(suratkeputusan.tglBerlakuAwal,'dd-mm-yyyy') as tglBerlakuAwal, "
			+"to_char(suratkeputusan.tglBerlakuAkhir,'dd-mm-yyyy') as tglBerlakuAkhir, "
			+"ruangan.id as ruanganId, "
			+"ruangan.namaRuangan as namaRuangan, "
			+"pegawai.id as pegawaiId, "
			+"pegawai.namaLengkap as namaPegawai, "
			+"komponenharga.id as komponenHargaId, "
			+"komponenharga.komponenHarga as namaKomponenHarga, "
			+"model.persenHargaSatuan as persenHargaSatuan, "
			+"model.hargaSatuan as hargaSatuan, "
			+"model.factorRate as factorRate, "
			+"model.operatorFactorRate as operatorFactorRate, "
			+"model.isByMonth as isByMonth, "
			+"model.isByYear as isByYear, "
			+"model.isByWeek as isByWeek, "
			+"model.isByDay as isByDay,"
			+"model.statusEnabled as statusEnabled "
			+ ") "
			+"from PegawaiSKGaji model  "
			+"left join model.noSK suratkeputusan " 
			+"left join suratkeputusan.ruangan ruangan " 
			+"left join model.pegawai pegawai  "
			+"left join model.komponenHarga komponenharga "
			+ "where model.id =:idPegawaiSkGaji")
	Map<String,Object> get(@Param("idPegawaiSkGaji") Integer idPegawaiSkGaji);
	
	@Query("select new Map(model.id as pegawaiSkGajiId, "
			+"suratkeputusan.id as suratKeputusanId, "
			+"suratkeputusan.noSK as noSuratKeputusan, "
			+"suratkeputusan.namaSK as namaSuratKeputusan, "
			+"to_char(suratkeputusan.tglBerlakuAwal,'dd-mm-yyyy') as tglBerlakuAwal, "
			+"to_char(suratkeputusan.tglBerlakuAkhir,'dd-mm-yyyy') as tglBerlakuAkhir, "
			+"ruangan.id as ruanganId, "
			+"ruangan.namaRuangan as namaRuangan, "
			+"pegawai.id as pegawaiId, "
			+"pegawai.namaLengkap as namaPegawai, "
			+"komponenharga.id as komponenHargaId, "
			+"komponenharga.komponenHarga as namaKomponenHarga, "
			+"model.persenHargaSatuan as persenHargaSatuan, "
			+"model.hargaSatuan as hargaSatuan, "
			+"model.factorRate as factorRate, "
			+"model.operatorFactorRate as operatorFactorRate, "
			+"model.isByMonth as isByMonth, "
			+"model.isByYear as isByYear, "
			+"model.isByWeek as isByWeek, "
			+"model.isByDay as isByDay,"
			+"model.statusEnabled as statusEnabled "
			+ ") "
			+"from PegawaiSKGaji model  "
			+"left join model.noSK suratkeputusan " 
			+"left join suratkeputusan.ruangan ruangan " 
			+"left join model.pegawai pegawai  "
			+"left join model.komponenHarga komponenharga "
			+ "where pegawai.id =:idPegawai")
	List<Map<String,Object>> getByPegawaiId(@Param("idPegawai") Integer idPegawai);

	@Query("select new Map(model.id as pegawaiSkGajiId, "
			+"suratkeputusan.id as suratKeputusanId, "
			+"suratkeputusan.noSK as noSuratKeputusan, "
			+"suratkeputusan.namaSK as namaSuratKeputusan, "
			+"to_char(suratkeputusan.tglBerlakuAwal,'dd-mm-yyyy') as tglBerlakuAwal, "
			+"to_char(suratkeputusan.tglBerlakuAkhir,'dd-mm-yyyy') as tglBerlakuAkhir, "
			+"ruangan.id as ruanganId, "
			+"ruangan.namaRuangan as namaRuangan, "
			+"pegawai.id as pegawaiId, "
			+"pegawai.namaLengkap as namaPegawai, "
			+"komponenharga.id as komponenHargaId, "
			+"komponenharga.komponenHarga as namaKomponenHarga, "
			+"model.persenHargaSatuan as persenHargaSatuan, "
			+"model.hargaSatuan as hargaSatuan, "
			+"model.factorRate as factorRate, "
			+"model.operatorFactorRate as operatorFactorRate, "
			+"model.isByMonth as isByMonth, "
			+"model.isByYear as isByYear, "
			+"model.isByWeek as isByWeek, "
			+"model.isByDay as isByDay ) "
			+"from PegawaiSKGaji model  "
			+"left join model.noSK suratkeputusan " 
			+"left join suratkeputusan.ruangan ruangan " 
			+"left join model.pegawai pegawai  "
			+"left join model.komponenHarga komponenharga")
	List<Map<String,Object>> getAll();	

}
