package com.eactive.eai.common.b2bextractor;

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.b2bextractor.loader.B2BExtractLoader;
import com.eactive.eai.common.b2bextractor.mapper.B2BExtractMapper;
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.b2bextractor.B2BExtract;

@Service
@Transactional
public class B2BExtractDAO extends BaseDAO {

	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Autowired
	private B2BExtractLoader b2bExtractEntityService;
	@Autowired
	private B2BExtractMapper b2bExtractMapper;

	public Map<String, B2BExtractVO> getAllB2BExtracts() throws DAOException {

		try {
			HashMap<String, B2BExtractVO> b2bMap = new HashMap<>();
			List<B2BExtract> b2bExtracts = b2bExtractEntityService.findAllOrderByEaisvcname();
			logger.info("[ @@ Load B2BExtract Configuration - starting >>>>>>>>>>>>>>>>> ]");

			for (B2BExtract b2bExtract : b2bExtracts) {
				B2BExtractVO vo = b2bExtractMapper.toVo(b2bExtract);
				b2bMap.put(b2bExtract.getId().getEaisvcname() + b2bExtract.getId().getSvcprcssno(), vo);
			}
			logger.info("[>>Load B2BExtract Configuration - ended ]");
			return b2bMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICBM101"));
		}
	}

	public Map<String, B2BExtractVO> getB2BExtract(String eaisvcname) throws DAOException {
		try {
			HashMap<String, B2BExtractVO> map = new HashMap<>();
			B2BExtract b2bExtract = b2bExtractEntityService.findByIdEaisvcname(eaisvcname);
			logger.info("[ @@ Load B2BExtract Configuration - starting >>>>>>>>>>>>>>>>> ]");

			if (b2bExtract != null) {
				B2BExtractVO vo = b2bExtractMapper.toVo(b2bExtract);
				String key = b2bExtract.getId().getEaisvcname() + b2bExtract.getId().getSvcprcssno();
				logger.info("@ " + vo.toString());
				map.put(key, vo);
			}

			logger.info("[>>Load B2BExtract Configuration - ended ]");
			return map;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICBM205"));
		}
	}

}
