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

@Repository("ipsrsPeminjamanAlatDao")
public interface IpsrsPeminjamanAlatDao extends PagingAndSortingRepository<IpsrsPeminjamanAlat, String>{

	@Query("select a.noPeminjaman "
			+ "from IpsrsPeminjamanAlat a "
			+ "order by a.noPeminjaman asc")
	List<String> noPeminjaman();
	
	
	
	@Query("select NEW Map ("
			+ "a.noRec as noRec, "
			+ "a.noPeminjaman as noPeminjaman, "
			+ "a.tglPeminjaman as tglPeminjaman, "
			+ "a.jumlah as jumlah, "
			+ "a.tglPengembalian as tglPengembalian, "
			+ "a.petugas as petugas, "
			+ "a.status as status, "
			+ "b.namaLengkap as peminjam, "
			+ "c.namaRuangan as ruangan, "
			+ "d.namaProduk as namaProduk, "
			+ "d.namaProduk as kdProduk, "
			+ "e.namaLengkap as petugas) "
			+ "from IpsrsPeminjamanAlat a left join a.registrasiAset registrasiAset "
			+ "left join a.peminjam b "
			+ "left join a.ruanganPeminjam c "
			+ "left join registrasiAset.produk d "
			+ "left join a.petugas e "
			+ "where a.status=:status")
	List<Map<String, Object>> getAllPeminjamanAlat(
			@Param("status") String status);
	
	@Query("select NEW Map ("
			+ "a.noPeminjaman as noPeminjaman, "
			+ "a.tglPeminjaman as tglPeminjaman, "
			+ "a.status as status, "
			+ "b.namaRuangan as ruanganPeminjam, "
			+ "c.namaLengkap as petugas, "
			+ "d.namaProduk as namaBarang, "
			+ "a.jumlah as jumlahBarang, "
			+ "e.satuanStandar as satuan, "
			+ "a.tglPengembalian) "
			+ "from IpsrsPeminjamanAlat a "
			+ "left join a.ruanganPeminjam b "
			+ "left join a.petugas c "
			+ "left join a.registrasiAset registrasiAset left join registrasiAset.produk d "
			+ "left join d.satuanStandar e "
			+ "where d.id=:id and "
			+ "date(a.tglPeminjaman) between "
			+ "date(:periodeAwal) and "
			+ "date(:periodeAkhir) and "
			+ "a.status=:status")
	List<Map<String, Object>> findPeminjamanAlatByPeriode(
			@Param("periodeAwal") String periodeAwal,
			@Param("periodeAkhir") String periodeAkhir,
			@Param("id") Integer id,
			@Param("status") String status);
}
