package com.jasamedika.medifirst2000.dao.custom.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.constants.Master;
import com.jasamedika.medifirst2000.dao.PegawaiDao;
import com.jasamedika.medifirst2000.dao.custom.PegawaiDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PegawaiJadwalKerja;
import com.jasamedika.medifirst2000.util.CommonUtil;

/**
 * Repository PegawaiDaoCustomImpl class
 * 
 * @author Lukman Hakim
 */
@Repository
public class PegawaiDaoCustomImpl extends CoreDaoImpl<Pegawai> implements PegawaiDaoCustom {

	@Autowired
	private PegawaiDao pegawaiDao;

	public PegawaiDaoCustomImpl() {
		super(Pegawai.class);
	}

	@Override
	public List<Map<String, Object>> findPegawaiByName(String namaPegawai, String idJabatan, Integer idSubUnitKerja) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map ");
		buffer.append(" ( ");
		buffer.append(" pegawai.id as idPegawai, " + " pegawai.nama as nama, " + " pegawai.namaLengkap as namaLengkap, "
				+ " pegawai.tglLahir as tglLahir, " + " pegawai.tempatLahir as tempatLahir,"
				+ " pegawai.gelarDepan as gelarDepan," + " pegawai.gelarBelakang as gelarBelakang,"
				+ " golongan.golonganPegawai as namaGolongan, " + " agama.agama as agama, "
				+ " detailKategoryPegawai.detailKategoryPegawai as detailKategoryPegawai, "
				+ " dokumen.namaJudulDokumen as namaJudulDokumen, " + " eselon.eselon as eselon, "
				+ " golonganDarah.golonganDarah as golonganDarah, " + " jabatanFungsional.namaJabatan as namaJabatan, "
				+ " jabatanInternal.namaJabatan as jabatanInternal, " + " jabatanInternal.id as idJabatanInternal,"
				+ " jabatanLamar.namaJabatan as jabatanLamar, " + " unitKerja.name as unitKerja, "
				+ " unitKerja.id as idUnitKerja, " + " subUnitKerja.name as subUnitKerja, "
				+ " subUnitKerja.id as idSubUnitKerja, " + " jabatanStruktural.namaJabatan as jabatanStruktural, "
				+ " jenisKelamin.jenisKelamin as jenisKelamin, " + " jenisPegawai.jenisPegawai as jenisPegawai, "
				+ " jenisPegawaiLamar.jenisPegawai as jenisPegawaiLamar, "
				+ " kategoriPegawai.kategoryPegawai as kategoriPegawai, " + " kedudukan.name as kedudukan, "
				+ " kualifikasiJurusan.kualifikasiJurusan as kualifikasiJurusan, "
				+ " negara.namaNegara as namaNegara,  " + " pangkat.namaPangkat as namaPangkat, "
				+ " pendidikan.namaPendidikan as namaPendidikan, "
				+ " penghasilanTidakKenaPajak.deskripsi as penghasilanTidakKenaPajak, "
				+ " range.namaRange as namaRange, " + " pegawai.nipPns as nipPns, " + " pegawai.nip as nip, "
				+ " pegawai.nikIntern as nikIntern, " + " pegawai.idFinger as idFinger, " + " ruangan.id as idRuangan, "
				+ " ruangan.namaRuangan as namaRuangan, " + " satuanKerja.satuanKerja as satuanKerja, "
				+ " shiftKerja.name as shiftKerja, " + " shiftKerja.id as idKelompokShiftKerja,"
				+ " statusKawin.statusPerkawinan as statusKawin, " + " statusPegawai.statusPegawai as statusPegawai, "
				+ " statusPerkawinanPegawai.statusPerkawinan as statusPerkawinanPegawai, " + " suku.suku as suku, "
				+ " titlePasien.namaTitle as titlePasien, " + " typePegawai.typePegawai as typePegawai) "
				+ " from MapPegawaiJabatanToUnitKerja mapPegawai  " + " left join mapPegawai.pegawai pegawai "
				+ " left join pegawai.golonganPegawai golongan  " + " left join pegawai.agama agama "
				+ " left join pegawai.detailKategoryPegawai detailKategoryPegawai "
				+ " left join pegawai.dokumen dokumen " + " left join pegawai.eselon eselon "
				+ " left join pegawai.golonganDarah golonganDarah "
				+ " left join pegawai.jabatanFungsional jabatanFungsional "
				+ " left join mapPegawai.subUnitKerjaPegawai subUnitKerja "
				+ " left join mapPegawai.unitKerjaPegawai unitKerja " + " left join mapPegawai.jabatan jabatanInternal "
				+ " left join pegawai.jabatanLamar jabatanLamar "
				+ " left join pegawai.jabatanStruktural jabatanStruktural "
				+ " left join pegawai.jenisKelamin jenisKelamin " + " left join pegawai.jenisPegawai jenisPegawai "
				+ " left join pegawai.jenisPegawaiLamar jenisPegawaiLamar "
				+ " left join pegawai.kategoryPegawai kategoriPegawai " + " left join pegawai.kedudukan kedudukan "
				+ " left join pegawai.kualifikasiJurusan kualifikasiJurusan " + " left join pegawai.negara negara "
				+ " left join pegawai.pangkat pangkat " + " left join pegawai.pendidikan pendidikan "
				+ " left join pegawai.penghasilanTidakKenaPajak penghasilanTidakKenaPajak "
				+ " left join pegawai.range range " + " left join pegawai.ruangan ruangan "
				+ " left join pegawai.satuanKerja satuanKerja " + " left join pegawai.shiftKerja shiftKerja "
				+ " left join pegawai.statusKawin statusKawin " + " left join pegawai.statusPegawai statusPegawai "
				+ " left join pegawai.statusPerkawinanPegawai statusPerkawinanPegawai "
				+ " left join pegawai.suku suku  " + " left join pegawai.titlePasien titlePasien "
				+ " left join pegawai.typePegawai typePegawai ");
		buffer.append(
				" where mapPegawai.isPrimary is true and mapPegawai.statusEnabled is true and pegawai.statusEnabled is true");
		boolean isUndefined = namaPegawai.equalsIgnoreCase("undefined");
		if (CommonUtil.isNotNullOrEmpty(namaPegawai)) {
			if (!isUndefined) {
				buffer.append(" and lower (pegawai.namaLengkap) like lower ('%" + namaPegawai + "%')  ");
			}
		}
		boolean isUndefinedidJabatan = idJabatan.equalsIgnoreCase("undefined");
		if (CommonUtil.isNotNullOrEmpty(idJabatan)) {
			if (!isUndefinedidJabatan) {
				buffer.append(" and jabatanInternal.id = " + idJabatan + "  ");
			}
		}
		boolean isUndefinedIdSubUnitKerja = idJabatan.equalsIgnoreCase("undefined");
		if (CommonUtil.isNotNullOrEmpty(idSubUnitKerja)) {
			if (!isUndefinedIdSubUnitKerja) {
				buffer.append(" and unitKerja.id = " + idSubUnitKerja + "  ");
			}
		}

		buffer.append("order by pegawai.namaLengkap ");

		Query query = em.createQuery(buffer.toString());

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findPegawaiByNamePesertaDidik(String namaPegawai) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map");
		buffer.append(" ( ");
		buffer.append(" p.nipPns , p.namaLengkap ,p.tglLahir , " + "p.tempatLahir , p.noHandphone," + "p.alamat , "
				+ "jabatanInternal.kdJabatan,jabatanInternal.namaJabatan, " + "golongan.id,golongan.golonganPegawai, "
				+ " statusKawin.statusPerkawinan, statusKawin.id, " + "agama.agama, agama.kdAgama, "
				+ " satuanKerja.kdSatuanKerja,satuanKerja.satuanKerja , "
				+ " jabatanStruktural.kdJabatan ,jabatanStruktural.namaJabatan, "
				+ " jenisKelamin.jenisKelamin,jenisKelamin.id,"
				+ " jabatanFungsional.kdJabatan ,jabatanFungsional.namaJabatan");
		buffer.append(" ) ");
		buffer.append("from Pegawai p " + " left join p.jabatanInternal jabatanInternal"
				+ " left join p.golonganPegawai golongan " + " left join p.statusKawin statusKawin "
				+ " left join p.agama agama " + " left join p.satuanKerja satuanKerja "
				+ " left join p.jabatanStruktural jabatanStruktural "
				+ " left join p.jabatanFungsional jabatanFungsional " + " left join p.jenisKelamin jenisKelamin "
				+ " where lower (p.namaLengkap) like lower ('%" + namaPegawai + "%')  " + " order by p.namaLengkap ");

		Query query = em.createQuery(buffer.toString());

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findAllPegawaiByJenisPegawaiPengajar() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map");
		buffer.append(" ( ");
		buffer.append(" p.nipPns , p.namaLengkap ,p.tglLahir , " + "p.tempatLahir , p.noHandphone," + "p.alamat , "
				+ "jabatanInternal.kdJabatan,jabatanInternal.namaJabatan, " + "golongan.id,golongan.golonganPegawai, "
				+ " statusKawin.statusPerkawinan, statusKawin.id, " + "agama.agama, agama.kdAgama, "
				+ " satuanKerja.kdSatuanKerja,satuanKerja.satuanKerja , "
				+ " jabatanStruktural.kdJabatan ,jabatanStruktural.namaJabatan, "
				+ " jenisKelamin.jenisKelamin,jenisKelamin.id,"
				+ " jabatanFungsional.kdJabatan ,jabatanFungsional.namaJabatan");
		buffer.append(" ) ");
		buffer.append("from Pegawai p " + " left join p.jabatanInternal jabatanInternal"
				+ " left join p.golonganPegawai golongan " + " left join p.statusKawin statusKawin "
				+ " left join p.agama agama " + " left join p.satuanKerja satuanKerja "
				+ " left join p.jabatanStruktural jabatanStruktural "
				+ " left join p.jabatanFungsional jabatanFungsional " + " left join p.jenisKelamin jenisKelamin "
				+ " left join p.jenisPegawai jenisPegawai " + " where p.statusEnabled is true and jenisPegawai=12  "
				+ " order by p.namaLengkap ");

		Query query = em.createQuery(buffer.toString());

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findAllPegawaiByJenisPegawaiPesertaDidik() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map ");
		buffer.append(" ( ");
		buffer.append(" pegawai.id as idPegawai, " + " pegawai.namaLengkap as nama, "
				+ " pegawai.tglLahir as tglLahir, " + " pegawai.tempatLahir as tempatLahir,"
				+ " golongan.name as namaGolongan, " + " agama.agama as agama, "
				+ " detailKategoryPegawai.detailKategoryPegawai as detailKategoryPegawai, "
				+ " dokumen.namaJudulDokumen as namaJudulDokumen, " + " eselon.eselon as eselon, "
				+ " golonganDarah.golonganDarah as golonganDarah, " + " jabatanFungsional.namaJabatan as namaJabatan, "
				+ " jabatanInternal.namaJabatan as jabatanInternal, " + " jabatanInternal.id as idJabatanInternal,"
				+ " jabatanLamar.namaJabatan as jabatanLamar, "
				+ " jabatanStruktural.namaJabatan as jabatanStruktural, "
				+ " jenisKelamin.jenisKelamin as jenisKelamin, " + " jenisPegawai.jenisPegawai as jenisPegawai, "
				+ " jenisPegawaiLamar.jenisPegawai as jenisPegawaiLamar, "
				+ " kategoriPegawai.kategoryPegawai as kategoriPegawai, " + " kedudukan.name as kedudukan, "
				+ " kualifikasiJurusan.kualifikasiJurusan as kualifikasiJurusan, "
				+ " negara.namaNegara as namaNegara,  " + " pangkat.namaPangkat as namaPangkat, "
				+ " pendidikan.namaPendidikan as namaPendidikan, "
				+ " penghasilanTidakKenaPajak.deskripsi as penghasilanTidakKenaPajak, "
				+ " range.namaRange as namaRange, " + " pegawai.nipPns as nipPns, " + " pegawai.nip as nip, "
				+ " pegawai.nikIntern as nikIntern, " + " pegawai.idFinger as idFinger, " + " ruangan.id as idRuangan, "
				+ " ruangan.namaRuangan as namaRuangan, " + " satuanKerja.satuanKerja as satuanKerja, "
				+ " shiftKerja.name as shiftKerja, " + " shiftKerja.id as idKelompokShiftKerja,"
				+ " statusKawin.statusPerkawinan as statusKawin, " + " statusPegawai.statusPegawai as statusPegawai, "
				+ " statusPerkawinanPegawai.statusPerkawinan as statusPerkawinanPegawai, " + " suku.suku as suku, "
				+ " titlePasien.namaTitle as titlePasien, " + " typePegawai.typePegawai as typePegawai) "
				+ " MapPegawaiJabatanToUnitKerja mapPegawai  " + " left join mapPegawai.pegawai pegawai "
				+ " left join pegawai.golonganPegawai golongan  " + " left join pegawai.agama agama "
				+ " left join pegawai.detailKategoryPegawai detailKategoryPegawai "
				+ " left join pegawai.dokumen dokumen " + " left join pegawai.eselon eselon "
				+ " left join pegawai.golonganDarah golonganDarah "
				+ " left join pegawai.jabatanFungsional jabatanFungsional "
				+ " left join mapPegawai.jabatan jabatanInternal " + " left join pegawai.jabatanLamar jabatanLamar "
				+ " left join pegawai.jabatanStruktural jabatanStruktural "
				+ " left join pegawai.jenisKelamin jenisKelamin " + " left join pegawai.jenisPegawai jenisPegawai "
				+ " left join pegawai.jenisPegawaiLamar jenisPegawaiLamar "
				+ " left join pegawai.kategoryPegawai kategoriPegawai " + " left join pegawai.kedudukan kedudukan "
				+ " left join pegawai.kualifikasiJurusan kualifikasiJurusan " + " left join pegawai.negara negara "
				+ " left join pegawai.pangkat pangkat " + " left join pegawai.pendidikan pendidikan "
				+ " left join pegawai.penghasilanTidakKenaPajak penghasilanTidakKenaPajak "
				+ " left join pegawai.range range " + " left join pegawai.ruangan ruangan "
				+ " left join pegawai.satuanKerja satuanKerja " + " left join pegawai.shiftKerja shiftKerja "
				+ " left join pegawai.statusKawin statusKawin " + " left join pegawai.statusPegawai statusPegawai "
				+ " left join pegawai.statusPerkawinanPegawai statusPerkawinanPegawai "
				+ " left join pegawai.suku suku  " + " left join pegawai.titlePasien titlePasien "
				+ " left join pegawai.typePegawai typePegawai ");
		buffer.append(
				"where mapPegawai.isPrimary is true and mapPegawai.statusEnabled is true and pegawai.statusEnabled is true and jenisPegawai.id = 13 ");

		buffer.append("order by pegawai.namaLengkap ");

		Query query = em.createQuery(buffer.toString());

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findPegawaiByName(String namaPegawai) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select ");
		buffer.append(" new map ");
		buffer.append(" ( ");
		buffer.append(" pegawai.id as idPegawai, " + " pegawai.namaLengkap as nama, "
				+ " pegawai.tglLahir as tglLahir, " + " pegawai.tempatLahir as tempatLahir,"
				+ " golongan.golonganPegawai as namaGolongan, " + " agama.agama as agama, "
				+ " detailKategoryPegawai.detailKategoryPegawai as detailKategoryPegawai, "
				+ " dokumen.namaJudulDokumen as namaJudulDokumen, " + " eselon.eselon as eselon, "
				+ " golonganDarah.golonganDarah as golonganDarah, " + " jabatanFungsional.namaJabatan as namaJabatan, "
				+ " jabatanInternal.namaJabatan as jabatanInternal, " + " jabatanInternal.id as idJabatanInternal,"
				+ " jabatanLamar.namaJabatan as jabatanLamar, "
				+ " jabatanStruktural.namaJabatan as jabatanStruktural, "
				+ " jenisKelamin.jenisKelamin as jenisKelamin, " + " jenisPegawai.jenisPegawai as jenisPegawai, "
				+ " jenisPegawaiLamar.jenisPegawai as jenisPegawaiLamar, "
				+ " kategoriPegawai.kategoryPegawai as kategoriPegawai, " + " kedudukan.name as kedudukan, "
				+ " kualifikasiJurusan.kualifikasiJurusan as kualifikasiJurusan, "
				+ " negara.namaNegara as namaNegara,  " + " pangkat.namaPangkat as namaPangkat, "
				+ " pendidikan.namaPendidikan as namaPendidikan, "
				+ " penghasilanTidakKenaPajak.deskripsi as penghasilanTidakKenaPajak, "
				+ " range.namaRange as namaRange, " + " pegawai.nipPns as nipPns, " + " pegawai.nip as nip, "
				+ " pegawai.nikIntern as nikIntern, " + " pegawai.idFinger as idFinger, " + " ruangan.id as idRuangan, "
				+ " ruangan.namaRuangan as namaRuangan, " + " satuanKerja.satuanKerja as satuanKerja, "
				+ " shiftKerja.name as shiftKerja, " + " shiftKerja.id as idKelompokShiftKerja,"
				+ " statusKawin.statusPerkawinan as statusKawin, " + " statusPegawai.statusPegawai as statusPegawai, "
				+ " statusPerkawinanPegawai.statusPerkawinan as statusPerkawinanPegawai, " + " suku.suku as suku, "
				+ " titlePasien.namaTitle as titlePasien, " + " typePegawai.typePegawai as typePegawai) "
				+ " from MapPegawaiJabatanToUnitKerja mapPegawai  " + " left join mapPegawai.pegawai pegawai "
				+ " left join pegawai.golonganPegawai golongan  " + " left join pegawai.agama agama "
				+ " left join pegawai.detailKategoryPegawai detailKategoryPegawai "
				+ " left join pegawai.dokumen dokumen " + " left join pegawai.eselon eselon "
				+ " left join pegawai.golonganDarah golonganDarah "
				+ " left join pegawai.jabatanFungsional jabatanFungsional "
				+ " left join mapPegawai.jabatan jabatanInternal " + " left join pegawai.jabatanLamar jabatanLamar "
				+ " left join pegawai.jabatanStruktural jabatanStruktural "
				+ " left join pegawai.jenisKelamin jenisKelamin " + " left join pegawai.jenisPegawai jenisPegawai "
				+ " left join pegawai.jenisPegawaiLamar jenisPegawaiLamar "
				+ " left join pegawai.kategoryPegawai kategoriPegawai " + " left join pegawai.kedudukan kedudukan "
				+ " left join pegawai.kualifikasiJurusan kualifikasiJurusan " + " left join pegawai.negara negara "
				+ " left join pegawai.pangkat pangkat " + " left join pegawai.pendidikan pendidikan "
				+ " left join pegawai.penghasilanTidakKenaPajak penghasilanTidakKenaPajak "
				+ " left join pegawai.range range " + " left join pegawai.ruangan ruangan "
				+ " left join pegawai.satuanKerja satuanKerja " + " left join pegawai.shiftKerja shiftKerja "
				+ " left join pegawai.statusKawin statusKawin " + " left join pegawai.statusPegawai statusPegawai "
				+ " left join pegawai.statusPerkawinanPegawai statusPerkawinanPegawai "
				+ " left join pegawai.suku suku  " + " left join pegawai.titlePasien titlePasien "
				+ " left join pegawai.typePegawai typePegawai ");
		buffer.append(
				" where mapPegawai.isPrimary is true and mapPegawai.statusEnabled is true and pegawai.statusEnabled is true and 1=1 ");
		boolean isUndefined = namaPegawai.equalsIgnoreCase("undefined");
		if (CommonUtil.isNotNullOrEmpty(namaPegawai)) {
			if (!isUndefined) {
				buffer.append(" and lower (pegawai.namaLengkap) like lower ('%" + namaPegawai + "%')  ");
			}
		}

		buffer.append("order by pegawai.namaLengkap ");

		Query query = em.createQuery(buffer.toString());

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findPegawaiCustom(String nama, Integer idUnitKerja, Integer idKedudukan,
			List<Integer> listStatusPegawaiId, String periode, List<Integer> listKedudukanException) {
		StringBuffer buffer = new StringBuffer();
		List<Map<String, Object>> list = new ArrayList<>();

		List<Integer> listPegawaiByUnitKerja = new ArrayList<>();
		if (CommonUtil.isNotNullOrEmpty(idUnitKerja)) {
			listPegawaiByUnitKerja = pegawaiDao.getPegawaiByIdUnitKerja(idUnitKerja);
			if (CommonUtil.isNullOrEmpty(listPegawaiByUnitKerja)) {
				return list;
			}
		}

		buffer.append("select new map (pegawai.id as idPegawai," + "pegawai.nama as nama,"
				+ "pegawai.namaLengkap as namaLengkap," + "pegawai.tglLahir as tglLahir,"
				+ "pegawai.qtyAnak as qtyAnak," + "pegawai.qtyTanggungan as qtyTanggungan,"
				+ "pegawai.qtyTotalJiwa as qtyTotalJiwa," + "kelompokJabatan.detailKelompokJabatan as kelompokJabatan,"
				+ "pegawai.tglMasuk as tglMasuk," + "pegawai.tglkeluar as tglKeluar," + "pegawai.kodeBank as kodeBank,"
				+ "pegawai.bankRekeningAtasNama as bankRekeningAtasNama,"
				+ "pegawai.bankRekeningNama as bankRekeningNama," + "pegawai.bankRekeningNomor as bankRekeningNomor,"
				+ "pegawai.kodeGapokId as idKodeGapok," + "pegawai.email as email,"
				+ "pegawai.emailAlternatif as emailAlternatif," + "pegawai.noTlp as noTlp,"
				+ "pegawai.noHandphone as noHandphone," + "pegawai.npwp as npwp," + "pegawai.noBPJS as noBPJS,"
				+ "pegawai.noCm as noCm," + "pegawai.alamat as alamat," + "pegawai.kodePos as kodePos,"
				+ "pegawai.nilaiJabatan as nilaiJabatan," + "levelTingkat.id as idLevelTingkat,"
				+ "levelTingkat.levelTingkat as levelTingkat," + "pegawai.grade as grade," + "pegawai.noSip as noSip,"
				+ "pegawai.tglTerbitSip as tglTerbitSip," + "pegawai.tglBerakhirSip as tglBerakhirSip,"
				+ "pegawai.noStr as noStr," + "pegawai.tglTerbitStr as tglTerbitStr,"
				+ "pegawai.tglBerakhirStr as tglBerakhirStr," + "pegawai.gelarDepan as gelarDepan,"
				+ "pegawai.gelarBelakang as gelarBelakang," + "pegawai.tempatLahir as tempatLahir,"
				+ "golongan.golonganPegawai as namaGolongan," + "agama.agama as agama,"
				+ "detailKategoryPegawai.detailKategoryPegawai as detailKategoryPegawai,"
				+ "dokumen.namaJudulDokumen as namaJudulDokumen," + "eselon.eselon as eselon,"
				+ "golonganDarah.golonganDarah as golonganDarah," + "pegawai.statusRhesus as rhesusGolonganDarah,"
				+ "jabatanFungsional.namaJabatan as namaJabatan," + "jabatanLamar.namaJabatan as jabatanLamar,"
				+ "jabatanStruktural.namaJabatan as jabatanStruktural," + "jenisKelamin.jenisKelamin as jenisKelamin,"
				+ "jenisPegawai.jenisPegawai as jenisPegawai," + "jenisPegawaiLamar.jenisPegawai as jenisPegawaiLamar,"
				+ "kategoriPegawai.kategoryPegawai as kategoriPegawai," + "kedudukan.id as kedudukanId,"
				+ "kedudukan.name as kedudukan," + "kualifikasiJurusan.kualifikasiJurusan as kualifikasiJurusan,"
				+ "negara.namaNegara as namaNegara," + "pangkat.namaPangkat as namaPangkat,"
				+ "pendidikan.namaPendidikan as namaPendidikan,"
				+ "penghasilanTidakKenaPajak.deskripsi as penghasilanTidakKenaPajak,"
				+ "pegawai.totalNilaiScore as totalNilaiScore," + "pegawai.tunjanganFungsional as tunjanganFungsional,"
				+ "pegawai.tunjanganPapua as tunjanganPapua," + "pegawai.tunjanganUmum as tunjanganUmum,"
				+ "pegawai.noIdentitas as noIdentitas," + "pegawai.nipPns as nipPns," + "pegawai.nip as nip,"
				+ "pegawai.nikIntern as nikIntern," + "pegawai.tanggalMeninggal as tanggalMeninggal,"
				+ "pegawai.pensiun as usiaPensiun," + "pegawai.tglPensiun as tglPensiun,"
				+ "pegawai.idFinger as idFinger," + "ruangan.id as idRuangan," + "ruangan.namaRuangan as namaRuangan,"
				+ "rekanan.id as idRuangan," + "rekanan.namaRekanan as namaRekanan,"
				+ "satuanKerja.satuanKerja as satuanKerja," + "shiftKerja.name as shiftKerja,"
				+ "shiftKerja.id as idKelompokShiftKerja," + "statusPegawai.statusPegawai as statusPegawai,"
				+ "statusKawin.statusPerkawinan as statusKawin,"
				+ "statusPerkawinanPegawai.statusPerkawinan as statusPerkawinanPegawai," + "suku.suku as suku,"
				+ "typePegawai.typePegawai as typePegawai," + "pegawai.statusEnabled as statusEnabled) "
				+ "from Pegawai pegawai " + "left join pegawai.detailKategoryPegawai detailKategoryPegawai "
				+ "left join pegawai.agama agama " + "left join pegawai.detailKelompokJabatan kelompokJabatan "
				+ "left join pegawai.dokumen dokumen " + "left join pegawai.eselon eselon "
				+ "left join pegawai.golonganDarah golonganDarah "
				+ "left join pegawai.jabatanFungsional jabatanFungsional "
				+ "left join pegawai.jabatanLamar jabatanLamar "
				+ "left join pegawai.jabatanStruktural jabatanStruktural "
				+ "left join pegawai.jenisKelamin jenisKelamin " + "left join pegawai.jenisPegawai jenisPegawai "
				+ "left join pegawai.jenisPegawaiLamar jenisPegawaiLamar "
				+ "left join pegawai.kategoryPegawai kategoriPegawai " + "left join pegawai.kedudukan kedudukan "
				+ "left join pegawai.kualifikasiJurusan kualifikasiJurusan "
				+ "left join pegawai.levelTingkat levelTingkat " + "left join pegawai.negara negara "
				+ "left join pegawai.pangkat pangkat " + "left join pangkat.kdGolonganPegawai golongan "
				+ "left join pegawai.pendidikan pendidikan "
				+ "left join pegawai.penghasilanTidakKenaPajak penghasilanTidakKenaPajak "
				+ "left join pegawai.ruangan ruangan " + "left join pegawai.satuanKerja satuanKerja "
				+ "left join pegawai.rekanan rekanan " + "left join pegawai.shiftKerja shiftKerja "
				+ "left join pegawai.statusPegawai statusPegawai " + "left join pegawai.statusKawin statusKawin "
				+ "left join pegawai.statusPerkawinanPegawai statusPerkawinanPegawai " + "left join pegawai.suku suku "
				+ "left join pegawai.typePegawai typePegawai " + "where pegawai.statusEnabled is true "
				+ "and pegawai.id not in (:listAdmin) ");

		// if (CommonUtil.isNotNullOrEmpty(idKedudukan)) {
		// if (listKedudukanException.contains(idKedudukan)) {
		// buffer.append("where (pegawai.statusEnabled is true or
		// pegawai.statusEnabled is false or pegawai.statusEnabled is null) ");
		// } else {
		// buffer.append("where pegawai.statusEnabled is true ");
		// }
		// } else {
		// buffer.append("where pegawai.statusEnabled is true ");
		// }

		if (CommonUtil.isNotNullOrEmpty(nama)) {
			buffer.append("and lower(pegawai.nama) like lower ('%" + nama + "%') ");
		}

		if (CommonUtil.isNotNullOrEmpty(idUnitKerja) && CommonUtil.isNotNullOrEmpty(listPegawaiByUnitKerja)
				&& listPegawaiByUnitKerja.size() > 0) {
			buffer.append("and pegawai.id in (:listPegawaiByUnitKerja) ");
		}

		if (CommonUtil.isNotNullOrEmpty(idKedudukan)) {
			buffer.append("and kedudukan.id = " + idKedudukan + " ");
		} else if (CommonUtil.isNullOrEmpty(idKedudukan)
				&& (CommonUtil.isNullOrEmpty(nama) || CommonUtil.isNotNullOrEmpty(idUnitKerja)
						|| CommonUtil.isNotNullOrEmpty(listStatusPegawaiId) || CommonUtil.isNotNullOrEmpty(periode))) {
			buffer.append("and kedudukan.id not in (:listKedudukanException) ");
		}

		if (CommonUtil.isNotNullOrEmpty(listStatusPegawaiId)) {
			buffer.append("and pegawai.kategoryPegawaiId in (:listStatusPegawaiId) ");
		} else if (CommonUtil.isNullOrEmpty(listStatusPegawaiId)) {
			buffer.append("and pegawai.kategoryPegawaiId not in (:nonSdmKategory) ");
		}

		if (CommonUtil.isNotNullOrEmpty(periode)) {
			buffer.append("and to_char(pegawai.tglMasuk,'yyyy-MM') = :periode ");
		}

		buffer.append("order by pegawai.nama");

		Query query = em.createQuery(buffer.toString());

		List<Integer> listAdmin = new ArrayList<>();
		listAdmin.addAll(Arrays.asList(Master.Pegawai.ADMINISTRATOR));
		listAdmin.addAll(Arrays.asList(Master.Pegawai.LOGIN_ADMIN));
		query.setParameter("listAdmin", listAdmin);

		if (CommonUtil.isNullOrEmpty(listStatusPegawaiId)) {
			query.setParameter("nonSdmKategory", Arrays.asList(Master.KategoryPegawai.NONSDM));
		}

		if (CommonUtil.isNotNullOrEmpty(idUnitKerja) && CommonUtil.isNotNullOrEmpty(listPegawaiByUnitKerja)
				&& listPegawaiByUnitKerja.size() > 0) {
			query.setParameter("listPegawaiByUnitKerja", listPegawaiByUnitKerja);
		}

		if (CommonUtil.isNullOrEmpty(idKedudukan)
				&& (CommonUtil.isNullOrEmpty(nama) || CommonUtil.isNotNullOrEmpty(idUnitKerja)
						|| CommonUtil.isNotNullOrEmpty(listStatusPegawaiId) || CommonUtil.isNotNullOrEmpty(periode))) {
			query.setParameter("listKedudukanException", listKedudukanException);
		}

		if (CommonUtil.isNotNullOrEmpty(listStatusPegawaiId)) {
			query.setParameter("listStatusPegawaiId", listStatusPegawaiId);
		}

		if (CommonUtil.isNotNullOrEmpty(periode)) {
			query.setParameter("periode", periode);
		}

		return query.getResultList();
	}

	@Override
	public List<Map<String, Object>> findPegawaiCustomByKategori(String nama, Integer idUnitKerja, Integer idKedudukan,
			String periode, List<Integer> listKedudukanException, Integer idKategori) {
		StringBuffer buffer = new StringBuffer();

		buffer.append("select new map (pegawai.id as idPegawai," + "pegawai.nama as nama,"
				+ "pegawai.namaLengkap as namaLengkap," + "pegawai.tglLahir as tglLahir,"
				+ "pegawai.qtyAnak as qtyAnak," + "pegawai.qtyTanggungan as qtyTanggungan,"
				+ "pegawai.qtyTotalJiwa as qtyTotalJiwa," + "kelompokJabatan.detailKelompokJabatan as kelompokJabatan,"
				+ "pegawai.tglMasuk as tglMasuk," + "pegawai.tglkeluar as tglKeluar," + "pegawai.kodeBank as kodeBank,"
				+ "pegawai.bankRekeningAtasNama as bankRekeningAtasNama,"
				+ "pegawai.bankRekeningNama as bankRekeningNama," + "pegawai.bankRekeningNomor as bankRekeningNomor,"
				+ "pegawai.kodeGapokId as idKodeGapok," + "pegawai.email as email,"
				+ "pegawai.emailAlternatif as emailAlternatif," + "pegawai.noTlp as noTlp,"
				+ "pegawai.noHandphone as noHandphone," + "pegawai.npwp as npwp," + "pegawai.noBPJS as noBPJS,"
				+ "pegawai.noCm as noCm," + "pegawai.alamat as alamat," + "pegawai.kodePos as kodePos,"
				+ "pegawai.nilaiJabatan as nilaiJabatan," + "levelTingkat.id as idLevelTingkat,"
				+ "levelTingkat.levelTingkat as levelTingkat," + "pegawai.grade as grade," + "pegawai.noSip as noSip,"
				+ "pegawai.tglTerbitSip as tglTerbitSip," + "pegawai.tglBerakhirSip as tglBerakhirSip,"
				+ "pegawai.noStr as noStr," + "pegawai.tglTerbitStr as tglTerbitStr,"
				+ "pegawai.tglBerakhirStr as tglBerakhirStr," + "pegawai.gelarDepan as gelarDepan,"
				+ "pegawai.gelarBelakang as gelarBelakang," + "pegawai.tempatLahir as tempatLahir,"
				+ "golongan.golonganPegawai as namaGolongan," + "agama.agama as agama,"
				+ "detailKategoryPegawai.detailKategoryPegawai as detailKategoryPegawai,"
				+ "dokumen.namaJudulDokumen as namaJudulDokumen," + "eselon.eselon as eselon,"
				+ "golonganDarah.golonganDarah as golonganDarah," + "pegawai.statusRhesus as rhesusGolonganDarah,"
				+ "jabatanFungsional.namaJabatan as namaJabatan," + "jabatanLamar.namaJabatan as jabatanLamar,"
				+ "jabatanStruktural.namaJabatan as jabatanStruktural," + "jenisKelamin.jenisKelamin as jenisKelamin,"
				+ "jenisPegawai.jenisPegawai as jenisPegawai," + "jenisPegawaiLamar.jenisPegawai as jenisPegawaiLamar,"
				+ "kategoriPegawai.kategoryPegawai as kategoriPegawai," + "kedudukan.id as kedudukanId,"
				+ "kedudukan.name as kedudukan," + "kualifikasiJurusan.kualifikasiJurusan as kualifikasiJurusan,"
				+ "negara.namaNegara as namaNegara," + "pangkat.namaPangkat as namaPangkat,"
				+ "pendidikan.namaPendidikan as namaPendidikan,"
				+ "penghasilanTidakKenaPajak.deskripsi as penghasilanTidakKenaPajak,"
				+ "pegawai.totalNilaiScore as totalNilaiScore," + "pegawai.tunjanganFungsional as tunjanganFungsional,"
				+ "pegawai.tunjanganPapua as tunjanganPapua," + "pegawai.tunjanganUmum as tunjanganUmum,"
				+ "pegawai.noIdentitas as noIdentitas," + "pegawai.nipPns as nipPns," + "pegawai.nip as nip,"
				+ "pegawai.nikIntern as nikIntern," + "pegawai.tanggalMeninggal as tanggalMeninggal,"
				+ "pegawai.pensiun as usiaPensiun," + "pegawai.tglPensiun as tglPensiun,"
				+ "pegawai.idFinger as idFinger," + "ruangan.id as idRuangan," + "ruangan.namaRuangan as namaRuangan,"
				+ "rekanan.id as idRuangan," + "rekanan.namaRekanan as namaRekanan,"
				+ "satuanKerja.satuanKerja as satuanKerja," + "shiftKerja.name as shiftKerja,"
				+ "shiftKerja.id as idKelompokShiftKerja," + "statusPegawai.statusPegawai as statusPegawai,"
				+ "statusKawin.statusPerkawinan as statusKawin,"
				+ "statusPerkawinanPegawai.statusPerkawinan as statusPerkawinanPegawai," + "suku.suku as suku,"
				+ "typePegawai.typePegawai as typePegawai," + "pegawai.statusEnabled as statusEnabled)"
				+ "from Pegawai pegawai " + "left join pegawai.detailKategoryPegawai detailKategoryPegawai "
				+ "left join pegawai.agama agama " + "left join pegawai.detailKelompokJabatan kelompokJabatan "
				+ "left join pegawai.dokumen dokumen " + "left join pegawai.eselon eselon "
				+ "left join pegawai.golonganDarah golonganDarah "
				+ "left join pegawai.jabatanFungsional jabatanFungsional "
				+ "left join pegawai.jabatanLamar jabatanLamar "
				+ "left join pegawai.jabatanStruktural jabatanStruktural "
				+ "left join pegawai.jenisKelamin jenisKelamin " + "left join pegawai.jenisPegawai jenisPegawai "
				+ "left join pegawai.jenisPegawaiLamar jenisPegawaiLamar "
				+ "left join pegawai.kategoryPegawai kategoriPegawai " + "left join pegawai.kedudukan kedudukan "
				+ "left join pegawai.kualifikasiJurusan kualifikasiJurusan "
				+ "left join pegawai.levelTingkat levelTingkat " + "left join pegawai.negara negara "
				+ "left join pegawai.pangkat pangkat " + "left join pangkat.kdGolonganPegawai golongan "
				+ "left join pegawai.pendidikan pendidikan "
				+ "left join pegawai.penghasilanTidakKenaPajak penghasilanTidakKenaPajak "
				+ "left join pegawai.ruangan ruangan " + "left join pegawai.satuanKerja satuanKerja "
				+ "left join pegawai.rekanan rekanan " + "left join pegawai.shiftKerja shiftKerja "
				+ "left join pegawai.statusPegawai statusPegawai " + "left join pegawai.statusKawin statusKawin "
				+ "left join pegawai.statusPerkawinanPegawai statusPerkawinanPegawai " + "left join pegawai.suku suku "
				+ "left join pegawai.typePegawai typePegawai " + "where pegawai.statusEnabled is true "
				+ "and kategoriPegawai.id = :idKategori ");

		if (CommonUtil.isNotNullOrEmpty(nama)) {
			buffer.append("and lower(pegawai.nama) like lower ('%" + nama + "%') ");
		}

		if (CommonUtil.isNotNullOrEmpty(idUnitKerja)) {
			buffer.append("and pegawai.id in (:listPegawaiByUnitKerja) ");
		}

		if (CommonUtil.isNotNullOrEmpty(idKedudukan)) {
			buffer.append("and kedudukan.id = " + idKedudukan + " ");
		}

		if (CommonUtil.isNotNullOrEmpty(periode)) {
			buffer.append("and to_char(pegawai.tglMasuk,'yyyy-MM') = :periode ");
		}

		buffer.append("order by pegawai.nama");

		Query query = em.createQuery(buffer.toString());

		query.setParameter("idKategori", idKategori);

		if (CommonUtil.isNotNullOrEmpty(idUnitKerja)) {
			List<Integer> listPegawaiByUnitKerja = pegawaiDao.getPegawaiByIdUnitKerja(idUnitKerja);
			query.setParameter("listPegawaiByUnitKerja", listPegawaiByUnitKerja);
		}

		if (CommonUtil.isNotNullOrEmpty(periode)) {
			query.setParameter("periode", periode);
		}

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findPegawaiCustom(String nama, Integer idUnitKerja, Integer idKedudukan,
			List<Integer> listIdStatusPegawai, Integer idStatusPegawai, String periode, List<Integer> listStatusPegawai,
			List<Integer> listKedudukan) {
		StringBuffer buffer = new StringBuffer();
		List<Integer> listAdding = new ArrayList<Integer>();

		buffer.append("select new map (pegawai.id as idPegawai," + "pegawai.nama as nama,"
				+ "pegawai.namaLengkap as namaLengkap," + "pegawai.tglLahir as tglLahir,"
				+ "pegawai.qtyAnak as qtyAnak," + "pegawai.qtyTanggungan as qtyTanggungan,"
				+ "pegawai.qtyTotalJiwa as qtyTotalJiwa," + "kelompokJabatan.detailKelompokJabatan as kelompokJabatan,"
				+ "pegawai.tglMasuk as tglMasuk," + "pegawai.tglkeluar as tglKeluar," + "pegawai.kodeBank as kodeBank,"
				+ "pegawai.bankRekeningAtasNama as bankRekeningAtasNama,"
				+ "pegawai.bankRekeningNama as bankRekeningNama," + "pegawai.bankRekeningNomor as bankRekeningNomor," +
				// "pegawai.namaRekening as namaRekening,"+
				"pegawai.kodeGapokId as idKodeGapok," + "pegawai.email as email,"
				+ "pegawai.emailAlternatif as emailAlternatif," + "pegawai.noTlp as noTlp,"
				+ "pegawai.noHandphone as noHandphone," + "pegawai.npwp as npwp," + "pegawai.noBPJS as noBPJS,"
				+ "pegawai.noCm as noCm," + "pegawai.alamat as alamat," + "pegawai.kodePos as kodePos,"
				+ "pegawai.nilaiJabatan as nilaiJabatan," + "levelTingkat.id as idLevelTingkat,"
				+ "levelTingkat.levelTingkat as levelTingkat," + "pegawai.grade as grade," + "pegawai.noSip as noSip,"
				+ "pegawai.tglTerbitSip as tglTerbitSip," + "pegawai.tglBerakhirSip as tglBerakhirSip,"
				+ "pegawai.noStr as noStr," + "pegawai.tglTerbitStr as tglTerbitStr,"
				+ "pegawai.tglBerakhirStr as tglBerakhirStr," + "pegawai.gelarDepan as gelarDepan,"
				+ "pegawai.gelarBelakang as gelarBelakang," + "pegawai.tempatLahir as tempatLahir,"
				+ "golongan.golonganPegawai as namaGolongan," + "agama.agama as agama,"
				+ "detailKategoryPegawai.detailKategoryPegawai as detailKategoryPegawai,"
				+ "dokumen.namaJudulDokumen as namaJudulDokumen," + "eselon.eselon as eselon,"
				+ "golonganDarah.golonganDarah as golonganDarah," + "pegawai.statusRhesus as rhesusGolonganDarah,"
				+ "jabatanFungsional.namaJabatan as namaJabatan," + "jabatanLamar.namaJabatan as jabatanLamar,"
				+ "jabatanStruktural.namaJabatan as jabatanStruktural," + "jenisKelamin.jenisKelamin as jenisKelamin,"
				+ "jenisPegawai.jenisPegawai as jenisPegawai," + "jenisPegawaiLamar.jenisPegawai as jenisPegawaiLamar,"
				+ "kategoriPegawai.kategoryPegawai as kategoriPegawai," + "kedudukan.name as kedudukan,"
				+ "kualifikasiJurusan.kualifikasiJurusan as kualifikasiJurusan," + "negara.namaNegara as namaNegara,"
				+ "pangkat.namaPangkat as namaPangkat," + "pendidikan.namaPendidikan as namaPendidikan,"
				+ "penghasilanTidakKenaPajak.deskripsi as penghasilanTidakKenaPajak,"
				+ "pegawai.totalNilaiScore as totalNilaiScore," + "pegawai.tunjanganFungsional as tunjanganFungsional,"
				+ "pegawai.tunjanganPapua as tunjanganPapua," + "pegawai.tunjanganUmum as tunjanganUmum," +
				// "range.namaRange as namaRange,"+
				"pegawai.noIdentitas as noIdentitas," + "pegawai.nipPns as nipPns," + "pegawai.nip as nip,"
				+ "pegawai.nikIntern as nikIntern," + "pegawai.tanggalMeninggal as tanggalMeninggal,"
				+ "pegawai.pensiun as usiaPensiun," + "pegawai.tglPensiun as tglPensiun,"
				+ "pegawai.idFinger as idFinger," + "ruangan.id as idRuangan," + "ruangan.namaRuangan as namaRuangan,"
				+ "rekanan.id as idRuangan," + "rekanan.namaRekanan as namaRekanan,"
				+ "satuanKerja.satuanKerja as satuanKerja," + "shiftKerja.name as shiftKerja,"
				+ "shiftKerja.id as idKelompokShiftKerja," +
				// "statusKawin.statusPerkawinan as statusKawin,"+
				"statusPegawai.statusPegawai as statusPegawai,"
				+ "statusPerkawinanPegawai.statusPerkawinan as statusPerkawinanPegawai," + "suku.suku as suku," +
				// "titlePasien.namaTitle as titlePasien,"+
				"typePegawai.typePegawai as typePegawai," + "pegawai.statusEnabled as statusEnabled)"
				+ "from Pegawai pegawai " +
				// "left join pegawai.golonganPegawai golongan "+
				"left join pegawai.detailKategoryPegawai detailKategoryPegawai " + "left join pegawai.agama agama "
				+ "left join pegawai.detailKelompokJabatan kelompokJabatan " + "left join pegawai.dokumen dokumen "
				+ "left join pegawai.eselon eselon " + "left join pegawai.golonganDarah golonganDarah "
				+ "left join pegawai.jabatanFungsional jabatanFungsional "
				+ "left join pegawai.jabatanLamar jabatanLamar "
				+ "left join pegawai.jabatanStruktural jabatanStruktural "
				+ "left join pegawai.jenisKelamin jenisKelamin " + "left join pegawai.jenisPegawai jenisPegawai "
				+ "left join pegawai.jenisPegawaiLamar jenisPegawaiLamar "
				+ "left join pegawai.kategoryPegawai kategoriPegawai " + "left join pegawai.kedudukan kedudukan "
				+ "left join pegawai.kualifikasiJurusan kualifikasiJurusan "
				+ "left join pegawai.levelTingkat levelTingkat " + "left join pegawai.negara negara "
				+ "left join pegawai.pangkat pangkat " + "left join pangkat.kdGolonganPegawai golongan "
				+ "left join pegawai.pendidikan pendidikan "
				+ "left join pegawai.penghasilanTidakKenaPajak penghasilanTidakKenaPajak " +
				// "left join pegawai.range range "+
				"left join pegawai.ruangan ruangan " + "left join pegawai.satuanKerja satuanKerja "
				+ "left join pegawai.rekanan rekanan " + "left join pegawai.shiftKerja shiftKerja " +
				// "left join pegawai.statusKawin statusKawin "+
				"left join pegawai.statusPegawai statusPegawai "
				+ "left join pegawai.statusPerkawinanPegawai statusPerkawinanPegawai " + "left join pegawai.suku suku "
				+
				// "left join pegawai.titlePasien titlePasien "+
				"left join pegawai.typePegawai typePegawai " + "where pegawai.statusEnabled is true ");

		if (CommonUtil.isNotNullOrEmpty(nama)) {
			buffer.append("and lower(pegawai.nama) like lower ('%" + nama + "%') ");
		}

		if (CommonUtil.isNotNullOrEmpty(idUnitKerja)) {
			buffer.append("and pegawai.id in (:listPegawaiByUnitKerja) ");
		}

		if (CommonUtil.isNotNullOrEmpty(idKedudukan)) {
			buffer.append("and kedudukan.id = " + idKedudukan + " ");
		} else if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNullOrEmpty(nama)) {
			buffer.append("and kedudukan.id not in (:listIdKedudukan) ");
		} else if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNotNullOrEmpty(idUnitKerja)) {
			buffer.append("and kedudukan.id not in (:listIdKedudukan) ");
		} else if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNotNullOrEmpty(idStatusPegawai)) {
			buffer.append("and kedudukan.id not in (:listIdKedudukan) ");
		} else if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNotNullOrEmpty(listIdStatusPegawai)) {
			buffer.append("and kedudukan.id not in (:listIdKedudukan) ");
		}

		if (CommonUtil.isNotNullOrEmpty(listIdStatusPegawai) || CommonUtil.isNotNullOrEmpty(idStatusPegawai)) {
			if (listIdStatusPegawai.equals(listStatusPegawai)) {
				buffer.append("and pegawai.kategoryPegawaiId in (:listIdStatusPegawai) ");
				buffer.append("and kedudukan.id not in (:listIdKedudukan) ");
			} else {
				buffer.append("and pegawai.kategoryPegawaiId in (:listIdStatusPegawai) ");
			}
		}

		if (CommonUtil.isNotNullOrEmpty(periode)) {
			buffer.append("and to_char(pegawai.tglMasuk,'yyyy-MM') = :periode ");
		}

		buffer.append("order by pegawai.nama");

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(idUnitKerja)) {
			List<Integer> listPegawaiByUnitKerja = pegawaiDao.getPegawaiByIdUnitKerja(idUnitKerja);
			query.setParameter("listPegawaiByUnitKerja", listPegawaiByUnitKerja);
		}

		if (CommonUtil.isNotNullOrEmpty(listIdStatusPegawai)) {
			if (listIdStatusPegawai.equals(listStatusPegawai)) {
				if (CommonUtil.isNotNullOrEmpty(idStatusPegawai)) {
					if (listIdStatusPegawai.contains(idStatusPegawai)) {
						listAdding.add(idStatusPegawai);
						query.setParameter("listIdStatusPegawai", listAdding);
						query.setParameter("listIdKedudukan", listKedudukan);
					} else {
						listAdding.addAll(listIdStatusPegawai);
						listAdding.add(idStatusPegawai);
						query.setParameter("listIdStatusPegawai", listAdding);
						query.setParameter("listIdKedudukan", listKedudukan);
					}
				} else {
					query.setParameter("listIdStatusPegawai", listIdStatusPegawai);
					query.setParameter("listIdKedudukan", listKedudukan);
				}
			} else {
				if (CommonUtil.isNotNullOrEmpty(idStatusPegawai)) {
					if (listIdStatusPegawai.contains(idStatusPegawai)) {
						listAdding.add(idStatusPegawai);
						query.setParameter("listIdStatusPegawai", listAdding);
					} else {
						listAdding.addAll(listIdStatusPegawai);
						listAdding.add(idStatusPegawai);
						query.setParameter("listIdStatusPegawai", listAdding);
					}
				} else {
					query.setParameter("listIdStatusPegawai", listIdStatusPegawai);
				}
			}

		} else {
			if (CommonUtil.isNotNullOrEmpty(idStatusPegawai)) {
				listAdding.add(idStatusPegawai);
				query.setParameter("listIdStatusPegawai", listAdding);
			}
		}

		if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNullOrEmpty(nama)) {
			query.setParameter("listIdKedudukan", listKedudukan);
		} else if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNotNullOrEmpty(idUnitKerja)) {
			query.setParameter("listIdKedudukan", listKedudukan);
		} else if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNotNullOrEmpty(idStatusPegawai)) {
			query.setParameter("listIdKedudukan", listKedudukan);
		} else if (CommonUtil.isNullOrEmpty(idKedudukan) && CommonUtil.isNotNullOrEmpty(listIdStatusPegawai)) {
			query.setParameter("listIdKedudukan", listKedudukan);
		}

		if (CommonUtil.isNotNullOrEmpty(periode)) {
			query.setParameter("periode", periode);
		}

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findHariLiburPegawaiNonShift(Integer tahun, Integer bulan) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select new map(model2.id as idKalendar,model2.tanggal as tanggal,model2.hariKeDlmBulan as tgl,"
				+ "model1.namaExternal as namaHariLibur,model1.statusEnabled as statusEnabled,model2.namaHari as namaHari)");
		buffer.append(" from MapKalenderToHariLibur model1 right join model1.tanggal model2");
		buffer.append(" where model2.tahunKalender = " + tahun + " and model2.bulanKeDlmTahun = " + bulan);

		Query query = em.createQuery(buffer.toString());

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<PegawaiJadwalKerja> findJadwalByTanggalRuangan(Integer subUnitKerja, Integer tahun, Integer bulan,
			Integer pegawai) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select model from PegawaiJadwalKerja model, MapPegawaiJabatanToUnitKerja mapPegawai ");
		buffer.append(" left join model.pegawai pegawai ");
		buffer.append(" left join mapPegawai.subUnitKerjaPegawai subUnitKerja ");
		buffer.append(" where mapPegawai.pegawaiId = pegawai.id ");
		buffer.append(" and model.pegawaiId = mapPegawai.pegawaiId ");
		buffer.append(" and subUnitKerja.id= " + subUnitKerja);
		buffer.append(" and model.tanggal.bulanKeDlmTahun= " + bulan);
		buffer.append(" and model.tanggal.tahunKalender =  " + tahun);
		buffer.append(" and model.pegawai.id  =  " + pegawai);

		Query query = em.createQuery(buffer.toString());

		List<PegawaiJadwalKerja> list = query.getResultList();

		return list;
	}

	@Override
	public List<PegawaiJadwalKerja> findJadwalByTanggal(Integer tahun, Integer bulan, Integer pegawai) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" select model from PegawaiJadwalKerja model, MapPegawaiJabatanToUnitKerja mapPegawai ");
		buffer.append(" left join model.pegawai pegawai ");
		buffer.append(" left join mapPegawai.subUnitKerjaPegawai subUnitKerja ");
		buffer.append(" where mapPegawai.pegawaiId = pegawai.id ");
		buffer.append(" and model.pegawaiId = mapPegawai.pegawaiId ");
		buffer.append(" and pegawai.shiftKerjaId in (1,2) ");
		buffer.append(" and model.tanggal.bulanKeDlmTahun= " + bulan);
		buffer.append(" and model.tanggal.tahunKalender =  " + tahun);
		buffer.append(" and model.pegawai.id  =  " + pegawai);

		Query query = em.createQuery(buffer.toString());

		List<PegawaiJadwalKerja> list = query.getResultList();

		return list;
	}

	@Override
	public List<Map<String, Object>> findListLemburVerifiedBySdm(String periode) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(
				" select new Map(count(monitoringAbsen.verifikasiUnitKerjaFK) as jumlahLembur,monitoringAbsen.verifikasiSdmFK as noRecSdm,monitoringAbsen.pegawaiId as pegawaiId) ");
		buffer.append(" FROM MonitoringAbsen monitoringAbsen ");
		buffer.append(" WHERE  monitoringAbsen.tanggal like  '%" + periode + "' ");
		buffer.append(" AND  monitoringAbsen.verifikasiSdmFK IS NOT NULL ");
		buffer.append(" GROUP BY monitoringAbsen.verifikasiSdmFK,monitoringAbsen.pegawaiId  ");
		Query query = em.createQuery(buffer.toString());
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public List<Pegawai> findPegawaiAktifAndVerifiedByUnitKerja(String periode) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("SELECT distinct new Pegawai(pegawai.id,pegawai.namaLengkap) FROM ");
		buffer.append(" MonitoringAbsen monitoringAbsen");
		buffer.append(" LEFT JOIN monitoringAbsen.pegawai pegawai ");
		buffer.append(" WHERE monitoringAbsen.verifikasiUnitKerjaFK IS NOT NULL ");
		buffer.append(" AND monitoringAbsen.tanggal like  '%" + periode + "' ");
		buffer.append(" AND pegawai.statusPegawai.kdStatusPegawai=2 ");
		Query query = em.createQuery(buffer.toString());
		List<Pegawai> list = query.getResultList();

		return list;
	}

}
