package cn.com.ctop.common.module.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author jeecg-boot
 * 2019年11月12日10:04:00
 */
public class PropertiesUtils {
    private static final Logger log = LoggerFactory.getLogger(PropertiesUtils.class);
    private static Map<String, String> cache = new HashMap<>();
    public static String getConfig(String key) {
        return getValue("kuaishou_config", key);
    }
    public static String getValue(String name, String key) {
        try {
            String cacheValue = cache.get(name + key);
            if (cacheValue != null) {
                return cacheValue;
            }
            Properties props = new Properties();
            InputStream in = PropertiesUtils.class.getResourceAsStream("/"
                    + name + ".properties");
            props.load(in);
            String value = props.getProperty(key);
            cache.put(name + key, value);
            props.clone();
            in.close();
            return value;
        } catch (Exception e) {
            log.error("PropertiesUtil getCacheValue Error", e);
        }
        return null;
    }

    public static Map<String, String> getMap(String name) {
        try {
            Properties props = new Properties();
            InputStream in = PropertiesUtils.class.getResourceAsStream("/"
                    + name + ".properties");
            props.load(in);
            Iterator<Object> it = props.keySet().iterator();
            Map<String, String> map = new HashMap<>();
            while (it.hasNext()) {
                String key = it.next().toString();
                String value = props.getProperty(key);
                map.put(key, value);
            }
            props.clone();
            in.close();
            return map;
        } catch (Exception e) {
            log.error("PropertiesUtil getMap Error", e);
        }
        return null;
    }
}
