package com.eactive.eai.common.sessioninfo;

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.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.sessioninfo.loader.TerminalInfoLoader;
import com.eactive.eai.common.sessioninfo.mapper.TerminalInfoMapper;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.sessioninfo.TerminalInfo;

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

	@Autowired
	private TerminalInfoLoader terminalInfoLoader;

	@Autowired
	private TerminalInfoMapper terminalInfoMapper;
	
	public void upsertTerminalInfo(TerminalInfoVO vo) throws DAOException {
		try {
			TerminalInfo terminalInfo = null;
			Optional<TerminalInfo> optional = terminalInfoLoader.findById(vo.getTerminalNo());
			if(optional.isPresent()) {
				terminalInfo = optional.get();
				terminalInfo.setSocSessionStatus( vo.getSocSessionStatusInfo() );
		        terminalInfo.setTerminalNo( vo.getTerminalNo() );
		        terminalInfo.setBrnCd( vo.getBrnCode() );
		        terminalInfo.setBrnName( vo.getBrnName() );
		        terminalInfo.setCreateTime( vo.getCreateTime() );
		        terminalInfo.setRofcCd( vo.getRofcCode() );
		        terminalInfo.setSocInstance( vo.getSocInstance() );
		        terminalInfo.setSocRemoteAddr( vo.getSocRemoteAddr() );
			}
			else {
				terminalInfo = terminalInfoMapper.toEntity(vo);
				terminalInfoLoader.save(terminalInfo);
			}
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICKE118"));
		}
	}
}
