package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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.dao.RIS_OrderDao;
import com.jasamedika.medifirst2000.dao.RIS_ReportDao;
import com.jasamedika.medifirst2000.entities.RIS_Order;
import com.jasamedika.medifirst2000.entities.RIS_Report;
import com.jasamedika.medifirst2000.service.RIS_ReportService;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Service("RIS_ReportService")
public class RIS_ReportServiceImpl implements RIS_ReportService{

	@Autowired
	private RIS_ReportDao rIS_ReportDao;
	
	@Autowired
	private RIS_OrderDao rIS_OrderDao;
	
	@Override
	public Map<String, Object> save(RIS_Report vo) {	
		Map<String, Object> result = new HashMap<>();
		if(CommonUtil.isNotNullOrEmpty(vo)) {
			RIS_Report model = rIS_ReportDao.save(vo);
			result.put("id", model.getREPORT_KEY());
		}
		return result;
	}

	@Override
	public void delete(RIS_Report vo) {
		if(CommonUtil.isNotNullOrEmpty(vo)) {
			rIS_ReportDao.delete(vo.getREPORT_KEY());
		}
	}
	
	@Override
	public RIS_Report getOne(RIS_Report vo) {
		RIS_Report result = new RIS_Report();
		if(CommonUtil.isNotNullOrEmpty(vo.getREPORT_KEY())) {
			result = rIS_ReportDao.findOne(vo.getREPORT_KEY());
		}
		return result;
	}
	
	@Override
	public List<RIS_Report> getAll() {
		List<RIS_Report> result = new ArrayList<>();
		result = (List<RIS_Report>) rIS_ReportDao.findAll();
		return result;
	}

	@Override
	public List<Map<String, Object>> getReportByTglCreate(String startDate, String endDate) {
		List<Map<String, Object>> result = rIS_ReportDao.getReportByTglCreate(startDate.replace("-", ""), endDate.replace("-", ""));
		for(Map<String, Object> data : result)
		{
			RIS_Order order = rIS_OrderDao.getOneByAccessionNum(data.get("accesion_num")+"");
			if(CommonUtil.isNotNullOrEmpty(order))
			{
				data.put("patientId", order.getPATIENT_ID());
			}
			else
			{
				data.put("patientId", "");
			}
		}
		return result;
	}
}
