package com.lantone.structure.util;

import com.google.common.collect.Maps;
import com.lantone.common.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/3/6 17:21
 */
@Component
public final class SpecialStorageUtil {
    private final Map<String, Map<String, Object>> hospitalCache = Maps.newHashMap();
    private final ThreadLocal<String> hospitalIdThreadLocal = new ThreadLocal<>();

    public <T> T getJsonStringValue(String key) {
        return getValByHospitalId(hospitalIdThreadLocal.get(), key);
    }

    public <T> T getValByHospitalId(String hospitalId, String key) {
        if (StringUtil.isBlank(hospitalId)) {
            return null;
        }
        Map<String, Object> map = hospitalCache.get(hospitalId);
        if (map == null) {
            return null;
        }
        return (T) map.get(key);
    }

    public <T> void setJsonStringValue(String key, Object value) {
        setValByHospitalId(hospitalIdThreadLocal.get(), key, value);
    }

    public <T> void setValByHospitalId(String hospitalId, String key, Object value) {
        if (StringUtil.isBlank(hospitalId)) {
            return;
        }
        Map<String, Object> map = hospitalCache.get(hospitalId);
        if (map == null) {
            return;
        }
        map.put(key, value);
    }

    public ThreadLocal<String> getHospitalIdThreadLocal() {
        return hospitalIdThreadLocal;
    }

    public Map<String, Map<String, Object>> getHospitalCache() {
        return hospitalCache;
    }

}