package com.qmth.jkserver.core.cache;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Repository("memoryCacheDao")
public class MemoryCacheDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(MemoryCacheDao.class);

    private static ConcurrentHashMap<String, Long> cacheTime = new ConcurrentHashMap<String, Long>();// 缓存到期时间

    private static ConcurrentHashMap<String, Object> objectCache = new ConcurrentHashMap<String, Object>();// 缓存

    private static ConcurrentHashMap<String, HashMap<String, Object>> objectMapCache = new ConcurrentHashMap<String, HashMap<String, Object>>();// 缓存

    private static ConcurrentHashMap<String, String> stringCache = new ConcurrentHashMap<String, String>();// 缓存

    //清理过期的缓存
    public static void chean() {
        Set<String> expiredKeys = new HashSet<>();//已经过期的key
        for (String key : cacheTime.keySet()) {
            if (cacheTime.get(key) != null && cacheTime.get(key) < System.currentTimeMillis()) {
                expiredKeys.add(key);
            }
        }
        for (String key : expiredKeys) {
            if (key.indexOf("_") > -1) {
                for (String m : objectMapCache.keySet()) {
                    for (String field : objectMapCache.get(m).keySet()) {
                        if ((key + "_" + field).equals(key)) {
                            objectMapCache.remove(key);
                        }
                    }
                }
            } else {
                cacheTime.remove(key);
                objectCache.remove(key);
                stringCache.remove(key);
            }
            LOGGER.info("---成功清理缓存,KEY=" + key + "----------------------------------------");
        }
    }

    public String getStr(String key) {
        // 缓存过期
        if (cacheTime.get(key) != null && cacheTime.get(key) < System.currentTimeMillis()) {
            return null;
        }
        return stringCache.get(key);
    }

    public boolean setStr(String key, String value, int expireTime) {
        // 如果为-1，存储一年
        if (expireTime == -1) {
            expireTime = 360 * 24 * 60 * 60;
        }
        stringCache.put(key, value);
        cacheTime.put(key, System.currentTimeMillis() + expireTime * 1000);
        return true;
    }

    public Object getObj(String key) {
        // 缓存过期
        if (cacheTime.get(key) != null && cacheTime.get(key) < System.currentTimeMillis()) {
            return null;
        }
        return objectCache.get(key);
    }

    public Object getObj(String key, String field) {
        // 缓存过期
        if (StringUtils.isNotEmpty(field)) {
            if (cacheTime.get(key + "_" + field) != null && cacheTime.get(key + "_" + field) < System.currentTimeMillis()) {
                return null;
            }
            if (objectMapCache.containsKey(key)) {
                return objectMapCache.get(key).get(field);
            }
        } else {
            return objectMapCache.get(key);
        }
        return null;
    }

    public boolean setObj(String key, Object value, int expireTime) { //单位是秒
        // 如果为-1，存储一年
        if (expireTime == -1) {
            expireTime = 360 * 24 * 60 * 60;
        }
        objectCache.put(key, value);
        cacheTime.put(key, System.currentTimeMillis() + expireTime * 1000);
        return true;
    }

    public boolean setObj(String key, String field, Object value, int expireTime) {
        // 如果为-1，存储一年
        if (expireTime == -1) {
            expireTime = 360 * 24 * 60 * 60;
        }
        HashMap<String, Object> map;
        if (objectMapCache.containsKey(key)) {
            map = objectMapCache.get(key);
            map.put(field, value);
        } else {
            map = new HashMap<String, Object>();
            map.put(field, value);
            objectMapCache.put(key, map);
        }
        cacheTime.put(key + "_" + field, System.currentTimeMillis() + expireTime * 1000);
        return true;
    }

    public boolean delStr(String key) {
        stringCache.remove(key);
        cacheTime.remove(key);
        return false;
    }

    public boolean delObj(String key) {
        objectCache.remove(key);
        objectMapCache.remove(key);
        cacheTime.remove(key);
        return false;
    }

    public boolean delObj(String key, String field) {
        if (objectMapCache.containsKey(key)) {
            objectMapCache.get(key).remove(field);
        }
        cacheTime.remove(key + "_" + field);
        return false;
    }

}
