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

@Repository
public interface SubUnitKerjaDao extends PagingAndSortingRepository<SubUnitKerjaPegawai, Integer> {

	@Query("select model from SubUnitKerjaPegawai model where model.statusEnabled is true and model.id != 0 order by model.name")
	List<SubUnitKerjaPegawai> getAllSubUnitKerja();

	@Query("select model from SubUnitKerjaPegawai model where model.statusEnabled is true and lower(model.name) like lower(:name) order by model.name")
	List<SubUnitKerjaPegawai> getSubUnitKerjaByName(@Param("name") String name);

	@Query("select model from SubUnitKerjaPegawai model where model.statusEnabled is true and model.unitKerjaId = :id order by model.name")
	List<SubUnitKerjaPegawai> getSubUnitKerjaByUnitkerja(@Param("id") Integer id);

	@Query("select distinct new Map(sk.id as subId,sk.name as subName," + "uk.id as unitId,uk.name as unitName) "
			+ "from SubUnitKerjaPegawai sk " + "inner join sk.unitKerja uk " + "where sk.statusEnabled is true "
			+ "and sk.id in (:listSubunitKerjaId) " + "order by sk.name")
	List<Map<String, Object>> findSubunitKerjaById(@Param("listSubunitKerjaId") List<Integer> listIdSubunitKerja);

	@Query("select new Map(model.id as subunitKerjaId,model.unitKerjaId as unitKerjaId) "
			+ "from SubUnitKerjaPegawai model " + "where model.statusEnabled is true "
			+ "and model.id in (:listSubunitKerjaId) " + "order by model.name")
	List<Map<String, Object>> findSubDanUnitKerjaById(@Param("listSubunitKerjaId") List<Integer> listIdSubunitKerja);

	@Query("select subUnitKerja.id " + "from MapPegawaiJabatanToUnitKerja mappeg " + "left join mappeg.pegawai p "
			+ "inner join mappeg.subUnitKerjaPegawai subUnitKerja " + "where mappeg.statusEnabled is true "
			+ "and mappeg.isPrimary is true " + "and mappeg.statusEnabled is true " + "and p.statusEnabled is true "
			+ "and p.id = :idPegawai")
	Integer getByIdPegawai(@Param("idPegawai") Integer idPegawai);

	@Query("select new Map(model.id as id, model.name as name, model.unitKerjaId as unitKerjaId) "
			+ "from SubUnitKerjaPegawai model " + "where model.statusEnabled is true "
			+ "and model.id in (:listSubunitKerjaId) " + "order by model.name")
	List<Map<String, Object>> findSubunitKerja(@Param("listSubunitKerjaId") List<Integer> listIdSubunitKerja);

	@Query("select new Map(model.id as id, model.name as namaSubunitKerja) " + "from SubUnitKerjaPegawai model "
			+ "where model.statusEnabled is true and model.unitKerjaId=:idUnitKerjaPegawai " + "order by model.name")
	List<Map<String, Object>> getSubunitKerjaByUnitKerja(@Param("idUnitKerjaPegawai") Integer idUnitKerjaPegawai);

	@Query("select model.id " + "from SubUnitKerjaPegawai model " + "where model.statusEnabled is true "
			+ "and model.unitKerjaId in (:listUnitKerjaId) " + "order by model.id")
	List<Integer> getSubunitKerjaByUnitKerja(@Param("listUnitKerjaId") List<Integer> listIdUnitKerja);

	/**
	 * @param listIdUnitKerja
	 *            Kelompok unit kerja tertentu
	 * @return Daftar unit kerja dan subunit kerja dari kelompok unit kerja
	 *         tertentu
	 */
	@Query("select new Map(skr.id as subunitKerjaId, skr.name as subunitKerja, "
			+ "ukr.id as unitKerjaId, ukr.name as unitKerja) " + "from SubUnitKerjaPegawai skr "
			+ "inner join skr.unitKerja ukr " + "where skr.statusEnabled is true " + "and ukr.id in (:listUnitKerjaId) "
			+ "order by ukr.name, skr.name")
	List<Map<String, Object>> findSubunitKerjaByUnitKerja(@Param("listUnitKerjaId") List<Integer> listIdUnitKerja);

	@Query("select new Map(model.id as id, model.name as name) " + "from SubUnitKerjaPegawai model "
			+ "where model.id=:id and model.statusEnabled is true")
	Map<String, Object> getSubunitKerjaById(@Param("id") Integer id);

	@Query("select new Map(subunit.id as id, subunit.name as name, subunit.name as names) "
			+ "from SubUnitKerjaPegawai subunit " + "where subunit.statusEnabled is true "
			+ "and subunit.unitKerjaId = :idUnitKerja " + "order by subunit.name")
	List<Map<String, Object>> getSubUnitKerjaCustomByUnitKerja(@Param("idUnitKerja") Integer idUnitKerja);

	@Query("select new Map(ukj.id as unitKerjaId,ukj.name as unitKerja,"
			+ "skj.id as subunitKerjaId,skj.name as subunitKerja) " + "from SubUnitKerjaPegawai skj "
			+ "inner join skj.unitKerja ukj " + "where skj.statusEnabled is true " + "and lower(skj.name) like 'kk%' "
			+ "order by skj.id")
	List<Map<String, Object>> findAllKelompokKerjaMedis();

}
