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

@Repository("PencatatanSuhuMesinDao")
public interface PencatatanSuhuMesinDao extends PagingAndSortingRepository<PencatatanSuhuMesin, String> {
	@Query("select new map(" +
			" a.id as id, " +
			" a.statusEnabled as statusEnabled, " +
			" a.kodeExternal as kodeExternal, " +
			" a.namaExternal as namaExternal, " +
			" a.kapasitas as kapasitas, " +
			" b.id as idMesin, " +
			" b.namaExternal as namaMesin, " +
			" c.id as idSatuan, " +
			" c.namaExternal as satuan) " +
			" from Mesin a left join a.mesin b left join a.satuan c" +
			" where a.statusEnabled in('true')")
	List<Map<String, Object>> getMesin();

	@Query("select new map(pencatatanSuhuMesin.noRec as noRes, " +
			" pencatatanSuhuMesin.tanggal as tanggal, " +
			" pencatatanSuhuMesin.programMesin as programMesin, " +
			" pencatatanSuhuMesin.waktuMulai as waktuMulai, " +
			" pencatatanSuhuMesin.waktuSelesai as waktuSelesai, " +
			" pencatatanSuhuMesin.suhu as suhu, " +
			" pencatatanSuhuMesin.tekanan as tekanan, " +
			" pencatatanSuhuMesin.lamaProses as lamaProses, " +
			" mesin.id as idMesin, mesin.namaExternal as namaMesin, " +
			" petugas.id as idPegawai, petugas.namaExternal as namaPegawai) " +
			" from PencatatanSuhuMesin pencatatanSuhuMesin " +
			" left join pencatatanSuhuMesin.mesin mesin " +
			" left join pencatatanSuhuMesin.petugas petugas " +
			" where pencatatanSuhuMesin.keterangan in('Pencatatan') " +
			" and pencatatanSuhuMesin.statusEnabled in('true')")
	List<Map<String, Object>> getPencatatanSuhuMesin();

	@Query("select new map(pencatatanSuhuMesin.noRec as noRes, " +
			" pencatatanSuhuMesin.tanggal as tanggal, " +
			" pencatatanSuhuMesin.programMesin as programMesin, " +
			" pencatatanSuhuMesin.waktuMulai as waktuMulai, " +
			" pencatatanSuhuMesin.waktuSelesai as waktuSelesai, " +
			" pencatatanSuhuMesin.suhu as suhu, " +
			" pencatatanSuhuMesin.tekanan as tekanan, " +
			" pencatatanSuhuMesin.lamaProses as lamaProses, " +
			" mesin.id as idMesin, mesin.namaExternal as namaMesin, " +
			" petugas.id as idPegawai, petugas.namaExternal as namaPegawai) " +
			" from PencatatanSuhuMesin pencatatanSuhuMesin " +
			" left join pencatatanSuhuMesin.mesin mesin " +
			" left join pencatatanSuhuMesin.petugas petugas " +
			" where pencatatanSuhuMesin.keterangan in('Grafik') " +
			" and pencatatanSuhuMesin.statusEnabled in('true')")
	List<Map<String, Object>> getPencatatanSuhuMesinGrafik();
	
	@Query("select new map(pencatatanSuhuMesin.noRec as noRes, " +
			" pencatatanSuhuMesin.tanggal as tanggal, " +
			" pencatatanSuhuMesin.programMesin as programMesin, " +
			" pencatatanSuhuMesin.waktuMulai as waktuMulai, " +
			" pencatatanSuhuMesin.waktuSelesai as waktuSelesai, " +
			" pencatatanSuhuMesin.suhu as suhu, " +
			" pencatatanSuhuMesin.tekanan as tekanan, " +
			" pencatatanSuhuMesin.lamaProses as lamaProses, " +
			" mesin.id as idMesin, mesin.namaExternal as namaMesin, " +
			" petugas.id as idPegawai, petugas.namaExternal as namaPegawai) " +
			" from PencatatanSuhuMesin pencatatanSuhuMesin " +
			" left join pencatatanSuhuMesin.mesin mesin " +
			" left join pencatatanSuhuMesin.petugas petugas " +
			" where pencatatanSuhuMesin.keterangan in('Bowidick') " +
			" and date(pencatatanSuhuMesin.tanggal) between date(:startDate) and date(:endDate) " +
			" and pencatatanSuhuMesin.statusEnabled in('true')")
	List<Map<String, Object>> getPencatatanSuhuMesinBowidick(@Param("startDate")String startDate, @Param("endDate")String endDate);
	
	//Pecatatan Suhu Mesin Bowidick by norec
	@Query("select new map(pencatatanSuhuMesin.noRec as noRes, " +
			" pencatatanSuhuMesin.tanggal as tanggal, " +
			" pencatatanSuhuMesin.programMesin as programMesin, " +
			" pencatatanSuhuMesin.waktuMulai as waktuMulai, " +
			" pencatatanSuhuMesin.waktuSelesai as waktuSelesai, " +
			" pencatatanSuhuMesin.suhu as suhu, " +
			" pencatatanSuhuMesin.tekanan as tekanan, " +
			" pencatatanSuhuMesin.lamaProses as lamaProses, " +
			" mesin.id as idMesin, mesin.namaExternal as namaMesin, " +
			" petugas.id as idPegawai, petugas.namaExternal as namaPegawai) " +
			" from PencatatanSuhuMesin pencatatanSuhuMesin " +
			" left join pencatatanSuhuMesin.mesin mesin " +
			" left join pencatatanSuhuMesin.petugas petugas " +
			" where pencatatanSuhuMesin.keterangan in('Bowidick') and pencatatanSuhuMesin.noRec=:noRec")
	List<Map<String, Object>> getPencatatanShuhuMesinBowidickByNoRec(@Param("noRec")String noRec);
	
	//BOwidick by Norec
	@Query("select new map(bowidick.noRec as noRes, " +
			" bowidick.tanggal as tanggal, " +
			" bowidick.programMesin as programMesin, " +
			" bowidick.suhu as suhu, " +
			" bowidick.hasil as hasil, " +
			" bowidick.keterangan as keterangan, " +
			" mesin.id as idMesin, mesin.namaExternal as namaMesin, " +
			" operator.id as idOperator, operator.namaExternal as namaOperator," +
			" supervisor.id as idSupervisor, supervisor.namaExternal as nameSupervisor) " +
			" from Bowidick bowidick " +
			" left join bowidick.mesin mesin " +
			" left join bowidick.operator operator " +
			" left join bowidick.supervisor supervisor " +
			" left join bowidick.pencatatanSuhuMesin pencatatanSuhuMesin " +
			" where pencatatanSuhuMesin.noRec=:noRec")
	List<Map<String, Object>> getBowidickByNoRec(@Param("noRec")String noRec);
	
	//BiologicalSudahDisterile by Norec
	@Query("select new map(bowidick.noRec as noRes, " +
			" bowidick.tanggal as tanggal, " +
			" bowidick.suhuPensterilan as suhuPensterilan, " +
			" bowidick.SuhuIncubator as SuhuIncubator, " +
			" bowidick.hasil as hasil, " +
			" bowidick.jamMasuk as jamMasuk," +
			" bowidick.jamKeluar as jamKeluar," +
			" bowidick.keterangan as keterangan, " +
			" operator.id as idOperator, operator.namaExternal as namaOperator) " +
			" from BiologicalSudahDisterile bowidick " +
			" left join bowidick.operator operator " +
			" left join bowidick.pencatatanSuhuMesin pencatatanSuhuMesin " +
			" where pencatatanSuhuMesin.noRec=:noRec and bowidick.keterangan in('Biological')")
	List<Map<String, Object>> getBiologicalSudahDisterileByNoRec(@Param("noRec")String noRec);
	
	//Attest by Norec
	@Query("select new map(bowidick.noRec as noRes, " +
			" bowidick.tanggal as tanggal, " +
			" bowidick.suhuPensterilan as suhuPensterilan, " +
			" bowidick.SuhuIncubator as SuhuIncubator, " +
			" bowidick.hasil as hasil, " +
			" bowidick.jamMasuk as jamMasuk," +
			" bowidick.jamKeluar as jamKeluar," +
			" bowidick.keterangan as keterangan, " +
			" operator.id as idOperator, operator.namaExternal as namaOperator) " +
			" from BiologicalSudahDisterile bowidick " +
			" left join bowidick.operator operator " +
			" left join bowidick.pencatatanSuhuMesin pencatatanSuhuMesin " +
			" where pencatatanSuhuMesin.noRec=:noRec and bowidick.keterangan in('Attest')")
	List<Map<String, Object>> getAttestByNoRec(@Param("noRec")String noRec);
	
	@Query("select new Map(mappingMesin.namaExternal as namaMesin, "+
			"catSuhuMesin.programMesin as progMesinCycle, "+
			"catSuhuMesin.suhu as temp, "+
			"catSuhuMesin.petugasId as idOperator, "+
			"operator.namaLengkap as namaOperator, "+
			"to_char(catSuhuMesin.tanggal,'dd/MM/yyyy') as tglCatSuhuMesin) "+
			"from PencatatanSuhuMesin catSuhuMesin "+
			"left join catSuhuMesin.mesin mesin "+
			"left join mesin.mesin mappingMesin "+
			"left join catSuhuMesin.petugas operator "+
			"where catSuhuMesin.statusEnabled is true "+
			"and mesin.statusEnabled is true "+
			"and mappingMesin.statusEnabled is true "+
			"and operator.statusEnabled is true "+
			"and catSuhuMesin.mesinId = :idMesin "+
			"and mesin.mesinId = :idMappingMesin "+
			"and to_char(catSuhuMesin.tanggal,'yyyy-MM-dd') = :tglCatSuhuMesin "+
			"and catSuhuMesin.keterangan = 'Pencatatan'")
	Map<String, Object> getReportPencatatanSuhuMesin(
			@Param("idMesin")Integer idMesin, 
			@Param("idMappingMesin")Integer idMappingMesin, 
			@Param("tglCatSuhuMesin")String tglCatSuhuMesin);
	
	@Query("select new Map(mappingMesin.namaExternal as namaMesin, "+
			"catSuhuMesin.programMesin as progMesinCycle, "+
			"catSuhuMesin.suhu as temp, "+
			"catSuhuMesin.tekanan as tekanan, "+
			"catSuhuMesin.lamaProses as lamaProses, "+
			"catSuhuMesin.petugasId as idOperator, "+
			"operator.namaLengkap as namaOperator, "+
			"to_char(catSuhuMesin.tanggal,'dd/MM/yyyy') as tglCatSuhuMesin) "+
			"from PencatatanSuhuMesin catSuhuMesin "+
			"left join catSuhuMesin.mesin mesin "+
			"left join mesin.mesin mappingMesin "+
			"left join catSuhuMesin.petugas operator "+
			"where catSuhuMesin.statusEnabled is true "+
			"and mesin.statusEnabled is true "+
			"and mappingMesin.statusEnabled is true "+
			"and operator.statusEnabled is true "+
			"and catSuhuMesin.mesinId = :idMesin "+
			"and mesin.mesinId = :idMappingMesin "+
			"and to_char(catSuhuMesin.tanggal,'yyyy-MM-dd') = :tglCatSuhuMesin "+
			"and catSuhuMesin.keterangan = 'Grafik'")
	List<Map<String, Object>> getReportGrafikSuhuMesin(
			@Param("idMesin")Integer idMesin, 
			@Param("idMappingMesin")Integer idMappingMesin, 
			@Param("tglCatSuhuMesin")String tglCatSuhuMesin);

}
