package com.jasamedika.medifirst2000.dao;

import com.jasamedika.medifirst2000.entities.MapJabatanProfesi;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

/**
 * @author salmanoe
 * @since Dec 31, 2021
 */
public interface MapJabatanProfesiDao extends JpaRepository<MapJabatanProfesi, Integer> {

	@Query("select new Map(mjp.id as id," + "prf.id as profesiId,prf.namaProfesi as profesi,"
			+ "jb.id as jabatanId,(jb.id || ' - ' ||jb.namaJabatan) as jabatan) " + "from MapJabatanProfesi mjp "
			+ "inner join mjp.profesi prf " + "inner join mjp.jabatan jb " + "where mjp.statusEnabled is true "
			+ "order by jb.namaJabatan, jb.id, prf.namaProfesi")
	List<Map<String, Object>> findMapping();

	@Query(value = "select mjp.* from sdm_mapjabatanprofesi_m mjp where mjp.jabatanfk = :jabatanId limit 1", nativeQuery = true)
	Optional<MapJabatanProfesi> findByJabatan(@Param("jabatanId") Integer idJabatan);

	@Query("select distinct mjj.pegawaiId " +
			"from MapJabatanProfesi mjp, MapPegawaiJabatanToUnitKerja mjj " +
			"where mjp.jabatanId = mjj.jabatanId " +
			"and mjp.statusEnabled is true " +
			"and mjj.statusEnabled is true " +
			"and mjp.profesiId in (:listProfesiId)")
	List<Integer> findIdPegawaiByProfesiIn(@Param("listProfesiId") List<Integer> listIdProfesi);
}
