package com.jasamedika.medifirst2000.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
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.dto.AsetDto;
import com.jasamedika.medifirst2000.entities.RegistrasiAset;
import com.jasamedika.medifirst2000.entities.StrukOrder;

/**
 * Repository class for Departemen
 * 
 * @author Roberto
 */
@Repository("RegistrasiAsetDao")
public interface RegistrasiAsetDao
		extends PagingAndSortingRepository<RegistrasiAset, String>, JpaSpecificationExecutor<RegistrasiAset> {

	@Query("select count(e) from RegistrasiAset e ")
	int countRegistrasiAset();

	@Query("select NEW com.jasamedika.medifirst2000.entities.RegistrasiAset(p.noRec,p.noRegisterAset,r) from RegistrasiAset p left join p.ruangan r where p.strukPelayananDetail.noRec=:noRec ")
	List<RegistrasiAset> findByStrukPelayananDetil(@Param("noRec") String noRec);

	@Query("select NEW com.jasamedika.medifirst2000.entities.RegistrasiAset(p.noRec,p.noRegisterAset,p.produk,p.strukPelayanan,p.strukPelayananDetail,p.asalProduk) from RegistrasiAset p where p.noRec=:noRec ")
	RegistrasiAset findByNoRec(@Param("noRec") String noRec);
	
	@Query("select p from RegistrasiAset p where p.noRec=:noRec ")
	RegistrasiAset findById(@Param("noRec") String noRec);

	// registrasi aset ambulance - sarpras
	@Query("select NEW Map(rgs.noRec as noRecAset,rgs.noRegisterAset as noRegis,rgs.noPolisi as noPolisi,r.id as idRuangan,r.namaRuangan as namaRuangan,"
			+ "p.id as idProduk,p.namaProduk as namaProduk) from RegistrasiAset rgs,Ruangan r,Produk p where p.id=rgs.produkId "
			+ "and r.id=rgs.ruanganId and r.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdRuanganAsetAmbulance')")
	List<AsetDto> findAssetAmbulance();

	// registrasi aset kendaraan dinas & pejabat - sarpras
	@Query("select NEW Map(rgs.noRec as noRecAset,rgs.noRegisterAset as noRegis,rgs.noPolisi as noPolisi,r.id as idRuangan,r.namaRuangan as namaRuangan,"
			+ "p.id as idProduk,p.namaProduk as namaProduk) from RegistrasiAset rgs,Produk p,Ruangan r where p.id=rgs.produkId "
			+ "and r.id=rgs.ruanganId and r.id in (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdRuanganKendaraanDinas')")
	List<AsetDto> findAssetKendaraanDinas();

	@Query("select new Map( "
			+ "registrasiAset.noRegisterAset as noRegisterAset,produk.kdProduk as kdProduk,registrasiAset.kdBmn as kodeBmn, "
			+ "registrasiAset.kdAspac as kdAspak, "
			+ "registrasiAset.kdRsabhk as kdRs,"
			+ "produk.namaProduk as namaProduk,"
			+ "satuan.satuanStandar as satuan,"
			+ "satuan.id as satuanStandarId,"
			+ "jp.jenisProduk as jenisProduk,"
			+ "jp.id as jenisProdukId,"
			+ "djp.detailJenisProduk as detailJenisProduk,"
			+ "djp.id as detailJenisProdukId,"
			+ "kelompokAset.id as kelompokAsetId,"
			+ "kelompokAset.kelompokAset as kelompokAset,"
			+ "typeProduk.id as typeProdukId,"
			+ "merkProduk.id as merkProdukId,"
			+ "merkProduk.merkProduk as merkProduk,"
			+ "registrasiAset.noSeri as noSeri,"
			+ "registrasiAset.dayaListrik as dayaListrik,"
			+ "registrasiAset.klasifikasiTeknologi as klasifikasiTeknologi,"
			+ "registrasiAset.hargaPerolehan as hargaPerolehan,"
			+ "supplier.id as supplierId,"
			+ "supplier.namaRekanan as namaSupplier,"
			+ "registrasiAset.tahunPerolehan as tahunPerolehan,"
			+ "registrasiAset.usiaTeknis as usiaTeknis,"
			+ "registrasiAset.usiaPakai as usiaPakai,"
			+ "registrasiAset.sisaUmur as sisaUmur,"
			+ "registrasiAset.keteranganLainnya as spesifikasi,"
			+ "registrasiAset.image as imageUrl"
			+ ")"
			+ "from RegistrasiAset registrasiAset "
			+ "left join registrasiAset.produk produk "
			+ "left join registrasiAset.satuanStandar satuan "
			+ "left join produk.detailJenisProduk djp "
			+ "left join djp.jenisProduk jp "
			+ "left join registrasiAset.kelompokAset kelompokAset "
			+ "left join registrasiAset.supplier supplier "
			+ "left join registrasiAset.typeProduk typeProduk "
			+ "left join registrasiAset.merkProduk merkProduk  where registrasiAset.noRec=:noRec ")
	Map<String,Object> findByNoRecDetail(@Param("noRec") String noRec);

	@Query("select new map ( produk.namaProduk as namaProduk,produk.id as idProduk ) "
			+ " from RegistrasiAset registerAset  " + " left join registerAset.produk produk "
			+ " left join registerAset.typeProduk typeProduk " + " left join registerAset.merkProduk merkProduk "
			+ " where registerAset.ruanganId  " + " in (:ruanganId) group by  produk.namaProduk,produk.id order by  produk.namaProduk asc")
	List<Map<String, Object>> getAsetByRuangan(@Param("ruanganId") Integer ruanganId);
	
	@Query("select new map ( " + " coalesce(typeProduk.typeProduk,'-') as typeProduk,coalesce(registerAset.noSeri,'-') as noSeri, "
			+ " coalesce(merkProduk.merkProduk,'-') as merkProduk, "
			+ " registerAset.noRec as noRec,registerAset.noRegisterAset as noRegisterAset,produk.namaProduk as namaProduk,produk.id as idProduk ) "
			+ " from RegistrasiAset registerAset  " + " left join registerAset.produk produk "
			+ " left join registerAset.typeProduk typeProduk " + " left join registerAset.merkProduk merkProduk "
			+ " where registerAset.ruanganId  " + " in (:ruanganId) and produk.id=:produkId  order by  registerAset.noRegisterAset desc")
	List<Map<String, Object>> getAsetByProduk(@Param("ruanganId") Integer ruanganId,@Param("produkId") Integer produkId);

	@Query("select new Map(strukOrder.noOrder as noOrder,strukOrder.tglOrder as tglOrder,strukOrder.keteranganOrder as keterangan,coalesce(statusPekerjaan.statusPekerjaan,'-') as statusPekerjaan) from PlanningPelayanan planningPelayanan "
			+ "left join planningPelayanan.strukPlaning strukPlanning "
			+ "left join planningPelayanan.strukOrder strukOrder "
			+ "left join planningPelayanan.registrasiAset registerAsset "
			+ "left join registerAsset.produk produk "
			+ "left join strukPlanning.statusPekerjaan statusPekerjaan "
			+ "where  strukOrder.ruanganId=:ruanganId and registerAsset.noRec=:noRec "
			+ "group by strukOrder.noOrder,strukOrder.tglOrder,strukOrder.keteranganOrder,statusPekerjaan.statusPekerjaan "
			+ "order by  strukOrder.noOrder desc")
	List<Map<String,Object>> findHistoryPerbaikan(@Param("noRec") String noRec,@Param("ruanganId") Integer ruanganId);
	
	@Query("select strukOrder from PlanningPelayanan planningPelayanan "
			+ "left join planningPelayanan.strukPlaning strukPlanning "
			+ "left join planningPelayanan.strukOrder strukOrder "
			+ "left join planningPelayanan.registrasiAset registerAsset "
			+ "left join registerAsset.produk produk "
			+ "left join strukPlanning.statusPekerjaan statusPekerjaan where registerAsset.noRec=:noRec ")
	List<StrukOrder> findStrukOrderByNoRec(@Param("noRec") String noRec);

	@Query("select new map(p.id as noRec, p.format as format,p.nomor as nomor,p.kategori as kategori,p.namaFile as namaFile,p.tanggal as tanggal) from HistoryAlat p where p.kategori=:kategori and p.registrasiAsetId=:noRec ")
	List<Map<String,Object>> historySertifikat(@Param("noRec") String noRec,@Param("kategori") String kategori);

	@Query("select new map(p.id as noRec, p.format as format,p.nomor as nomor,p.kategori as kategori,p.namaFile as namaFile,p.tanggal as tanggal) from HistoryAlat p where  p.id=:id ")
	Map<String, Object> getFormatDownload(@Param("id") Integer id);

	@Query("select new map(p.id as noRec, p.format as format,p.nomor as nomor,p.kategori as kategori,p.namaFile as namaFile,p.tanggal as tanggal) from HistoryAlat p left join p.registrasiAset registrasiAset where  registrasiAset.noRec=:noRec and p.kategori=:kategori ")
	List<Map<String,Object>> findAsetByRuanganList(@Param("noRec") String noRec, @Param("kategori") String kategori);
	
	@Query("select new map(p.id as noRec, p.format as format,p.nomor as nomor,p.kategori as kategori,p.namaFile as namaFile,p.tanggal as tanggal) from HistoryAlat p left join p.registrasiAset registrasiAset where  registrasiAset.noRec=:noRec ")
	List<Map<String,Object>> findAsetByRuanganList(@Param("noRec") String noRec);


}
