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.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.entities.IndeksKinerjaDokter;

/**
 * @author salmanoe
 * @since Nov 29, 2021
 */
@Repository("indeksKinerjaDokterDao")
public interface IndeksKinerjaDokterDao extends PagingAndSortingRepository<IndeksKinerjaDokter, String> {

	@Query("select new Map(ikid.noRec as noRec," + "ikid.bulan as bulan," + "ikid.unitKerjaId as unitKerjaId,"
			+ "ikid.subunitKerjaId as subunitKerjaId," + "ikid.pegawaiId as pegawaiId," + "ikid.iki as iki,"
			+ "ikid.skor as skor," + "ikid.skorDasar as skorDasar) " + "from IndeksKinerjaDokter ikid "
			+ "where ikid.statusEnabled is true "
			+ "and to_char(ikid.bulan,'yyyy-MM') between :bulanAwal and :bulanAkhir")
	List<Map<String, Object>> findAll(@Param("bulanAwal") String bulanAwal, @Param("bulanAkhir") String bulanAkhir)
			throws JpaSystemException;

	@Query("select new Map(ikid.unitKerjaId as unitKerjaId," + "ikid.subunitKerjaId as subunitKerjaId,"
			+ "coalesce(sum(ikid.skorDasar),0) as totalSkorDasar) " + "from IndeksKinerjaDokter ikid "
			+ "where ikid.statusEnabled is true "
			+ "and to_char(ikid.bulan,'yyyy-MM') between :bulanAwal and :bulanAkhir "
			+ "group by ikid.unitKerjaId, ikid.subunitKerjaId " + "order by ikid.unitKerjaId, ikid.subunitKerjaId")
	List<Map<String, Object>> findTotalSkorByKelompokKerja(@Param("bulanAwal") String bulanAwal,
			@Param("bulanAkhir") String bulanAkhir) throws JpaSystemException;

}
