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

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

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.dao.custom.K3LaporanKecelakaanKerjaDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.K3LaporanKecelakaanKerja;
import com.jasamedika.medifirst2000.entities.LkkIdentifikasiKejadian;
import com.jasamedika.medifirst2000.entities.LkkIdentifikasiKorban;
import com.jasamedika.medifirst2000.entities.LkkSaksiKejadian;

/**
 * Repository Custom class
 * 
 * @author Reza
 */

@Repository
public class K3LaporanKecelakaanKerjaDaoCustomImpl extends 
			CoreDaoImpl<K3LaporanKecelakaanKerja> implements K3LaporanKecelakaanKerjaDaoCustom{

	public K3LaporanKecelakaanKerjaDaoCustomImpl() {
		super(K3LaporanKecelakaanKerja.class);
	}
	
	@Override
	public List<Map<String, Object>> getUnitRuangan(){
		Query query = em.createQuery("select NEW Map (model.id as ruanganId, model.namaRuangan as namaRuangan) from Ruangan model");
		return query.getResultList();
	}
	
	@Override
	public List<Map<String, Object>> getIdentifikasiKejadian(Integer idPegawai){
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map (");
		buffer.append("a.noRec as noRec_verifikasi, ");
		buffer.append("a.pelaporId as pelaporId, ");
		buffer.append("a.verifikasi as verifikasi, ");
		buffer.append("b.namaLengkap as namaLengkap, ");
		buffer.append("c.noRec as noRec, ");
		buffer.append("c.tempatKejadian as tempatKejadian, ");
		buffer.append("c.tanggal as tanggal, ");
		buffer.append("c.awalKejadian as awalKejadian, ");
		buffer.append("c.jamKejadian as jamKejadian, ");
		buffer.append("c.kondisiSaatKejadian as kondisiSaatKejadian, ");
		buffer.append("c.penolongPertama as penolongPertama, ");
		buffer.append("c.tindakanPenolong as tindakanPenolong) ");
		buffer.append("from K3LaporanKecelakaanKerja a ");
		buffer.append("left join a.pelapor b ");
		buffer.append("left join a.lkkIdentifikasiKejadian c where ");
		buffer.append("a.verifikasi=:param1 or a.verifikasi=:param2 or a.verifikasi=:param3 ");
		buffer.append("order by c.tanggal desc");
		
		Query query = em.createQuery(buffer.toString());
		if(idPegawai==10){
			query.setParameter("param1", 1);
			query.setParameter("param2", 0);
			query.setParameter("param3", 0);
		} else if(idPegawai==11){
			query.setParameter("param1", 2);
			query.setParameter("param2", 0);
			query.setParameter("param3", 0);
		} else if(idPegawai==2){
			query.setParameter("param1", 1);
			query.setParameter("param2", 2);
			query.setParameter("param3", 4);
		}

		return query.getResultList();
	}
	
	@Override
	public List<Map<String, Object>> getLkkIdentifikasiKorban(String noRec){
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map (");
		buffer.append("b.noRec as noRec_korban, ");
		buffer.append("b.namaKorban as namaKorban, ");
		buffer.append("b.niKependudukan as niKependudukan, ");
		buffer.append("b.tempatLahir as tempatLahir, ");
		buffer.append("b.tglLahir as tglLahir, ");
		buffer.append("b.jenisKelamin as jenisKelamin, ");
		buffer.append("b.statusPekerjaan as statusPekerjaan, ");
		buffer.append("b.statusJabatan as statusJabatan, ");
		buffer.append("c.id as idRuangan, ");
		buffer.append("c.namaRuangan as namaRuangan, ");
		buffer.append("d.noRec as noRec_tindakLanjut, ");
		buffer.append("d.bagianTubuh as bagianTubuh, ");
		buffer.append("d.jenisCedera as jenisCedera, ");
		buffer.append("d.jenisPengobatan as jenisPengobatan, ");
		buffer.append("d.tindakanRawatInap as tindakanRawatInap, ");
		buffer.append("d.tindakanRawatJalan as tindakanRawatJalan, ");
		buffer.append("d.tindakanDiLuarRsab as tindakanDiLuarRsab, ");
		buffer.append("d.tindakanLanjutan as tindakanLanjutan, ");
		buffer.append("e.noRec as noRec_kerugian, ");
		buffer.append("e.lamaTidakKerjaPerJam as lamaTidakKerjaPerJam, ");
		buffer.append("e.lamaIstirahatPerJam as lamaIstirahatPerJam, ");
		buffer.append("e.lamaPemulihanPerHari as lamaPemulihanPerHari, ");
		buffer.append("e.ketKerugianWaktu as ketKerugianWaktu, ");
		buffer.append("e.biayaPostKecelakaan as biayaPostKecelakaan, ");
		buffer.append("e.biayaPengobatan as biayaPengobatan, ");
		buffer.append("e.biayaPemulihan as biayaPemulihan, ");
		buffer.append("e.ketKerugianKesehatan as ketKerugianKesehatan) ");
		buffer.append("from LkkIdentifikasiKejadian a ");
		buffer.append("left join a.lkkIdentifikasiKorban b ");
		buffer.append("left join b.unitRuangan c ");
		buffer.append("left join b.lkkRencanaTindakLanjut d ");
		buffer.append("left join b.lkkPerkiraanKerugian e ");
		buffer.append("where b.lkkIdentifikasiKejadianId=:noRec ");
		buffer.append("and (b.statusEnabled is null or b.statusEnabled is true) ");
		
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		return query.getResultList();
	}
	
	@Override
	public List<Map<String, Object>> getLkkSaksiKejadian(String noRec){
		StringBuffer buffer = new StringBuffer();
		buffer.append("select NEW Map (");
		buffer.append("b.noRec as noRec, ");
		buffer.append("b.namaSaksi as namaSaksi, ");
		buffer.append("b.pekerjaan as pekerjaan, ");
		buffer.append("b.noKontak as noKontak) ");
		buffer.append("from LkkIdentifikasiKejadian a ");
		buffer.append("left join a.lkkSaksiKejadian b ");
		buffer.append("where b.lkkIdentifikasiKejadianId=:noRec");

		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", noRec);
		return query.getResultList();
	}
	
	@Override
	@Transactional
	public Integer updateIdentifikasiKejadian(LkkIdentifikasiKejadian model){
		StringBuffer buffer = new StringBuffer();
		buffer.append("update LkkIdentifikasiKejadian model set ");
		buffer.append("model.tempatKejadian=:tempatKejadian, ");
		buffer.append("model.tanggal=:tanggal, ");
		buffer.append("model.awalKejadian=:awalKejadian, ");
		buffer.append("model.jamKejadian=:jamKejadian, ");
		buffer.append("model.kondisiSaatKejadian=:kondisiSaatKejadian, ");
		buffer.append("model.penolongPertama=:penolongPertama, ");
		buffer.append("model.tindakanPenolong=:tindakanPenolong ");
		buffer.append("where model.noRec=:noRec");
		
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", model.getNoRec());
		query.setParameter("tempatKejadian", model.getTempatKejadian());
		query.setParameter("tanggal", model.getTanggal());
		query.setParameter("awalKejadian", model.getAwalKejadian());
		query.setParameter("jamKejadian", model.getJamKejadian());
		query.setParameter("kondisiSaatKejadian", model.getKondisiSaatKejadian());
		query.setParameter("penolongPertama", model.getPenolongPertama());
		query.setParameter("tindakanPenolong", model.getTindakanPenolong());
		
		for(LkkIdentifikasiKorban model1 : model.getLkkIdentifikasiKorban()){
			StringBuffer buffer2 = new StringBuffer();
			buffer2.append("update LkkRencanaTindakLanjut model set ");
			buffer2.append("model.tindakanLanjutan=:tindakanLanjutan ");
			buffer2.append("where model.lkkIdentifikasiKorbanId=:noRec");
			Query query2 = em.createQuery(buffer2.toString());
			query2.setParameter("noRec", model1.getNoRec());
			query2.setParameter("tindakanLanjutan", model1.getLkkRencanaTindakLanjut().getTindakanLanjutan());
			query2.executeUpdate();			
		}
		
			
		return query.executeUpdate();
	}
	
	@Override
	@Transactional
	public Integer updateIdentifikasiKorban(LkkIdentifikasiKorban model){
		StringBuffer buffer1 = new StringBuffer();
		buffer1.append("update LkkIdentifikasiKorban model set ");
		buffer1.append("model.namaKorban=:namaKorban, ");
		buffer1.append("model.niKependudukan=:niKependudukan, ");
		buffer1.append("model.tempatLahir=:tempatLahir, ");
		buffer1.append("model.tglLahir=:tglLahir, ");
		buffer1.append("model.jenisKelamin=:jenisKelamin, ");
		buffer1.append("model.statusPekerjaan=:statusPekerjaan, ");
		buffer1.append("model.statusJabatan=:statusJabatan, ");
		buffer1.append("model.unitRuangan=:unitRuangan ");
		buffer1.append("where model.noRec=:noRec");
		Query query1 = em.createQuery(buffer1.toString());
		query1.setParameter("noRec", model.getNoRec());
		query1.setParameter("namaKorban", model.getNamaKorban());
		query1.setParameter("niKependudukan", model.getNiKependudukan());
		query1.setParameter("tempatLahir", model.getTempatLahir());
		query1.setParameter("tglLahir", model.getTglLahir());
		query1.setParameter("jenisKelamin", model.getJenisKelamin());
		query1.setParameter("statusPekerjaan", model.getStatusPekerjaan());
		query1.setParameter("statusJabatan", model.getStatusJabatan());
		query1.setParameter("unitRuangan", model.getUnitRuangan());
		Integer result1 = query1.executeUpdate();
		
		Integer result2 = 0;
		if(result1!=0){
			StringBuffer buffer2 = new StringBuffer();
			buffer2.append("update LkkRencanaTindakLanjut model set ");
			buffer2.append("model.bagianTubuh=:bagianTubuh, ");
			buffer2.append("model.jenisCedera=:jenisCedera, ");
			buffer2.append("model.jenisPengobatan=:jenisPengobatan, ");
			buffer2.append("model.tindakanRawatInap=:tindakanRawatInap, ");
			buffer2.append("model.tindakanRawatJalan=:tindakanRawatJalan, ");
			buffer2.append("model.tindakanDiLuarRsab=:tindakanDiLuarRsab ");
			buffer2.append("where model.noRec=:noRec");
			Query query2 = em.createQuery(buffer2.toString());
			query2.setParameter("noRec", model.getLkkRencanaTindakLanjut().getNoRec());
			query2.setParameter("bagianTubuh", model.getLkkRencanaTindakLanjut().getBagianTubuh());
			query2.setParameter("jenisCedera", model.getLkkRencanaTindakLanjut().getJenisCedera());
			query2.setParameter("jenisPengobatan", model.getLkkRencanaTindakLanjut().getJenisPengobatan());
			query2.setParameter("tindakanRawatInap", model.getLkkRencanaTindakLanjut().getTindakanRawatInap());
			query2.setParameter("tindakanRawatJalan", model.getLkkRencanaTindakLanjut().getTindakanRawatJalan());
			query2.setParameter("tindakanDiLuarRsab", model.getLkkRencanaTindakLanjut().getTindakanDiLuarRsab());
			result2 = query2.executeUpdate();
		}
		
		Integer result3 = 0;
		if(result2!=0){
			StringBuffer buffer3 = new StringBuffer();
			buffer3.append("update LkkPerkiraanKerugian model set ");
			buffer3.append("model.lamaTidakKerjaPerJam=:lamaTidakKerjaPerJam, ");
			buffer3.append("model.lamaIstirahatPerJam=:lamaIstirahatPerJam, ");
			buffer3.append("model.lamaPemulihanPerHari=:lamaPemulihanPerHari, ");
			buffer3.append("model.ketKerugianWaktu=:ketKerugianWaktu, ");
			buffer3.append("model.biayaPostKecelakaan=:biayaPostKecelakaan, ");
			buffer3.append("model.biayaPengobatan=:biayaPengobatan, ");
			buffer3.append("model.biayaPemulihan=:biayaPemulihan, ");
			buffer3.append("model.ketKerugianKesehatan=:ketKerugianKesehatan ");
			buffer3.append("where noRec=:noRec");
			Query query3 = em.createQuery(buffer3.toString());
			query3.setParameter("noRec", model.getLkkPerkiraanKerugian().getNoRec());
			query3.setParameter("lamaTidakKerjaPerJam", model.getLkkPerkiraanKerugian().getLamaTidakKerjaPerJam());
			query3.setParameter("lamaIstirahatPerJam", model.getLkkPerkiraanKerugian().getLamaTidakKerjaPerJam());
			query3.setParameter("lamaPemulihanPerHari", model.getLkkPerkiraanKerugian().getLamaPemulihanPerHari());
			query3.setParameter("ketKerugianWaktu", model.getLkkPerkiraanKerugian().getKetKerugianWaktu());
			query3.setParameter("biayaPostKecelakaan", model.getLkkPerkiraanKerugian().getBiayaPostKecelakaan());
			query3.setParameter("biayaPengobatan", model.getLkkPerkiraanKerugian().getBiayaPengobatan());
			query3.setParameter("biayaPemulihan", model.getLkkPerkiraanKerugian().getBiayaPemulihan());
			query3.setParameter("ketKerugianKesehatan", model.getLkkPerkiraanKerugian().getKetKerugianKesehatan());
			result3 = query3.executeUpdate();
		}
		
		return result3;
	}
	
	@Override
	@Transactional
	public Integer updateSaksiKejadian(LkkSaksiKejadian model) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("update LkkSaksiKejadian model set ");
		buffer.append("model.namaSaksi=:namaSaksi, ");
		buffer.append("model.pekerjaan=:pekerjaan, ");
		buffer.append("model.noKontak=:noKontak ");
		buffer.append("where model.noRec=:noRec");
		Query query = em.createQuery(buffer.toString());
		query.setParameter("noRec", model.getNoRec());
		query.setParameter("namaSaksi", model.getNamaSaksi());
		query.setParameter("pekerjaan", model.getPekerjaan());
		query.setParameter("noKontak", model.getNoKontak());
		return query.executeUpdate();
	}
	
	@Override
	@Transactional
	public void deleteIdentifikasiKejadian(String noRec){
		K3LaporanKecelakaanKerja k3LaporanKecelakaanKerja = em.find(K3LaporanKecelakaanKerja.class, noRec);
		em.remove(em.contains(k3LaporanKecelakaanKerja) ? k3LaporanKecelakaanKerja : em.merge(k3LaporanKecelakaanKerja));
	}
	
	@Override
	@Transactional
	public void deleteIdentifikasiKorban(String noRec){
		LkkIdentifikasiKorban lkkIdentifikasiKorban = em.find(LkkIdentifikasiKorban.class, noRec);
		em.remove(em.contains(lkkIdentifikasiKorban) ? lkkIdentifikasiKorban : em.merge(lkkIdentifikasiKorban));
	}
	
	@Override
	@Transactional
	public void deleteSaksiKejadian(String noRec) {
		LkkSaksiKejadian lkkSaksiKejadian = em.find(LkkSaksiKejadian.class, noRec);
		em.remove(em.contains(lkkSaksiKejadian) ? lkkSaksiKejadian : em.merge(lkkSaksiKejadian));
	}
	
	@Override
	@Transactional
	public Integer verifLaporanKecelakaan(K3LaporanKecelakaanKerja model){
		Query query = em.createQuery("update K3LaporanKecelakaanKerja model set model.verifikasi=:verifikasi where model.noRec=:noRec");
		query.setParameter("verifikasi", model.getVerifikasi());
		query.setParameter("noRec", model.getNoRec());
		return query.executeUpdate();
	}

}
