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 com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PlanningPelayananDao;
import com.jasamedika.medifirst2000.dao.StrukPlanningDao;
import com.jasamedika.medifirst2000.dao.custom.CssdSterilDaoCustom;
import com.jasamedika.medifirst2000.entities.PlanningPelayanan;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StatusPekerjaan;
import com.jasamedika.medifirst2000.entities.StrukPelayanan;
import com.jasamedika.medifirst2000.entities.StrukPlanning;
import com.jasamedika.medifirst2000.service.CssdSterilService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.CssdSterilDetailVO;
import com.jasamedika.medifirst2000.vo.CssdSterilVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;

@Service("CssdSterilService")
public class CssdSterilServiceImpl extends BaseAngaranServiceImpl implements CssdSterilService {

	@Autowired
	private CssdSterilDaoCustom cssdSterilDaoCustom;
	
	@Autowired
	private StrukPlanningDao strukPlanningDao;
	
	@Autowired
	private BaseConverterImpl<ProdukVO,Produk> produkConverter;
	
	@Autowired
	private PlanningPelayananDao planningPelayananDao;

	@Override
	public Map<String, Object> saveSteril(CssdSterilVO vo) {
		Ruangan ruanganAsal=cssdSterilDaoCustom.findRuanganAsalByStrukPelayanan(vo.getStrukPelayanan().getNoRec());
		Ruangan ruanganTujuan=cssdSterilDaoCustom.findRuanganTujuanByStrukPelayanan(vo.getStrukPelayanan().getNoRec());
		StrukPlanning strukPlanning=new StrukPlanning();
		if(CommonUtil.isNotNullOrEmpty(vo.getNoRec())){
			strukPlanning=strukPlanningDao.findOne(vo.getNoRec());
		}
		strukPlanning.setKdruanganasal(ruanganAsal);
		strukPlanning.setJenisHarga(vo.getJenisHarga());
		strukPlanning.setSiklus(vo.getSiklus());
		strukPlanning.setKdruangan(ruanganTujuan);
		strukPlanning.setDeskripsiplanning(vo.getJenisSterilisasi());
		strukPlanning.setNamaplanning("Steril");
		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(CssdSterilDetailVO detailVo:vo.getCssdSterilDetail()){
			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.setStrukPlaning(strukPlanning);
			planningPelayanan.setStrukPelayanan(strukPelayanan);
			planningPelayanan.setTglPlanningAkhirAn(vo.getJamSelesai());
			planningPelayanan.setTglPlanningAkhir(vo.getJamSelesai());
			planningPelayanan.setTglPlanningAwalAn(vo.getJamMulai());
			planningPelayanan.setTglPlanning(vo.getTanggal());
			planningPelayanan.setQtyProdukRetur(0.0);
			planningPelayanan.setStatus(detailVo.getStatus());
			/*if(CommonUtil.isNotNullOrEmpty(vo.getRegistrasiAset())){
				RegistrasiAset registrasiAset=registrasiAsetDao.findById(vo.getRegistrasiAset().getNoRec());
				planningPelayanan.setRegistrasiAset(registrasiAset);
				if(CommonUtil.isNotNullOrEmpty(registrasiAset.getProduk())){
					planningPelayanan.setProduk(registrasiAset.getProduk());
				}
			}*/
			if(CommonUtil.isNotNullOrEmpty(detailVo.getProduk())){
				planningPelayanan.setProduk(produkConverter.transferVOToModel(detailVo.getProduk(), new Produk()));
			}
			
			planningPelayanan.setQtyProdukExec(detailVo.getQtyProduk());
			planningPelayanan.setKeterangan(detailVo.getHasil());
			planningPelayanan.setTglPelayanan(new Date());
			planningPelayanan.setStatus(true);
			planningPelayananDao.save(planningPelayanan);
		}
		
		
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("noRec", strukPlanning.getNoRec());
		
		return data;
		
	}

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

	
}
