package com.manage.service.cache;

import com.manage.vo.Power_UserVo;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CacheManager {
    private static Map<String,Cache> cacheMap = new ConcurrentHashMap<String,Cache>();
    //添加异常数量
    public static void addExcCount(String type){
        if("noExc".equals(type)){
        }
        if("exc".equals(type)){
        }
    }

    private  static Cache getCache(String key) {
        return (Cache) cacheMap.get(key);
    }

    private static boolean hasCache(String key) {
        return cacheMap.containsKey(key);
    }

    public static void clearAll() {
        cacheMap.clear();
    }

    public static void clearAll(String type) {
        Iterator i = cacheMap.entrySet().iterator();
        String key;
        try {
            while (i.hasNext()) {
                Map.Entry entry = (Map.Entry) i.next();
                key = (String) entry.getKey();
                if (key.equals(type)) {
                    clearOnly(key);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void clearOnly(String key) {
        cacheMap.remove(key);
    }

    public static void putCache(String key, Cache obj) {
        cacheMap.put(key, obj);
        //移除不属于该token的
        if(obj.getValue() instanceof Power_UserVo) {
            Power_UserVo powerUser = (Power_UserVo) obj.getValue();
            CacheManager.removeCacheByObject(powerUser, key);
        }
    }

    public static Cache getCacheInfo(String key) {
        if (hasCache(key)) {
            Cache cache = getCache(key);
            if (cacheExpired(cache)) {
                //过期,移除
                clearOnly(key);
                return null;
            }
            return cache;
        }else {
            return null;
        }
    }

    //是否过期
    private static boolean cacheExpired(Cache cache) {
        if (null == cache) {
            return false;
        }
        long nowDt = System.currentTimeMillis();
        long loginMill = cache.getLoginMill();
        long cacheDt = cache.getTimeOut();
        try {
            long checkTime = nowDt - loginMill;
            if (checkTime >= cacheDt) {
                return true;
            } else {
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            return true;
        }
    }

    //根据用户信息删除缓存
    private static void removeCacheByObject(Power_UserVo obj,String token) {
        try {
            if(cacheMap.size() > 0) {
                for (Map.Entry<String, Cache> map : cacheMap.entrySet()) {
                    Cache cache = CacheManager.getCacheInfo(map.getKey());
                    assert cache != null;
                    if (null != cache.getValue() && cache.getValue() instanceof Power_UserVo) {
                        Power_UserVo o = (Power_UserVo) cache.getValue();
                        if (obj.getUserName().equals(o.getUserName()) && !map.getKey().equals(token)) {
                            clearOnly(map.getKey());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}