package com.eactive.eai.common.c2rservice;

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 org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.common.c2rservice.loader.C2RServiceLoader;
import com.eactive.eai.common.c2rservice.mapper.C2RServiceMapper;
import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.c2rservice.C2RService;

@Service
@Transactional
public class C2RServiceDAO extends BaseDAO {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Autowired
	C2RServiceLoader c2rServiceEntityService;

	@Autowired
	C2RServiceMapper c2rServiceMapper;

	public Map<String, C2RServiceVO> getAllC2RServices() throws DAOException {
		try {
			HashMap<String, C2RServiceVO> c2rMap = new HashMap<>();
			List<C2RService> c2rServices = c2rServiceEntityService.findByUseynOrderByDefault("1");
			logger.info("[ @@ Load C2RService Configuration - starting >>>>>>>>>>>>>>>>> ]");

			for (C2RService c2rService : c2rServices) {
				C2RServiceVO vo = c2rServiceMapper.toVo(c2rService);
				logger.info("@ " + vo.toString());
				String key = c2rService.getId().getExtnlinstiidnfiname() + c2rService.getId().getSvcprcssno();
				c2rMap.put(key, vo);
			}
			logger.info("[>>Load C2RService Configuration - ended ]");
			return c2rMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		}
	}

	public Map<String, C2RServiceVO> getC2RService(String c2rExtnlInstiCd) throws DAOException {
		try {
			HashMap<String, C2RServiceVO> c2rMap = new HashMap<>();
			C2RService c2rService = c2rServiceEntityService.findByIdExtnlinstiidnfinameOrderByDefault(c2rExtnlInstiCd);
			logger.info("[ @@ Load C2RService getC2RService Configuration - starting >>>>>>>>>>>>>>>>> ]");

			if (c2rService != null) {
				C2RServiceVO vo = c2rServiceMapper.toVo(c2rService);
				logger.info("@ " + vo.toString());
				String key = c2rService.getId().getExtnlinstiidnfiname() + c2rService.getId().getSvcprcssno();
				c2rMap.put(key, vo);
			}

			logger.info("[>>Load C2RService getC2RService Configuration - ended ]");
			return c2rMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		}
	}

}
