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

@Repository("HargaNettoProdukByKelasDao")
public interface HargaNettoProdukByKelasDao extends PagingAndSortingRepository<HargaNettoProdukByKelas, Integer> {

	@Query("select model from HargaNettoProdukByKelas model where model.produk.id=:produkId and model.kelas.id=:kelasId "
			+ "and model.asalProduk.id=:asalProdukId and model.jenisTarif.id=:jenisTarifId")
	public List<HargaNettoProdukByKelas> findProdByKelas(@Param("produkId") Integer produkId,
			@Param("kelasId") Integer kelasId, @Param("asalProdukId") Integer asalProdukId,
			@Param("jenisTarifId") Integer jenisTarifId);

	@Query("select NEW Map(" + "a.hargaNetto1 as hargaNetto1," + "a.hargaNetto2 as hargaNetto2,"
			+ "a.hargaSatuan as hargaSatuan," + "a.factorRate as factorRate," + "a.persenDiscount as persenDiscount) "
			+ "from HargaNettoProdukByKelas a " + "left join a.kelas b " + "left join a.produk c "
			+ "where b.id=:kelasId and c.id=:produkId")
	public Map<String, Object> getProdukByKelas(@Param("kelasId") Integer kelasId, @Param("produkId") Integer produkId);

	@Query("select NEW Map(produk.id as produkId, " + " produk.namaProduk as namaProduk, "
			+ " kelas.id as kelasId, kelas.namaKelas as namaKelas) " + " from HargaNettoProdukByKelas hargaNetto "
			+ " join hargaNetto.produk produk " + " left join hargaNetto.kelas kelas " + " order by produk.id")
	public List<Map<String, Object>> getListprodukInMapping();

	@Query("select new Map(hnk.produkId as produkId,hnk.kelasId as kelasId) " + "from HargaNettoProdukByKelas hnk "
			+ "where hnk.produkId = :produkId " + "and hnk.kelasId = :kelasId " + "and hnk.statusEnabled is true")
	public List<Map<String, Object>> checkExisting(@Param("kelasId") Integer idKelas,
			@Param("produkId") Integer idProduk);

	@Query("select new Map(hnk.produkId as produkId,hnk.kelasId as kelasId) " + "from HargaNettoProdukByKelas hnk "
			+ "where hnk.produkId = :produkId " + "and hnk.kelasId = :kelasId " + "and hnk.id <> :mappingId "
			+ "and hnk.statusEnabled is true")
	public List<Map<String, Object>> checkExisting(@Param("kelasId") Integer idKelas,
			@Param("produkId") Integer idProduk, @Param("mappingId") Integer idMapping);

	@Query("select new Map(hnk.hargaNetto1 as tarif,kls.namaKelas as kelas) " + "from HargaNettoProdukByKelas hnk "
			+ "inner join hnk.kelas kls " + "where hnk.statusEnabled is true " + "and hnk.produkId = :produkId "
			+ "and hnk.kelasId in (:listKelasId) " + "order by hnk.hargaNetto1")
	public List<Map<String, Object>> findTarifTindakan(@Param("produkId") Integer idProduk,
			@Param("listKelasId") List<Integer> listIdKelas);

}
