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

/**
 * Repository class for Ruangan
 * 
 * @author Lukman Hakim
 */
@Repository("PegawaiJadwalKerjaDao")
public interface PegawaiJadwalKerjaDao extends PagingAndSortingRepository<PegawaiJadwalKerja, Integer> {
	// custom query
	@Query("select model from PegawaiJadwalKerja model where model.ruangan.id=:ruangan and model.tanggal.bulanKeDlmTahun=:bulan and model.tanggal.tahunKalender = :tahun and model.pegawai.id  = :pegawai")
	public List<PegawaiJadwalKerja> findJadwalByTanggalRuangan(@Param("ruangan") Integer ruangan,
			@Param("tahun") Integer tahun, @Param("bulan") Integer bulan, @Param("pegawai") Integer pegawai);

	@Query("select model from PegawaiJadwalKerja model "
			+ "where cast(model.tanggal.bulanKeDlmTahun as integer) = :bulan "
			+ "and cast(model.tanggal.tahunKalender as integer) = :tahun " + "and model.pegawai.id = :pegawai")
	public List<PegawaiJadwalKerja> findJadwalByBulan(@Param("tahun") Integer tahun, @Param("bulan") Integer bulan,
			@Param("pegawai") Integer pegawai);

	@Query("select new map( pegawai.id as idPegawai, " + "pegawai.namaLengkap as namaPegawai, "
			+ "shiftKerja.id as idShiftKerja) " + "from Pegawai pegawai " + "left join pegawai.shiftKerja shiftKerja "
			+ "left join pegawai.ruangan ruangan " + "where ruangan.id = :idRuangan")
	public List<Map<String, Object>> findPegawaiByRuangan(@Param("idRuangan") Integer idRuangan);

	@Query("select new map( pegawai.id as idPegawai, " + "pegawai.namaLengkap as namaPegawai, "
			+ "shiftKerja.id as idShiftKerja) " + "from Pegawai pegawai " + "left join pegawai.shiftKerja shiftKerja "
			+ "left join pegawai.ruangan ruangan " + "left join pegawai.kategoryPegawai kategori "
			+ "left join pegawai.statusPegawai statusPegawai "
			+ "where ruangan.id = :idRuangan and statusPegawai.id = 2 " + "and kategori.id != 11")
	public List<Map<String, Object>> findPegawaiAktifDanKaryawanByRuangan(@Param("idRuangan") Integer idRuangan);

	@Query("select new Map(jadwalKerja.id as id, " + "jadwalKerja.shiftId as shiftId, "
			+ "shift.flagKetidakhadiran as flagKetidakhadiran) " + "from PegawaiJadwalKerja jadwalKerja "
			+ "left join jadwalKerja.pegawai pegawai " + "left join jadwalKerja.tanggal kalender "
			+ "left join jadwalKerja.shift shift " + "where to_char(kalender.tanggal,'yyyy-MM-dd')= :date "
			+ "and pegawai.id = :pegawaiId")
	Map<String, Object> getJadwal(@Param("pegawaiId") Integer pegawaiId, @Param("date") String date);

	@Query("select jadwalKerja.id " + "from PegawaiJadwalKerja jadwalKerja " + "left join jadwalKerja.pegawai pegawai "
			+ "left join jadwalKerja.tanggal kalender " + "left join jadwalKerja.shift shift "
			+ "where kalender.tanggal between :start and :end " + "and pegawai.id = :pegawaiId")
	List<Integer> getListJadwal(@Param("pegawaiId") Integer pegawaiId, @Param("start") Date start,
			@Param("end") Date end);

	@Query("select count(kalender.tanggal) " + "from PegawaiJadwalKerja jadwalKerja "
			+ "left join jadwalKerja.pegawai pegawai " + "left join jadwalKerja.tanggal kalender "
			+ "left join jadwalKerja.shift shift "
			+ "where to_char(kalender.tanggal,'yyyy-MM-dd') between :tglAwal and :tglAkhir "
			+ "and shift.namaShift != 'Libur' " + "and pegawai.id = :pegawaiId")
	Integer getJadwalCustom(@Param("pegawaiId") Integer pegawaiId, @Param("tglAwal") String tglAwal,
			@Param("tglAkhir") String tglAkhir);

	@Query("select kalender.tanggal " + "from Kalender kalender " + "where kalender.id= :id")
	Date getTanggal(@Param("id") Integer id);

	@Query("select kalender.id " + "from Kalender kalender "
			+ "where to_char(kalender.tanggal,'yyyy-MM-dd') = :tanggal")
	Integer getIdTgl(@Param("tanggal") String tanggal);

	@Query("select new Map(shiftKerja.id as idShift, shiftKerja.namaShift as namaShift) "
			+ "from PegawaiJadwalKerja jadwalKerja " + "inner join jadwalKerja.shift shiftKerja "
			+ "left join jadwalKerja.tanggal kalender " + "where shiftKerja.statusEnabled is true "
			+ "and to_char(kalender.tanggal, 'yyyy-MM-dd') between :tgl1 and :tgl2 "
			+ "and jadwalKerja.pegawaiId = :idPegawai " + "order by to_char(kalender.tanggal, 'yyyy-MM-dd')")
	List<Map<String, Object>> getNamaShiftKerjaPegawaiByTanggal(@Param("tgl1") String tgl1, @Param("tgl2") String tgl2,
			@Param("idPegawai") Integer idPegawai);

	@Query("select new Map(sk.id as idKode,sk.kodeExternal as kodeShift,"
			+ "to_char(kl.tanggal, 'yyyy-MM-dd') as tanggal) " + "from PegawaiJadwalKerja jk "
			+ "inner join jk.tanggal kl " + "inner join jk.shift sk " + "where jk.pegawaiId = :pegawaiId "
			+ "and kl.tanggal between :tglAwal and :tglAkhir " + "order by kl.tanggal")
	List<Map<String, Object>> getKuantitasHariKerja(@Param("pegawaiId") Integer idPegawai,
			@Param("tglAwal") Date tglAwal, @Param("tglAkhir") Date tglAkhir);
}
