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

/**
 * Repository class for Produk
 * 
 * @author Chandra
 */
@Repository("MapRuanganToProdukDao")
public interface MapRuanganToProdukDao extends PagingAndSortingRepository<MapRuanganToProduk, Integer> {
	List<MapRuanganToProduk> findByProdukId(Integer id);

	@Query("select model from MapRuanganToProduk model where model.ruangan.id=:ruanganId and status='MA'")
	List<MapRuanganToProduk> findProdukByRuangan(@Param("ruanganId") Integer ruanganId);

	@Query("select NEW Map (" + "c.id as produkId) " + "from MapRuanganToProduk a " + "left join a.ruangan b "
			+ "left join a.produk c " + "where b.id=:ruanganId and c.id=:produkId")
	Map<String, Object> getByRuanganProduk(@Param("ruanganId") Integer ruanganId, @Param("produkId") Integer produkId);

	@Query("select distinct produk.detailJenisProdukId from MapRuanganToProduk model "
			+ "left join model.produk produk " + "left join produk.detailJenisProduk detailJenisProduk "
			+ "where model.ruanganId=:ruanganId")
	List<Integer> findJenisProdukExepetion(@Param("ruanganId") Integer ruanganId);

	@Query("select distinct new Map(pr.id as id, pr.namaProduk as namaProduk, pr.reportDisplay as reportDisplay) "
			+ "from MapRuanganToProduk mapr " + "left join mapr.produk pr "
			+ "where mapr.statusEnabled is true and pr.statusEnabled is true "
			+ "and mapr.ruanganId = :ruanganId and pr.namaProduk like %:namaProduk%")
	List<Map<String, Object>> findProdukRuanganByMapProduk(@Param("ruanganId") Integer ruanganId,
			@Param("namaProduk") String namaProduk);

	@Query("select distinct rng.namaRuangan || ' - ' || dpt.reportDisplay " + "from MapRuanganToProduk mrp "
			+ "inner join mrp.ruangan rng " + "inner join rng.departemen dpt " + "where mrp.kodeExternal = '2017' "
			+ "and mrp.produkId = :produkId " + "and rng.statusEnabled is true " + "and dpt.statusEnabled is true "
			+ "order by rng.namaRuangan || ' - ' || dpt.reportDisplay")
	List<String> findRuanganByTindakan(@Param("produkId") Integer idProduk);

}
