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

@Repository("targetLayananDao")
public interface TargetLayananDao extends PagingAndSortingRepository<TargetLayanan, String> {

	@Query("select new Map(sk.id as kelompokKerjaId," + "ikd.id as indikatorId," + "tl.target as target,"
			+ "to_char(tl.periode,'yyyy-MM') as periode," + "tl.tglHitung as tglHitung) " + "from TargetLayanan tl "
			+ "inner join tl.subUnitKerjaPegawai sk " + "inner join tl.indikatorDetail ikd "
			+ "inner join ikd.indikatorKinerja ikm " + "where sk.statusEnabled is true "
			+ "and ikd.statusEnabled is true " + "and ikm.statusEnabled is true and ikm.statusVerifikasi is true "
			+ "and to_char(tl.periode,'yyyy-MM') between :tglAwal and :tglAkhir " + "and ikd.id in (:listIndikatorId) "
			+ "order by sk.name, tl.tglHitung desc")
	public List<Map<String, Object>> getLastTargetLayananJamKerja(@Param("tglAwal") String tglAwal,
			@Param("tglAkhir") String tglAkhir, @Param("listIndikatorId") List<Integer> listIdIndikator);

	@Query("select distinct new Map(tl.subUnitKerjaPegawaiId as kelompokKerjaId,"
			+ "tl.indikatorDetailId as indikatorId," + "pg.id as pegawaiId,"
			+ "tl.target as target,tl.capaian as capaian,tl.tglHitung as tglHitung) "
			+ "from TargetLayanan tl, MapPegawaiJabatanToUnitKerja mj " + "inner join mj.pegawai pg "
			+ "where tl.subUnitKerjaPegawaiId = mj.subUnitKerjaPegawaiId " + "and tl.statusEnabled is true "
			+ "and mj.statusEnabled is true " + "and pg.statusEnabled is true "
			+ "and pg.kategoryPegawaiId in (:listKategoryId) " + "and pg.jenisPegawaiId = :jenisPegawaiId "
			+ "and to_char(tl.periode,'yyyy-MM') = :bulan "
			+ "order by tl.subUnitKerjaPegawaiId, tl.indikatorDetailId, tl.tglHitung desc, pg.namaLengkap")
	public List<Map<String, Object>> findDataPembagianTarget(@Param("listKategoryId") List<Integer> listIdKategori,
			@Param("jenisPegawaiId") Integer idJenisPegawai, @Param("bulan") String bulan);

	@Query("select tl from TargetLayanan tl " + "where tl.statusEnabled is true "
			+ "and to_char(tl.periode,'yyyy-MM') between :tglAwal and :tglAkhir "
			+ "order by tl.subUnitKerjaPegawaiId, indikatorDetailId, tl.periode, tl.tglHitung desc")
	public List<TargetLayanan> findAllTargetLayanan(@Param("tglAwal") String tglAwal,
			@Param("tglAkhir") String tglAkhir);

	@Query("select tl from TargetLayanan tl " + "where tl.statusEnabled is true "
			+ "and to_char(tl.periode,'yyyy-MM') between :tglAwal and :tglAkhir "
			+ "and tl.subUnitKerjaPegawaiId = :kelompokKerjaId "
			+ "order by tl.subUnitKerjaPegawaiId, tl.indikatorDetailId, tl.periode, tl.tglHitung desc")
	public List<TargetLayanan> findAllTargetLayanan(@Param("tglAwal") String tglAwal,
			@Param("tglAkhir") String tglAkhir, @Param("kelompokKerjaId") Integer idKelompokKerja);

}
