package com.eactive.eai.common.sysdomain;

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.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.sysdomain.loader.SysDomainLoader;
import com.eactive.eai.common.sysdomain.mapper.SysDomainMapper;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.sysdomain.SysDomain;

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

	@Autowired
	private SysDomainLoader sysDomainLoader;

	@Autowired
	private SysDomainMapper sysDomainMapper;

	public List getAllSysDomains() throws DAOException {
		try {
			List<SysDomain> list = sysDomainLoader.findAll();
			List<SysDomainVO> volist = new ArrayList<>();
			list.forEach(e -> volist.add(sysDomainMapper.toVo(e)));
			return volist;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICSD101"));
		}
	}

	public SysDomainVO getSysDomain(String sysDomainNm) throws DAOException {
		try {
			Optional<SysDomain> entity = sysDomainLoader.findById(sysDomainNm);
			SysDomainVO vo = null;
			if (entity.isPresent()) {
				vo = sysDomainMapper.toVo(entity.get());
			}
			return vo;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICSD103"));
		}
	}

}
