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

/**
 * Repository class for Jabatan
 * 
 * @author Generator
 */
@Repository("JabatanDao")
public interface JabatanDao extends PagingAndSortingRepository<Jabatan, Integer> {

	@Query("SELECT new map (model.id as id) from Jabatan model where lower(model.namaJabatan) like lower('dokter%') ")
	List<Map<String, Object>> findIdJabatanInternalDokterOnly();

	@Query("SELECT new map (model.id as id,model.namaJabatan as namaJabatan) "
			+ "from Jabatan model where model.statusEnabled is true")
	List<Map<String, Object>> findAllJabatan();

	@Query("SELECT new Map (model.id as id,model.namaJabatan as namaJabatan,model.kdJabatan as kdJabatan) "
			+ "from Jabatan model where model.id =:id and model.statusEnabled is true")
	Map<String, Object> getJabatanById(@Param("id") Integer id);

	@Query("SELECT jenisJabatanId from Jabatan model where model.id =:id and model.statusEnabled is true")
	Integer getIdJenisJabatanByJabatan(@Param("id") Integer id);

	@Query("SELECT model from Jabatan model where model.jenisJabatanId = :jenisJabatanId ")
	List<Jabatan> getJabatanStruktural(@Param("jenisJabatanId") Integer jenisJabatanId);

	String strJabatanByJenis = "SELECT new Map(jb.id as idJabatan, jb.namaJabatan as namaJabatan) from Jabatan jb "
			+ "where jb.statusEnabled is true " + "and jb.jenisJabatanId = :jenisJabatanId";

	String byUnitKerja = " and jb.unitKerjaId = :unitKerjaId";

	String sortNamaJabatan = " order by jb.namaJabatan";

	@Query(strJabatanByJenis + sortNamaJabatan)
	List<Map<String, Object>> getJabatanByJenisJabatan(@Param("jenisJabatanId") Integer jenisJabatanId);

	@Query(strJabatanByJenis + byUnitKerja + sortNamaJabatan)
	List<Map<String, Object>> findModelJabatanByJenisUnit(@Param("jenisJabatanId") Integer jenisJabatanId,
			@Param("unitKerjaId") Integer idUnitKerja);

	@Query("select new Map(jab.id as id, jab.namaJabatan as namaJabatan) "
			+ "from Jabatan jab where jab.statusEnabled is true " + "and jab.jenisJabatanId in (1,5) "
			+ "and lower(jab.namaJabatan) not like ('plh%')")
	List<Map<String, Object>> getListJabatanFungsionalStruktural();

	String strDuplikatJabatan = "select new Map(jb.id as id, jb.namaJabatan as namaJabatan) "
			+ "from Jabatan jb where jb.statusEnabled is true "
			+ "and lower(regexp_replace(jb.namaJabatan,'[^0-9a-zA-Z]+', '', 'g')) = :namaJabatan "
			+ "and jb.jenisJabatanId = :jenisJabatanId";

	String unitKerjaId = " and jb.unitKerjaId = :unitKerjaId";

	String notJabatanId = " and jb.id <> :jabatanId";

	@Query(strDuplikatJabatan)
	List<Map<String, Object>> checkNamaJabatan(@Param("namaJabatan") String namaJabatan,
			@Param("jenisJabatanId") Integer jenisJabatanId);

	@Query(strDuplikatJabatan + notJabatanId)
	List<Map<String, Object>> checkNamaJabatan(@Param("jabatanId") Integer idJabatan,
			@Param("namaJabatan") String namaJabatan, @Param("jenisJabatanId") Integer idJenisJabatan);

	@Query(strDuplikatJabatan + unitKerjaId)
	List<Map<String, Object>> checkNamaJabatan(@Param("namaJabatan") String namaJabatan,
			@Param("jenisJabatanId") Integer idJenisJabatan, @Param("unitKerjaId") Integer idUnitKerja);

	@Query(strDuplikatJabatan + notJabatanId + unitKerjaId)
	List<Map<String, Object>> checkNamaJabatan(@Param("jabatanId") Integer idJabatan,
			@Param("namaJabatan") String namaJabatan, @Param("jenisJabatanId") Integer idJenisJabatan,
			@Param("unitKerjaId") Integer idUnitKerja);

	String strJabatan = "select new Map(jb.id as id,jb.namaJabatan as namaJabatan,"
			+ "jb.levelJabatan as levelJabatan,jb.subLevelJabatan as levelDireksi,"
			+ "jj.id as jenisJabatanId,jj.jenisJabatan as jenisJabatan," + "uk.id as unitKerjaId,uk.name as unitKerja,"
			+ "el.id as eselonId,el.eselon as eselon,"
			+ "kj.id as kelompokJabatanId,kj.namaKelompokJabatan as kelompokJabatan) " + "from Jabatan jb "
			+ "inner join jb.jenisJabatan jj "
			+ "left join jb.unitKerja uk with (uk.id <> 0 and uk.statusEnabled is true) "
			+ "left join jb.eselon el with (el.id <> 0 and el.statusEnabled is true) "
			+ "left join jb.kelompokJabatan kj with (kj.id between 1 and 10 and kj.statusEnabled is true) "
			+ "where jb.statusEnabled is true " + "and jj.statusEnabled is true ";

	String namaJabatan = " and lower(jb.namaJabatan) like %:namaJabatan%";

	String jenisJabatan = " and jj.id = :jenisJabatanId";

	String unitKerja = " and uk.id = :unitKerjaId";

	String sortMasterJabatan = " order by jb.namaJabatan, uk.name, jj.jenisJabatan";

	@Query(strJabatan + sortMasterJabatan)
	List<Map<String, Object>> findJabatan();

	@Query(strJabatan + namaJabatan + sortMasterJabatan)
	List<Map<String, Object>> findJabatanByNama(@Param("namaJabatan") String namaJabatan);

	@Query(strJabatan + jenisJabatan + sortMasterJabatan)
	List<Map<String, Object>> findJabatanByJenis(@Param("jenisJabatanId") Integer idJenisJabatan);

	@Query(strJabatan + unitKerja + sortMasterJabatan)
	List<Map<String, Object>> findJabatanByUnit(@Param("unitKerjaId") Integer idUnitKerja);

	@Query(strJabatan + namaJabatan + jenisJabatan + sortMasterJabatan)
	List<Map<String, Object>> findJabatanByNamaJenis(@Param("namaJabatan") String namaJabatan,
			@Param("jenisJabatanId") Integer idJenisJabatan);

	@Query(strJabatan + namaJabatan + unitKerja + sortMasterJabatan)
	List<Map<String, Object>> findJabatanByNamaUnit(@Param("namaJabatan") String namaJabatan,
			@Param("unitKerjaId") Integer idUnitKerja);

	@Query(strJabatan + jenisJabatan + unitKerja + sortMasterJabatan)
	List<Map<String, Object>> findJabatanByJenisUnit(@Param("jenisJabatanId") Integer idJenisJabatan,
			@Param("unitKerjaId") Integer idUnitKerja);

	@Query(strJabatan + namaJabatan + jenisJabatan + unitKerja + sortMasterJabatan)
	List<Map<String, Object>> findJabatanByNamaJenisUnit(@Param("namaJabatan") String namaJabatan,
			@Param("jenisJabatanId") Integer idJenisJabatan, @Param("unitKerjaId") Integer idUnitKerja);

	@Query("select new Map(jb.id as id,jb.namaJabatan as namaJabatan," + "jb.kelompokJabatanId as kelompokJabatanId) "
			+ "from Jabatan jb " + "where jb.statusEnabled is true " + "and jb.unitKerjaId = :unitKerjaId "
			+ "and jb.jenisJabatanId = :jenisJabatanId " + "group by jb.id, jb.namaJabatan "
			+ "order by jb.namaJabatan")
	List<Map<String, Object>> findJabatan(@Param("jenisJabatanId") Integer idJenisJabatan,
			@Param("unitKerjaId") Integer idUnitKerja);

	@Query("select new Map(nj.kelompokJabatanId as kelompokJabatanId," + "min(nj.nilaiTerendah) as nilaiTerendah,"
			+ "max(nj.nilaiTertinggi) as nilaiTertinggi) " + "from NilaiKelompokJabatan nj "
			+ "where nj.statusEnabled is true " + "and nj.kelompokJabatanId in (:listKelompokJabatanId) "
			+ "group by nj.kelompokJabatanId " + "order by min(nj.nilaiTerendah)")
	List<Map<String, Object>> findPeringkatJabatan(@Param("listKelompokJabatanId") List<Integer> listIdKelompokJabatan);

	@Query("select new Map(jb.id as jabatanId,jb.namaJabatan as namaJabatan) " + "from Jabatan jb "
			+ "where id in (:listJabatanId) " + "and jb.kelompokJabatanId in (3,4) " + "and jb.statusEnabled is true "
			+ "order by jb.namaJabatan")
	List<Map<String, Object>> findJabatanByListId(@Param("listJabatanId") List<Integer> listIdJabatan);

	@Query("select distinct new Map(jb.id as id, jb.namaJabatan as jabatan, (jb.id || ' - ' || jb.namaJabatan) as namaJabatan) "
			+ "from Jabatan jb " + "where jb.statusEnabled is true " + "and jb.unitKerjaId = :unitKerjaId "
			+ "order by jb.namaJabatan, jb.id")
	List<Map<String, Object>> findByUnitKerja(@Param("unitKerjaId") Integer idUnitKerja);

}
