package com.eactive.eai.common.b2bservice;

import java.util.Collection;
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.b2bservice.loader.B2BServiceLoader;
import com.eactive.eai.common.b2bservice.mapper.B2BServiceMapper;
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.b2bservice.B2BService;

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

	@Autowired
	private B2BServiceLoader b2bServiceEntityService;

	@Autowired
	private B2BServiceMapper b2bServiceMapper;

	public Map<String, B2BServiceVO> getAllB2BServices() throws DAOException {
		String useyn = "1";
		try {
			HashMap<String, B2BServiceVO> b2bServiceMap = new HashMap<>(); // B2BService Table
			List<B2BService> b2bServices = b2bServiceEntityService.findByUseynOrderByDefault(useyn);
			logger.info("[ @@ Load B2BService Configuration - starting >>>>>>>>>>>>>>>>> ]");
			String key = null;
			for (B2BService b2bService : b2bServices) {
				B2BServiceVO vo = b2bServiceMapper.toVo(b2bService);
				key = b2bService.getId().getExtnlinstiidnfiname() + b2bService.getId().getSvcprcssno();
				b2bServiceMap.put(key, vo);
			}
			logger.info("[>>Load B2BService Configuration - ended ]");
			return b2bServiceMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		}
	}

	public Map<String, B2BServiceVO> getB2BService(String b2bExtnlInstiCd) throws DAOException {
		try {
			HashMap<String, B2BServiceVO> map = new HashMap<>();
			Collection<B2BService> b2bServices = b2bServiceEntityService
					.findByIdExtnlinstiidnfinameOrderByDefault(b2bExtnlInstiCd);
			logger.info("[ @@ Load B2BService getB2BService Configuration - starting >>>>>>>>>>>>>>>>> ]");
			String key = null;
			for (B2BService b2bService : b2bServices) {
				B2BServiceVO vo = b2bServiceMapper.toVo(b2bService);
				logger.debug("@ " + vo.toString());
				key = b2bService.getId().getExtnlinstiidnfiname() + b2bService.getId().getSvcprcssno();
				map.put(key, vo);
			}
			logger.info("[>>Load B2BService getB2BService Configuration - ended ]");
			return map;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		}

	}
}
