package com.jasamedika.medifirst2000.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.CssdUjiVisualDAO;
import com.jasamedika.medifirst2000.dao.PlanningPelayananDao;
import com.jasamedika.medifirst2000.dao.StrukPlanningDao;
import com.jasamedika.medifirst2000.dao.custom.CssdMonitoringSteamDaoCustom;
import com.jasamedika.medifirst2000.entities.CssdUjiVisual;
import com.jasamedika.medifirst2000.entities.PlanningPelayanan;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StatusBaikTidakBaik;
import com.jasamedika.medifirst2000.entities.StatusBerubahTidak;
import com.jasamedika.medifirst2000.entities.StatusPekerjaan;
import com.jasamedika.medifirst2000.entities.StrukPelayanan;
import com.jasamedika.medifirst2000.entities.StrukPlanning;
import com.jasamedika.medifirst2000.service.CssdMonitoringSteamService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.CssdMonitoringSteamDetailVO;
import com.jasamedika.medifirst2000.vo.CssdMonitoringSteamVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.StatusBaikTidakBaikVO;
import com.jasamedika.medifirst2000.vo.StatusBerubahTidakVO;

@Service("CssdMonitoringSteamService")
public class CssdMonitoringSteamServiceImpl extends BaseAngaranServiceImpl implements CssdMonitoringSteamService {

	@Autowired
	private CssdMonitoringSteamDaoCustom cssdMonitoringSteamDaoCustom;
	
	@Autowired
	private StrukPlanningDao strukPlanningDao;
	
	@Autowired
	private CssdUjiVisualDAO cssdUjiVisualDAO;
	
	@Autowired
	private BaseConverterImpl<ProdukVO,Produk> produkConverter;
	

	@Autowired
	private BaseConverterImpl<StatusBerubahTidakVO,StatusBerubahTidak> statusBerubahTidakConverter;
	
	@Autowired
	private BaseConverterImpl<StatusBaikTidakBaikVO,StatusBaikTidakBaik> statusBaikTidakBaikConverter;
	
	
	@Autowired
	private PlanningPelayananDao planningPelayananDao;

	@Override
	@Transactional(readOnly=false)
	public Map<String, Object> saveMonitoringSteam(CssdMonitoringSteamVO vo) {
		Ruangan ruanganAsal=cssdMonitoringSteamDaoCustom.findRuanganAsalByStrukPelayanan(vo.getStrukPelayanan().getNoRec());
		Ruangan ruanganTujuan=cssdMonitoringSteamDaoCustom.findRuanganTujuanByStrukPelayanan(vo.getStrukPelayanan().getNoRec());
		StrukPlanning strukPlanning=new StrukPlanning();
		if(CommonUtil.isNotNullOrEmpty(vo.getNoRec())){
			strukPlanning=strukPlanningDao.findOne(vo.getNoRec());
		}
		strukPlanning.setKdruanganasal(ruanganAsal);
		strukPlanning.setKdruangan(ruanganTujuan);
		strukPlanning.setDeskripsiplanning("MonitoringSteam");
		strukPlanning.setNamaplanning("MonitoringSteam");
		strukPlanning.setKdalamattujuan(null);
		strukPlanning.setKdasalanggaran(null);
		strukPlanning.setKddokumensk(null);
		strukPlanning.setKdjenisanggaran(null);
		strukPlanning.setKdjenistempat(null);
		strukPlanning.setKdkelompoktransaksi(null);
		strukPlanning.setKdpegawaipjawab(null);
		strukPlanning.setKdpegawaipjawabevaluasi(null);
		strukPlanning.setKdperiodeaccount(null);
		strukPlanning.setKdProfile((short) 0);
		strukPlanning.setTglplanning(vo.getTanggal());
		strukPlanning.setTglpegajuan(new Date());
		strukPlanning.setKdrekanan(null);
		StatusPekerjaan statusPekerjaan=new StatusPekerjaan();
		statusPekerjaan.setId(3);
		strukPlanning.setStatusPekerjaan(statusPekerjaan);
		strukPlanning=strukPlanningDao.save(strukPlanning);
		
		for(CssdMonitoringSteamDetailVO detailVo:vo.getCssdMonitoringSteamDetailVO()){
			StrukPelayanan strukPelayanan=new StrukPelayanan();
			strukPelayanan.setNoRec(vo.getStrukPelayanan().getNoRec());
			PlanningPelayanan planningPelayanan=new PlanningPelayanan();
			if(CommonUtil.isNotNullOrEmpty(detailVo.getNoRec())){
				planningPelayanan=planningPelayananDao.findOne(detailVo.getNoRec());
			}
			planningPelayanan.setKdProfile((short) 0);
			planningPelayanan.setStrukOrder(null);
			planningPelayanan.setStrukPelayanan(strukPelayanan);
			planningPelayanan.setTglPlanningAkhirAn(detailVo.getJamMulaiSterilisasi());
			planningPelayanan.setTglPlanningAkhir(new Date());
			planningPelayanan.setTglPlanningAwalAn(detailVo.getJamSelesaiSterilisasi());
			planningPelayanan.setTglPlanning(vo.getTanggal());
			planningPelayanan.setQtyProdukRetur(0.0);
			planningPelayanan.setStatus(true);
		
			if(CommonUtil.isNotNullOrEmpty(detailVo.getProduk())){
				planningPelayanan.setProduk(produkConverter.transferVOToModel(detailVo.getProduk(), new Produk()));
			}
			
			planningPelayanan.setQtyProdukExec(detailVo.getQtyProduk());
			planningPelayanan.setKeterangan(detailVo.getHasil());
			planningPelayanan.setStrukPlaning(strukPlanning);
			planningPelayanan.setTglPelayanan(new Date());
			planningPelayananDao.save(planningPelayanan);
			CssdUjiVisual cssdUjiVisual=new CssdUjiVisual();
			if(CommonUtil.isNotNullOrEmpty(planningPelayanan.getNoRec())){
				cssdUjiVisual=cssdUjiVisualDAO.findByParent(planningPelayanan.getNoRec());
			}
			if(CommonUtil.isNullOrEmpty(cssdUjiVisual)){
				cssdUjiVisual=new CssdUjiVisual();
			}
			cssdUjiVisual.setAlatBerkarat(detailVo.getAlatBerkarat());
			cssdUjiVisual.setJamMulaiSterilisasi(detailVo.getJamMulaiSterilisasi());
			cssdUjiVisual.setKdProfile((short) 0);
			cssdUjiVisual.setKebersihanAlat(detailVo.getKebersihanAlat());
			cssdUjiVisual.setKelayakanAlat(detailVo.getKelayakanAlat());
			cssdUjiVisual.setKelenturanAlat(detailVo.getKelenturanAlat());
			cssdUjiVisual.setKemampuanMenjepit(detailVo.getKemampuanMenjepit());
			cssdUjiVisual.setKetajamanAlat(detailVo.getKetajamanAlat());
			cssdUjiVisual.setKeutuhanAlat(detailVo.getKeutuhanAlat());
			cssdUjiVisual.setKondisiAlat(detailVo.getKondisiAlat());
			cssdUjiVisual.setLinenYangdikemas(detailVo.getLinenYangdikemas());
			cssdUjiVisual.setTindakan(detailVo.getTindakan());
			cssdUjiVisual.setWarnaAlat(detailVo.getWarnaAlat());
			cssdUjiVisual.setPlanningPelayanan(planningPelayanan);
			cssdUjiVisual.setLinenYangdikemas(detailVo.getLinenYangdikemas());
			cssdUjiVisual.setJamMulaiSterilisasi(detailVo.getJamMulaiSterilisasi());
			cssdUjiVisual.setJamSelesaiSterilisasi(detailVo.getJamSelesaiSterilisasi());
			cssdUjiVisualDAO.save(cssdUjiVisual);
		}
		
		
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("noRec", strukPlanning.getNoRec());
		
		return data;
		
	}

	@Override
	public Map<String, Object> detailMonitoringSteam(String noRec) {
		
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("header", cssdMonitoringSteamDaoCustom.findStrukPelayananByNoRec(noRec));
		data.put("detail", cssdMonitoringSteamDaoCustom.findStrukPelayananDetailByNoRec(noRec));
		return data;
		
	}

	
}
