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.ItPelaksanaanPerbaikan;
import com.jasamedika.medifirst2000.entities.ItPerbaikan;

@Repository("itPerbaikanDao")
public interface ItPerbaikanDao extends PagingAndSortingRepository<ItPerbaikan, String>{

	@Query("select a.noOrder from ItPerbaikan a order by a.noOrder asc")
	List<String> noOrder();
	
	@Query("select distinct NEW Map ("
			+ "b.kdRuangan as kdRuangan, "
			+ "b.namaRuangan as namaRuangan, "
			+ "b.id as id) "
			+ "from RegistrasiAset a "
			+ "left join a.ruangan b "
			+ "where b.id is not null order by b.id")
	List<Map<String, Object>> getRuanganMaintenance();
	
	@Query("select distinct NEW Map ("
			+ "a.noRec as noRec, "
			+ "a.noSeri as noSeri, "
			+ "b.kdRuangan as kdRuangan, "
			+ "b.namaRuangan as namaRuangan, "
			+ "c.kdProduk as kdProduk, "
			+ "c.namaProduk as namaProduk, "
			+ "d.merkProduk as merkProduk, "
			+ "e.typeProduk as typeProduk, "
			+ "g.jenisProduk as kategoriKerusakan, "
			+ "g.id as kategoriKerusakanId) "
			+ "from RegistrasiAset a "
			+ "left join a.ruangan b "
			+ "left join a.produk c "
			+ "left join a.merkProduk d "
			+ "left join a.typeProduk e "
			+ "left join c.detailJenisProduk f "
			+ "left join f.jenisProduk g "
			+ "left join g.kelompokProduk h "
			+ "where b.id=:id and b.id is not null and h.id = 23 ")
	List<Map<String, Object>> getAllRegistrasiByRuangan(
			@Param("id") Integer id);
	
	@Query("select distinct NEW Map ("
			+ "a.noRec as noRec, "
			+ "a.noSeri as noSeri, "
			+ "b.kdRuangan as kdRuangan, "
			+ "b.namaRuangan as namaRuangan, "
			+ "c.kdProduk as kdProduk, "
			+ "c.namaProduk as namaProduk, "
			+ "d.merkProduk as merkProduk, "
			+ "e.typeProduk as typeProduk, "
			+ "g.jenisProduk as kategoriKerusakan, "
			+ "g.id as kategoriKerusakanId) "
			+ "from RegistrasiAset a "
			+ "left join a.ruangan b "
			+ "left join a.produk c "
			+ "left join a.merkProduk d "
			+ "left join a.typeProduk e "
			+ "left join c.detailJenisProduk f "
			+ "left join f.jenisProduk g "
			+ "left join g.kelompokProduk h "
			+ "where a.noRec=:noRec ")
	List<Map<String, Object>> getAllRegistrasiByNoRec(
			@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.namaLengkap as namaLengkap) "
			+ "from Pegawai a "
			+ "where a.ruanganId=:id order by a.namaLengkap")
	List<Map<String, Object>> getPegawaiByRuangan(
			@Param("id") Integer id);
	
	@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>> getTeknisi();
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "b.namaProduk as namaProduk, "
			+ "c.satuanStandar as satuanStandar, "
			+ "a.hargaNetto1 as hargaNetto1) "
			+ "from StokProdukGlobal a "
			+ "left join a.produk b "
			+ "left join b.satuanStandar c "
			+ "left join b.detailJenisProduk d "
			+ "where a.ruanganId=229 and b.detailJenisProdukId=449")
	List<Map<String, Object>> getSukuCadang();
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "a.noOrder as noOrder, "
			+ "d.kdProduk as kdProduk, "
			+ "d.namaProduk as namaProduk, "
			+ "a.tglPesan as tglPesan, "
			+ "e.merkProduk as merkProduk, "
			+ "f.typeProduk as typeProduk, "
			+ "c.noSeri as noSeri, "
			+ "a.keluhan as keluhan, "
			+ "b.namaLengkap as pelapor, "
			+ "h.namaRuangan as namaRuangan, "
			+ "g.statusPengerjaan as statusPengerjaan, "
			+ "g.statusRespon as statusRespon, "
			+ "g.ketStatusRespon as ketStatusRespon) "
			+ "from ItPerbaikan a "
			+ "left join a.pelapor b "
			+ "left join a.registrasiAset c "
			+ "left join c.produk d "
			+ "left join c.merkProduk e "
			+ "left join c.typeProduk f "
			+ "left join a.itStatusPerbaikan g "
			+ "left join b.ruangan h "
			+ "where g.statusRespon=0 "
			+ "and (a.statusEnabled is null or a.statusEnabled is true) "
			+ "order by a.tglPesan desc")
	List<Map<String, Object>> getAllPermintaanPerbaikan();
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "b.namaLengkap as pelapor, "
			+ "c.statusPengerjaan as statusPengerjaan, "
			+ "c.statusRespon as statusRespon, "
			+ "c.waktuRespon as waktuRespon, "
			+ "c.ketStatusRespon as keteranganRespon, "
			+ "e.id as ruanganId, "
			+ "b.id as pelaporId, "
			+ "substring(f.keteranganOrder,31,20) as ketKerusakan,"
			+ "a.noOrder as noOrder,"
			+ "a.tglPesan as tglPesan, "
			+ "d.noRec as noRecAsset, "
			+ "a.keluhan as keluhan, "
			+ "g.analisaTeknisi as analisaTeknisi, "
			+ "g.analisaKerusakan as analisaKerusakan, "
			+ "g.jenisPekerjaan as jenisPekerjaan, "
			+ "g.tglMulaiPeriksa as tglMulaiPeriksa )"
			+ "from ItPerbaikan a "
			+ "left join a.pelapor b "
			+ "left join a.itStatusPerbaikan c "
			+ "left join a.registrasiAset d "
			+ "left join b.ruangan e "
			+ "left join a.strukOrder f "
			+ "left join a.itPelaksanaanPerbaikan g "
			+ "where a.noRec=:noRec "
			+ "and (a.statusEnabled is null or a.statusEnabled is true) ")
	Map<String, Object> getOnePermintaanPerbaikan(
			@Param("noRec") String noRec);
	
	@Query("select a from ItPelaksanaanPerbaikan a "
			+ "left join a.itPelaksanaanPerbaikanSukuCadang b "
			+ "left join a.itPelaksanaanPerbaikanListTeknisi c "
			+ "where a.itPerbaikanId=:noRec")
	ItPelaksanaanPerbaikan getAllItPerbaikanByNoRec(
			@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "a.tglPesan as tglPesan, "
			+ "e.namaProduk as namaProduk, "
			+ "d.namaRuangan as namaRuangan, "
			+ "c.namaLengkap as namaLengkap, "
			+ "a.keluhan as keluhan) "
			+ "from ItPerbaikan a "
			+ "left join a.registrasiAset b "
			+ "left join a.pelapor c "
			+ "left join c.ruangan d "
			+ "left join b.produk e "
			+ "where a.noRec=:noRec")
	Map<String, Object> getAlertPermintaanPerbaikan(
			@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.noRec as noRec, "
			+ "a.noOrder as noOrder, "
			+ "d.kdProduk as kdProduk, "
			+ "d.namaProduk as namaProduk, "
			+ "a.tglPesan as tglPesan, "
			+ "e.merkProduk as merkProduk, "
			+ "f.typeProduk as typeProduk, "
			+ "c.noSeri as noSeri, "
			+ "a.keluhan as keluhan, "
			+ "b.namaLengkap as pelapor, "
			+ "h.namaRuangan as namaRuangan, "
			+ "g.statusPengerjaan as statusPengerjaan, "
			+ "g.statusRespon as statusRespon, "
			+ "g.ketStatusRespon as ketStatusRespon, "
			+ "h.id as ruanganId, "
			+ "substring(i.keteranganOrder,31,20) as ketKerusakan) "
			+ "from ItPerbaikan a "
			+ "left join a.pelapor b "
			+ "left join a.registrasiAset c "
			+ "left join c.produk d "
			+ "left join c.merkProduk e "
			+ "left join c.typeProduk f "
			+ "left join a.itStatusPerbaikan g "
			+ "left join b.ruangan h "
			+ "left join a.strukOrder i "
			+ "where date(a.tglPesan) between date(:tanggalAwal) and date(:tanggalAkhir) "
			+ "and (a.statusEnabled is null or a.statusEnabled is true) "
			+ "order by a.tglPesan desc")	
	List<Map<String, Object>> getAllPermintaanPerbaikanRev1(@Param("tanggalAwal") String tanggalAwal, @Param("tanggalAkhir") String tanggalAkhir);
	
	@Query("select NEW Map ("
			+ "a.id as id, "
			+ "a.noRec as noRec, "
			+ "a.noOrder as noOrder, "
			+ "d.kdProduk as kdProduk, "
			+ "d.namaProduk as namaProduk, "
			+ "a.tglPesan as tglPesan, "
			+ "e.merkProduk as merkProduk, "
			+ "f.typeProduk as typeProduk, "
			+ "c.noSeri as noSeri, "
			+ "a.keluhan as keluhan, "
			+ "b.namaLengkap as pelapor, "
			+ "h.namaRuangan as namaRuangan, "
			+ "g.statusPengerjaan as statusPengerjaan, "
			+ "g.statusRespon as statusRespon, "
			+ "g.ketStatusRespon as ketStatusRespon, "
			+ "h.id as ruanganId, "
			+ "substring(i.keteranganOrder,31,20) as ketKerusakan) "
			+ "from ItPerbaikan a "
			+ "left join a.pelapor b "
			+ "left join a.registrasiAset c "
			+ "left join c.produk d "
			+ "left join c.merkProduk e "
			+ "left join c.typeProduk f "
			+ "left join a.itStatusPerbaikan g "
			+ "left join b.ruangan h "
			+ "left join a.strukOrder i "
			+ "where h.id = :ruanganId "
			+ "and (a.statusEnabled is null or a.statusEnabled is true) "
			+ "order by a.tglPesan desc")	
	List<Map<String, Object>> getPermintaanPerbaikanByRuanganId(@Param("ruanganId") Integer ruanganId);
	
	@Query("select distinct new Map( jp.jenisProduk as kategoriKerusakan, jp.id as id)"
			+ "from JenisProduk jp "
			+ "left join jp.kelompokProduk kp "
			+ "where kp.id = 23 "
			+ "order by jp.jenisProduk asc")
	List<Map<String, Object>> getKategoriKerusakan();
	
	@Query("select NEW Map (r.noRegisterAset as noRegisterAset,r.noRec as noRec,r.noSeri as noSeri,jp.jenisProduk as jenisProduk, "
			+ "p.namaProduk as namaProduk,p.id as produkId, "
			+ "m.merkProduk as merkProduk,t.typeProduk as typeProduk) "
			+ "from RegistrasiAset r "
			+ "left join r.produk p "
			+ "left join p.detailJenisProduk djp "
			+ "left join djp.jenisProduk jp "
			+ "left join jp.kelompokProduk k "
			+ "left join r.merkProduk m "
			+ "left join r.typeProduk t "
			+ "where jp.id = :id "
			+ "order by jp.jenisProduk asc")
	List<Map<String, Object>> getAssetByKelompokProduk(@Param("id") Integer id);
	
	@Query("select model from ItPerbaikan model where model.noRec=:noRec and (model.statusEnabled is null or model.statusEnabled is true)")
	public ItPerbaikan findByNoRec(@Param("noRec") String noRec);
	
	@Query("select NEW Map ("
			+ "count(a.noRec) as jumlahRequest, "
			+ "e.namaRuangan as namaRuangan) "
			+ "from ItPerbaikan a "
			+ "left join a.pelapor b "
			+ "left join a.itStatusPerbaikan c "
			+ "left join b.ruangan e "
			+ "where c.statusPengerjaan = 0 "
			+ "and (a.statusEnabled is null or a.statusEnabled is true) "
			+ "group by e.namaRuangan ")
	List<Map<String, Object>> getNotifikasRequestiItPerbaikan();
	
	@Query("select NEW Map ("
			+ "count(a.noRec) as jumlahRespon, "
			+ "e.namaRuangan as namaRuangan, "
			+ "c.statusRespon as statusRespon, "
			+ "c.ketStatusRespon as ketStatusRespon,"
			+ "c.statusPengerjaan as statusPengerjaan ) "
			+ "from ItPerbaikan a "
			+ "left join a.pelapor b "
			+ "left join a.itStatusPerbaikan c "
			+ "left join b.ruangan e "
			+ "where c.statusRespon > 0 "
			+ "and (a.statusEnabled is null or a.statusEnabled is true) "
			+ "group by e.namaRuangan,c.statusRespon,c.ketStatusRespon,c.statusPengerjaan")
	List<Map<String, Object>> getNotifikasResponItPerbaikan();
	
//	@Query("select NEW Map ("
//			+ "a.noRec as noRec, "
//			+ "a.noOrder as noOrder,"
//			+ "b.namaLengkap as pelapor, "
//			+ "c.statusPengerjaan as statusPengerjaan, "
//			+ "c.statusRespon as statusRespon, "
//			+ "c.waktuRespon as waktuRespon, "
//			+ "c.ketStatusRespon as keteranganRespon, "
//			+ "e.namaRuangan as namaRuangan, "
//			+ "e.id as ruanganId, "
//			+ "a.keluhan as keluhan, "
//			+ "a.tglPesan as tglPesan, "
//			+ "g.tglMulaiPeriksa as tglMulaiPeriksa, "
//			+ "b.id as pelaporId, "
//			+ "d.noRec as noRecAsset, "
//			+ "g.analisaTeknisi as analisaTeknisi, "
//			+ "g.analisaKerusakan as analisaKerusakan, "
//			+ "g.jenisPekerjaan as jenisPekerjaan) "
//			+ "from ItPerbaikan a "
//			+ "left join a.pelapor b "
//			+ "left join a.itStatusPerbaikan c "
//			+ "left join a.registrasiAset d "
//			+ "left join b.ruangan e "
//			+ "left join a.itPelaksanaanPerbaikan g "
//			+ "where a.noRec=:noRec "
//			+ "and (a.statusEnabled is null or a.statusEnabled is true) "
//			+ "")
//	List<Map<String, Object>> getLaporanPerbaikanIT(@Param("tanggalAwal") String tanggalAwal, @Param("tanggalAkhir") String tanggalAkhir,@Param("ruanganId") Integer ruanganId,@Param("statusPekerjaan") Integer statusPekerjaan);
	
}
