package com.kbstar.eai.common.businessday;

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.util.Logger;
import com.eactive.eai.data.entity.onl.businessday.BusinessDay;
import com.eactive.eai.data.entity.onl.businessday.BusinessDayEvent;
import com.eactive.eai.data.entity.onl.businessday.BusinessDayEventLog;
import com.eactive.eai.data.entity.onl.businessday.BusinessDayEventLogId;
import com.kbstar.eai.common.businessday.loader.BusinessDayEventLoader;
import com.kbstar.eai.common.businessday.loader.BusinessDayEventLogLogger;
import com.kbstar.eai.common.businessday.loader.BusinessDayLoader;
import com.kbstar.eai.common.businessday.mapper.BusinessDayEventMapper;
import com.kbstar.eai.common.businessday.mapper.BusinessDayMapper;

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

	@Autowired
	private BusinessDayLoader businessDayLoader;

	@Autowired
	private BusinessDayMapper businessDayMapper;

	@Autowired
	private BusinessDayEventLoader businessDayEventLoader;

	@Autowired
	private BusinessDayEventMapper businessDayEventMapper;

	@Autowired
	private BusinessDayEventLogLogger businessDayEventLogLogger;

	// 일자전환 변경대상목록 정보 전체 조회
	public Map<String, BusinessDayVO> getAllMessages() throws DAOException {
		try {
			List<BusinessDay> businessDays = businessDayLoader.findAll();
			Map<String, BusinessDayVO> businessDayMap = new HashMap<>();

			logger.info("[ @@ getAllMessages Configuration - starting >>>>>>>>>>>>>>>>> ]");
			for (BusinessDay businessDay : businessDays) {
				BusinessDayVO vo = businessDayMapper.toVo(businessDay);
				businessDayMap.put(vo.getEAISvcName(), vo);
				logger.info("@ " + vo.toString());
			}
			logger.info("[>>getAllMessages Configuration - ended ]");
			return businessDayMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICIM101"));
		}
	}

	public Map<String, EventSystemInfoVO> getEventSystemInfo() throws DAOException {
		try {
			List<BusinessDayEvent> businessDayEvents = businessDayEventLoader.findAll();
			Map<String, EventSystemInfoVO> businessDayEventMap = new HashMap<>();

			logger.info("[ @@ getAllMessages Configuration - starting >>>>>>>>>>>>>>>>> ]");
			for (BusinessDayEvent businessDayEvent : businessDayEvents) {
				EventSystemInfoVO vo = businessDayEventMapper.toVo(businessDayEvent);
				businessDayEventMap.put(vo.getEvntRecvSysName(), vo);
				logger.info("@ " + vo.toString());
			}
			logger.info("[>>getAllMessages Configuration - ended ]");
			return businessDayEventMap;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICIM101"));
		}
	}

	public void addEventLog(String evntRecvSysName, String eAIEvntDstcd, String evntSendYMS,
			String evntPrcssRsultDpstYMS, String prcssRsultYn) throws DAOException {
		try {
			BusinessDayEventLog entity = new BusinessDayEventLog();
			BusinessDayEventLogId id = new BusinessDayEventLogId();

			id.setEaievntdstcd(eAIEvntDstcd);
			id.setEvntrecvsysname(evntRecvSysName);
			id.setEvntsendyms(evntSendYMS);

			entity.setId(id);
			entity.setEvntprcssrsultdpstyms(evntPrcssRsultDpstYMS);
			entity.setPrcssrsultyn(prcssRsultYn);

			businessDayEventLogLogger.save(entity);
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICBM102"));
		}
	}
}
