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.JenisBahanDao;
import com.jasamedika.medifirst2000.entities.JenisBahan;
import com.jasamedika.medifirst2000.service.JenisBahanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.JenisBahanVO;

@Service("JenisBahanService")
public class JenisBahanServiceImpl implements JenisBahanService {
	@Autowired
	private JenisBahanDao jenisBahanDao;
	
	@Autowired
	private BaseConverterImpl<JenisBahanVO, JenisBahan> jenisBahanConverter;

	@Override
	public Map<String, Object> saveJenisBahan(JenisBahanVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		JenisBahan jenisBahan = new JenisBahan();
		
		if(CommonUtil.isNotNullOrEmpty(vo)){
			jenisBahan = jenisBahanConverter.transferVOToModel(vo, jenisBahan);
		}
		
		/*
		jenisBahan.setReportDisplay(vo.getNamaExternal());
		jenisBahan.setKodeExternal(vo.getKodeExternal());
		jenisBahan.setNamaExternal(vo.getNamaExternal());
		jenisBahan.setJenisBahan(vo.getJenisBahan());
		*/	
		jenisBahan.setKdJenisBahan(StringUtil.formatNumber(String.valueOf(jenisBahanDao.count()+1), 3));
		
		JenisBahan jenisBahanResult = this.jenisBahanDao.save(jenisBahan);
		if(CommonUtil.isNotNullOrEmpty(jenisBahanResult)){
			result.put("id", jenisBahanResult.getId());
		}
		
		return result;
	}

	@Override
	public Map<String, Object> getJenisBahan() {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> data = this.jenisBahanDao.getAll();
		if(CommonUtil.isNotNullOrEmpty(data)){
			result.put("data", data);
		}
		return result;
	}

	@Override
	public Map<String, Object> getJenisBahanById(Integer id) {
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> data = this.jenisBahanDao.getJenisBahanById(id);
		if(CommonUtil.isNotNullOrEmpty(data)){
			result.put("data", data);
		}
		return result;
	}

}
