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.PlanningPelayanan;
import com.jasamedika.medifirst2000.entities.StrukPlanning;

@Repository("strukPlanningDao")
public interface StrukPlanningDao extends PagingAndSortingRepository<StrukPlanning, String>{

	@Query("select strukPlaning from PlanningPelayanan p left join p.strukPlaning strukPlaning left join p.strukOrder strukOrder where strukOrder.noRec=:noRecStrukOrder and strukPlaning.noRec is not null")
	List<StrukPlanning> findByNoRecStrukOrder(@Param("noRecStrukOrder") String noRecStrukOrder);

	@Query("select p from PlanningPelayanan p left join p.strukPlaning strukPlaning left join p.strukOrder strukOrder where strukOrder.noRec=:noRecStrukOrder")
	List<PlanningPelayanan> findPlaningPelayananByStrukOrder(@Param("noRecStrukOrder") String noRecStrukOrder);

	@Query("select new Map(asalSukuCadang.id as sukuCadangId,asalSukuCadang.asalSukuCadang as asalSukuCadang,rekananSkKontrak.noRec as noRecRekananSkKontrak,rekanan.namaRekanan as namaRekanan,rekananSkKontrakDetail.noRec as noRecRekananSkKontrakDetail,produk.namaProduk as namaProduk,jenisPekerjaan.id as jenisPekerjaanId,jenisPekerjaan.jenisPekerjaan as jenisPekerjaan,rekanan.namaRekanan as namaRekanan,strukPlaning.namarekanan as namaRekanan,strukPlaning.keteranganlainnya as keteranganRespon,p.keterangan as keterangan,p.tglDiperiksa as tanggalDiperiksa,p.jenisKerusakan as jenisKerusakan,p.analisaKerusakan as analisaKerusakan,statusPekerjaan.statusPekerjaan as statusPekerjaan,statusPekerjaan.id as statusPekerjaanId ) from PlanningPelayanan p  "
			+ "left join p.strukPlaning strukPlaning  "
			+ "left join p.strukOrder strukOrder  "
			+ "left join strukPlaning.rekananSkKontrak rekananSkKontrak "
			+ "left join strukPlaning.rekananSkKontrakDetail rekananSkKontrakDetail "
			+ "left join rekananSkKontrak.rekanan rekanan "
			+ "left join strukPlaning.statusPekerjaan statusPekerjaan "
			+ "left join strukPlaning.asalSukuCadang  asalSukuCadang "
			+ "left join strukPlaning.jenisPekerjaan jenisPekerjaan left join rekananSkKontrakDetail.produk produk "
			+ "where strukOrder.noRec=:noRecStrukOrder")
	List<Map<String,Object>> findStrukPlaningAndStrukOrder(@Param("noRecStrukOrder") String noRecStrukOrder);

	@Query("select new Map( p.terukurCatuTegangan as terukurCatuTegangan,p.terukurKelembabanRuangan as terukurKelembabanRuangan,p.terukurTemperaturRuangan as terukurTemperaturRuangan,"
		+" p.kebutuhanAlatCatuTegangan as kebutuhanAlatCatuTegangan,p.kebutuhanAlatKelembabanRuangan as kebutuhanAlatKelembabanRuangan,p.kebutuhanAlatTemperaturRuangan as kebutuhanAlatTemperaturRuangan ) from PlanningPelayanan p  "
		+" 	left join p.strukPlaning strukPlaning "
		+" 	left join p.strukOrder strukOrder "
		+" 	left join strukPlaning.rekananSkKontrak reknanSkKontrak "
		+" 	left join strukPlaning.rekananSkKontrakDetail rekakanSkKontrakDetail "
		+" 	left join reknanSkKontrak.rekanan rekanan "
		+" 	left join strukPlaning.statusPekerjaan as statusPekerjaan "
		+" 	left join strukPlaning.jenisPekerjaan jenisPekerjaan "
		+" 	where strukOrder.noRec=:noRecStrukOrder")
	List<Map<String,Object>> findParameterStrukPlaningAndStrukOrder(@Param("noRecStrukOrder") String noRecStrukOrder);
	
	@Query("select new Map(sp.noplanning as noplanning, sp.deskripsiplanning as deskripsiplanning, "
			+ " rekanan.id as rekanaId, rekanan.namaRekanan as namaRekanan,"
			+ " ruangan.id as ruanganId, ruangan.namaRuangan as namaRuangan) "
			+ " from StrukPlanning sp "
			+ " left join sp.kdkelompoktransaksi kelompoktransaksi "
			+ " left join sp.kdrekanan rekanan "
			+ " left join sp.kdruangan ruangan"
			+ " where sp.noplanning = :noPlanning"
			+ " and sp.tglplanning between to_date( :dateStart , 'dd-MM-yyyy') and to_date( :dateEnd , 'dd-MM-yyyy') "
			+ " and kelompoktransaksi.id = 77 ")
	List<Map<String,Object>> getListStrukPlanningByNoPlanningAndDate(@Param("noPlanning") String noPlanning,@Param("dateStart") String dateStart,@Param("dateEnd") String dateEnd);

	@Query("select new Map(sp.noplanning as noplanning, sp.deskripsiplanning as deskripsiplanning, sp.namaplanning as namaplanning ,"
			+ " rekanan.id as rekanaId, rekanan.namaRekanan as namaRekanan, "
			+ " ruangan.id as ruanganId, ruangan.namaRuangan as namaRuangan) "
			+ " from StrukPlanning sp "
			+ " left join sp.kdkelompoktransaksi kelompoktransaksi "
			+ " left join sp.kdrekanan rekanan "
			+ " left join sp.kdruangan ruangan"
			+ " where sp.tglplanning between to_date( :dateStart , 'dd-MM-yyyy') and to_date( :dateEnd , 'dd-MM-yyyy') "
			+ " and kelompoktransaksi.id = 77 ")
	List<Map<String,Object>> getListStrukPlanningByDate(@Param("dateStart") String dateStart,@Param("dateEnd") String dateEnd);
 
	@Query("select model from StrukPlanning model where model.noplanning = :noPlanning")
	StrukPlanning getSrukPlanningByNoPlanning(@Param("noPlanning")String noPlanning);
}
