package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.List;
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.UnitOrgDao;
import com.jasamedika.medifirst2000.entities.UnitOrg;
import com.jasamedika.medifirst2000.service.UnitOrgService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.UnitOrgVO;

@Service("unitOrgService")
public class UnitOrgServiceImpl implements UnitOrgService{

	@Autowired
	private UnitOrgDao unitOrgDao;
	
	@Autowired
	private BaseConverterImpl<UnitOrgVO, UnitOrg> unitOrgConverter;

	
	@Override
	public Map<String, Object> saveUnitOrg(UnitOrgVO vo) {
		UnitOrg unitOrg = new UnitOrg();
		Map<String,Object> result = new HashMap<String,Object>();
		
		if(CommonUtil.isNotNullOrEmpty(vo)){
			unitOrg = unitOrgConverter.transferVOToModel(vo, new UnitOrg());
		}
		
		if(CommonUtil.isNotNullOrEmpty(unitOrg)){
			UnitOrg resultUnitOrg = unitOrgDao.save(unitOrg);
			result.put("kodePengendali", resultUnitOrg.getKodeUnitOrg());
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> findAllUnitOrg() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<UnitOrg> resultModel = (List<UnitOrg>) unitOrgDao.findAll();
		
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> findByKodeUnitOrg(String KodeUnitOrg) {
		Map<String, Object> result = new HashMap<String, Object>();
		UnitOrg resultModel = unitOrgDao.findByKodeUnitOrg(KodeUnitOrg);
		
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> findOneUnitOrg() {
		Map<String, Object> result = new HashMap<String, Object>();
		UnitOrg resultModel = unitOrgDao.findByKodeUnitOrg("04");
		
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}
	
}
