package com.jasamedika.medifirst2000.dao;

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

import org.springframework.data.domain.Pageable;
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.StrukPelayanan;
import com.jasamedika.medifirst2000.entities.StrukPelayananDetail;

/**
 * @author Shakato
 */
@Repository("StrukPelayananDao")
public interface StrukPelayananDao extends PagingAndSortingRepository<StrukPelayanan, String> {

	// @Query("select model from StrukPelayananDetail detail join fetch
	// detail.nostruk model where model.kdkelompoktransaksi.id = (Select
	// cast(s.nilaiField as int) from SettingDataFixed s where
	// s.namaField='KdLimbahInStruk')")
	// List<StrukPelayananDetail> findStrukLimbahMasuk();

	@Query("select t from StrukPelayananDetail t join t.nostruk model where model.kdkelompoktransaksi.id = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdLimbahInStruk')")
	List<StrukPelayananDetail> findStrukLimbahMasuk();

	// @Query("select SUM(model.qtyproduk) from StrukPelayanan model where
	// date(model.tglstruk) between date(:periodeAwal) and date(:periodeAkhir)")
	// public Double getToftalBeratLimbahB3MasukByPeriode(@Param("periodeAwal")
	// String periodeAwal,
	// @Param("periodeAkhir") String periodeAkhir);

	@Query("select coalesce(SUM(model.qtyproduk),0.0) from StrukPelayanan model where date(model.tglstruk) between date(:periodeAwal) and date(:periodeAkhir) and  model.jenisLimbahB3Masuk.id=:jenisLimbahB3Masuk and model.kdkelompoktransaksi = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdTransaksiKeslingInStrukPelayanan') ")
	public Double getTotalBeratLimbahB3MasukByPeriode(@Param("periodeAwal") String periodeAwal,
			@Param("periodeAkhir") String periodeAkhir, @Param("jenisLimbahB3Masuk") Integer jenisLimbahB3Masuk);

	@Query("select new map(limbah.id as jenisLimbahB3Masuk,limbah.jenisLimbahB3masuk as limbag,SUM(model.qtyproduk) as total) from StrukPelayanan model left join model.jenisLimbahB3Masuk limbah where date(model.tglstruk) between  date(:periodeAwal) and date(:periodeAkhir) and model.kdkelompoktransaksi = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdTransaksiKeslingInStrukPelayanan') group by limbah.id,limbah.jenisLimbahB3masuk")
	public List<Map<String, Object>> getTotalBeratLimbahB3MasukByPeriodeList(@Param("periodeAwal") String periodeAwal,
			@Param("periodeAkhir") String periodeAkhir);

	@Query("select  coalesce(SUM(model.qtyproduk),0.0) as total from StrukPelayanan model left join model.jenisLimbahB3Masuk limbah where date(model.tglstruk) between  date(:periodeAwal) and date(:periodeAkhir) and model.kdkelompoktransaksi = (Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdTransaksiKeslingInStrukPelayanan') ")
	public Double getTotalB3MasukByPeriode(@Param("periodeAwal") String periodeAwal,
			@Param("periodeAkhir") String periodeAkhir);

	@Query("select new Map(" + " model.supplierId as supplierId, "
			+ " coalesce(supplier.namaRekanan,'-') as namaSupplier, " + " coalesce(model.noRekap,'-') as noKontrak, "
			+ " coalesce(model.tanggal,null) as tanggal, " + " coalesce(supplier.namaRekanan) as namaSupplier, "
			+ " coalesce(supplier.telepon,'-') as telepon, " + " coalesce(strukPelayanan.nostruk,'-') as noPphp, "
			+ " supplier.alamatLengkap as alamatLengkap ) " + " from StrukPelayananDetail strukPelayananDetail "
			+ " left join strukPelayananDetail.nostruk strukPelayanan "
			+ " left join strukPelayananDetail.kartuPengendaliDetail kartuPengendaliDetail "
			+ " left join kartuPengendaliDetail.strukRekapPemenang model  " + " left join model.supplier supplier  "
			+ " where strukPelayanan.noRec=:noRec ")
	public List<Map<String, Object>> findByNoRecHead(@Param("noRec") String noRec, Pageable topOne);

	// @Query("Select cast(s.nilaiField as int) from SettingDataFixed s where
	// s.namaField='KdTransaksiKeslingInStrukPelayanan'")
	// public Integer getKdTransaksiKesling();

	@Query("select distinct new Map(" + "b.nocm.noCm as noCm, " + "b.nocm.namaPasien as namaPasien, "
			+ "b.nocm.jenisKelamin.jenisKelamin as jenisKelamin, " + "a.noOrder as noOrder,"
			+ "t.noresep.tglResep as tglResep," + "t.noresep.noRec as noRecStrukResep,"
			+ "t.noresep.noResep as noResep, " + "t.noresep.tglAmbilResep as tglAmbilResep, "
			+ "t.noresep.namaLengkapAmbilResep as namaLengkapAmbilResep," + "t.noresep.namaPemberi as namaPemberi,"
			+ "c.noSBK as noSbk," + "d.nosbm as noSbm" + ") " + "from StrukPelayananDetail t  "
			+ "left join t.noorder a " + "left join t.nostruk b " + "left join b.nosbklast c "
			+ "left join b.nosbmlast d	"
			+ "where b.kdkelompoktransaksi.id=(Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdKelompokTransaksiObatAlkes')")
	public List<Map<String, Object>> getPelayanaObatLuar();
	
	@Query("select distinct new Map(" 
			+ "b.nocm.noCm as noCm, " 
			+ "b.nocm.namaPasien as namaPasien, "
			+ "b.nocm.jenisKelamin.jenisKelamin as jenisKelamin, " 
			+ "a.noOrder as noOrder,"
			+ "t.nostruk.noRec as noRecStrukPelayanan,"
			+ "t.noresep.tglResep as tglResep," 
			+ "t.noresep.noRec as noRecStrukResep,"
			+ "t.noresep.noResep as noResep, " 
			+ "t.noresep.tglAmbilResep as tglAmbilResep, "
			+ "t.noresep.namaLengkapAmbilResep as namaLengkapAmbilResep," 
			+ "t.noresep.namaPemberi as namaPemberi,"
			+ "c.noSBK as noSbk," 
			+ "d.nosbm as noSbm)"
			+ "from StrukPelayananDetail t  "
			+ "left join t.noorder a " 
			+ "left join t.nostruk b " 
			+ "left join b.nosbklast c "
			+ "left join b.nosbmlast d "
			+ "where b.kdkelompoktransaksi.id=(Select cast(s.nilaiField as int) "
			+ "from SettingDataFixed s where s.namaField='KdKelompokTransaksiPenjualanBebas')")
	public List<Map<String, Object>> getPenjualanBebas();
	
	@Query("select new Map ("
			+ "a.noRec as noRecStrukPelayananDetail, "
			+ "b.noRec as noRecStrukPelayanan,"
			+ "c.namaProduk as namaProduk) "
			+ "from StrukPelayananDetail a "
			+ "left join a.nostruk b "
			+ "left join a.kdproduk c "
			+ "where b.noRec=:noRec")
	public List<Map<String, Object>> getProdukIdPenjualanBebas(
			@Param("noRec") String noRec);

	@Query("select distinct new Map(" + "t.noRec as noRecStrukResep," + "t.noResep as noResep, "
			+ "t.tglAmbilResep as tglAmbilResep, " + "t.namaLengkapAmbilResep as namaLengkapAmbilResep,"
			+ "t.namaPemberi as namaPemberi) " + "from StrukResep t " + "where t.noRec=:noRec")
	public Map<String, Object> findStrukResepByNoRec(@Param("noRec") String noRec);

	@Query("select distinct new Map(" + "b.nocm.noCm as noCm, " + "b.nocm.namaPasien as namaPasien, "
			+ "b.nocm.jenisKelamin.jenisKelamin as jenisKelamin, " + "a.noOrder as noOrder,"
			+ "t.noresep.tglResep as tglResep," + "t.noresep.noRec as noRecStrukResep,"
			+ "t.noresep.noResep as noResep, " + "t.noresep.tglAmbilResep as tglAmbilResep, "
			+ "t.noresep.namaLengkapAmbilResep as namaLengkapAmbilResep," + "t.noresep.namaPemberi as namaPemberi) "
			+ "from StrukPelayananDetail t  " + "left join t.noorder a " + "left join t.nostruk b "
			+ "where b.kdkelompoktransaksi.id=(Select cast(s.nilaiField as int) from  SettingDataFixed s where s.namaField='KdKelompokTransaksiObatAlkes')")
	public List<Map<String, Object>> getPelayanaObatOnlyResep();

	// add by Arya 2017-02-22
	@Query("select model from StrukPelayanan model where model.noRec=:noRec ")
	public StrukPelayanan findByNoRec(@Param("noRec") String noRec);

	// add by Arya 2017-03-20
	@Query("select new map(sdf.namaField as namaField,sdf.nilaiField as nilaiField) "
			+ "from SettingDataFixed sdf where sdf.namaField like '%RuanganVerifikasiPembayaranHutang%' ")
	public List<Map<String, Object>> getRuanganVerifikasiPembayaranHutang();
	
	@Query("select new Map("
			+ "sp.noRec as noRec, "
			+ "sp.nocm.namaPasien as namaPasien, "
			+ "sp.nocm.noCm as noCm) "
//			+ "sp.nocm.noCm as noCm, "
//			+ "pd.ruangan.namaRuangan as ruangPerawatan, "
//			+ "pd.ruangan.kelasHead as kelas, "
//			+ "sp.nocm.tempatLahir as tempatLahir, "
//			+ "sp.nocm.tglLahir as tanggalLahir, "
//			+ "a.alamatLengkap as alamat, "
//			+ "a.namaDesaKelurahan as kelurahan, "
//			+ "a.rtrw as rtRw, "
//			+ "kec.namaKecamatan as kecamatan, "
//			+ "kk.namaKotaKabupaten as kotaKabupaten, "
//			+ "p.namaPropinsi as propinsi) "
			+ "from StrukPelayanan sp "
			+ "left join sp.nocm cm "
			+ "left join sp.noregistrasi pd "
//			+ "left join cm.alamats a "
//			+ "left join a.kecamatan kec "
//			+ "left join a.kotaKabupaten kk "
//			+ "left join a.propinsi p "
//			+ "where a.isPrimaryAddress=1 "
			+ "where pd.noRegistrasi=:noregistrasi")
	public Map<String, Object> findStrukPelayanan(@Param("noregistrasi") String noregistrasi);
	
	@Query("select sp "
			+ "from StrukPelayanan sp "
			+ "left join sp.noregistrasi pd "
			+ "where pd.noRegistrasi=:noregistrasi")
	public StrukPelayanan findOneByNoRegistrasi(@Param("noregistrasi") String noregistrasi);
	
	@Query("select new Map(nostruk.noRec as noRecStrukPelayanan, "
			+ "produk.id as produkId, "
			+"produk.namaProduk as namaProduk,"
			+"model.qtyproduk as qtyProduk, "
			+"noclosing.noClosing as noClosing) "
			+"from StrukPelayananDetail model "
			+"left join model.kdproduk produk "
			+"left join model.noplanning noplanning "
			+"left join model.nostruk nostruk "
			+"left join nostruk.kdruanganasal ruanganAsal "
			+"left join nostruk.noclosing noclosing "
			+"where noplanning.noplanning=:noPlanning and ruanganAsal.id=93")
	public List<Map<String, Object>> findStrukPelayananByNoPlanning1(@Param("noPlanning") String noPlanning);
	
	@Query("select new Map(model.noRec as noRecStrukPelayananDetail,"
			+ "nostruk.noRec as noRecStrukPelayanan, "
			+ "nostruk.namaLengkapAmbil as namaLengkapAmbil,"
			+ "to_char(nostruk.tglAmbil,'yyyy-mm-dd') as tglAmbil,"
			+ "nostruk.keteranganAmbil as keteranganAmbil,"
			+ "pasien.id as pelangganId, "
			+ "pasien.namaPasien as namaPelanggan, "
			+ "produk.id as produkId, "
			+"produk.namaProduk as namaProduk,"
			+ "ruanganAsal.id as ruanganAsalId, "
			+"ruanganAsal.namaRuangan as namaRuanganAsal,"
			+"model.qtyproduk as qtyProduk, "
			+"noclosing.noRec as noClosing) "
			+"from StrukPelayananDetail model "
			+"left join model.kdproduk produk "
			+"left join model.noplanning noplanning "
			+"left join model.nostruk nostruk "
			+"left join nostruk.nocm pasien "
			+"left join nostruk.noclosing noclosing "
			+"left join nostruk.kdruanganasal ruanganAsal "
			+"where nostruk.noRec=:noRecStrukPelayanan ")
	public List<Map<String, Object>> findStrukPelayananByNoRec(@Param("noRecStrukPelayanan") String noRecStrukPelayanan);

	@Query("select new map ("
			+ "b.nostruk as nostruk, "
			+ "c.nosbm as nosbm) "
			+ "from PelayananPasien a "
			+ "left join a.strukPelayanan b "
			+ "left join b.nosbmlast c "
			+ "where b.noRec=:noRec")
	public Map<String, Object> checkDeletePelayanan(
			@Param("noRec") String noRec);
	
	@Query("select strukpelayanan "+
			"from StrukPelayanan strukpelayanan "+
			"left join strukpelayanan.noregistrasi pasienDaftar "+
			"where pasienDaftar.noRec =:noRec")
	public List<StrukPelayanan> getStrukPelayanan(
			@Param("noRec") String noRec);
}
