package com.jasamedika.medifirst2000.dao.custom;

import javax.persistence.Query;

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

import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.IpsrsKalibrasi;
import com.jasamedika.medifirst2000.entities.IpsrsKontakService;
import com.jasamedika.medifirst2000.entities.IpsrsMaintenance;
import com.jasamedika.medifirst2000.entities.IpsrsPemeliharaan;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Repository
public class IpsrsMaintenanceDaoCustomImpl extends 
			CoreDaoImpl<IpsrsMaintenance> implements IpsrsMaintenanceDaoCustom{

	public IpsrsMaintenanceDaoCustomImpl() {
		super(IpsrsMaintenance.class);
	}

	@Override
	@Transactional
	public void deleteIpsrsPemeliharaan(IpsrsMaintenance model){
		try{
			IpsrsPemeliharaan ipsrsPemeliharaan = em.find(IpsrsPemeliharaan.class, model.getIpsrsPemeliharaan().noRec);
			em.remove(em.contains(ipsrsPemeliharaan) ? ipsrsPemeliharaan : em.merge(ipsrsPemeliharaan));
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	@Override
	@Transactional
	public void deleteIpsrsKontakService(IpsrsMaintenance model){
		try{
			IpsrsKontakService ipsrsKontakService = em.find(IpsrsKontakService.class, model.getIpsrsKontakService().noRec);
			em.remove(em.contains(ipsrsKontakService) ? ipsrsKontakService : em.merge(ipsrsKontakService));
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	@Override
	@Transactional
	public void deleteIpsrsKalibrasi(IpsrsMaintenance model){
		try{
			IpsrsKalibrasi ipsrsKalibrasi = em.find(IpsrsKalibrasi.class, model.getIpsrsKalibrasi().noRec);
			em.remove(em.contains(ipsrsKalibrasi) ? ipsrsKalibrasi : em.merge(ipsrsKalibrasi));
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	@Override
	@Transactional
	public Integer updateStatusPemeliharaan(String noRec){
		IpsrsPemeliharaan ipsrsPemeliharaan = em.find(IpsrsPemeliharaan.class, noRec);
		String status = ipsrsPemeliharaan.getStatusPemeliharaan();
		if(CommonUtil.isNullOrEmpty(status)){
			Query query = em.createQuery("update IpsrsPemeliharaan model set model.statusPemeliharaan=:statusPemeliharaan where model.noRec=:noRec");
			query.setParameter("statusPemeliharaan", "1");
			query.setParameter("noRec", noRec);
			return query.executeUpdate();
		} else{
			Query query = em.createQuery("update IpsrsPemeliharaan model set model.statusPemeliharaan=:statusPemeliharaan where model.noRec=:noRec");
			query.setParameter("statusPemeliharaan", String.valueOf(Integer.valueOf(status)+1));
			query.setParameter("noRec", noRec);
			return query.executeUpdate();
		}
	}
	
	@Override
	@Transactional
	public Integer updateStatusKontakService(String noRec){
		IpsrsKontakService ipsrsKontakService = em.find(IpsrsKontakService.class, noRec);
		String status = ipsrsKontakService.getStatusKontakService();
		if(CommonUtil.isNullOrEmpty(status)){
			Query query = em.createQuery("update IpsrsKontakService model set model.statusKontakService=:statusKontakService where model.noRec=:noRec");
			query.setParameter("statusKontakService", "1");
			query.setParameter("noRec", noRec);
			return query.executeUpdate();
		} else{
			Query query = em.createQuery("update IpsrsKontakService model set model.statusKontakService=:statusKontakService where model.noRec=:noRec");
			query.setParameter("statusKontakService", String.valueOf(Integer.valueOf(status)+1));
			query.setParameter("noRec", noRec);
			return query.executeUpdate();
		}
	}
	
	@Override
	@Transactional
	public Integer updateKalibrasi(String noRec){
		IpsrsKalibrasi ipsrsKalibrasi = em.find(IpsrsKalibrasi.class, noRec);
		String status = ipsrsKalibrasi.getStatusKalibrasi();
		if(CommonUtil.isNullOrEmpty(status)){
			Query query = em.createQuery("update IpsrsKalibrasi model set model.statusKalibrasi=:statusKalibrasi where model.noRec=:noRec");
			query.setParameter("statusKalibrasi", "1");
			query.setParameter("noRec", noRec);
			return query.executeUpdate();
		} else{
			Query query = em.createQuery("update IpsrsKalibrasi model set model.statusKalibrasi=:statusKalibrasi where model.noRec=:noRec");
			query.setParameter("statusKalibrasi", String.valueOf(Integer.valueOf(status)+1));
			query.setParameter("noRec", noRec);
			return query.executeUpdate();
		}
	}
}
