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.UsulanEvaluasi;
import com.jasamedika.medifirst2000.entities.UsulanEvaluasiKomponen;

@Repository("UsulanEvaluasiDao")
public interface UsulanEvaluasiDao extends PagingAndSortingRepository<UsulanEvaluasi, String> {
	@Query("select count(a.noUsulan) from "
			+ " UsulanEvaluasi a where a.noUsulan like %:kode%")
	Long countNoEvaluasi(@Param("kode") String kode);
	
	/* Evaluasi Rekanan*/
	@Query("select new Map(usulanEvaluasi.noRec as noRec ,ruanganTujuan.namaRuangan as namaRuangan, "
			+ " komponenEvaluasi.komponenEvaluasi as komponenEvaluasi, "
			+ " model.rekomendasi as rekomendasi , model.kajian as hasilEvaluasi )  "
			+ "from UsulanEvaluasiKomponen model "
			+ " left join model.usulanEvaluasi usulanEvaluasi "
			+ " left join model.ruanganTujuan ruanganTujuan " 
			+ " left join model.komponenEvaluasi as komponenEvaluasi"
			+ " where ruanganTujuan.id = :ruanganId "
			+ " and usulanEvaluasi.tglUsulan between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')"
			+ " and usulanEvaluasi.noUsulan like %:noUsulan% ")
	List<Map<String,Object>> listUsulanEvaluasiByRuanganTujuan(@Param("tglAwal") String tglAwal,@Param("tglAkhir") String tglAkhir,@Param("noUsulan") String noUsulan,
			@Param("ruanganId")Integer ruanganId);
	
	@Query("select new Map(usulanEvaluasi.noRec as noRec ,ruanganTujuan.namaRuangan as namaRuangan, "
			+ " komponenEvaluasi.komponenEvaluasi as komponenEvaluasi, "
			+ " model.rekomendasi as rekomendasi , model.kajian as hasilEvaluasi ) "
			+ "from UsulanEvaluasiKomponen model "
			+ " left join model.usulanEvaluasi usulanEvaluasi "
			+ " left join model.komponenEvaluasi as komponenEvaluasi "
			+ " left join model.ruanganTujuan ruanganTujuan "
			+ " where ruanganTujuan.id = :ruanganId "
			+ " and usulanEvaluasi.tglUsulan between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')"
			+ "   ")
	List<Map<String,Object>> listUsulanEvaluasiByRuanganTujuan(@Param("tglAwal") String tglAwal,@Param("tglAkhir") String tglAkhir,
			@Param("ruanganId")Integer ruanganId);
	
	@Query("select new Map(model.noRec as noRec, model.noUsulan as noUsulan , rekanan.namaRekanan as namaRekanan,model.namaUsulan as namaUsulan, model.tglUsulan  as tglUsulan) "
			+ " from UsulanEvaluasi model "
			+ " left join model.jenisEvaluasi jenisEvaluasi"
			+ " left join model.ruanganPembuat ruanganPembuat "
			+ " left join model.rekanan rekanan "
			+ " where ruanganPembuat.id = :ruanganId"
			+ " and jenisEvaluasi.id = :idJenisEvaluasi "
			+ " and model.tglUsulan between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')"
			+ "  ")
	List<Map<String,Object>> listUsulanEvaluasiByRuanganPembuat(@Param("tglAwal") String tglAwal,@Param("tglAkhir") String tglAkhir,
			@Param("ruanganId")Integer ruanganId,@Param("idJenisEvaluasi") Integer idJenisEvaluasi);
	
	@Query("select new Map(ruanganTujuan.namaRuangan as namaRuangan, "
			+ " komponenEvaluasi.komponenEvaluasi as komponenEvaluasi, "
			+ " model.rekomendasi as rekomendasi , model.kajian as hasilEvaluasi ,model.usulan as usulan ,"
			+ " pegawaiRekomendasi.namaLengkap as namaLengkap) "
			+ "from UsulanEvaluasiKomponen model "
			+ " left join model.usulanEvaluasi usulanEvaluasi "
			+ " left join model.komponenEvaluasi as komponenEvaluasi "
			+ " left join model.ruanganTujuan ruanganTujuan "
			+ " left join model.pegawaiRekomendasi pegawaiRekomendasi "
			+ "  where usulanEvaluasi.noRec =:noRec ")
	List<Map<String,Object>> listUsulanEvaluasiKomponenByNoRecEvaluasi(@Param("noRec") String noRec);
	
	@Query("select new Map(model.noRec as noRec, model.noUsulan as noUsulan , rekanan.namaRekanan as namaRekanan, model.tglUsulan  as tglUsulan) "
			+ " from UsulanEvaluasi model "
			+ " left join model.ruanganPembuat ruanganPembuat "
			+ " left join model.rekanan rekanan"
			+ " where model.noRec = :noRec "
			+ "  ")
	Map<String,Object> getUsulanEvaluasiByNoRec(@Param("noRec") String noRec);
	
	@Query("select new Map(model.noRec as noRec, model.noUsulan as noUsulan  , model.namaUsulan as namaUsulan,"
			+ "rekanan.namaRekanan as namaRekanan, model.tglUsulan  as tglUsulan)"
			+ " from UsulanEvaluasi model "
			+ " left join model.rekanan rekanan"
			+ " left join model.jenisEvaluasi jenisEvaluasi"
			+ " left join model.ruanganPembuat ruanganPembuat "
			+ " where ruanganPembuat.id = :ruanganId "
			+ " and jenisEvaluasi.id = :idJenisEvaluasi "
			+ " and model.tglUsulan between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')"
			+ " and model.noUsulan like %:noUsulan% ")
	List<Map<String,Object>> listUsulanEvaluasiByRuanganPembuat(@Param("tglAwal") String tglAwal,@Param("tglAkhir") String tglAkhir,@Param("noUsulan") String noUsulan,
			@Param("ruanganId")Integer ruanganId,@Param("idJenisEvaluasi") Integer idJenisEvaluasi);
	
	@Query("select new Map(model.noRec as noRec,rekanan.namaRekanan as namaRekanan, "
			+ " model.noUsulan as noUsulan, model.tglUsulan as tglUsulan,model.namaUsulan as namaUsulan,"
			+ " ruanganPembuat.id as idRuanganPembuat, ruanganPembuat.namaRuangan as namaRuanganPembuat )"
			+ " from UsulanEvaluasi model "
			+ " left join model.rekanan rekanan"
			+ " left join model.ruanganPembuat ruanganPembuat" 			 
			+ " where  "
			+ " model.noUsulan = :noUsulan ")
	Map<String,Object> getUsulanEvaluasiByNoPlanning(@Param("noUsulan") String noUsulan);
	
	@Query("select new Map(model.noRec as noRec, "
			+ " ruanganTujuan.id as idRuanganTujuan , ruanganTujuan.namaRuangan as namaRuanganTujuan,"
			+ "  komponenEvaluasi.komponenEvaluasi as komponenEvaluasi, "
			+ " model.rekomendasi as rekomendasi , model.kajian as kajian ,model.usulan as usulan) "
			+ " from UsulanEvaluasiKomponen model "
			+ " left join model.usulanEvaluasi uek "
			+ " left join model.ruanganTujuan ruanganTujuan"
			//+ " left join model.kelompokEvaluasi kelompokEvaluasi"
			+ " left join model.komponenEvaluasi komponenEvaluasi"
			+ " where "
			+ "  uek.noRec = :noUsulan ")
	List<Map<String,Object>> listUsulanEvaluasiByNoPlanning(@Param("noUsulan") String noUsulan);
	
	@Query("select model from UsulanEvaluasiKomponen model "
			+ " left join model.usulanEvaluasi usulanEvaluasi "
			+ " left join model.ruanganTujuan ruanganTujuan "
			+ " where ruanganTujuan.id = :ruanganId "
			+ " and usulanEvaluasi.noUsulan like %:noUsulan% ")
	List<UsulanEvaluasiKomponen> listUsulanEvaluasi(@Param("noUsulan") String noUsulan, @Param("ruanganId")Integer ruanganId);

	/* End Evaluasi Rekanan*/
	
	/**** Usulan Evaluasi  ***/
	@Query("select new Map(usulanEvaluasi.noRec as noRec ,ruanganTujuan.namaRuangan as namaRuangan, "
			+ " komponenEvaluasi.komponenEvaluasi as komponenEvaluasi, "
			+ " model.rekomendasi as rekomendasi , model.kajian as kajian , model.usulan as usulan )  "
			+ "from UsulanEvaluasiKomponen model "
			+ " left join model.usulanEvaluasi usulanEvaluasi "
			+ " left join model.pegawaiRekomendasi pegawaiRekomendasi " 
			+ " left join model.komponenEvaluasi as komponenEvaluasi "
			+ " left join pegawaiRekomendasi.ruangan ruanganTujuan"
			+ " where pegawaiRekomendasi.id = :idPegawai "
			+ " and usulanEvaluasi.tglUsulan between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')"
			+ " and usulanEvaluasi.noUsulan like %:noUsulan% ")
	List<Map<String,Object>> getUsulanEvaluasiByRuanganTujuan(@Param("tglAwal") String tglAwal,@Param("tglAkhir") String tglAkhir,@Param("noUsulan") String noUsulan,
			@Param("idPegawai")Integer idPegawai);
	
	@Query("select new Map(usulanEvaluasi.noRec as noRec ,ruanganTujuan.namaRuangan as namaRuangan, "
			+ " komponenEvaluasi.komponenEvaluasi as komponenEvaluasi, "
			+ " model.rekomendasi as rekomendasi , model.kajian as kajian , model.usulan as usulan) "
			+ "from UsulanEvaluasiKomponen model "
			+ " left join model.usulanEvaluasi usulanEvaluasi "
			+ " left join model.komponenEvaluasi as komponenEvaluasi "
			+ " left join model.pegawaiRekomendasi pegawaiRekomendasi " 
			+ " left join pegawaiRekomendasi.ruangan ruanganTujuan"
			+ " where pegawaiRekomendasi.id = :idPegawai "
			+ " and usulanEvaluasi.tglUsulan between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')"
			+ "   ")
	List<Map<String,Object>> getUsulanEvaluasiByRuanganTujuan(@Param("tglAwal") String tglAwal,@Param("tglAkhir") String tglAkhir,
			@Param("idPegawai")Integer idPegawai);
}
