package com.eactive.eai.common.circuitBreaker.type;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.common.circuitbreak.loader.CircuitBreakerConfigLoader;
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.circuitbreaker.CircuitBreakerConfig;

@Service
@Transactional
public class TypedCircuitBreakerDAO extends BaseDAO {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	@Autowired
	private CircuitBreakerConfigLoader loader;
	@Autowired
	private TypedCircuitBreakerMapper mapper;

	public List<TypedCircuitBreakerVO> getList() throws DAOException {
		try {
			Iterable<CircuitBreakerConfig> rows = this.loader.findAll();
			List<TypedCircuitBreakerVO> result = new ArrayList<>();
			logger.info("[ @@ Load UrlCircuitBreaker Configuration - starting >>>>>>>>>>>>>>>>> ]");
			for ( CircuitBreakerConfig row : rows ) {
				result.add(this.mapper.toVo(row));
			}

			logger.info("[>> Load UrlCircuitBreaker Configuration - ended ]");
			return result;
		} catch (Exception var5) {
			throw new DAOException(ExceptionUtil.getErrorCode(var5, "RECEAICMM101"));
		}
	}

	public TypedCircuitBreakerVO get(String key) throws DAOException {
		TypedCircuitBreakerVO vo;
		try {
			logger.info("[ @@ Load UrlCircuitBreaker Configuration - starting >>>>>>>>>>>>>>>>> " + key + "]");
			Optional<CircuitBreakerConfig> optionalRow = this.loader.findById(key);
			if (!optionalRow.isPresent()) {
				vo = null;
				return vo;
			}

			vo = this.mapper.toVo((CircuitBreakerConfig) optionalRow.get());
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		} finally {
			logger.info("[>> Load UrlCircuitBreaker Configuration - ended " + key + "]");
		}

		return vo;
	}
}