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

@Repository("ipsrsMaintenanceDao")
public interface IpsrsMaintenanceDao extends PagingAndSortingRepository<IpsrsMaintenance, String>{
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.namaLengkap as namaLengkap) "
			+ "from Pegawai a "
			+ "where a.jenisPegawaiId=10 "
			+ "order by a.namaLengkap")
	List<Map<String, Object>> getTeknisiMaintenance();
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.namaRekanan as namaRekanan) "
			+ "from Rekanan a "
			+ "where a.jenisRekananId=3 "
			+ "order by a.namaRekanan")
	List<Map<String, Object>> getRekananMaintenance();
	
	@Query("select NEW Map ("
			+ "a.noRec as noRekMaintenance, "
			+ "b.noRec as noRekAset, "
			+ "b.noSeri as noSeri, "
			+ "c.noRec as noRecPemeliharaan, "
			+ "c.jadwalPemeliharaan as jadwalPemeliharaan, "
			+ "c.statusPemeliharaan as jumlahKehadiran, "
			+ "d.id as ruanganId, "
			+ "d.namaRuangan as namaRuangan, "
			+ "b1.id as produkId, "
			+ "b1.namaProduk as namaProduk, "
			+ "b2.merkProduk as merkProduk, "
			+ "b3.typeProduk as typeProduk) "
			+ "from IpsrsMaintenance a "
			+ "left join a.registrasiAset b "
			+ "left join a.ipsrsPemeliharaan c "
			+ "left join a.ruangan d "
			+ "left join b.produk b1 "
			+ "left join b.merkProduk b2 "
			+ "left join b.typeProduk b3 "
			+ "where c.jadwalPemeliharaan is not null")
	List<Map<String, Object>> getJadwalPemeliharaan();
	
	@Query("select NEW Map ("
			+ "b.id as teknisiId, "
			+ "b.namaLengkap as namaLengkap) "
			+ "from IpsrsTeknisiPemeliharaan a "
			+ "left join a.teknisi b "
			+ "where a.ipsrsPemeliharaanId=:noRec")
	List<Map<String, Object>> getPendampingPemeliharaan(
			@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRekMaintenance, "
			+ "b.noRec as noRekAset, "
			+ "b.noSeri as noSeri, "
			+ "c.noRec as noRecKontakService, "
			+ "c.jadwalKontakService as jadwalKontakService, "
			+ "c.statusKontakService as jumlahKehadiran, "
			+ "d.id as ruanganId, "
			+ "d.namaRuangan as namaRuangan, "
			+ "b1.id as produkId, "
			+ "b1.namaProduk as namaProduk, "
			+ "b2.merkProduk as merkProduk, "
			+ "b3.typeProduk as typeProduk,"
			+ "c1.id as rekananId, "
			+ "c1.namaRekanan as namaRekanan) "
			+ "from IpsrsMaintenance a "
			+ "left join a.registrasiAset b "
			+ "left join a.ipsrsKontakService c "
			+ "left join a.ruangan d "
			+ "left join b.produk b1 "
			+ "left join b.merkProduk b2 "
			+ "left join b.typeProduk b3 "
			+ "left join c.rekanan c1 "
			+ "where c.jadwalKontakService is not null")
	List<Map<String, Object>> getJadwalKontakService();
	
	@Query("select NEW Map ("
			+ "b.id as teknisiId, "
			+ "b.namaLengkap as namaLengkap) "
			+ "from IpsrsTeknisiKontakService a "
			+ "left join a.teknisi b "
			+ "where a.ipsrsKontakServiceId=:noRec")
	List<Map<String, Object>> getPendampingKontakService(
			@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRekMaintenance, "
			+ "b.noRec as noRekAset, "
			+ "b.noSeri as noSeri, "
			+ "c.noRec as noRecKalibrasi, "
			+ "c.jadwalKalibrasi as jadwalKalibrasi, "
			+ "c.statusKalibrasi as jumlahKehadiran, "
			+ "d.id as ruanganId, "
			+ "d.namaRuangan as namaRuangan, "
			+ "b1.id as produkId, "
			+ "b1.namaProduk as namaProduk, "
			+ "b2.merkProduk as merkProduk, "
			+ "b3.typeProduk as typeProduk, "
			+ "c1.id as rekananId, "
			+ "c1.namaRekanan as namaRekanan) "
			+ "from IpsrsMaintenance a "
			+ "left join a.registrasiAset b "
			+ "left join a.ipsrsKalibrasi c "
			+ "left join a.ruangan d "
			+ "left join b.produk b1 "
			+ "left join b.merkProduk b2 "
			+ "left join b.typeProduk b3 "
			+ "left join c.rekanan c1 "
			+ "where c.jadwalKalibrasi is not null")
	List<Map<String, Object>> getJadwalKalibrasi();
	
	@Query("select NEW Map ("
			+ "b.id as teknisiId, "
			+ "b.namaLengkap as namaLengkap) "
			+ "from IpsrsTeknisiKalibrasi a "
			+ "left join a.teknisi b "
			+ "where a.ipsrsKalibrasiId=:noRec")
	List<Map<String, Object>> getPendampingKalibrasi(
			@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "b.noSeri as noSeri, "
			+ "c.id as ruanganId, "
			+ "d.id as produkId, "
			+ "d.namaProduk as namaProduk, "
			+ "e.merkProduk as merkProduk, "
			+ "f.typeProduk as typeProduk) "
			+ "from IpsrsMaintenance a "
			+ "left join a.registrasiAset b "
			+ "left join b.ruangan c "
			+ "left join b.produk d "
			+ "left join b.merkProduk e "
			+ "left join b.typeProduk f")
	List<Map<String, Object>> getAllAsetMaintenance();
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "d.jadwalPemeliharaan as jadwalPemeliharaan, "
			+ "f.namaProduk as namaProduk, "
			+ "b.noSeri as noSeri, "
			+ "c.namaRuangan as namaRuangan) "
			+ "from IpsrsMaintenance a "
			+ "left join a.registrasiAset b "
			+ "left join a.ruangan c "
			+ "left join a.ipsrsPemeliharaan d "
			+ "left join b.produk f where "
			+ "d.alertPemeliharaan=Date(:alertPemeliharaan)")
	List<Map<String, Object>> getAlertPemeliharaan(
			@Param("alertPemeliharaan") String alertPemeliharaan);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "d.jenisPeriode as jenisPeriode, "
			+ "d.jadwalKontakService as jadwalKontakService, "
			+ "f.namaProduk as namaProduk, "
			+ "b.noSeri as noSeri, "
			+ "c.namaRuangan as namaRuangan) "
			+ "from IpsrsMaintenance a "
			+ "left join a.registrasiAset b "
			+ "left join a.ruangan c "
			+ "left join a.ipsrsKontakService d "
			+ "left join b.produk f where "
			+ "d.alertKontakService=:dayName or "
			+ "d.alertKontakService=:dateNumber or "
			+ "d.alertKontakService=:dateFullFormat")
	List<Map<String, Object>> getAlertKontakService(
			@Param("dayName") String dayName, 
			@Param("dateNumber") String dateNumber, 
			@Param("dateFullFormat") String dateFullFormat);
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "d.jadwalKalibrasi as jadwalKalibrasi, "
			+ "f.namaProduk as namaProduk, "
			+ "b.noSeri as noSeri, "
			+ "c.namaRuangan as namaRuangan) "
			+ "from IpsrsMaintenance a "
			+ "left join a.registrasiAset b "
			+ "left join a.ruangan c "
			+ "left join a.ipsrsKalibrasi d "
			+ "left join b.produk f where "
			+ "d.alertKalibrasi=Date(:alertKalibrasi)")
	List<Map<String, Object>> getAlertKalibrasi(
			@Param("alertKalibrasi") String alertKalibrasi);

	
	
}
