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


@Repository("UraianTugasTransaksiDao")
public interface UraianTugasTransaksiDao extends PagingAndSortingRepository<UraianTugasTransaksi, String>{
	
	@Query(" select new Map(rincianKegiatan.rincianKegiatan as rincianKegiatan,uraianTugasTransaksi.periode as periode,"+
			" coalesce(uraianTugasTransaksi.total,0) as total,uraianTugasTransaksi.noRec as noRec,"+
			" coalesce(uraianTugasTransaksi.hasil,0) as hasil, coalesce(uraianTugasTransaksi.nilai,0) as nilai,"+
			" coalesce(uraianTugasTransaksi.bobot,0) as bobot,rincianKegiatan.satuan as satuan,coalesce(uraianTugasTransaksi.target,0) as target)"+  
			" from UraianTugasTransaksi uraianTugasTransaksi"+
			" left join uraianTugasTransaksi.rincianKegiatan rincianKegiatan"+
			" left join uraianTugasTransaksi.pegawai pegawai"+
			" where uraianTugasTransaksi.periode =:periode and pegawai.id =:idPegawai"+
			" and rincianKegiatan.id =:idRincianKegiatan "+
			" and uraianTugasTransaksi.statusEnabled is true "+
			" and rincianKegiatan.statusEnabled is true "+
			" and pegawai.statusEnabled is true ")
	public Map<String,Object> findUraianTugas(@Param("idPegawai") Integer idPegawai,@Param("idRincianKegiatan") Integer idRincianKegiatan,@Param("periode") String periode);

/*	@Query(" select new Map(uraianTugas.bobot as bobot, uraianTugas.target as target, "+
			" uraianTugas.periode as periode )"+  
			" from UraianTugasM uraianTugas"+
			" left join uraianTugas.jabatan jabatan"+
			" left join uraianTugas.rincianKegiatan rincianKegiatan"+
			" where jabatan.id = :idJabatan and rincianKegiatan.id = :idRincianKegiatan"+
			" and uraianTugas.periode =:periode ")
	public Map<String,Object> findUraianTugasMaster(@Param("idJabatan") Integer idJabatan,@Param("idRincianKegiatan") Integer idRincianKegiatan,@Param("periode") String periode);
*/	
	@Query(" select new Map(uraianTugas.periode as periode, rincianKegiatan.rincianKegiatan as rincianKegiatan,"+
			" rincianKegiatan.satuan as satuan,rincianKegiatan.id as idRincianKegiatan )"+  
			" from UraianTugasM uraianTugas"+
			" left join uraianTugas.jabatan jabatan"+
			" left join uraianTugas.rincianKegiatan rincianKegiatan"+
			" where jabatan.id = :idJabatan"+
			" and uraianTugas.periode =:periode ")
	public List<Map<String,Object>> findAllUraianTugasByIdJabatanAndPeriode(@Param("idJabatan") Integer idJabatan,@Param("periode") String periode);
	
	@Query(" select new Map(uraianTugas.periode as periode, rincianKegiatan.rincianKegiatan as rincianKegiatan,"+
			" rincianKegiatan.satuan as satuan,rincianKegiatan.id as idRincianKegiatan )"+  
			" from UraianTugasM uraianTugas"+
			" left join uraianTugas.pelaksanaanTugas pelaksanaanTugas "+
			" left join uraianTugas.jabatan jabatan"+
			" left join uraianTugas.rincianKegiatan rincianKegiatan"+
			" where jabatan.id = :idJabatan and pelaksanaanTugas.id=1"+
			" and uraianTugas.statusEnabled is true"+
			" and pelaksanaanTugas.statusEnabled is true"+
			" and jabatan.statusEnabled is true"+
			" and rincianKegiatan.statusEnabled is true")
	public List<Map<String,Object>> findAllUraianTugasByIdJabatan(@Param("idJabatan") Integer idJabatan);
	
	
	@Query(" select uraianTugasTransaksi.capaian "+  
			" from DetailUraianTugasTransaksi uraianTugasTransaksi"+
			" left join uraianTugasTransaksi.pegawai pegawai"+
			" left join uraianTugasTransaksi.rincianKegiatan rincianKegiatan"+
			" where pegawai.id = :idPegawai and rincianKegiatan.id=:idRincianKegiatan"+
			" and to_char(uraianTugasTransaksi.tanggal,'yyyy-MM-dd')=:periode ")
	public Double findCapaianByTanggal(@Param("idPegawai") Integer idPegawai,@Param("idRincianKegiatan") Integer idRincianKegiatan,@Param("periode") String periode);
	
	@Query(" select new map(detailUraianTugas.capaian as capaian,rincianKegiatan.id as rincianKegiatanId,rincianKegiatan.satuan as satuan, "+
			" rincianKegiatan.rincianKegiatan as rincianKegiatan, uraianTugasTransaksi.bobot as bobot,uraianTugasTransaksi.target as target) "+  
			" from UraianTugasTransaksi uraianTugasTransaksi"+
			" left join uraianTugasTransaksi.detailUraianTugas detailUraianTugas"+
			" left join detailUraianTugas.pegawai pegawai"+
			" left join detailUraianTugas.rincianKegiatan rincianKegiatan"+
			" where pegawai.id = :idPegawai "+
			" and to_char(detailUraianTugas.tanggal,'yyyy-MM-dd')=:periode "+
			" and uraianTugasTransaksi.bobot is not null and uraianTugasTransaksi.target is not null")
	public List<Map<String, Object>> findUraianTugasByTanggal(@Param("idPegawai") Integer idPegawai,@Param("periode") String periode);
	
	@Query(" select new map(uraianTugasTransaksi.capaian as capaian,rincianKegiatan.id as rincianKegiatanId,"+
			" rincianKegiatan.rincianKegiatan as rincianKegiatan, rincianKegiatan.satuan as satuan, uraianTugasTransaksi.noRec as noRec) "+  
			" from DetailUraianTugasTransaksi uraianTugasTransaksi"+
			" left join uraianTugasTransaksi.pegawai pegawai"+
			" left join uraianTugasTransaksi.rincianKegiatan rincianKegiatan"+
			" where pegawai.id = :idPegawai "+
			" and to_char(uraianTugasTransaksi.tanggal,'yyyy-MM-dd')=:periode and rincianKegiatan.id =:idRincianKegiatan  ")
	public Map<String, Object> findUraianTugasByTanggalAndRincianKegiatan(@Param("idPegawai") Integer idPegawai,@Param("idRincianKegiatan") Integer idRincianKegiatan,@Param("periode") String periode);
	
	@Query(" select new map(rincianKegiatan.id as rincianKegiatanId,rincianKegiatan.satuan as satuan, "+
			" rincianKegiatan.rincianKegiatan as rincianKegiatan, uraianTugasTransaksi.bobot as bobot,uraianTugasTransaksi.target as target) "+  
			" from UraianTugasTransaksi uraianTugasTransaksi"+
			" left join uraianTugasTransaksi.pegawai pegawai"+
			" left join uraianTugasTransaksi.rincianKegiatan rincianKegiatan"+
			" where pegawai.id = :idPegawai "+
			" and uraianTugasTransaksi.periode=:periode "+
			" and uraianTugasTransaksi.bobot is not null and uraianTugasTransaksi.target is not null")
	public List<Map<String, Object>> findUraianTugasByPeriode(@Param("idPegawai") Integer idPegawai,@Param("periode") String periode);
	
	@Query(" select new map(rincianKegiatan.id as rincianKegiatanId,rincianKegiatan.satuan as satuan, "+
			" rincianKegiatan.rincianKegiatan as rincianKegiatan, uraianTugasTransaksi.bobot as bobot,uraianTugasTransaksi.target as target) "+  
			" from UraianTugasTransaksi uraianTugasTransaksi"+
			" left join uraianTugasTransaksi.pegawai pegawai"+
			" left join uraianTugasTransaksi.rincianKegiatan rincianKegiatan"+
			" where pegawai.id = :idPegawai "+
			" and uraianTugasTransaksi.bobot is not null and uraianTugasTransaksi.target is not null")
	public List<Map<String, Object>> findUraianTugas(@Param("idPegawai") Integer idPegawai);
	
	@Query(" select new map(detailUraianTugas.capaian as capaian,rincianKegiatan.id as rincianKegiatanId,rincianKegiatan.satuan as satuan, "+
			" rincianKegiatan.rincianKegiatan as rincianKegiatan, uraianTugasTransaksi.bobot as bobot,uraianTugasTransaksi.target as target) "+  
			" from UraianTugasTransaksi uraianTugasTransaksi"+
			" left join uraianTugasTransaksi.detailUraianTugas detailUraianTugas"+
			" left join detailUraianTugas.pegawai pegawai"+
			" left join detailUraianTugas.rincianKegiatan rincianKegiatan"+
			" where pegawai.id = :idPegawai "+
			" and to_char(detailUraianTugas.tanggal,'yyyy-MM')=:periode and rincianKegiatan.id =:idRincianKegiatan")
	public List<Map<String, Object>> findDetailUraianTugasByPeriode(@Param("idPegawai") Integer idPegawai,@Param("idRincianKegiatan") Integer idRincianKegiatan,@Param("periode") String periode);
	
}
