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.entities.Produk;
import com.jasamedika.medifirst2000.entities.StrukPelayananDNoBatch;
import com.jasamedika.medifirst2000.entities.StrukPelayananDetail;

/**
 * @author Shakato
 */
@Repository("StrukPelayananDetailDao")
public interface StrukPelayananDetailDao extends PagingAndSortingRepository<StrukPelayananDetail, String>,JpaSpecificationExecutor<StrukPelayananDetail> {

	@Query("select p from StrukPelayananDetail p left join fetch p.kdasalproduk asalProduk left join fetch p.kdproduk produk left join fetch p.kdsatuanstandar satuan where p.noRec=:noRec ")
	StrukPelayananDetail findByNorec(@Param("noRec") String noRec);

	@Query("select pro from StrukPelayananDetail p join  p.kdproduk pro where p.noRec=:strukPelayananDetailId")
	Produk findProdukByStrukPelayananDetail(@Param("strukPelayananDetailId") String strukPelayananDetailId);

	@Query("select model from StrukPelayananDNoBatch model where model.kdproduk.id=:idProduk and model.nostruk.qtyproduk > model.nostruk.qtyprodukoutint")
	List<StrukPelayananDNoBatch> findByProdukId(@Param("idProduk")Integer idProduk);

	@Query("select new map ( "
			+" produk.namaProduk as namaProduk, "
			+" produk.id as idProduk, "
			+" asalProduk.asalProduk as asalProduk,  "
			+" asalProduk.id as idAsalProduk,  "
			+" pengendali.namaPengendali as namaPengendali, "
			+" asalProduk.asalProduk as asalProduk, "
			+" detail.hargasatuan as hargaSatuan, "
			+" detail.qtyproduk as volumeDiminta, "
			+" detail.hargasatuan as  hargaSatuan,  "
			+" detail.hargadiscount as hargadiscount ,  "
			+" detail.hargappn as hargaPpn, "
			+" detail.noRec as noRec, "
			+" detail.hasilKonversi as hasilKonversi, "
			+" coalesce(detail.persendiscount,0.0) as diskon, "
			+" coalesce(detail.persenppn,0.0) as ppn, "
			+" kartuPengendaliDetail.noRec as noRecKartuPengendaliDetail, "
			+" satuanStandar.satuanStandar as satuanStandar,  "
			+" satuanStandar.id as idSatuan,"
			+" produk.namaProduk as namaProduk,"
			+" spek.volumeBarang as volumeKontrak, "
			+" coalesce(kondisiBarang.name,'-') as kondisiBarang, "
			+" kondisiBarang.id as idKondisiBarang) "
			+" from StrukPelayananDetail detail "
			+" left join detail.nostruk header "
			+" left join detail.kondisiBarang kondisiBarang "
			+" right join detail.kartuPengendaliDetail kartuPengendaliDetail "
			+" left join kartuPengendaliDetail.detailRup detailRup "
			+" left join detailRup.detailSpekAnggaran spek "
			+" left join spek.produk produk "
			+" left join spek.detailAnggaran detailAnggaran  "
			+" left join detailAnggaran.asalProduk asalProduk  "
			+" left join kartuPengendaliDetail.kartuPengendali kartuPengendali "  
			+" left join spek.produk produk  "
			+" left join spek.pengendali pengendali "
			+" left join detailAnggaran.akun akun "
			+" left join detailAnggaran.asalProduk asalProduk "
			+" left join produk.satuanStandar satuanStandar "
			+" left join kartuPengendaliDetail.strukRekapPemenang strukRekapPemenang "
			+" where kartuPengendaliDetail.noRec is not null and header.noRec=:noRec ")
	List<Map<String,Object>> strukPelayananByParent(@Param("noRec")String noRec);
	

	@Query("select new map ( "
			+" produk.namaProduk as namaProduk, "
			+" produk.id as idProduk, "
			+" asalProduk.asalProduk as asalProduk,  "
			+" asalProduk.id as idAsalProduk,  "
			+" pengendali.namaPengendali as namaPengendali, "
			+" asalProduk.asalProduk as asalProduk, "
			+" detail.hargasatuan as hargaSatuan, "
			+" detail.qtyproduk as volumeDiminta, "
			+" detail.hargasatuan as  hargaSatuan,  "
			+" detail.hargadiscount as hargadiscount ,  "
			+" detail.hargappn as hargaPpn, "
			+" detail.noRec as noRec, "
			+" detail.hasilKonversi as hasilKonversi, "
			+" coalesce(detail.persendiscount,0.0) as diskon, "
			+" coalesce(detail.persenppn,0.0) as ppn, "
			+" kartuPengendaliDetail.noRec as noRecKartuPengendaliDetail, "
			+" satuanStandar.satuanStandar as satuanStandar,  "
			+" satuanStandar.id as idSatuan,"
			+" produk.namaProduk as namaProduk,"
			+" spek.volumeBarang as volumeKontrak, "
			+" coalesce(kondisiBarang.name,'-') as kondisiBarang, "
			+" kondisiBarang.id as idKondisiBarang) "
			+" from StrukPelayananDetail detail "
			+" left join detail.nostruk header "
			+" left join detail.kondisiBarang kondisiBarang "
			+" right join detail.kartuPengendaliDetail kartuPengendaliDetail "
			+" left join kartuPengendaliDetail.detailRup detailRup "
			+" left join detailRup.detailSpekAnggaran spek "
			+" left join spek.produk produk "
			+" left join spek.detailAnggaran detailAnggaran  "
			+" left join detailAnggaran.asalProduk asalProduk  "
			+" left join kartuPengendaliDetail.kartuPengendali kartuPengendali "  
			+" left join spek.produk produk  "
			+" left join spek.pengendali pengendali "
			+" left join detailAnggaran.akun akun "
			+" left join detailAnggaran.asalProduk asalProduk "
			+" left join produk.satuanStandar satuanStandar "
			+" left join kartuPengendaliDetail.strukRekapPemenang strukRekapPemenang "
			+" where kartuPengendaliDetail.noRec is not null and detail.noRec=:noRec ")
	Map<String,Object> getStrukPelayananDetail(@Param("noRec")String noRec);

	StrukPelayananDetail findByNoRec(String noRec);

}
