package com.eactive.eai.apim.apigroup;

import com.eactive.eai.apim.apigroup.mapper.ApiGroupMapper;
import com.eactive.eai.data.entity.onl.apim.apigroup.ApiGroup;
import com.eactive.eai.apim.apigroup.loader.ApiGroupLoader;
import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ApiGroupDao  extends BaseDAO {

    @Autowired
    ApiGroupLoader apiGroupLoader;
    @Autowired
    ApiGroupMapper apiGroupMapper;

    public List<ApiGroupVO> getAllApiGroups() throws DAOException {
        try {
            List<ApiGroup> adapterGroups = apiGroupLoader.selectAllGroups();
            return adapterGroups.stream().map(apiGroupMapper::toVo).collect(Collectors.toList());
        } catch (Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAAC001"));
        }
    }

    public ApiGroupVO getApiGroup(String apiGroupId) throws DAOException {
        try {
            ApiGroup apiGroup = apiGroupLoader.getById(apiGroupId);
            return apiGroupMapper.toVo(apiGroup);
        } catch (Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAAC001"));
        }
    }
}
