package com.jasamedika.medifirst2000.dao;

import java.util.Date;
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.entities.OrderPelayanan;
import com.jasamedika.medifirst2000.entities.StrukOrder;

/**
 * Repository class for Produk
 * 
 * @author Askur
 */
@Repository("StrukOrderDao")
public interface StrukOrderDao
		extends PagingAndSortingRepository<StrukOrder, String>, JpaSpecificationExecutor<StrukOrder> {

	//@Query("select count(p.id) from StrukOrder p where :dateNow=:dateNow  ")
	@Query("select count(p.id) from StrukOrder p where p.tglPelayananAwal=:dateNow")
	Integer getCountByMonthAndYear(@Param("dateNow") Date dateNow);
	
	@Query("select count(p.id) from StrukOrder p where p.tglPelayananAwal=:dateNow")
	Integer getCountSewaAsrama(@Param("dateNow")Date dateNow);

	@Query("select s from StrukOrder s where s.noOrder=:noOrder or s.noOrderIntern=:noOrder ")
	StrukOrder findByNoOrder(@Param("noOrder") String noOrder);

	@Query("select count(s.tglPelayananAwal) from StrukOrder s where month(s.tglPelayananAwal)=:monthNow and day(s.tglPelayananAwal)=:dayNow and year(s.tglPelayananAwal)=:yearNow")
	Integer getCountByDayMonthAndYear(@Param("dayNow") Integer day, @Param("monthNow") Integer month,
			@Param("yearNow") Integer year);

	@Query("select s from StrukOrder s where s.noRec=:noRec")
	StrukOrder findByNoRec(@Param("noRec") String noRec);

	// Sewa Asrama - Sarpras
	@Query("select s from StrukOrder s where s.noOrder=:noOrder or s.noOrderIntern=:noOrder and s.status='Terisi'")
	StrukOrder findStatusKamar(@Param("noOrder") String noOrder);

	@Query("select s from StrukOrder s, OrderPelayanan o, PemakaianKendaraanDinas p where s.noRec=o.noOrderId and o.noRec=p.orderPelayananId and p.noRec=:noRec and s.status='DIPAKAI'")
	StrukOrder findStatusKendaraanDinasDipakai(@Param("noRec") String noRec);

	// Sewa Lahan - Sarpras
	@Query("select s from StrukOrder s where s.noOrder=:noOrder and s.status='DIPAKAI' and s.noOrderIntern like '%SL%'")
	StrukOrder findStatusSewaLahan(@Param("noOrder") String noOrder);

	// Sewa Lahan - Sarpras
	@Query("select s from StrukOrder s where s.noRec=:noRec and s.status='DIPAKAI'")
	StrukOrder findStatusDipakai(@Param("noRec") String noRec);

	// Sewa Lahan - Sarpras
	@Query("select s from StrukOrder s where s.noRec=:noRec and s.status='ORDER'")
	StrukOrder findStatusOrder(@Param("noRec") String noRec);

	// kendaraan dinas & pejabat - sarpras
	@Query("select pkd from StrukOrder so,PengembalianKendaraanDinas pkd where so.noRec=pkd.strukOrderId and so.noOrderIntern like '%KD%'")
	List<StrukOrder> getAllKendaraanDinasDanPejabat();

	@Query("select so from StrukOrder so where so.noOrderIntern = :noOrder")
	StrukOrder findByNoOrderIntern(@Param("noOrder") String noOrder);

	// pemulasaraan jenazah - sarpras
	@Query("select NEW Map(pdk.id as idProduk,pdk.namaProduk as namaProduk,hrg.id as idHarga,hrg.hargaSatuan as hargaSatuan) "
			+ "from MappingPelayananJenazah mpj,OrderPelayanan op,StrukOrder so,Produk pdk,"
			+ "HargaNettoProdukByKelas hrg where op.noRec=mpj.orderPelayananId and so.noRec=op.noOrderId and pdk.id=mpj.produkId "
			+ "and hrg.id=mpj.hargaId and so.noRec=:noRec")
	List<StrukOrder> findRincianBiayaPemulasaraanJenazah(@Param("noRec") String noRec);

	@Query("Select op from OrderPelayanan op where op.noRec=:noRec ")
	OrderPelayanan findDataOrderPelayanan(@Param("noRec") String noRec);

	@Query("select new Map( strukOrder.tglOrder as tanggalOrder,strukKonfirmasi.tglKonfirmasi as tglKonfirmasi) from KirimProduk kirimProduk left join kirimProduk.noKirim kirim "
		+"	right join kirim.noorder strukOrder  "
		+"	left join strukOrder.ruangan ruangan "
		+"	left join kirimProduk.noKonfirmasi strukKonfirmasi "
		+"	left join strukOrder.ruanganTujuan ruanganTujuan "
		+"	left join strukOrder.jenisPermintaan jenisPermintaan "
		+"	left join strukOrder.pegawaiOrder pegawai "
		+"	where strukOrder.noRec is not null and kirim is not null and strukOrder.noRec=:noRec "
		+"	group by strukOrder.tglOrder,"
		+"	strukKonfirmasi.tglKonfirmasi")
	Map<String,Object> getSelisih(@Param("noRec") String noRec);
	
	//author by iwankasan 26052017
	@Query("select count(a.noOrder) from "
			+ " StrukOrder a where a.noOrder like %:kode%")
	Long countNoOrderSI(@Param("kode") String kode);
	
	//author by iwankasan 26052017
	@Query("select a.noOrder from StrukOrder a "
			+ "where a.noOrder like %:kode% order by a.noOrder asc")
	List<String> noOrderSI(@Param("kode") String kode);
	
	//author by iwankasan 26052017
	@Query("select new map(a.noRec as noRec, a.noOrder as noOrder, " +
				" a.isDelivered as idDelivered, a.tglOrder as tglOrder, " +
				" a.tglOrderExpired as tlgOrderExp, a.keteranganKeperluan as ketKeperluan, " +
				" b.id as idJenisDok, b.jenisDokumen as jenisDok, c.id as idRuangan, " +
				" c.namaRuangan as namaRuangan, d.noRec as noRecOP, " +
				" d.produk.id as idProduk, d.produk.namaProduk as nmProduk) " +
				" from StrukOrder a left join a.jenisDokumen b " +
				" left join a.ruangan c left join a.orderPelayanan d " +
				" where a.noOrder like 'SI%' and a.noOrder=:noOrder")
	Map<String, Object> getOrderSIbyNoOrder(@Param("noOrder") String noOrder);
	
	//author by iwankasan 26052017
	@Query("select new map(a.noRec as noRec, a.noOrder as noOrder, " +
				" a.isDelivered as idDelivered, a.tglOrder as tglOrder, " +
				" a.tglOrderExpired as tlgOrderExp, a.keteranganKeperluan as ketKeperluan, " +
				" b.id as idJenisDok, b.jenisDokumen as jenisDok, c.id as idRuangan, " +
				" c.namaRuangan as namaRuangan, d.noRec as noRecOP, " +         //Order Pelayanan
				" d.produk.id as idProduk, d.produk.namaProduk as nmProduk, " +
				" (select id from Ruangan where id = a.ruanganTujuanId) as idRuanganTujuan, " +
				" (select namaRuangan from Ruangan where id = a.ruanganTujuanId) as namaRuanganTujuan) " +
				" from StrukOrder a left join a.jenisDokumen b " +
				" left join a.ruangan c left join a.orderPelayanan d " +
				" where a.noOrder like 'SI%'")
	List<Map<String, Object>> getOrderSI();
	

//	@Query(" select new Map( \n"
//			+ " s.noRec as noRec, \n"
//			+ " s.noCmId as pasienId, \n"
//			+ " s.noCm.namaPasien as namaPasien, \n"
//			+ " s.tglOrder as tglOrder, \n"
//			+ " s.ruanganId as ruanganId, \n"
//			+ " s.ruangan.namaRuangan as namaRuangan, \n"
//			+ " s.pegawaiOrderId as pegawaiOrderId, \n"
//			+ " s.pegawaiOrder.namaLengkap as namaPegawai, \n")
	
//	@Query("select NEW Map () from StrukOrder a ")
//	List<Map<String, Object>> HistoryPenunjang();
}
