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.CssdPengemasanDaoCustom;
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.CssdPengemasanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.CssdPengemasanDetailVO;
import com.jasamedika.medifirst2000.vo.CssdPengemasanVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;

@Service("CssdPengemasanService")
public class CssdPengemasanServiceImpl extends BaseAngaranServiceImpl implements CssdPengemasanService {

	@Autowired
	private CssdPengemasanDaoCustom cssdPengemasanDaoCustom;
	
	@Autowired
	private StrukPlanningDao strukPlanningDao;
	
	@Autowired
	private BaseConverterImpl<ProdukVO,Produk> produkConverter;
	
	@Autowired
	private PlanningPelayananDao planningPelayananDao;

	@Override
	public Map<String, Object> savePengemasan(CssdPengemasanVO vo) {
		Ruangan ruanganAsal=cssdPengemasanDaoCustom.findRuanganAsalByStrukPelayanan(vo.getStrukPelayanan().getNoRec());
		Ruangan ruanganTujuan=cssdPengemasanDaoCustom.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("Pengemasan");
		strukPlanning.setNamaplanning("Pengemasan");
		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(CssdPengemasanDetailVO detailVo:vo.getCssdPengemasanDetail()){
			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(detailVo.getJamSelesai());
			planningPelayanan.setTglPlanningAkhir(detailVo.getJamSelesai());
			planningPelayanan.setTglPlanningAwalAn(detailVo.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());
			planningPelayananDao.save(planningPelayanan);
		}
		
		
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("noRec", strukPlanning.getNoRec());
		
		return data;
		
	}

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

	
}
