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

@Repository("BobotJenisIndikatorDao")
public interface BobotJenisIndikatorDao extends PagingAndSortingRepository<BobotJenisIndikator, String> {

	String strQryGrid = "select new Map(bj.noRec as noRec," + "bj.bulan as bulanBerlaku,"
			+ "to_char(bj.bulan,'MM-yyyy') as bulanBerlakuFormatted," + "bj.jenisIndikator as jenisIndikatorId,"
			+ "(case when bj.jenisIndikator = 1 then 'Kuantitas' " + "when bj.jenisIndikator = 2 then 'Kualitas' "
			+ "when bj.jenisIndikator = 3 then 'Perilaku' end) as jenisIndikator,"
			+ "kj.id as kelompokJabatanId,kj.namaKelompokJabatan as kelompokJabatan," + "bj.persentase as persentase,"
			+ "bj.tanggalPembaharuanData as tglPembaharuanData) " + "from BobotJenisIndikator bj "
			+ "left join bj.kelompokJabatan kj " + "where bj.statusEnabled is true ";

	String cdtBulan = "and to_char(bj.bulan,'yyyy-MM') <= :periode ";

	String cdtKelompokJabatanNull = "and bj.kelompokJabatanId is null ";

	String cdtKelompokJabatan = "and kj.id = :kelompokJabatanId ";

	String cdtTglPembaharuanData = "and to_char(bj.tanggalPembaharuanData,'yyyy-mm-dd hh24:mi:ss') = :tglPembaharuanData ";

	String sortGrid = "order by bj.bulan desc, kj.namaKelompokJabatan, bj.jenisIndikator";

	@Query(strQryGrid + cdtBulan + cdtKelompokJabatanNull + sortGrid)
	public List<Map<String, Object>> findBobotJenisByBulanBerlaku(@Param("periode") String periode);

	@Query(strQryGrid + cdtBulan + cdtKelompokJabatan + sortGrid)
	public List<Map<String, Object>> findBobotJenisByBulanBerlaku(@Param("periode") String periode,
			@Param("kelompokJabatanId") Integer idKelompokJabatan);

	@Query(strQryGrid + cdtTglPembaharuanData + sortGrid)
	public List<Map<String, Object>> findBobotJenisByTglPembaharuanData(
			@Param("tglPembaharuanData") String tglPembaharuanData);

	@Query("select distinct bj.bulan " + "from BobotJenisIndikator bj, Jabatan jb "
			+ "where bj.kelompokJabatanId = jb.kelompokJabatanId " + "and bj.statusEnabled is true "
			+ "and to_char(bj.bulan,'yyyy-MM') <= :periode " + "and jb.id = :jabatanId " + "order by bj.bulan desc")
	public List<Date> findBulanBerlaku(@Param("periode") String periode, @Param("jabatanId") Integer idJabatan);

	@Query("select distinct bj.bulan " + "from BobotJenisIndikator bj " + "where bj.statusEnabled is true "
			+ "and to_char(bj.bulan,'yyyy-MM') <= :periode " + "and bj.kelompokJabatanId is null "
			+ "order by bj.bulan desc")
	public List<Date> findBulanBerlaku(@Param("periode") String periode);

	@Query("select distinct new Map(bj.jenisIndikator as id," + "(case when bj.jenisIndikator = 1 then 'Kuantitas' "
			+ "when bj.jenisIndikator = 2 then 'Kualitas' "
			+ "when bj.jenisIndikator = 3 then 'Perilaku' end) as jenisIndikator," + "bj.persentase as persentase) "
			+ "from BobotJenisIndikator bj, Jabatan jb " + "where bj.kelompokJabatanId = jb.kelompokJabatanId "
			+ "and bj.statusEnabled is true " + "and bj.bulan = :periode " + "and jb.id = :jabatanId "
			+ "order by bj.jenisIndikator")
	public List<Map<String, Object>> findBobotJenisKontrak(@Param("periode") Date periode,
			@Param("jabatanId") Integer idJabatan);

	@Query("select distinct new Map(bj.jenisIndikator as id," + "(case when bj.jenisIndikator = 1 then 'Kuantitas' "
			+ "when bj.jenisIndikator = 2 then 'Kualitas' "
			+ "when bj.jenisIndikator = 3 then 'Perilaku' end) as jenisIndikator," + "bj.persentase as persentase) "
			+ "from BobotJenisIndikator bj " + "where bj.statusEnabled is true " + "and bj.bulan = :periode "
			+ "and bj.kelompokJabatanId is null " + "order by bj.jenisIndikator")
	public List<Map<String, Object>> findBobotJenisKontrak(@Param("periode") Date periode);

	@Query("select new Map(bj.noRec as noRec,bj.jenisIndikator as id,bj.persentase as persentase) "
			+ "from BobotJenisIndikator bj " + "where bj.statusEnabled is true "
			+ "and to_char(bj.bulan,'yyyy-MM-dd') = :periode " + "and bj.jenisIndikator = :jenisIndikator "
			+ "and bj.kelompokJabatanId = :kelompokJabatanId")
	public List<Map<String, Object>> findBobotJenis(@Param("periode") String periode,
			@Param("jenisIndikator") Integer jenisIndikator, @Param("kelompokJabatanId") Integer idKelompokJabatan);

	@Query("select new Map(bj.noRec as noRec,bj.jenisIndikator as id,bj.persentase as persentase) "
			+ "from BobotJenisIndikator bj " + "where bj.statusEnabled is true "
			+ "and to_char(bj.bulan,'yyyy-MM-dd') = :periode " + "and bj.jenisIndikator = :jenisIndikator "
			+ "and bj.kelompokJabatanId is null")
	public List<Map<String, Object>> findBobotJenis(@Param("periode") String periode,
			@Param("jenisIndikator") Integer jenisIndikator);

	@Query("select bj.noRec from BobotJenisIndikator bj "
			+ "where to_char(bj.tanggalPembaharuanData,'yyyy-mm-dd hh24:mi:ss') = :tglPembaharuanData")
	public List<String> findNoRecs(@Param("tglPembaharuanData") String tglPembaharuanData);

}
