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

@Repository("PengembalianKendaraanDinasDao")
public interface PengembalianKendaraanDinasDao extends PagingAndSortingRepository<PengembalianKendaraanDinas, String> {

//	@Query("select NEW com.jasamedika.medifirst2000.dto.PengembalianKendaraanDinasDto(s.noOrder,s.noOrderIntern,pdk.namaProduk,"
//			+ "a.noRegisterAset,a.noPolisi,s.tglPelayananAwal,s.alamatTempatTujuan,pgw.namaLengkap as petugas, "
//			+ "pg.namaLengkap as supir) from PemakaianKendaraanDinas p, OrderPelayanan o, StrukOrder s, RegistrasiAset a, "
//			+ "Produk pdk, Pegawai pgw, Pegawai pg,MappingPegawaiKendaraanDinas mp "
//			+ "where o.noRec=p.orderPelayananId and s.noRec=o.strukOrderId and a.noRec=p.assetId and pdk.id=o.produkAsetId "
//			+ "and pgw.id=mp.petugasId and pg.id=p.supirId and s.noRec=:noRec")
//	List<PengembalianKendaraanDinasDto> findPengembalianKendaraanDinasByNoRec(@Param("noRec") String noRec);

	@Query("select distinct NEW Map(so.noRec as noRec,so.noOrder as noOrder,pdk.id as namaProdukId,pdk.namaProduk as namaProduk,"
			+ "ast.noRegisterAset as noRegisAset,ast.noPolisi as noPolisi,so.tglPelayananAwal as tglKeberangkatan,"
			+ "up.id as namaRuanganId,up.namaRuangan as namaRuangan,so.alamatTempatTujuan as tujuan,pgw.id as supirId,"
			+ "pgw.namaLengkap as supir,ptg.id as petugasBerangkatId,ptg.namaLengkap as petugasBerangkat) "
			+ "from PemakaianKendaraanDinas pkd,MappingPegawaiKendaraanDinas mpkd "
			+ "left join mpkd.strukOrder so left join mpkd.petugas ptg left join pkd.orderPelayanan op left join op.produkAset pdk "
			+ "left join op.strukOrder so left join so.unitPemesan up left join pkd.asset ast left join pkd.supir pgw "
			+ "where so.noRec=:noRec order by so.noOrder")
	List<Map<String, Object>> findPengembalianKendaraanDinasByNoRec(@Param("noRec") String noRec);

	@Query("select NEW Map(so.noRec as noRec,so.noOrder as noOrder,so.tglPelayananAwal as tglPelayananAwal,"
			+ "so.tglPelayananAkhir as tglPelayananAkhir,so.waktuPengembalian as waktuPengembalian,"
			+ "r.id as idRuangan,r.namaRuangan as namaRuangan,so.alamatTempatTujuan as alamatTempatTujuan,so.status as status) "
			+ "from StrukOrder so,Ruangan r where r.id=so.unitPemesanId and so.noOrderIntern like '%KD%'")
	List<PengembalianKendaraanDinas> getAllKendaraanDinasDanPejabat();
	
	
	//show data by norec Pemakaian Kendaraan Dinas header
	//author iwankasan
	//date : 16.07.2018
	@Query("select NEW Map(so.noRec as noRec,so.noOrder as noOrder,pdk.id as namaProdukId,pdk.namaProduk as namaProduk,"
		+ "ast.noRegisterAset as noRegisAset,ast.noPolisi as noPolisi,so.tglPelayananAwal as tglKeberangkatan,"
		+ "up.id as namaRuanganId,up.namaRuangan as namaRuangan,so.alamatTempatTujuan as tujuan,pgw.id as supirId,"
		+ "pgw.namaLengkap as supir) "
		+ "from PemakaianKendaraanDinas pkd "
		+ "left join pkd.orderPelayanan op left join op.strukOrder so left join op.produkAset pdk "
		+ "left join so.unitPemesan up left join pkd.asset ast left join pkd.supir pgw "
		+ "where so.noRec=:noRec")
	List<Map<String, Object>> getPengembalianKendaraanDinasByNoRec(@Param("noRec")String noRec);
	
	//show data pengembalian kendaraan dinas
	//author iwankasan
	//date : 17.07.2018
	@Query("select NEW Map(so.noRec as noRecStrukOrder,so.noOrder as noOrder," +
		  " so.tglOrder as tglOrder, so.tglPelayananAwal as tglKeberangkatan, so.waktuPengembalian as tglPengembalian, pkd.noRec as noRec," +
		  " pkd.kmAwal as kmAwal, pkd.kmTerakhir as kmTerakhir, " +
		  " pkd.keterangan as keterangan, kk.id as idKondisi, kk.name as kondisi) " +
		  " from PengembalianKendaraanDinas pkd " +
		  " left join pkd.strukOrder so left join pkd.kondisiKendaraan kk " +
		  " where date(so.tglOrder) between date(:startDate) and date(:endDate) ")
	List<Map<String, Object>> getAllPengembalianKendaraanDinas(@Param("startDate")String startDate, @Param("endDate")String endDate);
	
	//show data detail Pengembalian kendaraan dinas
	//author iwankasan
	//date : 17.07.2018
	@Query("select NEW Map(so.noRec as noRec,so.noOrder as noOrder," +
			" so.tglOrder as tglOrder, " +
			" pkd.noRec as noRecPengembalian," +
			" pkd.kmAwal as kmAwal, pkd.kmTerakhir as kmTerakhir, " +
			" pkd.keterangan as keteranganKendaran, kk.id as idKondisi, " +
			" kk.name as kondisi) " +
			" from PengembalianKendaraanDinas pkd " +
			" left join pkd.strukOrder so " +
			" left join pkd.kondisiKendaraan kk " +
			" where so.noRec=:noRec")
	List<Map<String, Object>> getDetailPengembalianKendaraanDinasbyNoRec(@Param("noRec")String noRec);
	
}
