package cn.iselab.mooctest.user.service.impl;

import cn.iselab.mooctest.user.mapper.ConfigurationDao;
import cn.iselab.mooctest.user.model.Configuration;
import cn.iselab.mooctest.user.service.ConfigurationService;
import cn.iselab.mooctest.user.web.exception.ConfigurationNotExistException;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author guochao
 */
@Service
public class ConfigurationServiceImpl implements ConfigurationService {
    @Autowired
    private ConfigurationDao configurationDao;

    @Override
    public String getPlatformNameConfiguration() throws ConfigurationNotExistException {
        try{
            Configuration configuration = configurationDao.findByName("platformName");
            return configuration.getValue();
        }catch (Exception e){
            throw new ConfigurationNotExistException("数据库中无平台名称platformName的配置项");
        }

    }

    @Override
    public Map<String, String> getConfiguration() {
       return Lists.newArrayList(configurationDao.findAll()).stream().collect(Collectors.toMap(configuration -> configuration.getName(), configuration -> configuration.getValue()));
    }
}

