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

@Repository("JatahCutiDanIzinDao")
public interface JatahCutiDanIzinDao extends PagingAndSortingRepository<JatahCutiDanIzin, String> {
	@Query("select jatahCuti " + "from JatahCutiDanIzin jatahCuti " + "left join jatahCuti.pegawai pegawai "
			+ "left join jatahCuti.komponenIndex komponenIndex "
			+ "where jatahCuti.tahun = :tahun and pegawai.id = :idPegawai " + "and komponenIndex.id = :komponenIndexId")
	public JatahCutiDanIzin getJatahCuti(@Param("tahun") String tahun, @Param("idPegawai") Integer idPegawai,
			@Param("komponenIndexId") Integer komponenIndexId);

	@Query("select jatahCuti " + "from JatahCutiDanIzin jatahCuti " + "left join jatahCuti.pegawai pegawai "
			+ "left join jatahCuti.komponenIndex komponenIndex "
			+ "where jatahCuti.tahun = :tahun and pegawai.id = :idPegawai "
			+ "and komponenIndex.id in (:komponenIndexId)")
	public JatahCutiDanIzin getJatahCuti(@Param("tahun") String tahun, @Param("idPegawai") Integer idPegawai,
			@Param("komponenIndexId") List<Integer> komponenIndexId);

	@Query("select coalesce(jatahCuti.value,0)  " + "from JatahCutiDanIzin jatahCuti "
			+ "left join jatahCuti.pegawai pegawai " + "left join jatahCuti.komponenIndex komponenIndex "
			+ "where jatahCuti.tahun = :tahun and pegawai.id = :idPegawai " + "and komponenIndex.id = :komponenIndexId")
	public Integer getDataCuti(@Param("tahun") String tahun, @Param("idPegawai") Integer idPegawai,
			@Param("komponenIndexId") Integer komponenIndexId);

	@Query("select jatahCuti.isTangguhkan " + "from JatahCutiDanIzin jatahCuti "
			+ "left join jatahCuti.pegawai pegawai " + "left join jatahCuti.komponenIndex komponenIndex "
			+ "where jatahCuti.tahun = :tahun and pegawai.id = :idPegawai " + "and komponenIndex.id = :komponenIndexId")
	public Boolean getPenangguhanSisaCuti(@Param("tahun") String tahun, @Param("idPegawai") Integer idPegawai,
			@Param("komponenIndexId") Integer komponenIndexId);

	@Query("select new map(" + "jatahCuti.value as value, " + "jatahCuti.sisaCuti as sisaCuti, "
			+ "jatahCuti.cutiTerpakai as cutiTerpakai, " + "jatahCuti.tahun as tahun, "
			+ "jatahCuti.isTangguhkan as isTangguhkan, " + "pegawai.id as idPegawai, "
			+ "pegawai.namaLengkap as namaPegawai, " + "pegawai.nipPns as nipPegawai, " + "unitKerja.name as unitKerja,"
			+ "subUnitKerja.name as subUnitKerja)  "
			+ "from JatahCutiDanIzin jatahCuti, MapPegawaiJabatanToUnitKerja mapPegawai "
			+ "left join jatahCuti.pegawai pegawai " + "left join jatahCuti.komponenIndex komponenIndex "
			+ "left join mapPegawai.subUnitKerjaPegawai subUnitKerja "
			+ "left join mapPegawai.unitKerjaPegawai unitKerja " + "where mapPegawai.pegawaiId = pegawai.id "
			+ "and mapPegawai.isPrimary is true " + "and mapPegawai.statusEnabled is true "
			+ "and komponenIndex.id = :komponenIndexId " + "order by pegawai.id, jatahCuti.tahun")
	public List<Map<String, Object>> getAllJatahCuti(@Param("komponenIndexId") Integer komponenIndexId);

	@Query("select new map(" + "jatahCuti.noRec as noRec, " + "jatahCuti.komponenIndexId as komponenIndexId, "
			+ "jatahCuti.tahun as tahun, " + "jatahCuti.value as value, " + "jatahCuti.sisaCuti as sisaCuti, "
			+ "jatahCuti.cutiTerpakai as cutiTerpakai, " + "jatahCuti.isTangguhkan as isTangguhkan, "
			+ "pegawai.id as idPegawai, " + "pegawai.namaLengkap as namaPegawai, " + "pegawai.nipPns as nipPegawai, "
			+ "unitKerja.name as unitKerja," + "subUnitKerja.name as subUnitKerja)  "
			+ "from JatahCutiDanIzin jatahCuti, MapPegawaiJabatanToUnitKerja mapPegawai "
			+ "left join jatahCuti.pegawai pegawai " + "left join jatahCuti.komponenIndex komponenIndex "
			+ "left join mapPegawai.subUnitKerjaPegawai subUnitKerja "
			+ "left join mapPegawai.unitKerjaPegawai unitKerja " + "where mapPegawai.pegawaiId = pegawai.id "
			+ "and mapPegawai.isPrimary is true " + "and mapPegawai.statusEnabled is true "
			+ "and pegawai.id = :pegawaiId " + "and komponenIndex.id = :komponenIndexId "
			+ "order by pegawai.id, jatahCuti.tahun")
	public List<Map<String, Object>> getAllJatahCuti(@Param("komponenIndexId") Integer komponenIndexId,
			@Param("pegawaiId") Integer pegawaiId);

	@Query("select new map(" + "jatahCuti.noRec as noRec, " + "jatahCuti.komponenIndexId as komponenIndexId, "
			+ "komponenIndex.komponenIndex as komponenIndex, " + "jatahCuti.tahun as tahun, "
			+ "jatahCuti.value as value, " + "jatahCuti.sisaCuti as sisaCuti, "
			+ "jatahCuti.cutiTerpakai as cutiTerpakai, " + "jatahCuti.isTangguhkan as isTangguhkan, "
			+ "pegawai.id as idPegawai, " + "pegawai.namaLengkap as namaPegawai, " + "pegawai.nipPns as nipPegawai, "
			+ "unitKerja.name as unitKerja," + "subUnitKerja.name as subUnitKerja)  "
			+ "from JatahCutiDanIzin jatahCuti, MapPegawaiJabatanToUnitKerja mapPegawai "
			+ "left join jatahCuti.pegawai pegawai " + "left join jatahCuti.komponenIndex komponenIndex "
			+ "left join mapPegawai.subUnitKerjaPegawai subUnitKerja "
			+ "left join mapPegawai.unitKerjaPegawai unitKerja " + "where mapPegawai.pegawaiId = pegawai.id "
			+ "and mapPegawai.isPrimary is true " + "and mapPegawai.statusEnabled is true "
			+ "and cast(jatahCuti.tahun as int) >= cast(to_char(current_date,'yyyy') as int)-2 "
			+ "and pegawai.id = :pegawaiId " + "and komponenIndex.id in (:komponenIndexId) "
			+ "order by jatahCuti.tahun, komponenIndex.komponenIndex")
	public List<Map<String, Object>> getAllJatahCuti(@Param("komponenIndexId") List<Integer> komponenIndexId,
			@Param("pegawaiId") Integer pegawaiId);

	@Query("select l.tgl " + "from PlanningPegawaiStatus p " + "left join p.listTanggal l "
			+ "where p.statusEnabled is true and l.statusEnabled is true "
			+ "and p.approvalStatus = 1 and l.approvalStatus is true " + "and p.statusPegawaiPlanId = :idStatusPlan "
			+ "and p.pegawaiId = :idPegawai " + "and to_char(l.tgl, 'yyyy-MM-dd') = :tglCuti " + "order by l.tgl")
	public List<Date> getCutiTahunanDiTglCutiBersama(@Param("idStatusPlan") Integer idStatusPlan,
			@Param("idPegawai") Integer idPegawai, @Param("tglCuti") String tglCuti);

	@Query("select sum(jc.cutiTerpakai) from JatahCutiDanIzin jc "
			+ "where jc.pegawaiId = :pegawaiId and jc.tahun < :tahun")
	public Integer getHistoriCutiTerpakai(@Param("pegawaiId") Integer idPegawai, @Param("tahun") String tahun);

}
