package com.jasamedika.medifirst2000.dao;

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

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
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.Dokumen;
import com.jasamedika.medifirst2000.entities.DokumenMasuk;

@Repository("DokumenDao")
public interface DokumenDao extends PagingAndSortingRepository<Dokumen, Integer>, JpaSpecificationExecutor<Dokumen> {

	@Query("select new Dokumen(model.id,model.pegawaiPembuat,model.namaJudulDokumen,model.qDokumen,model.noDokumen) from Dokumen model where model.id=:id ")
	public Dokumen findById(@Param("id") Integer id);
	
	@Query("select new Dokumen(model.id,model.pegawaiPembuat,model.namaJudulDokumen,model.qDokumen,model.noDokumen) from Dokumen model join model.jenisDokumen jenisDokumen where jenisDokumen.id=:id ")
	public List<Dokumen> findByIdJenis(@Param("id") Integer id);
	
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, model.deskripsiDokumen as deskripsiDokumen,model.kdDokumen as kdDokumen,model.noDokumen as noDokumen,model.pathFile as pathFile) "
			+ " from Dokumen model "
			+" left join model.ruangan ruangan"
			+ " where model.isDokumenInOutInt IS NULL "
			+ " and model.noDokumen IS NOT NULL "
			+ " and ruangan.id = :idRuangan "
			+ " and model.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')")
	public List<Map<String,Object>> getListDokumenDraft(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir,@Param("idRuangan") Integer idRuangan );
	
	@Query("select new Map(model.pathFile as  pathFile, model.namaJudulDokumen as namaJudulDokumen,model.noDokumen as noDokumen)from Dokumen model where model.id=:id ")
	public Map<String,Object> findPathFileById(@Param("id") Integer id);
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, model.deskripsiDokumen as deskripsiDokumen,model.kdDokumen as kdDokumen,model.noDokumen as noDokumen,model.pathFile as pathFile,model.tglDokumen as tglDokumen) "
			+ "from Dokumen model where model.id=:id "
			+ " and model.isDokumenInOutInt IS NULL ")
	public Map<String,Object> getDraftSuratById(@Param("id") Integer id);
	
	@Query(" select new Map(dokumen.id as id, dokumen.deskripsiDokumen as deskripsiDokumen,dokumen.kdDokumen as kdDokumen,dokumen.noDokumen as noDokumen,dokumen.pathFile as pathFile,dokumen.pegawaiPembuat as pembuat, dokumenMasuk.kdruangantujuanlast as ruanganTujuan, dokumen.ruangan as ruanganAsal) from DokumenMasuk dokumenMasuk "
			+ " left join dokumenMasuk.kddokumen dokumen "
			+ " left join dokumenMasuk.kdruangantujuanlast ruangan "
			+ " where ruangan.id = :idRuangan "
			+ " and dokumen.isDokumenInOutInt IS NULL "
			+" and dokumen.tglDokumen IS NOT NULL "
			+ " and dokumen.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy') ")
	public List<Map<String,Object>> getListDokumenDraftByRuangan(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir , @Param("idRuangan") Integer idRuangan);
	@Query("select new Map(r.namaRuangan as namaRuangan, r.id as id) from DokumenMasuk dm "
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen d "
			+ " where d.id = :idDokumen "
			+ " and d.isDokumenInOutInt IS NULL ")
	public List<Map<String, Object>> getListRuanganByIdDokumen(@Param("idDokumen") Integer idDokumen);
	
	@Query("select new Map(dm.noRec as noRec) from DokumenMasuk dm "
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen d "
			+ " where d.id = :idDokumen "
			+ " and d.isDokumenInOutInt IS NULL ")
	public List<Map<String, Object>> getListDokumenMasukByIdDokumen(@Param("idDokumen") Integer idDokumen);
	
	@Query(" select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, "
			+ " model.deskripsiDokumen as deskripsiDokumen,model.kdDokumen as kdDokumen,"
			+ " model.noDokumen as noDokumen,model.pathFile as pathFile,model.tglDokumen as tglDokumen,"
			+ " dm.lampiranPerihal as lampiranPerihal,"
			+ " dm.noRec as noRec,dm.status as status) "
			+ " from DokumenMasuk dm"
			+ " left join dm.kddokumen model"
			+" left join dm.kdruangantujuanlast r "
			+ "  where model.id= :id and r.id= :idRuangan "
			+ " and model.isDokumenInOutInt IS NULL ")
	public Map<String,Object> getDraftSuratMasukByIdDokumenAndRuanganTujuan(@Param("id") Integer id,@Param("idRuangan") Integer idRuangan);
	
	@Query("Select new DokumenMasuk( model.noRec  as noRec, model.lampiranPerihal as lampiran, model.isVerifikasi ) from DokumenMasuk model where ObjectDokumenId = :idDokumen ")
	public List<DokumenMasuk> checkDokumenisVerifiedByAllRuangan(@Param("idDokumen") Integer idDokumen);
	
	@Query("select new Map(r.namaRuangan as namaRuangan, r.id as id,dm.isVerifikasi as status) from DokumenMasuk dm "
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen d "
			+ " where d.id = :idDokumen"
			+ "  and d.isDokumenInOutInt IS NULL ")
	public List<Map<String, Object>> getListRuanganByIdDokumenStatus(@Param("idDokumen") Integer idDokumen);
	
	@Query("select new Map("
			+ " r.namaRuangan as namaRuangan,"
			+ " dm.lampiranPerihal as lampiranPerihal) from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join dm.kdruangantujuanlast r "
			+ " where d.id = :idDokumen"
			+ " and d.isDokumenInOutInt IS NULL ")
	public List<Map<String, Object>> getVerbalKonsepByIdDokumen(@Param("idDokumen") Integer idDokumen);
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, model.kdDokumen as kdDokumen,model.noDokumen as noDokumen) "
			+ " from Dokumen model "
			+" left join model.ruangan ruangan"
			+ " where model.isDokumenInOutInt IS NULL "
			+ " and model.noDokumen IS NOT NULL "
			+ " and ruangan.id = :idRuangan "
			+  "")
	public List<Map<String,Object>> getListDokumenDraftByRuanganVerify(@Param("idRuangan") Integer idRuangan );
	
	@Query("select new Map("
			+ " statusBerkas.id as statusBerkasId,"
			+ " statusBerkas.name as statusBerkasName,"
			+ " jenisSurat.id as jenisSuratId,"
			+ " jenisSurat.name as jenisSuratName,"
			+ " sifatSurat.id as sifatSuratId,"
			+ " sifatSurat.name as sifatSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " ruanganPengirim.id as ruanganPengirimId,"
			+ " ruanganPengirim.namaRuangan as ruanganPengirimNamaRuangan,"
			+ " ruanganPenerima.id as ruanganPenerimaId,"
			+ " ruanganPenerima.namaRuangan as ruanganPenerimaNamaRuangan,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap,"
			+ " d.noDokumen as noDokumen,"
			+ " d.id as id,"
			+ " d.namaJudulDokumen as namaJudulDokumen,"
			+ " d.tglDokumen as tglDokumen,"
			+ " dm.noRec as noRec) from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join dm.statusBerkas statusBerkas "
			+ " left join dm.jenisSurat jenisSurat "
			+ " left join dm.sifatSurat sifatSurat "
			+ " left join dm.ruanganPengirim ruanganPengirim "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "
			//+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.ruanganPenerima ruanganPenerima "
			+ " where  d.isDokumenInOutInt IN (0,1)  "
			//+ " and r.id = :idRuangan "
			+ " and ruanganPengirim.id = :idRuangan "
			+  "and d.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')")
	public List<Map<String,Object>> getListDokumenMasukInternalExternal(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir,@Param("idRuangan") Integer idRuangan );
	@Query("select new Map("
			+ " statusBerkas.id as statusBerkasId,"
			+ " statusBerkas.name as statusBerkasName,"
			+ " jenisSurat.id as jenisSuratId,"
			+ " jenisSurat.name as jenisSuratName,"
			+ " sifatSurat.id as sifatSuratId,"
			+ " sifatSurat.name as sifatSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " ruanganPengirim.id as ruanganPengirimId,"
			+ " ruanganPengirim.namaRuangan as ruanganPengirimNamaRuangan,"
			+ " ruanganPenerima.id as ruanganPenerimaId,"
			+ " ruanganPenerima.namaRuangan as ruanganPenerimaNamaRuangan,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap,"
			+ " pegawaiPengantarSurat.namaLengkap as pegawaiPengantarSuratNamaLengkap,"
			+ " d.noDokumen as noDokumen,"
			+ " pegawaiPembuat.namaLengkap as  pegawaiPengirimNamaLengkap,"
			+ " d.id as id,"
			+ " d.namaJudulDokumen as namaJudulDokumen,"
			+ " d.tglDokumen as tglDokumen,"
			+ " dm.noRec as noRec,"
			+ " dm.jamTerima as jamTerima,"
			+ " dm.jamKirim as jamKirim) from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join d.pegawaiPembuat pegawaiPembuat "
			+ " left join dm.statusBerkas statusBerkas "
			+ " left join dm.jenisSurat jenisSurat "
			+ " left join dm.sifatSurat sifatSurat "
			+ " left join dm.ruanganPengirim ruanganPengirim "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "
			+ " left join dm.ruanganPenerima ruanganPenerima "
			+ " left join dm.pegawaiPengantarSurat pegawaiPengantarSurat"
			+ " where  d.isDokumenInOutInt IN (0,1)  "
			+ " and dm.noRec = :norec "
			+ " ")
	public Map<String,Object> getDokumenMasukInternalExternalBynoRec(@Param("norec") String norec );
	@Query("select new Map("
			+ " statusBerkas.id as statusBerkasId,"
			+ " statusBerkas.name as statusBerkasName,"
			+ " jenisSurat.id as jenisSuratId,"
			+ " jenisSurat.name as jenisSuratName,"
			+ " sifatSurat.id as sifatSuratId,"
			+ " sifatSurat.name as sifatSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " ruanganPengirim.id as ruanganPengirimId,"
			+ " ruanganPengirim.namaRuangan as ruanganPengirimNamaRuangan,"
			+ " ruanganPenerima.id as ruanganPenerimaId,"
			+ " ruanganPenerima.namaRuangan as ruanganPenerimaNamaRuangan,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap,"
			+ " d.noDokumen as noDokumen,"
			+ " d.id as id,"
			+ " d.namaJudulDokumen as namaJudulDokumen,"
			+ " d.tglDokumen as tglDokumen,"
			+ " dm.noRec as noRec,"
			+ " dm.jamTerima as jamTerima,"
			+ " dm.jamKirim as jamKirim ,"
			+ " pegawaiPengantarSurat.namaLengkap as pegawaiPengantarSurat,"			
			+ " strukkirim.tglkirim as  tglkirim) "
			+ " from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join dm.pegawaiPengantarSurat pegawaiPengantarSurat"
			+ " left join dm.statusBerkas statusBerkas "
			+ " left join dm.jenisSurat jenisSurat "
			+ " left join dm.sifatSurat sifatSurat "
			+ " left join dm.ruanganPengirim ruanganPengirim "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "
			+ " left join dm.ruanganPenerima ruanganPenerima "
			+ " left join dm.nokirimketujuanlast strukkirim"
			+ " where  d.isDokumenInOutInt IN (0,1)  "
			//+ " and dm.noRec = :norec "
			+ " and d.id = :idDokumen "
			+ " ")
	public List<Map<String,Object>> getDokumenMasukInternalExternalByidDokumen(@Param("idDokumen") Integer idDokumen );
	@Query("select new Map("
			+ " statusBerkas.id as statusBerkasId,"
			+ " statusBerkas.name as statusBerkasName,"
			+ " jenisSurat.id as jenisSuratId,"
			+ " jenisSurat.name as jenisSuratName,"
			+ " sifatSurat.id as sifatSuratId,"
			+ " sifatSurat.name as sifatSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " ruanganPengirim.id as ruanganPengirimId,"
			+ " ruanganPengirim.namaRuangan as ruanganPengirimNamaRuangan,"
			+ " ruanganPenerima.id as ruanganPenerimaId,"
			+ " ruanganPenerima.namaRuangan as ruanganPenerimaNamaRuangan,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap,"
			+ " d.noDokumen as noDokumen,"
			+ " d.id as id,"
			+ " d.namaJudulDokumen as namaJudulDokumen,"
			+ " d.tglDokumen as tglDokumen,"
			+ " dm.noRec as noRec,"
			+ " dm.jamTerima as jamTerima,"
			+ " dm.jamKirim as jamKirim ,"
			+ " pegawaiPengantarSurat.namaLengkap as pegawaiPengantarSurat,"			
			+ " strukkirim.tglkirim as  tglkirim) "
			+ " from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join dm.pegawaiPengantarSurat pegawaiPengantarSurat"
			+ " left join dm.statusBerkas statusBerkas "
			+ " left join dm.jenisSurat jenisSurat "
			+ " left join dm.sifatSurat sifatSurat "
			+ " left join dm.ruanganPengirim ruanganPengirim "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "
			+ " left join dm.ruanganPenerima ruanganPenerima "
			+ " left join dm.nokirimketujuanlast strukkirim"
			+ " where  d.isDokumenInOutInt IN (0,1)  "
			+ " and dm.sifatSurat IS NOT NULL "
			+ " and d.id = :idDokumen "
			+ " ")
	public List<Map<String,Object>> getDokumenMasukInternalExternalByidDokumenAndStatusBerkasNotNull(@Param("idDokumen") Integer idDokumen );
	
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, model.deskripsiDokumen as deskripsiDokumen,model.kdDokumen as kdDokumen,model.noDokumen as noDokumen,model.pathFile as pathFile,model.namaPegawaiPembuat as namaPegawaiPembuat) "
			+ " from Dokumen model "
			+" left join model.ruangan ruangan"
			+ " where model.isDokumenInOutInt IN (0,1) "
			+ " and model.noDokumen IS NOT NULL "
			+ " and ruangan.id = :idRuangan "
			+ " and model.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')")
	public List<Map<String,Object>> getListDokumenSuratMasukDistribusi(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir,@Param("idRuangan") Integer idRuangan );
	@Query("select new Map(r.namaRuangan as namaRuangan, r.id as id,dm.status as status,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap"
			+ ") from DokumenMasuk dm "
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen d "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " where d.id = :idDokumen"
			+" and dm.nodokumen_extern IS NOT NULL "
			+ "  and d.isDokumenInOutInt IN (0,1) ")
	public List<Map<String, Object>> getListSuratMasukRuanganByIdDokumenStatus(@Param("idDokumen") Integer idDokumen);
	@Query("select new Map(r.namaRuangan as namaRuangan, r.id as id,dm.status as status,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap"
			+ ") from DokumenMasuk dm "
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen d "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " where d.id = :idDokumen"
			+" and dm.statusBerkas IS NOT NULL"
			+ " and dm.sifatSurat IS NOT NULL "
			+ "  and d.isDokumenInOutInt IN (0,1) ")
	public List<Map<String, Object>> getListSuratMasukRuanganByIdDokumenStatusAndDistribusi(@Param("idDokumen") Integer idDokumen);
	@Query("select dm from DokumenMasuk dm "		 
			+ " left join dm.kddokumen d "
			+ " where d.id = :idDokumen  and dm.nodokumen_extern IS NOT NULL ")
	public List<DokumenMasuk> getAllDokumenMasukByIdDokumen(@Param("idDokumen") Integer idDokumen);
	
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, model.deskripsiDokumen as deskripsiDokumen,model.kdDokumen as kdDokumen,model.noDokumen as noDokumen,model.pathFile as pathFile,model.tglDokumen as tglDokumen) "
			+ "from Dokumen model where model.id=:id "
			+ " and model.isDokumenInOutInt IN (0,1) ")
	public Map<String,Object> getSuratMasukInternalEksternalByIdDokumen(@Param("id") Integer id);
	
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, "
			+ " model.deskripsiDokumen as deskripsiDokumen, "
			+ " model.kdDokumen as kdDokumen,model.noDokumen as noDokumen,"
			+ " model.pathFile as pathFile , model.tglDokumen as tglDokumen,"
			+ " ruangan.namaRuangan as ruanganAsal,"
			+ " r.namaRuangan as ruanganTujuan,"
			+ " pegawaiPembuat.namaLengkap as pembuatSurat,"
			+ " dm.status as status,"
			+ " dm.noRec as noRec) "
			+ " from DokumenMasuk dm "
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen model "
			+" left join model.ruangan ruangan"
			+ " left join model.pegawaiPembuat pegawaiPembuat"
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat"
			+ " where model.isDokumenInOutInt = 2 "
			+ " and ruangan.id = :idRuangan "
			+ " and model.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')")
	public List<Map<String,Object>> getListSuratMasukInternalEksternalPenomoran(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir,@Param("idRuangan") Integer idRuangan );
	
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, "
			+ " model.deskripsiDokumen as deskripsiDokumen, "
			+ " model.kdDokumen as kdDokumen,model.noDokumen as noDokumen,"
			+ " model.pathFile as pathFile , model.tglDokumen as tglDokumen,"
			+ " ruangan.namaRuangan as ruanganAsal,"
			+ " r.namaRuangan as ruanganTujuan,"
			+ " pegawaiPembuat.namaLengkap as pembuatSurat,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " dm.status as status,"
			+ " dm.noRec as noRec) "
			+ " from DokumenMasuk dm "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "	
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen model "
			+" left join model.ruangan ruangan"
			+ " left join model.pegawaiPembuat pegawaiPembuat"
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat"
			+ " where model.isDokumenInOutInt = 2 "
			+ " and r.id = :idRuangan "
			+ " and model.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')")
	public List<Map<String,Object>> getListSuratMasukInternalEksternalPenomoranTujuan(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir,@Param("idRuangan") Integer idRuangan );
	
	@Query("select new Map("
			+ " statusBerkas.id as statusBerkasId,"
			+ " statusBerkas.name as statusBerkasName,"
			+ " jenisSurat.id as jenisSuratId,"
			+ " jenisSurat.name as jenisSuratName,"
			+ " sifatSurat.id as sifatSuratId,"
			+ " sifatSurat.name as sifatSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " ruanganPengirim.id as ruanganPengirimId,"
			+ " ruanganPengirim.namaRuangan as ruanganPengirimNamaRuangan,"
			+ " ruanganPenerima.id as ruanganPenerimaId,"
			+ " ruanganPenerima.namaRuangan as ruanganPenerimaNamaRuangan,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap,"
			+ " d.noDokumen as noDokumen,"
			+ " d.id as id,"
			+ " d.namaJudulDokumen as namaJudulDokumen,"
			+ " d.tglDokumen as tglDokumen,"
			+ " dm.noRec as noRec,"
			+ " dm.jamTerima as jamTerima,"
			+ " dm.jamKirim as jamKirim) from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join dm.statusBerkas statusBerkas "
			+ " left join dm.jenisSurat jenisSurat "
			+ " left join dm.sifatSurat sifatSurat "
			+ " left join dm.ruanganPengirim ruanganPengirim "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "
			+ " left join dm.ruanganPenerima ruanganPenerima "
			+ " where  d.isDokumenInOutInt = 2 "
			+ " and dm.noRec = :norec "
			+ " ")
	public Map<String,Object> getDokumenMasukInternalExternalForNumberingBynoRec(@Param("norec") String norec );
	
	@Query("select new Map("			 
			+ " ruanganPengirim.id as ruanganPengirimId,"
			+ " ruanganPengirim.namaRuangan as ruanganPengirimNamaRuangan,"
			+ " ruanganPenerima.id as ruanganPenerimaId,"
			+ " ruanganPenerima.namaRuangan as ruanganPenerimaNamaRuangan,"
			+ " pegawaiPenerimaSurat.id as pegawaiPenerimaSuratId,"
			+ " pegawaiPenerimaSurat.namaLengkap as pegawaiPenerimaSuratNamaLengkap,"
			+ " d.noDokumen as noDokumen,"
			+ " d.id as id,"
			+ " d.namaJudulDokumen as namaJudulDokumen,"
			+ " d.tglDokumen as tglDokumen,"
			+ " dm.noRec as noRec,"
			+ " dm.jamTerima as jamTerima,"
			+ " dm.jamKirim as jamKirim,"
			+ " pegawaiPembuat.namaLengkap as  namaLengkap,"
			+ " strukverifikasi.noRec as strukVerifikasinoRec, "
			+ " dm.status as status) "
			+ " from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join dm.statusBerkas statusBerkas "
			+ " left join dm.jenisSurat jenisSurat "
			+ " left join dm.sifatSurat sifatSurat "
			+ " left join dm.ruanganPengirim ruanganPengirim "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "
			+ " left join dm.ruanganPenerima ruanganPenerima "
			+ " left join d.pegawaiPembuat pegawaiPembuat "
			+ " left join dm.nokirimketujuanlast strukkirim"
			+" left join dm.objectstrukverifikasifk strukverifikasi"
			+ " where  d.isDokumenInOutInt IN (0,1)  "
			+ " and ruanganPenerima.id = :idRuangan "
			+ " and strukkirim.tglkirim between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')"
			+ " ")
	public List<Map<String,Object>> getListDokumenSuratMasukDistribusiRuang(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir,@Param("idRuangan") Integer idRuangan );
	
	@Query("select count(*) from Dokumen model "
			+ " left join model.unitPengelolaPrakarsaSurat unitPengelolaPrakarsaSurat"
			+ " left join model.klasifikasiArsip klasifikasiArsip"
			+ " left join model.subKlasifikasiArsip subKlasifikasiArsip"
			+ " where model.tahun = :tahun "
			+ " and klasifikasiArsip.id= :idKlasifikasiArsip "
			+ " and subKlasifikasiArsip.id= :idSubKlasifikasiArsip "
			+ " and unitPengelolaPrakarsaSurat.id= :idUnitPemrakarsa ")
	public Integer getNoSuratKeEksternal(@Param("idUnitPemrakarsa") Integer idUnitPemrakarsa, @Param("idKlasifikasiArsip")Integer idKlasifikasiArsip, @Param("idSubKlasifikasiArsip")Integer idSubKlasifikasiArsip, @Param("tahun")Integer tahun);
	
	@Query("select count(*) from Dokumen model "
		+ " left join model.unitPengelolaPrakarsaSurat unitPengelolaPrakarsaSurat"
		+ " where model.tahun = :tahun and model.bulan= :bulan and unitPengelolaPrakarsaSurat.id= :idUnitPemrakarsa ")
	public Integer getNoSuratKeInternal(@Param("idUnitPemrakarsa")Integer idUnitPemrakarsa,@Param("bulan") Integer bulan, @Param("tahun") Integer tahun);
	
	/* LIST SURAT UNTUK EKSTERNAL*/
	@Query("select new Map(model.namaJudulDokumen as judulDokumen,model.id as id, "
			+ " model.deskripsiDokumen as deskripsiDokumen, "
			+ " model.kdDokumen as kdDokumen,model.noDokumen as noDokumen,"
			+ " model.pathFile as pathFile , model.tglDokumen as tglDokumen,"
			+ " ruangan.namaRuangan as ruanganAsal,"
			+ " r.namaRuangan as ruanganTujuan,"
			+ " pegawaiPembuat.namaLengkap as pembuatSurat,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " dm.status as status,"
			+ " dm.tujuanSurat as tujuanSurat,"
			+ " dm.noRec as noRec,"
			+ " metodeKirim.nama as namaMetodeKirim) "
			+ " from DokumenMasuk dm "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "	
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen model "
			+ " left join dm.metodeKirim metodeKirim "
			+" left join model.ruangan ruangan"
			+ " left join model.pegawaiPembuat pegawaiPembuat"
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat"
			+ " where model.isDokumenInOutInt = 3 "
			//+ " and r.id = :idRuangan "
			+ " and model.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')")
	public List<Map<String,Object>> getListSuratMasukKeEksternal(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir);
	
	@Query("select distinct new Map (  model.id as idDokumen, dm.jangkaWaktu as jangkaWaktu ) "
			+ " from DokumenMasuk dm "
		 	+ " left join dm.kddokumen model "
		 	+" left join model.ruangan r "
			+ " where dm.jangkaWaktu IS NOT NULL "
			 
			+ " and model.tglDokumen between to_date( :tglAwal , 'dd-MM-yyyy') and to_date( :tglAkhir , 'dd-MM-yyyy')")
	public List<Map<String,Object>> getListSuratMasukJangkaWaktu(@Param("tglAwal") String tglAwal , @Param("tglAkhir") String tglAkhir);
	@Query("select new Map("
			+ " statusBerkas.id as statusBerkasId,"
			+ " statusBerkas.name as statusBerkasName,"
			+ " jenisSurat.id as jenisSuratId,"
			+ " jenisSurat.name as jenisSuratName,"
			+ " sifatSurat.id as sifatSuratId,"
			+ " sifatSurat.name as sifatSuratName,"
			+ " tipePengirimSurat.id as tipePengirimSuratId,"
			+ " tipePengirimSurat.name as tipePengirimSuratName,"
			+ " ruanganPengirim.id as ruanganPengirimId,"
			+ " ruanganPengirim.namaRuangan as ruanganPengirimNamaRuangan,"
			+ " dm.tujuanSurat as tujuanSurat,"
			+ " d.noDokumen as noDokumen,"
			+ " d.id as id,"
			+ " d.namaJudulDokumen as namaJudulDokumen,"
			+ " d.tglDokumen as tglDokumen,"
			+ " dm.noRec as noRec,"
			+ " dm.jamTerima as jamTerima,"
			+ " dm.jamKirim as jamKirim ) "
			+ " from DokumenMasuk dm "
			+ " left join dm.kddokumen d "
			+ " left join dm.statusBerkas statusBerkas "
			+ " left join dm.jenisSurat jenisSurat "
			+ " left join dm.sifatSurat sifatSurat "
			+ " left join dm.ruanganPengirim ruanganPengirim "
			+ " left join dm.pegawaiPenerimaSurat pegawaiPenerimaSurat "
			+ " left join dm.tipePengirimSurat tipePengirimSurat "
			+ " left join dm.ruanganPenerima ruanganPenerima "
			+ " where dm.jangkaWaktu IS NOT NULL  "
			//+ " and dm.noRec = :norec "
			+ " and d.id = :idDokumen "
			+ " ")
	public List<Map<String,Object>> getDokumenMasukJangkaWaktuByidDokumen(@Param("idDokumen") Integer idDokumen );
	@Query("select new Map(r.namaRuangan as namaRuangan, r.id as id "
			+ ") from DokumenMasuk dm "
			+ " left join dm.kdruangantujuanlast r "
			+ " left join dm.kddokumen d "
			+" left join d.ruangan r "
		 	+ " where d.id = :idDokumen"
			+ "  and dm.jangkaWaktu IS NOT NULL ")
	public List<Map<String, Object>> getListRuanganTujuanSuratMasukJangkaWaktu(@Param("idDokumen") Integer idDokumen);
	
	@Query("select new Map(model.id as id, model.namaJudulDokumen as namaJudulDokumen) from Dokumen model where model.id = :dokumenId and model.statusEnabled is true")
	public Map<String, Object> getDokumenById(@Param("dokumenId") Integer dokumenId);
	
}
