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

/**
 * @author salmanoe
 * @since Mar 9, 2022
 */
@Repository("mapProdukKelompokKerjaDao")
public interface MapProdukKelompokKerjaDao extends PagingAndSortingRepository<MapProdukKelompokKerja, String> {

	String strAllMapping = "select new Map(mpk.noRec as noRec," + "skj.id as kelompokKerjaId,skj.name as kelompokKerja,"
			+ "prd.id as produkId,prd.namaProduk as produk) " + "from MapProdukKelompokKerja mpk "
			+ "inner join mpk.kelompokKerja skj " + "inner join mpk.produk prd " + "where mpk.statusEnabled is true ";

	String whrKelompokKerja = "and mpk.kelompokKerjaId in (:listKelompokKerjaId) ";

	String srtKkProduk = "order by mpk.kelompokKerjaId, mpk.produkId";

	/**
	 * @return Daftar Mapping Produk - Kelompok Kerja
	 */
	@Query(strAllMapping + srtKkProduk)
	List<Map<String, Object>> findAllMapping();

	/**
	 * @param listIdKelompokKerja Daftar ID Kelompok Kerja
	 * @return Daftar Mapping Produk - Kelompok Kerja
	 */
	@Query(strAllMapping + whrKelompokKerja + srtKkProduk)
	List<Map<String, Object>> findAllMapping(@Param("listKelompokKerjaId") List<Integer> listIdKelompokKerja);

	@Query("select distinct new Map(prd.id as id,prd.namaProduk as namaProduk) " + "from MapProdukKelompokKerja mpk "
			+ "inner join mpk.produk prd " + "order by prd.namaProduk")
	List<Map<String, Object>> findProdukMapping();

	@Query("select new Map(mpk.kelompokKerjaId as kelompokKerjaId,mpk.noRec as noRec) "
			+ "from MapProdukKelompokKerja mpk " + "where mpk.produkId = :produkId " + "order by mpk.kelompokKerjaId")
	List<Map<String, Object>> findMappingByProduk(@Param("produkId") Integer idProduk);

}
