package com.dxhy.common.auth;

import lombok.Data;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Data
public class AuthCache {

    protected static final String MAC_DATE_CHECK = "1";
    protected static final String INTERFACE_CHECK = "2";
    protected static final String CLIENT_COUNT = "3";
    private static final ConcurrentHashMap<String, Object> CACHE_MAP = new ConcurrentHashMap<>(4);

    protected static Object getAuthCache(String key) {
        final String key_ = "AUTH_CHECK_" + key;
        CacheData cacheData = (CacheData) CACHE_MAP.get(key_);
        if (null == cacheData) {
            return null;
        }
        if (System.currentTimeMillis() - cacheData.getCjsj() >= 60000) {//缓存超过1分钟
            CACHE_MAP.remove(key_);
            return null;
        }
        return cacheData.getValue();
    }

    protected static void putAuthCache(String key, Object value) {
        final String key_ = "AUTH_CHECK_" + key;
        CacheData cacheData = new CacheData();
        cacheData.setValue(value);
        cacheData.setCjsj(System.currentTimeMillis());
        CACHE_MAP.put(key_, cacheData);
    }


    protected static List<String> getAuthNsrsbhList() {
        List<CacheData> list = (List<CacheData>) CACHE_MAP.get("AUTH_CHECK_NSRSBH");
        if (null == list) {
            return Collections.emptyList();
        }
        return list.stream()
                .sorted(Comparator.comparing(CacheData::getCjsj))
                .map(CacheData::getValue)
                .map(String::valueOf)
                .collect(Collectors.toList());
    }

    protected static void putAuthNsrsbhList(List<CacheData> list) {
        CACHE_MAP.put("AUTH_CHECK_NSRSBH", list);
    }

}
