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

@Repository("anggaranRemunerasiDao")
public interface AnggaranRemunerasiDao extends PagingAndSortingRepository<AnggaranRemunerasi, String> {

	@Query("select distinct new Map(" + "ar.anggaranRemunTahun as anggaranTahun,"
			+ "ar.anggaranRemunBulan as anggaranBulan," + "ar.poinIndeksRupiah as pir,"
			+ "ar.totalNilaiJabatan as totNilaiJabatan," + "ar.tanggalPembaharuanData as tglPembaharuanData) "
			+ "from AnggaranRemunerasi ar " + "where ar.statusEnabled is true "
			+ "and to_char(ar.tahun,'yyyy') = to_char(now(),'yyyy') " + "order by ar.tanggalPembaharuanData desc")
	List<Map<String, Object>> findTahunIni();

	String strQryAnggaran = "select new Map(pg.id as pegawaiId,pg.namaLengkap as namaLengkap,"
			+ "mj.jabatanId as jabatanId," + "ar.anggaranRemunTahun as anggaranTahun,"
			+ "ar.anggaranRemunBulan as anggaranBulan," + "ar.poinIndeksRupiah as pir,"
			+ "ar.gajiHonorarium as plafonGaji," + "ar.maxInsentif as plafonInsentif,"
			+ "ar.tanggalPembaharuanData as tglPembaharuanData) "
			+ "from AnggaranRemunerasi ar, MapPegawaiJabatanToUnitKerja mj " + "inner join ar.grade gr "
			+ "inner join mj.pegawai pg " + "inner join mj.grade nj " + "where nj.gradeId = gr.id "
			+ "and ar.statusEnabled is true " + "and mj.statusEnabled is true " + "and pg.statusEnabled is true "
			+ "and gr.statusEnabled is true " + "and nj.statusEnabled is true "
			+ "and to_char(ar.tahun,'yyyy') = :tahun ";

	String cdtPegawai = "and mj.pegawaiId = :pegawaiId ";

	String cdtJabatan = "and mj.jabatanId = :jabatanId ";

	String sortDateUpdate = "order by ar.tanggalPembaharuanData desc";

	String sortPegawai = "order by pg.namaLengkap asc, ar.tanggalPembaharuanData desc";

	@Query(strQryAnggaran + cdtPegawai + cdtJabatan + sortDateUpdate)
	List<Map<String, Object>> findAnggaran(@Param("pegawaiId") Integer idPegawai, @Param("jabatanId") Integer idJabatan,
			@Param("tahun") String tahun);

	@Query(strQryAnggaran + sortPegawai)
	List<Map<String, Object>> findAnggaran(@Param("tahun") String tahun);

}
