package com.eactive.eai.common.restrict;

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.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.restrict.logger.RestrictInfoLoader;
import com.eactive.eai.common.restrict.logger.RestrictInfoLogLogger;
import com.eactive.eai.common.restrict.mapper.RestrictInfoMapper;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.util.NullControl;
import com.eactive.eai.data.entity.onl.restrict.RestrictInfo;
import com.eactive.eai.data.entity.onl.restrict.RestrictInfoLog;
import com.eactive.eai.data.entity.onl.restrict.RestrictInfoLogId;

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

	@Autowired
	private RestrictInfoLoader restrictInfoLoader;
	@Autowired
	private RestrictInfoLogLogger restrictInfoLogLogger;

	@Autowired
	private RestrictInfoMapper restrictInfoMapper;

	public Map<String, RestrictInfoVO> getAllMessages() throws DAOException {
		try {
			Map<String, RestrictInfoVO> routingMap = new HashMap<>();
			List<RestrictInfo> restrictInfos = restrictInfoLoader.findAll();
			if (logger.isInfo())
				logger.info("[ @@ Load RestrictInfo Configuration - starting >>>>>>>>>>>>>>>>> ]");
			for (RestrictInfo restrictInfo : restrictInfos) {
				RestrictInfoVO vo = restrictInfoMapper.toVo(restrictInfo);
				routingMap.put(vo.getEAICtrlName(), vo);
				if (logger.isInfo())
					logger.info("@ " + vo.toString());
			}
			if (logger.isInfo())
				logger.info("[>>Load RestrictInfo Configuration - ended ]");
			return routingMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICIM101"));
		}
	}

	public void addResrtictLog(String bzwkDstcd, String msgDpstYMS, String eAISvcSerno, String eAISevrInstncName,
			String eAISvcName, String eAIOsidInstiCd, String eAICtrlNameDstcd) throws DAOException {
		try {
			RestrictInfoLog restrictInfoLog = new RestrictInfoLog();
			RestrictInfoLogId id = new RestrictInfoLogId();
			id.setEaibzwkdstcd(bzwkDstcd);
			id.setMsgdpstyms(msgDpstYMS);
			id.setEaisvcserno(NullControl.addSpace(eAISvcSerno));
			restrictInfoLog.setId(id);
			restrictInfoLog.setEaisevrinstncname(eAISevrInstncName);
			restrictInfoLog.setEaisvcname(eAISvcName);
			restrictInfoLog.setEaiosidinsticd(NullControl.addSpace(eAIOsidInstiCd));
			restrictInfoLog.setEaictrlnamedstcd(NullControl.addSpace(eAICtrlNameDstcd));
			restrictInfoLogLogger.save(restrictInfoLog);
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICIM102"));
		}
	}
}
